Further rationalise our usage of assert()
[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 #include <afs/opr.h>
27
28 #include <afs/opr.h>
29 #include "afsutil.h"
30 #include "fileutil.h"
31 #include <lwp.h>
32
33 #if defined(AFS_PTHREAD_ENV)
34 /* can't include rx when we are libutil; it's too early */
35 #include <rx/rx.h>
36 #include <pthread.h>
37 static pthread_mutex_t serverLogMutex;
38 #define LOCK_SERVERLOG() MUTEX_ENTER(&serverLogMutex)
39 #define UNLOCK_SERVERLOG() MUTEX_EXIT(&serverLogMutex)
40
41 #ifdef AFS_NT40_ENV
42 #define NULLDEV "NUL"
43 #else
44 #define NULLDEV "/dev/null"
45 #endif
46
47 #else /* AFS_PTHREAD_ENV */
48 #define LOCK_SERVERLOG()
49 #define UNLOCK_SERVERLOG()
50 #endif /* AFS_PTHREAD_ENV */
51
52 #ifdef AFS_NT40_ENV
53 #define F_OK 0
54 #define O_NONBLOCK 0
55 #endif
56
57 static int
58 dummyThreadNum(void)
59 {
60     return -1;
61 }
62 static int (*threadNumProgram) (void) = dummyThreadNum;
63
64 static int serverLogFD = -1;
65
66 #ifndef AFS_NT40_ENV
67 int serverLogSyslog = 0;
68 int serverLogSyslogFacility = LOG_DAEMON;
69 char *serverLogSyslogTag = 0;
70 #endif
71
72 int LogLevel;
73 int mrafsStyleLogs = 0;
74 static int threadIdLogs = 0;
75 int printLocks = 0;
76 static char ourName[MAXPATHLEN];
77
78 void
79 SetLogThreadNumProgram(int (*func) (void) )
80 {
81     threadNumProgram = func;
82 }
83
84 void
85 WriteLogBuffer(char *buf, afs_uint32 len)
86 {
87     LOCK_SERVERLOG();
88     if (serverLogFD > 0)
89         (void)write(serverLogFD, buf, len);
90     UNLOCK_SERVERLOG();
91 }
92
93 int
94 LogThreadNum(void)
95 {
96   return (*threadNumProgram) ();
97 }
98
99 void
100 vFSLog(const char *format, va_list args)
101 {
102     time_t currenttime;
103     char tbuffer[1024];
104     char *info;
105     size_t len;
106     struct tm tm;
107     int num;
108
109     currenttime = time(NULL);
110     len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
111                    localtime_r(&currenttime, &tm));
112     info = &tbuffer[len];
113
114     if (mrafsStyleLogs || threadIdLogs) {
115         num = (*threadNumProgram) ();
116         if (num > -1) {
117             snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
118                      num);
119             info += strlen(info);
120         }
121     }
122
123     vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
124
125     len = strlen(tbuffer);
126     LOCK_SERVERLOG();
127 #ifndef AFS_NT40_ENV
128     if (serverLogSyslog) {
129         syslog(LOG_INFO, "%s", info);
130     } else
131 #endif
132     if (serverLogFD > 0)
133         (void)write(serverLogFD, tbuffer, len);
134     UNLOCK_SERVERLOG();
135
136 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
137     if (!serverLogSyslog) {
138         fflush(stdout);
139         fflush(stderr);         /* in case they're sharing the same FD */
140     }
141 #endif
142 }                               /*vFSLog */
143
144 /* VARARGS1 */
145 /*@printflike@*/
146 void
147 FSLog(const char *format, ...)
148 {
149     va_list args;
150
151     va_start(args, format);
152     vFSLog(format, args);
153     va_end(args);
154 }                               /*FSLog */
155
156 void
157 LogCommandLine(int argc, char **argv, const char *progname,
158                const char *version, const char *logstring,
159                void (*log) (const char *format, ...))
160 {
161     int i, l;
162     char *commandLine, *cx;
163
164     opr_Assert(argc > 0);
165
166     for (l = i = 0; i < argc; i++)
167         l += strlen(argv[i]) + 1;
168     if ((commandLine = malloc(l))) {
169         for (cx = commandLine, i = 0; i < argc; i++) {
170             strcpy(cx, argv[i]);
171             cx += strlen(cx);
172             *(cx++) = ' ';
173         }
174         commandLine[l-1] = '\0';
175         (*log)("%s %s %s%s(%s)\n", logstring, progname,
176                     version, strlen(version)>0?" ":"", commandLine);
177         free(commandLine);
178     } else {
179         /* What, we're out of memory already!? */
180         (*log)("%s %s%s%s\n", logstring,
181               progname, strlen(version)>0?" ":"", version);
182     }
183 }
184
185 static void*
186 DebugOn(void *param)
187 {
188     int loglevel = (intptr_t)param;
189     if (loglevel == 0) {
190         ViceLog(0, ("Reset Debug levels to 0\n"));
191     } else {
192         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
193     }
194     return 0;
195 }                               /*DebugOn */
196
197
198
199 void
200 SetDebug_Signal(int signo)
201 {
202     if (LogLevel > 0) {
203         LogLevel *= 5;
204
205 #if defined(AFS_PTHREAD_ENV)
206         if (LogLevel > 1 && threadNumProgram != NULL &&
207             threadIdLogs == 0) {
208             threadIdLogs = 1;
209         }
210 #endif
211     } else {
212         LogLevel = 1;
213
214 #if defined(AFS_PTHREAD_ENV)
215         if (threadIdLogs == 1)
216             threadIdLogs = 0;
217 #endif
218     }
219     printLocks = 2;
220 #if defined(AFS_PTHREAD_ENV)
221     DebugOn((void *)(intptr_t)LogLevel);
222 #else /* AFS_PTHREAD_ENV */
223     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
224 #endif /* AFS_PTHREAD_ENV */
225
226     (void)signal(signo, SetDebug_Signal);       /* on some platforms, this
227                                                  * signal handler needs to
228                                                  * be set again */
229 }                               /*SetDebug_Signal */
230
231 void
232 ResetDebug_Signal(int signo)
233 {
234     LogLevel = 0;
235
236     if (printLocks > 0)
237         --printLocks;
238 #if defined(AFS_PTHREAD_ENV)
239     DebugOn((void *)(intptr_t)LogLevel);
240 #else /* AFS_PTHREAD_ENV */
241     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
242 #endif /* AFS_PTHREAD_ENV */
243
244     (void)signal(signo, ResetDebug_Signal);     /* on some platforms,
245                                                  * this signal handler
246                                                  * needs to be set
247                                                  * again */
248 #if defined(AFS_PTHREAD_ENV)
249     if (threadIdLogs == 1)
250         threadIdLogs = 0;
251 #endif
252     if (mrafsStyleLogs)
253         OpenLog((char *)&ourName);
254 }                               /*ResetDebug_Signal */
255
256
257 void
258 SetupLogSignals(void)
259 {
260     (void)signal(SIGHUP, ResetDebug_Signal);
261     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
262     (void)signal(SIGTSTP, SetDebug_Signal);
263 #ifndef AFS_NT40_ENV
264     (void)signal(SIGPIPE, SIG_IGN);
265 #endif
266 }
267
268 int
269 OpenLog(const char *fileName)
270 {
271     /*
272      * This function should allow various libraries that inconsistently
273      * use stdout/stderr to all go to the same place
274      */
275     int tempfd, isfifo = 0;
276     char oldName[MAXPATHLEN];
277     struct timeval Start;
278     struct tm *TimeFields;
279     char FileName[MAXPATHLEN];
280
281 #ifndef AFS_NT40_ENV
282     struct stat statbuf;
283
284     if (serverLogSyslog) {
285         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
286         return (0);
287     }
288
289     /* Support named pipes as logs by not rotating them */
290     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
291         isfifo = 1;
292     }
293 #endif
294
295     if (mrafsStyleLogs) {
296         time_t t;
297         struct stat buf;
298         gettimeofday(&Start, NULL);
299         t = Start.tv_sec;
300         TimeFields = localtime(&t);
301         if (fileName) {
302             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
303                 strcpy((char *)&ourName, (char *)fileName);
304         }
305     makefilename:
306         snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
307                  ourName, TimeFields->tm_year + 1900,
308                  TimeFields->tm_mon + 1, TimeFields->tm_mday,
309                  TimeFields->tm_hour, TimeFields->tm_min,
310                  TimeFields->tm_sec);
311         if(lstat(FileName, &buf) == 0) {
312             /* avoid clobbering a log */
313             TimeFields->tm_sec++;
314             goto makefilename;
315         }
316         if (!isfifo)
317             rk_rename(fileName, FileName);      /* don't check error code */
318         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
319     } else {
320         strcpy(oldName, fileName);
321         strcat(oldName, ".old");
322
323         /* don't check error */
324         if (!isfifo)
325             rk_rename(fileName, oldName);
326         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
327     }
328
329     if (tempfd < 0) {
330         printf("Unable to open log file %s\n", fileName);
331         return -1;
332     }
333     /* redirect stdout and stderr so random printf's don't write to data */
334     (void)freopen(fileName, "a", stdout);
335     (void)freopen(fileName, "a", stderr);
336 #ifdef HAVE_SETVBUF
337     setvbuf(stderr, NULL, _IONBF, 0);
338 #else
339     setbuf(stderr, NULL);
340 #endif
341
342 #if defined(AFS_PTHREAD_ENV)
343     MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0);
344 #endif /* AFS_PTHREAD_ENV */
345
346     serverLogFD = tempfd;
347
348     return 0;
349 }                               /*OpenLog */
350
351 int
352 ReOpenLog(const char *fileName)
353 {
354     int isfifo = 0;
355 #if !defined(AFS_NT40_ENV)
356     struct stat statbuf;
357 #endif
358
359     if (access(fileName, F_OK) == 0)
360         return 0;               /* exists, no need to reopen. */
361
362 #if !defined(AFS_NT40_ENV)
363     if (serverLogSyslog) {
364         return 0;
365     }
366
367     /* Support named pipes as logs by not rotating them */
368     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
369         isfifo = 1;
370     }
371 #endif
372
373     LOCK_SERVERLOG();
374     if (serverLogFD > 0)
375         close(serverLogFD);
376     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
377     if (serverLogFD > 0) {
378         (void)freopen(fileName, "a", stdout);
379         (void)freopen(fileName, "a", stderr);
380 #ifdef HAVE_SETVBUF
381 #ifdef SETVBUF_REVERSED
382         setvbuf(stderr, _IONBF, NULL, 0);
383 #else
384         setvbuf(stderr, NULL, _IONBF, 0);
385 #endif
386 #else
387         setbuf(stderr, NULL);
388 #endif
389
390     }
391     UNLOCK_SERVERLOG();
392     return serverLogFD < 0 ? -1 : 0;
393 }