util: LogCommandLine: argc is an int, so assert that it's positive
[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 #include <afs/opr.h>
27
28 #include "afsutil.h"
29 #include "fileutil.h"
30 #include <lwp.h>
31
32 #if defined(AFS_PTHREAD_ENV)
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 int LogLevel;
72 int mrafsStyleLogs = 0;
73 static int threadIdLogs = 0;
74 int printLocks = 0;
75 static char ourName[MAXPATHLEN];
76
77 void
78 SetLogThreadNumProgram(int (*func) (void) )
79 {
80     threadNumProgram = func;
81 }
82
83 void
84 WriteLogBuffer(char *buf, afs_uint32 len)
85 {
86     LOCK_SERVERLOG();
87     if (serverLogFD > 0)
88         (void)write(serverLogFD, buf, len);
89     UNLOCK_SERVERLOG();
90 }
91
92 int
93 LogThreadNum(void)
94 {
95   return (*threadNumProgram) ();
96 }
97
98 void
99 vFSLog(const char *format, va_list args)
100 {
101     time_t currenttime;
102     char tbuffer[1024];
103     char *info;
104     size_t len;
105     struct tm tm;
106     int num;
107
108     currenttime = time(NULL);
109     len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
110                    localtime_r(&currenttime, &tm));
111     info = &tbuffer[len];
112
113     if (mrafsStyleLogs || threadIdLogs) {
114         num = (*threadNumProgram) ();
115         if (num > -1) {
116             snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
117                      num);
118             info += strlen(info);
119         }
120     }
121
122     vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
123
124     len = strlen(tbuffer);
125     LOCK_SERVERLOG();
126 #ifndef AFS_NT40_ENV
127     if (serverLogSyslog) {
128         syslog(LOG_INFO, "%s", info);
129     } else
130 #endif
131     if (serverLogFD > 0)
132         (void)write(serverLogFD, tbuffer, len);
133     UNLOCK_SERVERLOG();
134
135 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
136     if (!serverLogSyslog) {
137         fflush(stdout);
138         fflush(stderr);         /* in case they're sharing the same FD */
139     }
140 #endif
141 }                               /*vFSLog */
142
143 /* VARARGS1 */
144 /*@printflike@*/
145 void
146 FSLog(const char *format, ...)
147 {
148     va_list args;
149
150     va_start(args, format);
151     vFSLog(format, args);
152     va_end(args);
153 }                               /*FSLog */
154
155 void
156 LogCommandLine(int argc, char **argv, const char *progname,
157                const char *version, const char *logstring,
158                void (*log) (const char *format, ...))
159 {
160     int i, l;
161     char *commandLine, *cx;
162
163     opr_Assert(argc > 0);
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         gettimeofday(&Start, NULL);
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         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 }