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