util: Remove undocumented magic of mrafs-style logs
[openafs.git] / src / util / serverLog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*  serverLog.c     - Server logging                                      */
11 /*                                                                        */
12 /*  Information Technology Center                                         */
13 /*  Date: 05/21/97                                                        */
14 /*                                                                        */
15 /*  Function    - These routiens implement logging from the servers       */
16 /*                                                                        */
17 /* ********************************************************************** */
18
19 #include <afsconfig.h>
20 #include <afs/param.h>
21 #include <afs/stds.h>
22
23 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
24
25 #include <roken.h>              /* Must come after procmgmt.h */
26 #ifdef AFS_PTHREAD_ENV
27  #include <opr/softsig.h>
28  #include <afs/procmgmt_softsig.h>      /* Must come after softsig.h */
29 #endif
30 #include <afs/opr.h>
31 #include "afsutil.h"
32 #include "fileutil.h"
33 #include <lwp.h>
34
35 #if defined(AFS_PTHREAD_ENV)
36 #include <pthread.h>
37 static pthread_once_t serverLogOnce = PTHREAD_ONCE_INIT;
38 static pthread_mutex_t serverLogMutex;
39 #define LOCK_SERVERLOG() opr_Verify(pthread_mutex_lock(&serverLogMutex) == 0)
40 #define UNLOCK_SERVERLOG() opr_Verify(pthread_mutex_unlock(&serverLogMutex) == 0)
41
42 #ifdef AFS_NT40_ENV
43 #define NULLDEV "NUL"
44 #else
45 #define NULLDEV "/dev/null"
46 #endif
47
48 #else /* AFS_PTHREAD_ENV */
49 #define LOCK_SERVERLOG()
50 #define UNLOCK_SERVERLOG()
51 #endif /* AFS_PTHREAD_ENV */
52
53 #ifdef AFS_NT40_ENV
54 #define F_OK 0
55 #define O_NONBLOCK 0
56 #endif
57
58 static int
59 dummyThreadNum(void)
60 {
61     return -1;
62 }
63 static int (*threadNumProgram) (void) = dummyThreadNum;
64
65 static int serverLogFD = -1;
66
67 #ifndef AFS_NT40_ENV
68 int serverLogSyslog = 0;
69 int serverLogSyslogFacility = LOG_DAEMON;
70 char *serverLogSyslogTag = 0;
71 #endif
72
73 int LogLevel;
74 int mrafsStyleLogs = 0;
75 static int threadIdLogs = 0;
76 static int resetSignals = 0;
77 static char *ourName = NULL;
78
79 static void RotateLogFile(void);
80
81 void
82 SetLogThreadNumProgram(int (*func) (void) )
83 {
84     threadNumProgram = func;
85 }
86
87 void
88 WriteLogBuffer(char *buf, afs_uint32 len)
89 {
90     LOCK_SERVERLOG();
91     if (serverLogFD >= 0) {
92         if (write(serverLogFD, buf, len) < 0)
93             ; /* don't care */
94     }
95     UNLOCK_SERVERLOG();
96 }
97
98 int
99 LogThreadNum(void)
100 {
101   return (*threadNumProgram) ();
102 }
103
104 void
105 vFSLog(const char *format, va_list args)
106 {
107     time_t currenttime;
108     char tbuffer[1024];
109     char *info;
110     size_t len;
111     struct tm tm;
112     int num;
113
114     currenttime = time(NULL);
115     len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
116                    localtime_r(&currenttime, &tm));
117     info = &tbuffer[len];
118
119     if (threadIdLogs) {
120         num = (*threadNumProgram) ();
121         if (num > -1) {
122             snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
123                      num);
124             info += strlen(info);
125         }
126     }
127
128     vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
129
130     len = strlen(tbuffer);
131     LOCK_SERVERLOG();
132 #ifndef AFS_NT40_ENV
133     if (serverLogSyslog) {
134         syslog(LOG_INFO, "%s", info);
135     } else
136 #endif
137     if (serverLogFD >= 0) {
138         if (write(serverLogFD, tbuffer, len) < 0)
139             ; /* don't care */
140     }
141     UNLOCK_SERVERLOG();
142
143 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
144     if (!serverLogSyslog) {
145         fflush(stdout);
146         fflush(stderr);         /* in case they're sharing the same FD */
147     }
148 #endif
149 }                               /*vFSLog */
150
151 /* VARARGS1 */
152 /*@printflike@*/
153 void
154 FSLog(const char *format, ...)
155 {
156     va_list args;
157
158     va_start(args, format);
159     vFSLog(format, args);
160     va_end(args);
161 }                               /*FSLog */
162
163 void
164 LogCommandLine(int argc, char **argv, const char *progname,
165                const char *version, const char *logstring,
166                void (*log) (const char *format, ...))
167 {
168     int i, l;
169     char *commandLine, *cx;
170
171     opr_Assert(argc > 0);
172
173     for (l = i = 0; i < argc; i++)
174         l += strlen(argv[i]) + 1;
175     if ((commandLine = malloc(l))) {
176         for (cx = commandLine, i = 0; i < argc; i++) {
177             strcpy(cx, argv[i]);
178             cx += strlen(cx);
179             *(cx++) = ' ';
180         }
181         commandLine[l-1] = '\0';
182         (*log)("%s %s %s%s(%s)\n", logstring, progname,
183                     version, strlen(version)>0?" ":"", commandLine);
184         free(commandLine);
185     } else {
186         /* What, we're out of memory already!? */
187         (*log)("%s %s%s%s\n", logstring,
188               progname, strlen(version)>0?" ":"", version);
189     }
190 }
191
192 void
193 LogDesWarning(void)
194 {
195     /* The blank newlines help this stand out a bit more in the log. */
196     ViceLog(0, ("\n"));
197     ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
198     ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
199     ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
200     ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
201     ViceLog(0, ("WARNING: for details.\n"));
202     ViceLog(0, ("\n"));
203 }
204
205 static void*
206 DebugOn(void *param)
207 {
208     int loglevel = (intptr_t)param;
209     if (loglevel == 0) {
210         ViceLog(0, ("Reset Debug levels to 0\n"));
211     } else {
212         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
213     }
214     return 0;
215 }                               /*DebugOn */
216
217
218
219 void
220 SetDebug_Signal(int signo)
221 {
222     if (LogLevel > 0) {
223         LogLevel *= 5;
224
225 #if defined(AFS_PTHREAD_ENV)
226         if (LogLevel > 1 && threadNumProgram != NULL &&
227             threadIdLogs == 0) {
228             threadIdLogs = 1;
229         }
230 #endif
231     } else {
232         LogLevel = 1;
233
234 #if defined(AFS_PTHREAD_ENV)
235         if (threadIdLogs == 1)
236             threadIdLogs = 0;
237 #endif
238     }
239 #if defined(AFS_PTHREAD_ENV)
240     DebugOn((void *)(intptr_t)LogLevel);
241 #else /* AFS_PTHREAD_ENV */
242     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
243 #endif /* AFS_PTHREAD_ENV */
244
245     if (resetSignals) {
246         /* When pthreaded softsig handlers are not in use, some platforms
247          * require this signal handler to be set again. */
248         (void)signal(signo, SetDebug_Signal);
249     }
250 }                               /*SetDebug_Signal */
251
252 void
253 ResetDebug_Signal(int signo)
254 {
255     LogLevel = 0;
256
257 #if defined(AFS_PTHREAD_ENV)
258     DebugOn((void *)(intptr_t)LogLevel);
259 #else /* AFS_PTHREAD_ENV */
260     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
261 #endif /* AFS_PTHREAD_ENV */
262
263     if (resetSignals) {
264         /* When pthreaded softsig handlers are not in use, some platforms
265          * require this signal handler to be set again. */
266         (void)signal(signo, ResetDebug_Signal);
267     }
268 #if defined(AFS_PTHREAD_ENV)
269     if (threadIdLogs == 1)
270         threadIdLogs = 0;
271 #endif
272     if (mrafsStyleLogs) {
273         RotateLogFile();
274     }
275 }                               /*ResetDebug_Signal */
276
277
278 #ifdef AFS_PTHREAD_ENV
279 /*!
280  * Register pthread-safe signal handlers for server log management.
281  *
282  * \note opr_softsig_Init() must be called before this function.
283  */
284 void
285 SetupLogSoftSignals(void)
286 {
287     opr_softsig_Register(SIGHUP, ResetDebug_Signal);
288     opr_softsig_Register(SIGTSTP, SetDebug_Signal);
289 #ifndef AFS_NT40_ENV
290     (void)signal(SIGPIPE, SIG_IGN);
291 #endif
292 }
293 #endif /* AFS_PTHREAD_ENV */
294
295 /*!
296  * Register signal handlers for server log management.
297  *
298  * \note This function is deprecated and should not be used
299  *       in new code. This function should be removed when
300  *       all the servers have been converted to pthreads
301  *       and lwp has been removed.
302  */
303 void
304 SetupLogSignals(void)
305 {
306     resetSignals = 1;
307     (void)signal(SIGHUP, ResetDebug_Signal);
308     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
309     (void)signal(SIGTSTP, SetDebug_Signal);
310 #ifndef AFS_NT40_ENV
311     (void)signal(SIGPIPE, SIG_IGN);
312 #endif
313 }
314
315 #if defined(AFS_PTHREAD_ENV)
316 static void
317 InitServerLogMutex(void)
318 {
319     opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
320 }
321 #endif /* AFS_PTHREAD_ENV */
322
323 int
324 OpenLog(const char *fileName)
325 {
326     /*
327      * This function should allow various libraries that inconsistently
328      * use stdout/stderr to all go to the same place
329      */
330     int tempfd, isfifo = 0;
331     int code;
332     char *nextName = NULL;
333
334 #ifndef AFS_NT40_ENV
335     struct stat statbuf;
336 #endif
337
338 #if defined(AFS_PTHREAD_ENV)
339     opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
340 #endif /* AFS_PTHREAD_ENV */
341
342 #ifndef AFS_NT40_ENV
343     if (serverLogSyslog) {
344         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
345         return (0);
346     }
347 #endif
348
349     opr_Assert(fileName != NULL);
350
351 #ifndef AFS_NT40_ENV
352     /* Support named pipes as logs by not rotating them */
353     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
354         isfifo = 1;
355     }
356 #endif
357
358     if (mrafsStyleLogs) {
359         time_t t;
360         struct stat buf;
361         int tries;
362         struct tm *timeFields;
363
364         time(&t);
365         for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
366             timeFields = localtime(&t);
367             code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
368                      fileName, timeFields->tm_year + 1900,
369                      timeFields->tm_mon + 1, timeFields->tm_mday,
370                      timeFields->tm_hour, timeFields->tm_min,
371                      timeFields->tm_sec);
372             if (code < 0) {
373                 nextName = NULL;
374                 break;
375             }
376             if (lstat(nextName, &buf) == 0) {
377                 /* Avoid clobbering a log. */
378                 free(nextName);
379                 nextName = NULL;
380             }
381         }
382     } else {
383         code = asprintf(&nextName, "%s.old", fileName);
384         if (code < 0) {
385             nextName = NULL;
386         }
387     }
388     if (nextName != NULL) {
389         if (!isfifo)
390             rk_rename(fileName, nextName);      /* Don't check the error code. */
391         free(nextName);
392     }
393
394     tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
395     if (tempfd < 0) {
396         printf("Unable to open log file %s\n", fileName);
397         return -1;
398     }
399     /* redirect stdout and stderr so random printf's don't write to data */
400     if (freopen(fileName, "a", stdout) == NULL)
401         ; /* don't care */
402     if (freopen(fileName, "a", stderr) != NULL) {
403 #ifdef HAVE_SETVBUF
404         setvbuf(stderr, NULL, _IONBF, 0);
405 #else
406         setbuf(stderr, NULL);
407 #endif
408     }
409
410     /* Save our name for reopening. */
411     free(ourName);
412     ourName = strdup(fileName);
413     opr_Assert(ourName != NULL);
414
415     serverLogFD = tempfd;
416
417     return 0;
418 }                               /*OpenLog */
419
420 int
421 ReOpenLog(const char *fileName)
422 {
423     int isfifo = 0;
424 #if !defined(AFS_NT40_ENV)
425     struct stat statbuf;
426 #endif
427
428     if (access(fileName, F_OK) == 0)
429         return 0;               /* exists, no need to reopen. */
430
431 #if !defined(AFS_NT40_ENV)
432     if (serverLogSyslog) {
433         return 0;
434     }
435
436     /* Support named pipes as logs by not rotating them */
437     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
438         isfifo = 1;
439     }
440 #endif
441
442     LOCK_SERVERLOG();
443     if (serverLogFD >= 0)
444         close(serverLogFD);
445     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
446     if (serverLogFD >= 0) {
447         if (freopen(fileName, "a", stdout) == NULL)
448             ; /* don't care */
449         if (freopen(fileName, "a", stderr) != NULL) {
450 #ifdef HAVE_SETVBUF
451             setvbuf(stderr, NULL, _IONBF, 0);
452 #else
453             setbuf(stderr, NULL);
454 #endif
455         }
456
457     }
458     UNLOCK_SERVERLOG();
459     return serverLogFD < 0 ? -1 : 0;
460 }
461
462 /*!
463  * Rotate the log file by renaming then truncating.
464  */
465 static void
466 RotateLogFile(void)
467 {
468     LOCK_SERVERLOG();
469     if (ourName != NULL) {
470         if (serverLogFD >= 0) {
471             close(serverLogFD);
472             serverLogFD = -1;
473         }
474         OpenLog(ourName);
475     }
476     UNLOCK_SERVERLOG();
477 }
478
479 /*!
480  * Close the server log file.
481  *
482  * \note Must be preceeded by OpenLog().
483  */
484 void
485 CloseLog(void)
486 {
487     LOCK_SERVERLOG();
488 #ifndef AFS_NT40_ENV
489     if (serverLogSyslog) {
490         closelog();
491     } else
492 #endif
493     {
494         if (serverLogFD >= 0) {
495             close(serverLogFD);
496             serverLogFD = -1;
497         }
498         free(ourName);
499         ourName = NULL;
500     }
501     UNLOCK_SERVERLOG();
502 }