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