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