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