home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / syslog.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  214 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)syslog.c    5.34 (Berkeley) 6/26/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define _KERNEL
  39. #include "ixemul.h"
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/file.h>
  43. #include <sys/syslog.h>
  44. #include <sys/uio.h>
  45. #include <sys/errno.h>
  46. #include <netdb.h>
  47. #include <string.h>
  48. #include <stdarg.h>
  49. #include <time.h>
  50. #include <unistd.h>
  51. #include <paths.h>
  52. #include <stdio.h>
  53.  
  54. #define LogFile (u.u_LogFile)
  55. #define LogStat (u.u_LogStat)
  56. #define LogTag (u.u_LogTag)
  57. #define LogFacility (u.u_LogFacility)
  58. #define LogMask (u.u_LogMask)
  59.  
  60. /*
  61.  * syslog, vsyslog --
  62.  *    print message on log file; output is intended for syslogd(8).
  63.  */
  64.  
  65. void
  66. vsyslog(int pri, const char *fmt, va_list ap)
  67. {
  68.     register int cnt;
  69.     register char *p;
  70.     /* time_t now, time(); */
  71.     int fd, saved_errno;
  72.     char tbuf[2048], fmt_cpy[1024], *stdp = NULL, *ctime();
  73.     register struct user *usr = &u;
  74.  
  75.     if (usr->u_ixnetbase) {
  76.     netcall(NET_vsyslog, pri, fmt, ap);
  77.     }
  78.     else {
  79.     /* check for invalid bits or no priority set */
  80.     if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)) ||
  81.         !(LOG_MASK(pri) & LogMask))
  82.         return;
  83.  
  84.     saved_errno = errno;
  85.  
  86.     /* set default facility if none specified */
  87.     if ((pri & LOG_FACMASK) == 0)
  88.         pri |= LogFacility;
  89.  
  90.     /* build the message */
  91.     /* (void)syscall(SYS_time,&now); */
  92.     /*(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); */
  93.     (void)sprintf(tbuf, "<%d> ", pri);
  94.     for (p = tbuf; *p; ++p);
  95.  
  96.     if (LogStat & LOG_PERROR)
  97.         stdp = p;
  98.  
  99.     if (LogTag) {
  100.         (void)strcpy(p, LogTag);
  101.         for (; *p; ++p);
  102.     }
  103.     if (LogStat & LOG_PID) {
  104.         (void)sprintf(p, "[%d]", getpid());
  105.         for (; *p; ++p)
  106.         ;
  107.     }
  108.     if (LogTag) {
  109.         *p++ = ':';
  110.         *p++ = ' ';
  111.     }
  112.  
  113.     /* substitute error message for %m */
  114.     {
  115.         register char ch, *t1, *t2;
  116.         char *strerror();
  117.  
  118.             for (t1 = fmt_cpy; (ch = *fmt); ++fmt)
  119.         if (ch == '%' && fmt[1] == 'm') {
  120.             ++fmt;
  121.             for (t2 = strerror(saved_errno);
  122.                         (*t1 = *t2++); ++t1);
  123.         }
  124.         else
  125.             *t1++ = ch;
  126.         *t1 = '\0';
  127.     }
  128.  
  129.     (void)vsprintf(p, fmt_cpy, ap);
  130.  
  131.     cnt = strlen(tbuf);
  132.  
  133.     /* output to stderr if requested */
  134.     if (LogStat & LOG_PERROR) {
  135.         struct iovec iov[2];
  136.         register struct iovec *v = iov;
  137.  
  138.         v->iov_base = stdp;
  139.         v->iov_len = cnt - (stdp - tbuf);
  140.         ++v;
  141.         v->iov_base = "\n";
  142.         v->iov_len = 1;
  143.         (void)syscall(SYS_writev,STDERR_FILENO, iov, 2);
  144.     }
  145.     /* see if should attempt the console */
  146.     if (!(LogStat&LOG_CONS))
  147.         return;
  148.  
  149.     /*
  150.      * Output the message to the console; don't worry about blocking,
  151.      * if console blocks everything will.  Make sure the error reported
  152.      * is the one from the syslogd failure.
  153.      */
  154.     if ((fd = syscall(SYS_open,_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  155.         (void)strcat(tbuf, "\r\n");
  156.         cnt += 2;
  157.         p = index(tbuf, '>') + 1;
  158.         (void)syscall(SYS_write,fd, p, cnt - (p - tbuf));
  159.         (void)syscall(SYS_close,fd);
  160.     }
  161.     }
  162. }
  163.  
  164. void
  165. syslog(int pri, const 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. void
  175. openlog(const char *ident, int logstat, int logfac)
  176. {
  177.     if (u.u_ixnetbase) {
  178.     netcall(NET_openlog, ident, logstat, logfac);
  179.     }
  180.     else {
  181.     if (ident != NULL)
  182.         LogTag = (char *)ident;
  183.     LogStat = logstat;
  184.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  185.         LogFacility = logfac;
  186.     }
  187. }
  188.  
  189. void
  190. closelog(void)
  191. {
  192.     if (u.u_ixnetbase) {
  193.     netcall(NET_closelog);
  194.     }
  195.     else {
  196.     syscall(SYS_close,LogFile);
  197.     LogFile = -1;
  198.     }
  199. }
  200.  
  201. /* setlogmask -- set the log mask level */
  202. int
  203. setlogmask(int pmask)
  204. {
  205.   int omask;
  206.  
  207.   if (u.u_ixnetbase)
  208.     return netcall(NET_setlogmask, pmask);
  209.   omask = LogMask;
  210.   if (pmask != 0)
  211.     LogMask = pmask;
  212.   return omask;
  213. }
  214.