home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / netkit-a.06 / netkit-a / NetKit-A-0.06 / nfs-server-2.0 / logging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-04  |  4.4 KB  |  219 lines

  1. /*
  2.  * logging    This module handles the logging of requests.
  3.  *
  4.  * TODO:    Merge the two "XXX_log() calls.
  5.  *
  6.  * Authors:    Donald J. Becker, <becker@super.org>
  7.  *        Rick Sladkey, <jrs@world.std.com>
  8.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  9.  *
  10.  *        This software maybe be used for any purpose provided
  11.  *        the above copyright notice is retained.  It is supplied
  12.  *        as is, with no warranty expressed or implied.
  13.  */
  14.  
  15. #include "nfsd.h"
  16.  
  17. #ifdef HAVE_SYSLOG_H
  18. #include <syslog.h>
  19. #else
  20. #define LOG_FILE    "/etc/%s.log"
  21. #endif
  22.  
  23. static char printbuf[1024];        /* local print buffer        */
  24. static int logging = 0;            /* enable/disable DEBUG logs    */
  25. #ifndef HAVE_SYSLOG_H
  26. static char log_name[32];        /* name of this program        */
  27. static FILE *log_fp = (FILE *)NULL;    /* fp for the log file        */
  28. #endif
  29.  
  30. void log_open(progname)
  31. char *progname;
  32. {
  33. #ifdef HAVE_SYSLOG_H
  34.     openlog(progname, LOG_PID, LOG_DAEMON);
  35. #else
  36.     char path[1024];
  37.  
  38.     sprintf(path, LOG_FILE, progname);
  39.     log_fp = fopen(path, "a");
  40.     if (log_fp == (FILE *) NULL)
  41.         return;
  42.     (void) setbuf(log_fp, (char *) NULL);
  43.     sprintf(log_name, "%s[%d]", progname, getpid());
  44. #endif
  45. }
  46.  
  47. void toggle_logging(sig)
  48. int sig;
  49. {
  50.     (void) signal(sig, toggle_logging);
  51.     logging = 1 - logging;
  52. }
  53.  
  54. /* Write something to the system logfile. */
  55. #ifdef __STDC__
  56. void dprintf(int level, const char *fmt, ...)
  57. #else
  58. void dprintf(level, fmt, va_alist)
  59. int level;
  60. char *fmt;
  61. va_dcl
  62. #endif
  63. {
  64.     char buff[1024];
  65.     va_list args;
  66. #ifndef HAVE_SYSLOG_H
  67.     time_t now;
  68.     struct tm *tm;
  69. #endif
  70.  
  71. #ifdef __STDC__
  72.     va_start(args, fmt);
  73. #else
  74.     va_start(args);
  75. #endif
  76. #ifdef HAVE_VPRINTF
  77.     vsprintf(buff, fmt, args);
  78. #else
  79.     /* Figure out how to use _doprnt here. */
  80. #endif
  81.     va_end(args);
  82.  
  83.     if ((level == 0) || (level == 1 && logging == 1)) {
  84. #ifdef HAVE_SYSLOG_H
  85.         (void) syslog(LOG_ERR, "%s", buff);
  86. #else
  87.         if (log_fp == (FILE *) NULL)
  88.             return;
  89.         (void) time(&now);
  90.         tm = localtime(&now);
  91.         fprintf(log_fp, "%s %02d/%02d/%02d %02d:%02d %s",
  92.               log_name, tm->tm_mon + 1, tm->tm_mday, tm->tm_year,
  93.             tm->tm_hour, tm->tm_min, buff);
  94. #endif
  95.     }
  96. }
  97.  
  98. /* Log an incoming call. */
  99. void log_call(rqstp, xname, arg)
  100. struct svc_req *rqstp;
  101. char *xname;
  102. char *arg;
  103. {
  104.     char buff[1024];
  105.     register char *sp;
  106.     int i;
  107.  
  108.     if (logging == 0)
  109.         return;
  110.  
  111.     sp = buff;
  112.     sprintf(sp, "%s [%d ", xname, rqstp->rq_cred.oa_flavor);
  113.     sp += strlen(sp);
  114.     if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
  115.         struct authunix_parms *unix_cred;
  116.         struct tm *tm;
  117.  
  118.         unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
  119.         tm = localtime(&unix_cred->aup_time);
  120.         sprintf(sp, "%d/%d/%d %02d:%02d:%02d %s %d.%d",
  121.             tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  122.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  123.             unix_cred->aup_machname,
  124.             unix_cred->aup_uid,
  125.             unix_cred->aup_gid);
  126.         sp += strlen(sp);
  127.         if ((int) unix_cred->aup_len > 0) {
  128.             sprintf(sp, "+%d", unix_cred->aup_gids[0]);
  129.             sp += strlen(sp);
  130.             for (i = 1; i < unix_cred->aup_len; i++) {
  131.                 sprintf(sp, ",%d", unix_cred->aup_gids[i]);
  132.                 sp += strlen(sp);
  133.             }
  134.         }
  135.     }
  136.     dprintf(1, "%s]\n\t%s\n", buff, arg);
  137. }
  138.  
  139. char *pr_void()
  140. {
  141.     return ("");
  142. }
  143.  
  144. char *pr_nfs_fh(argp)
  145. nfs_fh *argp;
  146. {
  147.     return (fh_pr(argp));
  148. }
  149.  
  150. char *pr_sattrargs(argp)
  151. sattrargs *argp;
  152. {
  153.     sprintf(printbuf, "fh:%s m:%0o u/g:%d/%d size:%d atime:%#x mtime:%#x",
  154.         fh_pr(&argp->file), argp->attributes.mode,
  155.         argp->attributes.uid, argp->attributes.gid,
  156.         argp->attributes.size,
  157.      argp->attributes.atime.seconds, argp->attributes.mtime.seconds);
  158.     return (printbuf);
  159. }
  160.  
  161. char *pr_diropargs(argp)
  162. diropargs *argp;
  163. {
  164.     sprintf(printbuf, "fh:%s n:%s", fh_pr(&(argp->dir)), argp->name);
  165.     return (printbuf);
  166. }
  167.  
  168. char *pr_readargs(argp)
  169. readargs *argp;
  170. {
  171.     sprintf(printbuf, "%s: %d bytes at %d",
  172.         fh_pr(&argp->file), argp->count, argp->offset);
  173.     return (printbuf);
  174. }
  175.  
  176. char *pr_writeargs(argp)
  177. writeargs *argp;
  178. {
  179.     sprintf(printbuf, "%s: %d bytes at %d",
  180.         fh_pr(&argp->file), argp->data.data_len, argp->offset);
  181.     return (printbuf);
  182. }
  183.  
  184. char *pr_createargs(argp)
  185. createargs *argp;
  186. {
  187.     sprintf(printbuf, "fh:%s n:%s m:%0o u/g:%d/%d size:%d atime:%#x mtime:%#x",
  188.         fh_pr(&argp->where.dir), argp->where.name,
  189.         argp->attributes.mode, argp->attributes.uid,
  190.         argp->attributes.gid, argp->attributes.size,
  191.      argp->attributes.atime.seconds, argp->attributes.mtime.seconds);
  192.     return (printbuf);
  193. }
  194.  
  195. char *pr_renameargs(argp)
  196. renameargs *argp;
  197. {
  198.     return ("");
  199. }
  200.  
  201. char *pr_linkargs(argp)
  202. linkargs *argp;
  203. {
  204.     return ("");
  205. }
  206.  
  207. char *pr_symlinkargs(argp)
  208. symlinkargs *argp;
  209. {
  210.     return ("");
  211. }
  212.  
  213. char *pr_readdirargs(argp)
  214. readdirargs *argp;
  215. {
  216.     return (fh_pr(&argp->dir));
  217. }
  218.  
  219.