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