1 /*- 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.proprietary.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)logent.c 8.1 (Berkeley) 06/06/93"; 10 #endif /* not lint */ 11 12 #include "uucp.h" 13 #ifdef BSD4_2 14 #include <sys/time.h> 15 #else 16 #include <time.h> 17 #endif 18 #if defined(USG) || defined(BSD4_2) 19 #include <fcntl.h> 20 #endif 21 22 extern int errno; 23 extern int sys_nerr; 24 extern const char *const sys_errlist[]; 25 26 static FILE *Lp = NULL; 27 static FILE *Sp = NULL; 28 #ifndef USE_SYSLOG 29 static FILE *Ep = NULL; 30 #endif /* !USE_SYSLOG */ 31 static int pid = 0; 32 33 /*LINTLIBRARY*/ 34 35 /* 36 * make log entry 37 */ 38 FILE * 39 get_logfd(pname, logfilename) 40 char *pname; 41 char *logfilename; 42 { 43 FILE *fp; 44 int savemask; 45 #ifdef LOGBYSITE 46 char lfile[MAXFULLNAME]; 47 #endif LOGBYSITE 48 49 savemask = umask(LOGMASK); 50 #ifdef LOGBYSITE 51 if (pname != NULL) { 52 (void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, pname, Rmtname); 53 logfilename = lfile; 54 } 55 #endif LOGBYSITE 56 fp = fopen(logfilename, "a"); 57 umask(savemask); 58 if (fp) { 59 #ifdef F_SETFL 60 int flags; 61 flags = fcntl(fileno(fp), F_GETFL, 0); 62 fcntl(fileno(Lp), F_SETFL, flags|O_APPEND); 63 #endif /* F_SETFL */ 64 fioclex(fileno(fp)); 65 } else /* we really want to log this, but it's the logging that failed*/ 66 perror(logfilename); 67 return fp; 68 } 69 70 /* 71 * make a log entry 72 */ 73 mlogent(fp, status, text) 74 char *text, *status; 75 register FILE *fp; 76 { 77 register struct tm *tp; 78 extern struct tm *localtime(); 79 80 if (text == NULL) 81 text = ""; 82 if (status == NULL) 83 status = ""; 84 if (pid == 0) 85 pid = getpid(); 86 #ifdef USG 87 time(&Now.time); 88 Now.millitm = 0; 89 #else !USG 90 ftime(&Now); 91 #endif !USG 92 tp = localtime(&Now.time); 93 #ifdef USG 94 fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ", 95 #else !USG 96 fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ", 97 #endif !USG 98 User, Rmtname, tp->tm_mon + 1, tp->tm_mday, 99 tp->tm_hour, tp->tm_min, pid); 100 fprintf(fp, "%s %s\n", status, text); 101 102 /* Since it's buffered */ 103 #ifndef F_SETFL 104 lseek (fileno(fp), (long)0, 2); 105 #endif !F_SETFL 106 fflush (fp); 107 if (Debug) { 108 fprintf(stderr, "%s %s ", User, Rmtname); 109 #ifdef USG 110 fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1, 111 tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 112 #else !USG 113 fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, 114 tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 115 #endif !USG 116 fprintf(stderr, "%s %s\n", status, text); 117 } 118 } 119 120 /* 121 * close log file 122 */ 123 logcls() 124 { 125 if (Lp != NULL) 126 fclose(Lp); 127 Lp = NULL; 128 129 if (Sp != NULL) 130 fclose (Sp); 131 Sp = NULL; 132 #ifndef USE_SYSLOG 133 if (Ep != NULL) 134 fclose (Ep); 135 Ep = NULL; 136 #endif /* !USE_SYSLOG */ 137 } 138 139 /* 140 * Arrange to close fd on exec(II). 141 * Otherwise unwanted file descriptors are inherited 142 * by other programs. And that may be a security hole. 143 */ 144 #ifndef USG 145 #include <sgtty.h> 146 #endif 147 148 fioclex(fd) 149 int fd; 150 { 151 register int ret; 152 153 #if defined(USG) || defined(BSD4_2) 154 ret = fcntl(fd, F_SETFD, 1); /* Steve Bellovin says this does it */ 155 #else 156 ret = ioctl(fd, FIOCLEX, STBNULL); 157 #endif 158 if (ret) 159 DEBUG(2, "CAN'T FIOCLEX %d\n", fd); 160 } 161 162 logent(text, status) 163 char *text, *status; 164 { 165 if (Lp == NULL) 166 Lp = get_logfd(Progname, LOGFILE); 167 168 mlogent(Lp, status, text); 169 } 170 171 /* 172 * make system log entry 173 */ 174 log_xferstats(text) 175 char *text; 176 { 177 char tbuf[BUFSIZ]; 178 if (Sp == NULL) 179 Sp = get_logfd("xferstats", SYSLOG); 180 sprintf(tbuf, "(%ld.%02u)", Now.time, Now.millitm/10); 181 mlogent(Sp, tbuf, text); 182 } 183 184 #ifndef USE_SYSLOG 185 /* 186 * This is for sites that don't have a decent syslog() in their library 187 * This routine would be a lot simpler if syslog() didn't permit %m 188 * (or if printf did!) 189 */ 190 syslog(priority, format, p0, p1, p2, p3, p4) 191 int priority; 192 char *format; 193 { 194 char nformat[BUFSIZ], sbuf[BUFSIZ]; 195 register char *s, *d; 196 register int c; 197 long now; 198 199 s = format; 200 d = nformat; 201 while ((c = *s++) != '\0' && c != '\n' && d < &nformat[BUFSIZ]) { 202 if (c != '%') { 203 *d++ = c; 204 continue; 205 } 206 if ((c = *s++) != 'm') { 207 *d++ = '%'; 208 *d++ = c; 209 continue; 210 } 211 if ((unsigned)errno > sys_nerr) 212 sprintf(d, "error %d", errno); 213 else 214 strcpy(d, sys_errlist[errno]); 215 d += strlen(d); 216 } 217 *d = '\0'; 218 219 if (Ep == NULL) 220 Ep = get_logfd(NULL, ERRLOG); 221 sprintf(sbuf, nformat, p0, p1, p2, p3, p4); 222 mlogent(Ep, sbuf, ""); 223 } 224 #endif /* !USE_SYSLOG */ 225