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