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