cpl_nonsig.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * -------
  24. * 2003-06-27: file created (bogdan)
  25. */
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <time.h>
  33. #include <sys/uio.h>
  34. #include <signal.h>
  35. #include "../../mem/shm_mem.h"
  36. #include "../../mem/mem.h"
  37. #include "../../dprint.h"
  38. #include "cpl_nonsig.h"
  39. #include "CPL_tree.h"
  40. #define MAX_LOG_FILE_NAME 32
  41. #define FILE_NAME_SUFIX ".log"
  42. #define FILE_NAME_SUFIX_LEN (sizeof(FILE_NAME_SUFIX)-1)
  43. #define LOG_SEPARATOR ": "
  44. #define LOG_SEPARATOR_LEN (sizeof(LOG_SEPARATOR)-1)
  45. #define DEFAULT_LOG_NAME "default_log"
  46. #define DEFAULT_LOG_NAME_LEN (sizeof(DEFAULT_LOG_NAME)-1)
  47. #define LOG_TERMINATOR "\n"
  48. #define LOG_TERMINATOR_LEN (sizeof(LOG_TERMINATOR)-1)
  49. static char file[MAX_LOG_DIR_SIZE+1+MAX_LOG_FILE_NAME+FILE_NAME_SUFIX_LEN+1];
  50. static char *file_ptr;
  51. static inline void write_log( struct cpl_cmd *cmd)
  52. {
  53. struct iovec wr_vec[5];
  54. time_t now;
  55. char *time_ptr;
  56. int fd;
  57. int ret;
  58. /* build file name (cmd->s1 is the user name)*/
  59. if (cmd->s1.len>MAX_LOG_FILE_NAME)
  60. cmd->s1.len = MAX_LOG_FILE_NAME;
  61. memcpy(file_ptr, cmd->s1.s, cmd->s1.len );
  62. memcpy(file_ptr+cmd->s1.len,FILE_NAME_SUFIX,FILE_NAME_SUFIX_LEN);
  63. file_ptr[cmd->s1.len+FILE_NAME_SUFIX_LEN] = 0;
  64. /* get current date+time -> wr_vec[0] */
  65. time( &now );
  66. time_ptr = ctime( &now );
  67. wr_vec[0].iov_base = time_ptr;
  68. wr_vec[0].iov_len = strlen( time_ptr );
  69. /* ctime_r adds a \n at the end -> overwrite it with space */
  70. time_ptr[ wr_vec[0].iov_len-1 ] = ' ';
  71. /* log name (cmd->s2) -> wr_vec[1] */
  72. if (cmd->s2.s==0 || cmd->s2.len==0) {
  73. wr_vec[1].iov_base = DEFAULT_LOG_NAME;
  74. wr_vec[1].iov_len = DEFAULT_LOG_NAME_LEN;
  75. } else {
  76. wr_vec[1].iov_base = cmd->s2.s;
  77. wr_vec[1].iov_len = cmd->s2.len;
  78. }
  79. /* log separator -> wr_vec[2] */
  80. wr_vec[2].iov_base = LOG_SEPARATOR;
  81. wr_vec[2].iov_len = LOG_SEPARATOR_LEN;
  82. /* log comment (cmd->s3) -> wr_vec[3] */
  83. wr_vec[3].iov_base = cmd->s3.s;
  84. wr_vec[3].iov_len = cmd->s3.len;
  85. /* log terminator -> wr_vec[2] */
  86. wr_vec[4].iov_base = LOG_TERMINATOR;
  87. wr_vec[4].iov_len = LOG_TERMINATOR_LEN;
  88. /* [create+]open the file */
  89. fd = open( file, O_CREAT|O_APPEND|O_WRONLY, 0664);
  90. if (fd==-1) {
  91. LM_ERR("cannot open file [%s] : %s\n",
  92. file, strerror(errno) );
  93. return;
  94. }
  95. /* get the log */
  96. LM_DBG("logging into [%s]... \n",file);
  97. /* I'm really not interested in the return code for write ;-) */
  98. while ( (ret=writev( fd, wr_vec, 5))==-1 ) {
  99. if (errno==EINTR)
  100. continue;
  101. LM_ERR("writing to log file [%s] : %s\n",
  102. file, strerror(errno) );
  103. }
  104. close (fd);
  105. shm_free( cmd->s1.s );
  106. }
  107. static inline void send_mail( struct cpl_cmd *cmd)
  108. {
  109. char *argv[5];
  110. int pfd[2];
  111. pid_t pid;
  112. int i;
  113. if (pipe(pfd) < 0) {
  114. LM_ERR("pipe failed: %s\n",strerror(errno));
  115. return;
  116. }
  117. /* even if I haven't fork yet, I push the date on the pipe just to get
  118. * rid of one more malloc + copy */
  119. if (cmd->s3.len && cmd->s3.s) {
  120. if ( (i=write( pfd[1], cmd->s3.s, cmd->s3.len ))!=cmd->s3.len ) {
  121. LM_ERR("write returned error %s\n",
  122. strerror(errno));
  123. goto error;
  124. }
  125. }
  126. if ( (pid = fork()) < 0) {
  127. LM_ERR("fork failed: %s\n",strerror(errno));
  128. goto error;
  129. } else if (pid==0) {
  130. /* child -> close all descriptors excepting pfd[0] */
  131. /* 32 is the maximum number of inherited open file descriptors */
  132. for (i=3; i < 32; i++)
  133. if (i!=pfd[0])
  134. close(i);
  135. if (pfd[0] != STDIN_FILENO) {
  136. dup2(pfd[0], STDIN_FILENO);
  137. close(pfd[0]);
  138. }
  139. /* set the argument vector*/
  140. argv[0] = "mail";
  141. argv[1] = "-s";
  142. if (cmd->s2.s && cmd->s2.len) {
  143. /* put the subject in this format : <"$subject"\0> */
  144. if ( (argv[2]=(char*)pkg_malloc(1+cmd->s2.len+1+1))==0) {
  145. LM_ERR("cannot get pkg memory\n");
  146. goto child_exit;
  147. }
  148. argv[2][0] = '\"';
  149. memcpy(argv[2]+1,cmd->s2.s,cmd->s2.len);
  150. argv[2][cmd->s2.len+1] = '\"';
  151. argv[2][cmd->s2.len+2] = 0;
  152. } else {
  153. argv[2] = "\"[CPL notification]\"";
  154. }
  155. /* put the TO in <$to\0> format*/
  156. if ( (argv[3]=(char*)pkg_malloc(cmd->s1.len+1))==0) {
  157. LM_ERR("cannot get pkg memory\n");
  158. goto child_exit;
  159. }
  160. memcpy(argv[3],cmd->s1.s,cmd->s1.len);
  161. argv[3][cmd->s1.len] = 0;
  162. /* last element in vector mist be a null pointer */
  163. argv[4] = (char*)0;
  164. /* just debug */
  165. for(i=0;i<sizeof(argv)/sizeof(char*);i++)
  166. LM_DBG("argv[%d] = %s\n",i,argv[i]);
  167. /* once I copy localy all the data from shm mem -> free the shm */
  168. shm_free( cmd->s1.s );
  169. /* set an alarm -> sending the email shouldn't take more than 10 sec */
  170. alarm(10);
  171. /* run the external mailer */
  172. LM_DBG("new forked process created -> "
  173. "doing execv..\n");
  174. execv("/usr/bin/mail",argv);
  175. /* if we got here means execv exit with error :-( */
  176. LM_ERR("execv failed! (%s)\n",strerror(errno));
  177. child_exit:
  178. _exit(127);
  179. }
  180. /* parent -> close both ends of pipe */
  181. close(pfd[0]);
  182. close(pfd[1]);
  183. return;
  184. error:
  185. shm_free( cmd->s1.s );
  186. close(pfd[0]);
  187. close(pfd[1]);
  188. return;
  189. }
  190. void cpl_aux_process( int cmd_out, char *log_dir)
  191. {
  192. struct cpl_cmd cmd;
  193. int len;
  194. /* this process will ignore SIGCHLD signal */
  195. if (signal( SIGCHLD, SIG_IGN)==SIG_ERR) {
  196. LM_ERR("cannot set to IGNORE SIGCHLD signal\n");
  197. }
  198. /* set the path for logging */
  199. if (log_dir) {
  200. strcpy( file, log_dir);
  201. file_ptr = file + strlen(log_dir);
  202. *(file_ptr++) = '/';
  203. }
  204. while(1) {
  205. /* let's read a command from pipe */
  206. len = read( cmd_out, &cmd, sizeof(struct cpl_cmd));
  207. if (len!=sizeof(struct cpl_cmd)) {
  208. if (len>=0) {
  209. LM_ERR("truncated message"
  210. " read from pipe! -> discarded\n");
  211. } else if (errno!=EAGAIN) {
  212. LM_ERR("pipe reading failed: "
  213. " : %s\n",strerror(errno));
  214. }
  215. sleep(1);
  216. continue;
  217. }
  218. /* process the command*/
  219. switch (cmd.code) {
  220. case CPL_LOG_CMD:
  221. write_log( &cmd );
  222. break;
  223. case CPL_MAIL_CMD:
  224. send_mail( &cmd );
  225. break;
  226. default:
  227. LM_ERR("unknown command (%d) "
  228. "received! -> ignoring\n",cmd.code);
  229. } /* end switch*/
  230. }
  231. }