util: remove unused printLocks variable from 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 void
80 SetLogThreadNumProgram(int (*func) (void) )
81 {
82     threadNumProgram = func;
83 }
84
85 void
86 WriteLogBuffer(char *buf, afs_uint32 len)
87 {
88     LOCK_SERVERLOG();
89     if (serverLogFD >= 0) {
90         if (write(serverLogFD, buf, len) < 0)
91             ; /* don't care */
92     }
93     UNLOCK_SERVERLOG();
94 }
95
96 int
97 LogThreadNum(void)
98 {
99   return (*threadNumProgram) ();
100 }
101
102 void
103 vFSLog(const char *format, va_list args)
104 {
105     time_t currenttime;
106     char tbuffer[1024];
107     char *info;
108     size_t len;
109     struct tm tm;
110     int num;
111
112     currenttime = time(NULL);
113     len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
114                    localtime_r(&currenttime, &tm));
115     info = &tbuffer[len];
116
117     if (mrafsStyleLogs || threadIdLogs) {
118         num = (*threadNumProgram) ();
119         if (num > -1) {
120             snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
121                      num);
122             info += strlen(info);
123         }
124     }
125
126     vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
127
128     len = strlen(tbuffer);
129     LOCK_SERVERLOG();
130 #ifndef AFS_NT40_ENV
131     if (serverLogSyslog) {
132         syslog(LOG_INFO, "%s", info);
133     } else
134 #endif
135     if (serverLogFD >= 0) {
136         if (write(serverLogFD, tbuffer, len) < 0)
137             ; /* don't care */
138     }
139     UNLOCK_SERVERLOG();
140
141 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
142     if (!serverLogSyslog) {
143         fflush(stdout);
144         fflush(stderr);         /* in case they're sharing the same FD */
145     }
146 #endif
147 }                               /*vFSLog */
148
149 /* VARARGS1 */
150 /*@printflike@*/
151 void
152 FSLog(const char *format, ...)
153 {
154     va_list args;
155
156     va_start(args, format);
157     vFSLog(format, args);
158     va_end(args);
159 }                               /*FSLog */
160
161 void
162 LogCommandLine(int argc, char **argv, const char *progname,
163                const char *version, const char *logstring,
164                void (*log) (const char *format, ...))
165 {
166     int i, l;
167     char *commandLine, *cx;
168
169     opr_Assert(argc > 0);
170
171     for (l = i = 0; i < argc; i++)
172         l += strlen(argv[i]) + 1;
173     if ((commandLine = malloc(l))) {
174         for (cx = commandLine, i = 0; i < argc; i++) {
175             strcpy(cx, argv[i]);
176             cx += strlen(cx);
177             *(cx++) = ' ';
178         }
179         commandLine[l-1] = '\0';
180         (*log)("%s %s %s%s(%s)\n", logstring, progname,
181                     version, strlen(version)>0?" ":"", commandLine);
182         free(commandLine);
183     } else {
184         /* What, we're out of memory already!? */
185         (*log)("%s %s%s%s\n", logstring,
186               progname, strlen(version)>0?" ":"", version);
187     }
188 }
189
190 void
191 LogDesWarning(void)
192 {
193     /* The blank newlines help this stand out a bit more in the log. */
194     ViceLog(0, ("\n"));
195     ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
196     ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
197     ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
198     ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
199     ViceLog(0, ("WARNING: for details.\n"));
200     ViceLog(0, ("\n"));
201 }
202
203 static void*
204 DebugOn(void *param)
205 {
206     int loglevel = (intptr_t)param;
207     if (loglevel == 0) {
208         ViceLog(0, ("Reset Debug levels to 0\n"));
209     } else {
210         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
211     }
212     return 0;
213 }                               /*DebugOn */
214
215
216
217 void
218 SetDebug_Signal(int signo)
219 {
220     if (LogLevel > 0) {
221         LogLevel *= 5;
222
223 #if defined(AFS_PTHREAD_ENV)
224         if (LogLevel > 1 && threadNumProgram != NULL &&
225             threadIdLogs == 0) {
226             threadIdLogs = 1;
227         }
228 #endif
229     } else {
230         LogLevel = 1;
231
232 #if defined(AFS_PTHREAD_ENV)
233         if (threadIdLogs == 1)
234             threadIdLogs = 0;
235 #endif
236     }
237 #if defined(AFS_PTHREAD_ENV)
238     DebugOn((void *)(intptr_t)LogLevel);
239 #else /* AFS_PTHREAD_ENV */
240     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
241 #endif /* AFS_PTHREAD_ENV */
242
243     if (resetSignals) {
244         /* When pthreaded softsig handlers are not in use, some platforms
245          * require this signal handler to be set again. */
246         (void)signal(signo, SetDebug_Signal);
247     }
248 }                               /*SetDebug_Signal */
249
250 void
251 ResetDebug_Signal(int signo)
252 {
253     LogLevel = 0;
254
255 #if defined(AFS_PTHREAD_ENV)
256     DebugOn((void *)(intptr_t)LogLevel);
257 #else /* AFS_PTHREAD_ENV */
258     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
259 #endif /* AFS_PTHREAD_ENV */
260
261     if (resetSignals) {
262         /* When pthreaded softsig handlers are not in use, some platforms
263          * require this signal handler to be set again. */
264         (void)signal(signo, ResetDebug_Signal);
265     }
266 #if defined(AFS_PTHREAD_ENV)
267     if (threadIdLogs == 1)
268         threadIdLogs = 0;
269 #endif
270     if (mrafsStyleLogs) {
271         LOCK_SERVERLOG();
272         if (ourName != NULL) {
273             OpenLog(ourName);
274         }
275         UNLOCK_SERVERLOG();
276     }
277 }                               /*ResetDebug_Signal */
278
279
280 #ifdef AFS_PTHREAD_ENV
281 /*!
282  * Register pthread-safe signal handlers for server log management.
283  *
284  * \note opr_softsig_Init() must be called before this function.
285  */
286 void
287 SetupLogSoftSignals(void)
288 {
289     opr_softsig_Register(SIGHUP, ResetDebug_Signal);
290     opr_softsig_Register(SIGTSTP, SetDebug_Signal);
291 #ifndef AFS_NT40_ENV
292     (void)signal(SIGPIPE, SIG_IGN);
293 #endif
294 }
295 #endif /* AFS_PTHREAD_ENV */
296
297 /*!
298  * Register signal handlers for server log management.
299  *
300  * \note This function is deprecated and should not be used
301  *       in new code. This function should be removed when
302  *       all the servers have been converted to pthreads
303  *       and lwp has been removed.
304  */
305 void
306 SetupLogSignals(void)
307 {
308     resetSignals = 1;
309     (void)signal(SIGHUP, ResetDebug_Signal);
310     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
311     (void)signal(SIGTSTP, SetDebug_Signal);
312 #ifndef AFS_NT40_ENV
313     (void)signal(SIGPIPE, SIG_IGN);
314 #endif
315 }
316
317 #if defined(AFS_PTHREAD_ENV)
318 static void
319 InitServerLogMutex(void)
320 {
321     opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
322 }
323 #endif /* AFS_PTHREAD_ENV */
324
325 int
326 OpenLog(const char *fileName)
327 {
328     /*
329      * This function should allow various libraries that inconsistently
330      * use stdout/stderr to all go to the same place
331      */
332     int tempfd, isfifo = 0;
333     int code;
334     char *nextName = NULL;
335     struct timeval Start;
336     struct tm *TimeFields;
337
338 #ifndef AFS_NT40_ENV
339     struct stat statbuf;
340 #endif
341
342 #if defined(AFS_PTHREAD_ENV)
343     opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
344 #endif /* AFS_PTHREAD_ENV */
345
346 #ifndef AFS_NT40_ENV
347     if (serverLogSyslog) {
348         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
349         return (0);
350     }
351 #endif
352
353     opr_Assert(fileName != NULL);
354
355 #ifndef AFS_NT40_ENV
356     /* Support named pipes as logs by not rotating them */
357     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
358         isfifo = 1;
359     }
360 #endif
361
362     if (mrafsStyleLogs) {
363         time_t t;
364         struct stat buf;
365         gettimeofday(&Start, NULL);
366         t = Start.tv_sec;
367         TimeFields = localtime(&t);
368     makefilename:
369         code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
370                  fileName, TimeFields->tm_year + 1900,
371                  TimeFields->tm_mon + 1, TimeFields->tm_mday,
372                  TimeFields->tm_hour, TimeFields->tm_min,
373                  TimeFields->tm_sec);
374         if (code < 0) {
375             nextName = NULL;
376         } else if (lstat(nextName, &buf) == 0) {
377             /* avoid clobbering a log */
378             TimeFields->tm_sec++;
379             free(nextName);
380             nextName = NULL;
381             goto makefilename;
382         }
383     } else {
384         code = asprintf(&nextName, "%s.old", fileName);
385         if (code < 0) {
386             nextName = NULL;
387         }
388     }
389     if (nextName != NULL) {
390         if (!isfifo)
391             rk_rename(fileName, nextName);      /* Don't check the error code. */
392         free(nextName);
393     }
394
395     tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
396     if (tempfd < 0) {
397         printf("Unable to open log file %s\n", fileName);
398         return -1;
399     }
400     /* redirect stdout and stderr so random printf's don't write to data */
401     if (freopen(fileName, "a", stdout) == NULL)
402         ; /* don't care */
403     if (freopen(fileName, "a", stderr) != NULL) {
404 #ifdef HAVE_SETVBUF
405         setvbuf(stderr, NULL, _IONBF, 0);
406 #else
407         setbuf(stderr, NULL);
408 #endif
409     }
410
411     /* Save our name for reopening. */
412     free(ourName);
413     ourName = strdup(fileName);
414     opr_Assert(ourName != NULL);
415
416     serverLogFD = tempfd;
417
418     return 0;
419 }                               /*OpenLog */
420
421 int
422 ReOpenLog(const char *fileName)
423 {
424     int isfifo = 0;
425 #if !defined(AFS_NT40_ENV)
426     struct stat statbuf;
427 #endif
428
429     if (access(fileName, F_OK) == 0)
430         return 0;               /* exists, no need to reopen. */
431
432 #if !defined(AFS_NT40_ENV)
433     if (serverLogSyslog) {
434         return 0;
435     }
436
437     /* Support named pipes as logs by not rotating them */
438     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
439         isfifo = 1;
440     }
441 #endif
442
443     LOCK_SERVERLOG();
444     if (serverLogFD >= 0)
445         close(serverLogFD);
446     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
447     if (serverLogFD >= 0) {
448         if (freopen(fileName, "a", stdout) == NULL)
449             ; /* don't care */
450         if (freopen(fileName, "a", stderr) != NULL) {
451 #ifdef HAVE_SETVBUF
452             setvbuf(stderr, NULL, _IONBF, 0);
453 #else
454             setbuf(stderr, NULL);
455 #endif
456         }
457
458     }
459     UNLOCK_SERVERLOG();
460     return serverLogFD < 0 ? -1 : 0;
461 }
462
463 /*!
464  * Close the server log file.
465  *
466  * \note Must be preceeded by OpenLog().
467  */
468 void
469 CloseLog(void)
470 {
471     LOCK_SERVERLOG();
472 #ifndef AFS_NT40_ENV
473     if (serverLogSyslog) {
474         closelog();
475     } else
476 #endif
477     {
478         if (serverLogFD >= 0) {
479             close(serverLogFD);
480             serverLogFD = -1;
481         }
482         free(ourName);
483         ourName = NULL;
484     }
485     UNLOCK_SERVERLOG();
486 }