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