openafs-string-header-cleanup-20071030
[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 #if defined(AFS_PTHREAD_ENV)
45 #include <assert.h>
46 #include <pthread.h>
47 static pthread_mutex_t serverLogMutex;
48 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
49 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
50
51 #ifdef AFS_NT40_ENV
52 #define NULLDEV "NUL"
53 #else
54 #define NULLDEV "/dev/null"
55 #endif
56
57 #else /* AFS_PTHREAD_ENV */
58 #define LOCK_SERVERLOG()
59 #define UNLOCK_SERVERLOG()
60 #endif /* AFS_PTHREAD_ENV */
61
62 #ifdef AFS_NT40_ENV
63 #define F_OK 0
64 #define O_NONBLOCK 0
65 #endif
66
67 static int
68 dummyThreadNum(void)
69 {
70     return -1;
71 }
72 static int (*threadNumProgram) () = dummyThreadNum;
73
74 static int serverLogFD = -1;
75
76 #ifndef AFS_NT40_ENV
77 int serverLogSyslog = 0;
78 int serverLogSyslogFacility = LOG_DAEMON;
79 char *serverLogSyslogTag = 0;
80 #endif
81
82 #include <stdarg.h>
83 int LogLevel;
84 int mrafsStyleLogs = 0;
85 static int threadIdLogs = 0;
86 int printLocks = 0;
87 static char ourName[MAXPATHLEN];
88
89 void
90 SetLogThreadNumProgram(int (*func) () )
91 {
92     threadNumProgram = func;
93 }
94
95 void
96 WriteLogBuffer(char *buf, afs_uint32 len)
97 {
98     LOCK_SERVERLOG();
99     if (serverLogFD > 0)
100         (void)write(serverLogFD, buf, len);
101     UNLOCK_SERVERLOG();
102 }
103
104 int
105 LogThreadNum(void) 
106 {
107   return (*threadNumProgram) ();
108 }
109
110 void
111 vFSLog(const char *format, va_list args)
112 {
113     time_t currenttime;
114     char *timeStamp;
115     char tbuffer[1024];
116     char *info;
117     size_t len;
118     int num;
119
120     currenttime = time(0);
121     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
122     timeStamp[24] = ' ';        /* ts[24] is the newline, 25 is the null */
123     info = &timeStamp[25];
124
125     if (mrafsStyleLogs || threadIdLogs) {
126         num = (*threadNumProgram) ();
127         if (num > -1) {
128         (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
129                            num);
130         info += strlen(info);
131     }
132     }
133
134     (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
135                         args);
136
137     len = strlen(tbuffer);
138     LOCK_SERVERLOG();
139 #ifndef AFS_NT40_ENV
140     if (serverLogSyslog) {
141         syslog(LOG_INFO, "%s", info);
142     } else
143 #endif
144     if (serverLogFD > 0)
145         (void)write(serverLogFD, tbuffer, len);
146     UNLOCK_SERVERLOG();
147
148 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
149     if (!serverLogSyslog) {
150         fflush(stdout);
151         fflush(stderr);         /* in case they're sharing the same FD */
152     }
153 #endif
154 }                               /*vFSLog */
155
156 /* VARARGS1 */
157 /*@printflike@*/
158 void
159 FSLog(const char *format, ...)
160 {
161     va_list args;
162
163     va_start(args, format);
164     vFSLog(format, args);
165     va_end(args);
166 }                               /*FSLog */
167
168 static int
169 DebugOn(int loglevel)
170 {
171     if (loglevel == 0) {
172         ViceLog(0, ("Reset Debug levels to 0\n"));
173     } else {
174         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
175     }
176     return 0;
177 }                               /*DebugOn */
178
179
180
181 void
182 SetDebug_Signal(int signo)
183 {
184 /*    extern int IOMGR_SoftSig();*/
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(LogLevel);
206 #else /* AFS_PTHREAD_ENV */
207     IOMGR_SoftSig(DebugOn, 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(LogLevel);
224 #else /* AFS_PTHREAD_ENV */
225     IOMGR_SoftSig(DebugOn, 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_PTHREAD_ENV)
337     int tempfd;
338 #endif
339 #if !defined(AFS_NT40_ENV)
340     struct stat statbuf;
341 #endif
342
343     if (access(fileName, F_OK) == 0)
344         return 0;               /* exists, no need to reopen. */
345
346 #if !defined(AFS_NT40_ENV)
347     if (serverLogSyslog) {
348         return 0;
349     }
350
351     /* Support named pipes as logs by not rotating them */
352     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
353         isfifo = 1;
354     }
355 #endif
356
357     LOCK_SERVERLOG();
358     if (serverLogFD > 0)
359         close(serverLogFD);
360     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
361     if (serverLogFD > 0) {
362         (void)freopen(fileName, "a", stdout);
363         (void)freopen(fileName, "a", stderr);
364 #ifdef HAVE_SETVBUF
365 #ifdef SETVBUF_REVERSED
366         setvbuf(stderr, _IONBF, NULL, 0);
367 #else
368         setvbuf(stderr, NULL, _IONBF, 0);
369 #endif
370 #else
371         setbuf(stderr, NULL);
372 #endif
373
374     }
375     UNLOCK_SERVERLOG();
376     return serverLogFD < 0 ? -1 : 0;
377 }