You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

488 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: error.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* the logging subsystem */
  5. #include "config.h"
  6. #include "sysincludes.h"
  7. #include "mytypes.h"
  8. #include "compat.h"
  9. #include "utils.h"
  10. #include "vsnprintf_r.h"
  11. #include "snprinterr.h"
  12. #include "error.h"
  13. #include "sycls.h"
  14. /* translate MSG level to SYSLOG level */
  15. int syslevel[] = {
  16. LOG_DEBUG,
  17. LOG_INFO,
  18. LOG_NOTICE,
  19. LOG_WARNING,
  20. LOG_ERR,
  21. LOG_CRIT };
  22. struct diag_opts {
  23. const char *progname;
  24. int msglevel;
  25. int exitlevel;
  26. int syslog;
  27. FILE *logfile;
  28. int logfacility;
  29. bool micros;
  30. int exitstatus; /* pass signal number to error exit */
  31. bool withhostname; /* in custom logs add hostname */
  32. char *hostname;
  33. } ;
  34. static void _diag_exit(int status);
  35. struct diag_opts diagopts =
  36. { NULL, E_ERROR, E_ERROR, 0, NULL, LOG_DAEMON, false, 0 } ;
  37. static void msg2(
  38. #if HAVE_CLOCK_GETTIME
  39. struct timespec *now,
  40. #elif HAVE_GETTIMEOFDAY
  41. struct timeval *now,
  42. #else
  43. time_t *now,
  44. #endif
  45. int level, int exitcode, int handler, const char *text);
  46. static void _msg(int level, const char *buff, const char *syslp);
  47. sig_atomic_t diag_in_handler; /* !=0 indicates to msg() that in signal handler */
  48. sig_atomic_t diag_immediate_msg; /* !=0 prints messages even from within signal handler instead of deferring them */
  49. sig_atomic_t diag_immediate_exit; /* !=0 calls exit() from diag_exit() even when in signal handler. For system() */
  50. static struct wordent facilitynames[] = {
  51. {"auth", (void *)LOG_AUTH},
  52. #ifdef LOG_AUTHPRIV
  53. {"authpriv", (void *)LOG_AUTHPRIV},
  54. #endif
  55. #ifdef LOG_CONSOLE
  56. {"console", (void *)LOG_CONSOLE},
  57. #endif
  58. {"cron", (void *)LOG_CRON},
  59. {"daemon", (void *)LOG_DAEMON},
  60. #ifdef LOG_FTP
  61. {"ftp", (void *)LOG_FTP},
  62. #endif
  63. {"kern", (void *)LOG_KERN},
  64. {"local0", (void *)LOG_LOCAL0},
  65. {"local1", (void *)LOG_LOCAL1},
  66. {"local2", (void *)LOG_LOCAL2},
  67. {"local3", (void *)LOG_LOCAL3},
  68. {"local4", (void *)LOG_LOCAL4},
  69. {"local5", (void *)LOG_LOCAL5},
  70. {"local6", (void *)LOG_LOCAL6},
  71. {"local7", (void *)LOG_LOCAL7},
  72. {"lpr", (void *)LOG_LPR},
  73. {"mail", (void *)LOG_MAIL},
  74. {"news", (void *)LOG_NEWS},
  75. #ifdef LOG_SECURITY
  76. {"security", (void *)LOG_SECURITY},
  77. #endif
  78. {"syslog", (void *)LOG_SYSLOG},
  79. {"user", (void *)LOG_USER},
  80. {"uucp", (void *)LOG_UUCP}
  81. } ;
  82. /* serialize message for sending from signal handlers */
  83. struct sermsg {
  84. int severity;
  85. #if HAVE_CLOCK_GETTIME
  86. struct timespec ts;
  87. #else
  88. struct timeval tv;
  89. #endif
  90. } ;
  91. static int diaginitialized;
  92. static int diag_sock_send = -1;
  93. static int diag_sock_recv = -1;
  94. static int diag_msg_avail = 0; /* !=0: messages from within signal handler may be waiting */
  95. static int diag_sock_pair(void) {
  96. int handlersocks[2];
  97. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handlersocks) < 0) {
  98. diag_sock_send = -1;
  99. diag_sock_recv = -1;
  100. return -1;
  101. }
  102. diag_sock_send = handlersocks[1];
  103. diag_sock_recv = handlersocks[0];
  104. return 0;
  105. }
  106. static int diag_init(void) {
  107. if (diaginitialized) {
  108. return 0;
  109. }
  110. diaginitialized = 1;
  111. /* gcc with GNU libc refuses to set this in the initializer */
  112. diagopts.logfile = stderr;
  113. if (diag_sock_pair() < 0) {
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. #define DIAG_INIT ((void)(diaginitialized || diag_init()))
  119. void diag_set(char what, const char *arg) {
  120. DIAG_INIT;
  121. switch (what) {
  122. const struct wordent *keywd;
  123. case 'y': diagopts.syslog = true;
  124. if (arg && arg[0]) {
  125. if ((keywd =
  126. keyw(facilitynames, arg,
  127. sizeof(facilitynames)/sizeof(struct wordent))) == NULL) {
  128. Error1("unknown syslog facility \"%s\"", arg);
  129. } else {
  130. diagopts.logfacility = (int)(size_t)keywd->desc;
  131. }
  132. }
  133. openlog(diagopts.progname, LOG_PID, diagopts.logfacility);
  134. if (diagopts.logfile != NULL && diagopts.logfile != stderr) {
  135. fclose(diagopts.logfile);
  136. }
  137. diagopts.logfile = NULL;
  138. break;
  139. case 'f':
  140. if (diagopts.logfile != NULL && diagopts.logfile != stderr) {
  141. fclose(diagopts.logfile);
  142. }
  143. if ((diagopts.logfile = fopen(arg, "a")) == NULL) {
  144. Error2("cannot open log file \"%s\": %s", arg, strerror(errno));
  145. }
  146. break;
  147. case 's':
  148. if (diagopts.logfile != NULL && diagopts.logfile != stderr) {
  149. fclose(diagopts.logfile);
  150. }
  151. diagopts.logfile = stderr; break; /* logging to stderr is default */
  152. case 'p': diagopts.progname = arg;
  153. openlog(diagopts.progname, LOG_PID, diagopts.logfacility);
  154. break;
  155. case 'd': --diagopts.msglevel; break;
  156. case 'u': diagopts.micros = true; break;
  157. default: msg(E_ERROR, "unknown diagnostic option %c", what);
  158. }
  159. }
  160. void diag_set_int(char what, int arg) {
  161. DIAG_INIT;
  162. switch (what) {
  163. case 'D': diagopts.msglevel = arg; break;
  164. case 'e': diagopts.exitlevel = arg; break;
  165. case 'x': diagopts.exitstatus = arg; break;
  166. case 'h': diagopts.withhostname = arg;
  167. if ((diagopts.hostname = getenv("HOSTNAME")) == NULL) {
  168. struct utsname ubuf;
  169. uname(&ubuf);
  170. diagopts.hostname = strdup(ubuf.nodename);
  171. }
  172. break;
  173. default: msg(E_ERROR, "unknown diagnostic option %c", what);
  174. }
  175. }
  176. int diag_get_int(char what) {
  177. DIAG_INIT;
  178. switch (what) {
  179. case 'y': return diagopts.syslog;
  180. case 's': return diagopts.logfile == stderr;
  181. case 'd': case 'D': return diagopts.msglevel;
  182. case 'e': return diagopts.exitlevel;
  183. }
  184. return -1;
  185. }
  186. const char *diag_get_string(char what) {
  187. DIAG_INIT;
  188. switch (what) {
  189. case 'p': return diagopts.progname;
  190. }
  191. return NULL;
  192. }
  193. /* make sure that the diag_sock fds do not have this num */
  194. int diag_reserve_fd(int fd) {
  195. DIAG_INIT;
  196. if (diag_sock_send == fd) {
  197. diag_sock_send = Dup(fd);
  198. Close(fd);
  199. }
  200. if (diag_sock_recv == fd) {
  201. diag_sock_recv = Dup(fd);
  202. Close(fd);
  203. }
  204. return 0;
  205. }
  206. /* call this after a fork() from the child process to separate master/parent
  207. sockets from child sockets */
  208. int diag_fork() {
  209. Close(diag_sock_send);
  210. Close(diag_sock_recv);
  211. return diag_sock_pair();
  212. }
  213. /* Linux and AIX syslog format:
  214. Oct 4 17:10:37 hostname socat[52798]: D signal(13, 1)
  215. */
  216. void msg(int level, const char *format, ...) {
  217. struct diag_dgram diag_dgram;
  218. va_list ap;
  219. /* does not perform a system call if nothing todo, thanks diag_msg_avail */
  220. diag_dgram._errno = errno; /* keep for passing from signal handler to sock.
  221. reason is that strerror is definitely not
  222. async-signal-safe */
  223. DIAG_INIT;
  224. /* in normal program flow (not in signal handler) */
  225. /* first flush the queue of datagrams from the socket */
  226. if (diag_msg_avail && !diag_in_handler) {
  227. diag_msg_avail = 0; /* _before_ flush to prevent inconsistent state when signal occurs inbetween */
  228. diag_flush();
  229. }
  230. if (level < diagopts.msglevel) { return; }
  231. va_start(ap, format);
  232. /* we do only a minimum in the outer parts which may run in a signal handler
  233. these are: get actual time, level, serialized message and write them to socket
  234. */
  235. diag_dgram.op = DIAG_OP_MSG;
  236. #if HAVE_CLOCK_GETTIME
  237. clock_gettime(CLOCK_REALTIME, &diag_dgram.now);
  238. #elif HAVE_GETTIMEOFDAY
  239. gettimeofday(&diag_dgram.now, NULL);
  240. #else
  241. diag_dgram.now = time(NULL);
  242. #endif
  243. diag_dgram.level = level;
  244. diag_dgram.exitcode = diagopts.exitstatus;
  245. vsnprintf_r(diag_dgram.text, sizeof(diag_dgram.text), format, ap);
  246. if (diag_in_handler && !diag_immediate_msg) {
  247. send(diag_sock_send, &diag_dgram, sizeof(diag_dgram)-TEXTLEN + strlen(diag_dgram.text)+1, MSG_DONTWAIT
  248. #ifdef MSG_NOSIGNAL
  249. |MSG_NOSIGNAL
  250. #endif
  251. );
  252. diag_msg_avail = 1;
  253. va_end(ap);
  254. return;
  255. }
  256. msg2(&diag_dgram.now, diag_dgram.level, diagopts.exitstatus, 0, diag_dgram.text);
  257. va_end(ap); return;
  258. }
  259. void msg2(
  260. #if HAVE_CLOCK_GETTIME
  261. struct timespec *now,
  262. #elif HAVE_GETTIMEOFDAY
  263. struct timeval *now,
  264. #else
  265. time_t *now,
  266. #endif
  267. int level, /* E_INFO... */
  268. int exitcode, /* on exit use this exit code */
  269. int handler, /* message comes from signal handler */
  270. const char *text) {
  271. time_t epoch;
  272. unsigned long micros;
  273. #if HAVE_STRFTIME
  274. struct tm struct_tm;
  275. #endif
  276. #define BUFLEN 512
  277. char buff[BUFLEN], *bufp, *syslp;
  278. size_t bytes;
  279. #if HAVE_CLOCK_GETTIME
  280. epoch = now->tv_sec;
  281. #elif HAVE_GETTIMEOFDAY
  282. epoch = now->tv_sec;
  283. #else
  284. epoch = *now;
  285. #endif
  286. #if HAVE_STRFTIME
  287. bytes = strftime(buff, 20, "%Y/%m/%d %H:%M:%S", localtime_r(&epoch, &struct_tm));
  288. buff[bytes] = '\0';
  289. #else
  290. bytes = snprintf(buff, 11, F_time, epoch);
  291. #endif
  292. if (diagopts.micros) {
  293. #if HAVE_CLOCK_GETTIME
  294. micros = now->tv_nsec/1000;
  295. #elif HAVE_GETTIMEOFDAY
  296. micros = now->tv_usec;
  297. #else
  298. micros = 0;
  299. #endif
  300. bytes += sprintf(buff+19, ".%06lu ", micros);
  301. } else {
  302. buff[19] = ' '; buff[20] = '\0';
  303. }
  304. bytes = strlen(buff);
  305. bufp = buff + bytes;
  306. if (diagopts.withhostname) {
  307. bytes = sprintf(bufp, "%s ", diagopts.hostname), bufp+=bytes;
  308. }
  309. bytes = sprintf(bufp, "%s["F_pid"] ", diagopts.progname, getpid());
  310. bufp += bytes;
  311. syslp = bufp;
  312. *bufp++ = "DINWEF"[level];
  313. #if 0 /* only for debugging socat */
  314. if (handler) bufp[-1] = tolower(bufp[-1]); /* for debugging, low chars indicate messages from signal handlers */
  315. #endif
  316. *bufp++ = ' ';
  317. strncpy(bufp, text, BUFLEN-(bufp-buff)-1);
  318. strcat(bufp, "\n");
  319. _msg(level, buff, syslp);
  320. if (level >= diagopts.exitlevel) {
  321. if (E_NOTICE >= diagopts.msglevel) {
  322. snprintf_r(syslp, 16, "N exit(%d)\n", exitcode?exitcode:(diagopts.exitstatus?diagopts.exitstatus:1));
  323. _msg(E_NOTICE, buff, syslp);
  324. }
  325. exit(exitcode?exitcode:(diagopts.exitstatus?diagopts.exitstatus:1));
  326. }
  327. }
  328. static void _msg(int level, const char *buff, const char *syslp) {
  329. if (diagopts.syslog) {
  330. /* prevent format string attacks (thanks to CoKi) */
  331. syslog(syslevel[level], "%s", syslp);
  332. }
  333. if (diagopts.logfile) {
  334. fputs(buff, diagopts.logfile); fflush(diagopts.logfile);
  335. }
  336. }
  337. /* handle the messages in the queue */
  338. void diag_flush(void) {
  339. struct diag_dgram recv_dgram;
  340. char exitmsg[20];
  341. while (recv(diag_sock_recv, &recv_dgram, sizeof(recv_dgram)-1, MSG_DONTWAIT) > 0) {
  342. recv_dgram.text[TEXTLEN-1] = '\0';
  343. switch (recv_dgram.op) {
  344. case DIAG_OP_EXIT:
  345. /* we want the actual time, not when this dgram was sent */
  346. #if HAVE_CLOCK_GETTIME
  347. clock_gettime(CLOCK_REALTIME, &recv_dgram.now);
  348. #elif HAVE_GETTIMEOFDAY
  349. gettimeofday(&recv_dgram.now, NULL);
  350. #else
  351. recv_dgram.now = time(NULL);
  352. #endif
  353. if (E_NOTICE >= diagopts.msglevel) {
  354. snprintf_r(exitmsg, sizeof(exitmsg), "exit(%d)", recv_dgram.exitcode?recv_dgram.exitcode:1);
  355. msg2(&recv_dgram.now, E_NOTICE, recv_dgram.exitcode?recv_dgram.exitcode:1, 1, exitmsg);
  356. }
  357. exit(recv_dgram.exitcode?recv_dgram.exitcode:1);
  358. case DIAG_OP_MSG:
  359. if (recv_dgram._errno) {
  360. /* there might be a %m control in the string (glibc compatible,
  361. replace with strerror(...errno) ) */
  362. char text[TEXTLEN];
  363. errno = recv_dgram._errno;
  364. snprinterr(text, TEXTLEN, recv_dgram.text);
  365. msg2(&recv_dgram.now, recv_dgram.level, recv_dgram.exitcode, 1, text);
  366. } else {
  367. msg2(&recv_dgram.now, recv_dgram.level, recv_dgram.exitcode, 1, recv_dgram.text);
  368. }
  369. break;
  370. }
  371. }
  372. }
  373. /* use a new log output file descriptor that is dup'ed from the current one.
  374. this is useful when socat logs to stderr but fd 2 should be redirected to
  375. serve other purposes */
  376. int diag_dup(void) {
  377. int newfd;
  378. DIAG_INIT;
  379. if (diagopts.logfile == NULL) {
  380. return -1;
  381. }
  382. newfd = dup(fileno(diagopts.logfile));
  383. if (diagopts.logfile != stderr) {
  384. fclose(diagopts.logfile);
  385. }
  386. if (newfd >= 0) {
  387. diagopts.logfile = fdopen(newfd, "w");
  388. }
  389. return newfd;
  390. }
  391. /* this function is kind of async-signal-safe exit(). When invoked from signal
  392. handler it defers exit. */
  393. void diag_exit(int status) {
  394. struct diag_dgram diag_dgram;
  395. if (diag_in_handler && !diag_immediate_exit) {
  396. diag_dgram.op = DIAG_OP_EXIT;
  397. diag_dgram.exitcode = status;
  398. send(diag_sock_send, &diag_dgram, sizeof(diag_dgram)-TEXTLEN, MSG_DONTWAIT
  399. #ifdef MSG_NOSIGNAL
  400. |MSG_NOSIGNAL
  401. #endif
  402. );
  403. return;
  404. }
  405. _diag_exit(status);
  406. }
  407. static void _diag_exit(int status) {
  408. Exit(status);
  409. }
  410. /* a function that appears to the application like select() but that also
  411. monitors the diag socket diag_sock_recv and processes its messages.
  412. Do not call from within a signal handler. */
  413. int diag_select(int nfds, fd_set *readfds, fd_set *writefds,
  414. fd_set *exceptfds, struct timeval *timeout) {
  415. int result;
  416. fd_set save_readfds, save_writefds, save_exceptfds;
  417. if (readfds) { memcpy(&save_readfds, readfds, sizeof(*readfds)); }
  418. if (writefds) { memcpy(&save_writefds, writefds, sizeof(*writefds)); }
  419. if (exceptfds) { memcpy(&save_exceptfds, exceptfds, sizeof(*exceptfds)); }
  420. while (1) {
  421. FD_SET(diag_sock_recv, readfds);
  422. result = Select(nfds, readfds, writefds,
  423. exceptfds, timeout);
  424. if (!FD_ISSET(diag_sock_recv, readfds)) {
  425. /* select terminated not due to diag_sock_recv, normalt continuation */
  426. break;
  427. }
  428. diag_flush();
  429. if (readfds) { memcpy(readfds, &save_readfds, sizeof(*readfds)); }
  430. if (writefds) { memcpy(writefds, &save_writefds, sizeof(*writefds)); }
  431. if (exceptfds) { memcpy(exceptfds, &save_exceptfds, sizeof(*exceptfds)); }
  432. }
  433. return result;
  434. }