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