home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / inetd10.zip / SYSLOG.C < prev    next >
Text File  |  1995-04-02  |  9KB  |  301 lines

  1. /*  
  2. **  Modified by Jochen Friedrich <jochen@audio.ruessel.sub.org> for OS/2.
  3. */
  4. /*
  5.  * Copyright (c) 1983, 1988 Regents of the University of California.
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted provided
  9.  * that: (1) source distributions retain this entire copyright notice and
  10.  * comment, and (2) distributions including binaries display the following
  11.  * acknowledgement:  ``This product includes software developed by the
  12.  * University of California, Berkeley and its contributors'' in the
  13.  * documentation or other materials provided with the distribution and in
  14.  * all advertising materials mentioning features or use of this software.
  15.  * Neither the name of the University nor the names of its contributors may
  16.  * be used to endorse or promote products derived from this software without
  17.  * specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. /*
  24.  * SYSLOG -- print message on log file
  25.  *
  26.  * This routine looks a lot like printf, except that it outputs to the
  27.  * log file instead of the standard output.  Also:
  28.  *      adds a timestamp,
  29.  *      prints the module name in front of the message,
  30.  *      has some other formatting types (or will sometime),
  31.  *      adds a newline on the end of the message.
  32.  *
  33.  * The output of this routine is intended to be read by syslogd(8).
  34.  *
  35.  * Author: Eric Allman
  36.  * Modified to use UNIX domain IPC by Ralph Campbell
  37.  */
  38.  
  39. #include <os2.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <fcntl.h>
  43. #include <netinet/in.h>
  44. #include <stdarg.h>
  45. #include <stdio.h>
  46. #include <io.h>
  47. #include <string.h>
  48. #include <time.h>
  49. #include <process.h>
  50. #include <errno.h>
  51. #include <nerrno.h>
  52. #include <utils.h>
  53. #include "syslog.h"
  54.  
  55. static int      LogFile = -1;           /* fd for log */
  56. static int      connected;              /* have done connect */
  57. static int      LogStat = 0;            /* status bits, set by openlog() */
  58. static char     *LogTag = "syslog";     /* string to tag the entry with */
  59. static int      LogFacility = LOG_USER; /* default facility code */
  60.  
  61. char * tcp_errors[] =
  62. {
  63.     "Not owner",
  64.     "Don't know the error"
  65.     "No such process",
  66.     "Interrupted system call",
  67.     "Don't know the error",
  68.     "No such device or address",
  69.     "Don't know the error",
  70.     "Don't know the error",
  71.     "Bad file number",
  72.     "Don't know the error",
  73.     "Don't know the error",
  74.     "Don't know the error",
  75.     "Permission denied",
  76.     "Bad address",
  77.     "Don't know the error",
  78.     "Don't know the error",
  79.     "Don't know the error",
  80.     "Don't know the error",
  81.     "Don't know the error",
  82.     "Don't know the error",
  83.     "Don't know the error",
  84.     "Invalid argument",
  85.     "Don't know the error",
  86.     "Too many open files",
  87.     "Don't know the error",
  88.     "Don't know the error",
  89.     "Don't know the error",
  90.     "Don't know the error",
  91.     "Don't know the error",
  92.     "Don't know the error",
  93.     "Don't know the error",
  94.     "Broken pipe",
  95.     "Don't know the error",
  96.     "Don't know the error",
  97.     "Operation would block",
  98.     "Operation now in progress",
  99.     "Operation already in progress",
  100.     "Socket operation on non-socket",
  101.     "Destination address required",
  102.     "Message too long",
  103.     "Protocol wrong type for socket",
  104.     "Protocol not available",
  105.     "Protocol not supported",
  106.     "Socket type not supported",
  107.     "Operation not supported on socket",
  108.     "Protocol family not supported",
  109.     "Address family not supported by protocol family",
  110.     "Address already in use",
  111.     "Can't assign requested address",
  112.     "Network is down",
  113.     "Network is unreachable",
  114.     "Network dropped connection on reset",
  115.     "Software caused connection abort",
  116.     "Connection reset by peer",
  117.     "No buffer space available",
  118.     "Socket is already connected",
  119.     "Socket is not connected",
  120.     "Can't send after socket shutdown",
  121.     "Too many references: can't splice",
  122.     "Connection timed out",
  123.     "Connection refused",
  124.     "Too many levels of symbolic links",
  125.     "File name too long",
  126.     "Host is down",
  127.     "No route to host",
  128.     "Directory not empty",
  129.     "Don't know the error",
  130.     "Don't know the error",
  131.     "Don't know the error",
  132.     "Don't know the error",
  133.     "Don't know the error",
  134.     "Don't know the error",
  135.     "Don't know the error",
  136.     "Don't know the error",
  137.     "Don't know the error",
  138.     "Don't know the error",
  139.     "Don't know the error",
  140.     "Don't know the error",
  141.     "Don't know the error",
  142.     "Don't know the error",
  143.     "Don't know the error",
  144.     "Don't know the error",
  145.     "Don't know the error",
  146.     "Don't know the error",
  147.     "Don't know the error",
  148.     "Don't know the error",
  149.     "Don't know the error",
  150.     "Don't know the error",
  151.     "Don't know the error",
  152.     "Don't know the error",
  153.     "Don't know the error",
  154.     "Don't know the error",
  155.     "Don't know the error",
  156.     "Don't know the error",
  157.     "Don't know the error",
  158.     "Don't know the error",
  159.     "Don't know the error",
  160.     "Don't know the error",
  161.     "Don't know the error",
  162.     "OS/2 Error"
  163. };
  164.  
  165. void syslog(int pri, char *fmt, ...)
  166. {
  167.         va_list ap;
  168.  
  169.         va_start(ap,fmt);
  170.         vsyslog(pri, fmt, ap);
  171.         va_end(ap);
  172. }
  173.  
  174. char *mystrerror(int saved_errno)
  175. {
  176.     if ((saved_errno > SOCBASEERR) && (saved_errno < SOCEOS2ERR ))
  177.         return tcp_errors[saved_errno - SOCBASEERR - 1];
  178.     else
  179.         return strerror(saved_errno);
  180. }
  181.  
  182. void vsyslog(int pri, char *fmt, va_list ap)
  183. {
  184.         extern int errno;
  185.         register int cnt;
  186.         register char *p;
  187.         time_t now;
  188.         int fd, saved_errno;
  189.         char tbuf[2048], fmt_cpy[1024], *stdp;
  190.  
  191.         saved_errno = errno;
  192.  
  193.         /* see if we should just throw out this message */
  194.         if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
  195.                 return;
  196.         if (LogFile < 0 || !connected)
  197.                 openlog(LogTag, LogStat | LOG_NDELAY, 0);
  198.  
  199.         /* set default facility if none specified */
  200.         if ((pri & LOG_FACMASK) == 0)
  201.                 pri |= LogFacility;
  202.  
  203.         /* build the message */
  204.         (void)time(&now);
  205.         (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  206.         for (p = tbuf; *p; ++p);
  207.         if (LogStat & LOG_PERROR)
  208.                 stdp = p;
  209.         if (LogTag) {
  210.                 (void)strcpy(p, LogTag);
  211.                 for (; *p; ++p);
  212.         }
  213.         if (LogStat & LOG_PID) {
  214.                 (void)sprintf(p, "[%d]", getpid() );
  215.                 for (; *p; ++p);
  216.         }
  217.         if (LogTag) {
  218.                 *p++ = ':';
  219.                 *p++ = ' ';
  220.         }
  221.  
  222.         /* substitute error message for %m */
  223.         {
  224.                 register char ch, *t1, *t2;
  225.  
  226.                 for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  227.                         if (ch == '%' && fmt[1] == 'm') {
  228.                                 ++fmt;
  229.                                 for (t2 = mystrerror(saved_errno);
  230.                                     *t1 = *t2++; ++t1);
  231.                         }
  232.                         else
  233.                                 *t1++ = ch;
  234.                 *t1 = '\0';
  235.         }
  236.  
  237.         (void)vsprintf(p, fmt_cpy, ap);
  238.  
  239.         cnt = strlen(tbuf);
  240.  
  241.         /* output to stderr if requested */
  242.         if (LogStat & LOG_PERROR) {
  243.                 write(2, tbuf,cnt);
  244.                 write(2,"\r\n",2);
  245.         }
  246.  
  247.         /* output the message to the local logger */
  248.         if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS))
  249.                 return;
  250.  
  251. }
  252.  
  253. static struct sockaddr_in SyslogAddr;   /* AF_INET address of local logger */
  254. /*
  255.  * OPENLOG -- open system log
  256.  */
  257. void openlog(char *ident, int logstat, int logfac)
  258. {
  259.         sock_init();
  260.         if (ident != NULL)
  261.                 LogTag = ident;
  262.         LogStat = logstat;
  263.         if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  264.                 LogFacility = logfac;
  265.         if (LogFile == -1) {
  266.                 SyslogAddr.sin_family = AF_INET;
  267.                 SyslogAddr.sin_port = htons(514);
  268.                 SyslogAddr.sin_addr.s_addr = INADDR_ANY;
  269.                 if (LogStat & LOG_NDELAY) {
  270.                         LogFile = socket(AF_INET, SOCK_DGRAM, 0);
  271.                 }
  272.         }
  273.         if (LogFile != -1 && !connected &&
  274.             connect(LogFile, (struct sockaddr*) &SyslogAddr, sizeof(SyslogAddr)) != -1)
  275.                 connected = 1;
  276. }
  277.  
  278. /*
  279.  * CLOSELOG -- close the system log
  280.  */
  281. void closelog(void)
  282. {
  283.         (void) soclose(LogFile);
  284.         LogFile = -1;
  285.         connected = 0;
  286. }
  287.  
  288. static int      LogMask = 0xff;         /* mask of priorities to be logged */
  289. /*
  290.  * SETLOGMASK -- set the log mask level
  291.  */
  292. int setlogmask(int pmask)
  293. {
  294.         int omask;
  295.  
  296.         omask = LogMask;
  297.         if (pmask != 0)
  298.                 LogMask = pmask;
  299.         return (omask);
  300. }
  301.