afs_snprintf is dead, long live rk_snprintf
[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             snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
118                      num);
119             info += strlen(info);
120         }
121     }
122
123     vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
124
125     len = strlen(tbuffer);
126     LOCK_SERVERLOG();
127 #ifndef AFS_NT40_ENV
128     if (serverLogSyslog) {
129         syslog(LOG_INFO, "%s", info);
130     } else
131 #endif
132     if (serverLogFD > 0)
133         (void)write(serverLogFD, tbuffer, len);
134     UNLOCK_SERVERLOG();
135
136 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
137     if (!serverLogSyslog) {
138         fflush(stdout);
139         fflush(stderr);         /* in case they're sharing the same FD */
140     }
141 #endif
142 }                               /*vFSLog */
143
144 /* VARARGS1 */
145 /*@printflike@*/
146 void
147 FSLog(const char *format, ...)
148 {
149     va_list args;
150
151     va_start(args, format);
152     vFSLog(format, args);
153     va_end(args);
154 }                               /*FSLog */
155
156 void
157 LogCommandLine(int argc, char **argv, const char *progname,
158                const char *version, const char *logstring,
159                void (*log) (const char *format, ...))
160 {
161     int i, l;
162     char *commandLine, *cx;
163
164     for (l = i = 0; i < argc; i++)
165         l += strlen(argv[i]) + 1;
166     if ((commandLine = malloc(l))) {
167         for (cx = commandLine, i = 0; i < argc; i++) {
168             strcpy(cx, argv[i]);
169             cx += strlen(cx);
170             *(cx++) = ' ';
171         }
172         commandLine[l-1] = '\0';
173         (*log)("%s %s %s%s(%s)\n", logstring, progname,
174                     version, strlen(version)>0?" ":"", commandLine);
175         free(commandLine);
176     } else {
177         /* What, we're out of memory already!? */
178         (*log)("%s %s%s%s\n", logstring,
179               progname, strlen(version)>0?" ":"", version);
180     }
181 }
182
183 static void*
184 DebugOn(void *param)
185 {
186     int loglevel = (intptr_t)param;
187     if (loglevel == 0) {
188         ViceLog(0, ("Reset Debug levels to 0\n"));
189     } else {
190         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
191     }
192     return 0;
193 }                               /*DebugOn */
194
195
196
197 void
198 SetDebug_Signal(int signo)
199 {
200     if (LogLevel > 0) {
201         LogLevel *= 5;
202
203 #if defined(AFS_PTHREAD_ENV)
204         if (LogLevel > 1 && threadNumProgram != NULL &&
205             threadIdLogs == 0) {
206             threadIdLogs = 1;
207         }
208 #endif
209     } else {
210         LogLevel = 1;
211
212 #if defined(AFS_PTHREAD_ENV)
213         if (threadIdLogs == 1)
214             threadIdLogs = 0;
215 #endif
216     }
217     printLocks = 2;
218 #if defined(AFS_PTHREAD_ENV)
219     DebugOn((void *)(intptr_t)LogLevel);
220 #else /* AFS_PTHREAD_ENV */
221     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
222 #endif /* AFS_PTHREAD_ENV */
223
224     (void)signal(signo, SetDebug_Signal);       /* on some platforms, this
225                                                  * signal handler needs to
226                                                  * be set again */
227 }                               /*SetDebug_Signal */
228
229 void
230 ResetDebug_Signal(int signo)
231 {
232     LogLevel = 0;
233
234     if (printLocks > 0)
235         --printLocks;
236 #if defined(AFS_PTHREAD_ENV)
237     DebugOn((void *)(intptr_t)LogLevel);
238 #else /* AFS_PTHREAD_ENV */
239     IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
240 #endif /* AFS_PTHREAD_ENV */
241
242     (void)signal(signo, ResetDebug_Signal);     /* on some platforms,
243                                                  * this signal handler
244                                                  * needs to be set
245                                                  * again */
246 #if defined(AFS_PTHREAD_ENV)
247     if (threadIdLogs == 1)
248         threadIdLogs = 0;
249 #endif
250     if (mrafsStyleLogs)
251         OpenLog((char *)&ourName);
252 }                               /*ResetDebug_Signal */
253
254
255 void
256 SetupLogSignals(void)
257 {
258     (void)signal(SIGHUP, ResetDebug_Signal);
259     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
260     (void)signal(SIGTSTP, SetDebug_Signal);
261 #ifndef AFS_NT40_ENV
262     (void)signal(SIGPIPE, SIG_IGN);
263 #endif
264 }
265
266 int
267 OpenLog(const char *fileName)
268 {
269     /*
270      * This function should allow various libraries that inconsistently
271      * use stdout/stderr to all go to the same place
272      */
273     int tempfd, isfifo = 0;
274     char oldName[MAXPATHLEN];
275     struct timeval Start;
276     struct tm *TimeFields;
277     char FileName[MAXPATHLEN];
278
279 #ifndef AFS_NT40_ENV
280     struct stat statbuf;
281
282     if (serverLogSyslog) {
283         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
284         return (0);
285     }
286
287     /* Support named pipes as logs by not rotating them */
288     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
289         isfifo = 1;
290     }
291 #endif
292
293     if (mrafsStyleLogs) {
294         time_t t;
295         struct stat buf;
296         FT_GetTimeOfDay(&Start, 0);
297         t = Start.tv_sec;
298         TimeFields = localtime(&t);
299         if (fileName) {
300             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
301                 strcpy((char *)&ourName, (char *)fileName);
302         }
303     makefilename:
304         snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
305                  ourName, TimeFields->tm_year + 1900,
306                  TimeFields->tm_mon + 1, TimeFields->tm_mday,
307                  TimeFields->tm_hour, TimeFields->tm_min,
308                  TimeFields->tm_sec);
309         if(lstat(FileName, &buf) == 0) {
310             /* avoid clobbering a log */
311             TimeFields->tm_sec++;
312             goto makefilename;
313         }
314         if (!isfifo)
315             renamefile(fileName, FileName);     /* don't check error code */
316         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
317     } else {
318         strcpy(oldName, fileName);
319         strcat(oldName, ".old");
320
321         /* don't check error */
322         if (!isfifo)
323             renamefile(fileName, oldName);
324         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
325     }
326
327     if (tempfd < 0) {
328         printf("Unable to open log file %s\n", fileName);
329         return -1;
330     }
331     /* redirect stdout and stderr so random printf's don't write to data */
332     (void)freopen(fileName, "a", stdout);
333     (void)freopen(fileName, "a", stderr);
334 #ifdef HAVE_SETVBUF
335     setvbuf(stderr, NULL, _IONBF, 0);
336 #else
337     setbuf(stderr, NULL);
338 #endif
339
340 #if defined(AFS_PTHREAD_ENV)
341     MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0);
342 #endif /* AFS_PTHREAD_ENV */
343
344     serverLogFD = tempfd;
345
346     return 0;
347 }                               /*OpenLog */
348
349 int
350 ReOpenLog(const char *fileName)
351 {
352     int isfifo = 0;
353 #if !defined(AFS_NT40_ENV)
354     struct stat statbuf;
355 #endif
356
357     if (access(fileName, F_OK) == 0)
358         return 0;               /* exists, no need to reopen. */
359
360 #if !defined(AFS_NT40_ENV)
361     if (serverLogSyslog) {
362         return 0;
363     }
364
365     /* Support named pipes as logs by not rotating them */
366     if ((lstat(fileName, &statbuf) == 0)  && (S_ISFIFO(statbuf.st_mode))) {
367         isfifo = 1;
368     }
369 #endif
370
371     LOCK_SERVERLOG();
372     if (serverLogFD > 0)
373         close(serverLogFD);
374     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
375     if (serverLogFD > 0) {
376         (void)freopen(fileName, "a", stdout);
377         (void)freopen(fileName, "a", stderr);
378 #ifdef HAVE_SETVBUF
379 #ifdef SETVBUF_REVERSED
380         setvbuf(stderr, _IONBF, NULL, 0);
381 #else
382         setvbuf(stderr, NULL, _IONBF, 0);
383 #endif
384 #else
385         setbuf(stderr, NULL);
386 #endif
387
388     }
389     UNLOCK_SERVERLOG();
390     return serverLogFD < 0 ? -1 : 0;
391 }