fabf5209078b6b2a5b1ab7425b6a776168e88251
[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 "afsutil.h"
48 #include "fileutil.h"
49 #if defined(AFS_PTHREAD_ENV)
50 #include <assert.h>
51 #include <pthread.h>
52 static pthread_mutex_t serverLogMutex;
53 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
54 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
55
56 #ifdef AFS_NT40_ENV
57 #define NULLDEV "NUL"
58 #else
59 #define NULLDEV "/dev/null"
60 #endif
61
62 #else /* AFS_PTHREAD_ENV */
63 #define LOCK_SERVERLOG()
64 #define UNLOCK_SERVERLOG()
65 #endif /* AFS_PTHREAD_ENV */
66
67 #ifdef AFS_NT40_ENV
68 #define F_OK 0
69 #endif
70
71 char *(*threadNameProgram) ();
72
73 static int serverLogFD = -1;
74
75 #ifndef AFS_NT40_ENV
76 int serverLogSyslog = 0;
77 int serverLogSyslogFacility = LOG_DAEMON;
78 char *serverLogSyslogTag = 0;
79 #endif
80
81 #include <stdarg.h>
82 int LogLevel;
83 int mrafsStyleLogs = 0;
84 int printLocks = 0;
85 static char ourName[MAXPATHLEN];
86
87 void
88 WriteLogBuffer(char *buf, afs_uint32 len)
89 {
90     LOCK_SERVERLOG();
91     if (serverLogFD > 0)
92         (void)write(serverLogFD, buf, len);
93     UNLOCK_SERVERLOG();
94 }
95
96 void
97 vFSLog(const char *format, va_list args)
98 {
99     time_t currenttime;
100     char *timeStamp;
101     char tbuffer[1024];
102     char *info;
103     int len;
104     char *name;
105
106     currenttime = time(0);
107     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
108     timeStamp[24] = ' ';        /* ts[24] is the newline, 25 is the null */
109     info = &timeStamp[25];
110
111     if (mrafsStyleLogs) {
112         name = (*threadNameProgram) ();
113         (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
114                            name);
115         info += strlen(info);
116     }
117
118     (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
119                         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 static int
153 DebugOn(int loglevel)
154 {
155     if (loglevel == 0) {
156         ViceLog(0, ("Reset Debug levels to 0\n"));
157     } else {
158         ViceLog(0, ("Set Debug On level = %d\n", loglevel));
159     }
160     return 0;
161 }                               /*DebugOn */
162
163
164
165 void
166 SetDebug_Signal(int signo)
167 {
168 /*    extern int IOMGR_SoftSig();*/
169
170     if (LogLevel > 0) {
171         LogLevel *= 5;
172     } else {
173         LogLevel = 1;
174     }
175     printLocks = 2;
176 #if defined(AFS_PTHREAD_ENV)
177     DebugOn(LogLevel);
178 #else /* AFS_PTHREAD_ENV */
179     IOMGR_SoftSig(DebugOn, LogLevel);
180 #endif /* AFS_PTHREAD_ENV */
181
182     (void)signal(signo, SetDebug_Signal);       /* on some platforms, this
183                                                  * signal handler needs to
184                                                  * be set again */
185 }                               /*SetDebug_Signal */
186
187 void
188 ResetDebug_Signal(int signo)
189 {
190     LogLevel = 0;
191
192     if (printLocks > 0)
193         --printLocks;
194 #if defined(AFS_PTHREAD_ENV)
195     DebugOn(LogLevel);
196 #else /* AFS_PTHREAD_ENV */
197     IOMGR_SoftSig(DebugOn, (void *)LogLevel);
198 #endif /* AFS_PTHREAD_ENV */
199
200     (void)signal(signo, ResetDebug_Signal);     /* on some platforms,
201                                                  * this signal handler
202                                                  * needs to be set
203                                                  * again */
204     if (mrafsStyleLogs)
205         OpenLog((char *)&ourName);
206 }                               /*ResetDebug_Signal */
207
208
209 void
210 SetupLogSignals(void)
211 {
212     (void)signal(SIGHUP, ResetDebug_Signal);
213     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
214     (void)signal(SIGTSTP, SetDebug_Signal);
215 }
216
217 int
218 OpenLog(const char *fileName)
219 {
220     /*
221      * This function should allow various libraries that inconsistently
222      * use stdout/stderr to all go to the same place
223      */
224     int tempfd;
225     char oldName[MAXPATHLEN];
226     struct timeval Start;
227     struct tm *TimeFields;
228     char FileName[MAXPATHLEN];
229
230 #ifndef AFS_NT40_ENV
231     if (serverLogSyslog) {
232         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
233         return (0);
234     }
235 #endif
236
237     if (mrafsStyleLogs) {
238         TM_GetTimeOfDay(&Start, 0);
239         TimeFields = localtime(&Start.tv_sec);
240         if (fileName) {
241             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
242                 strcpy((char *)&ourName, (char *)fileName);
243         }
244         afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
245                      ourName, TimeFields->tm_year + 1900,
246                      TimeFields->tm_mon + 1, TimeFields->tm_mday,
247                      TimeFields->tm_hour, TimeFields->tm_min,
248                      TimeFields->tm_sec);
249         rename(fileName, FileName);     /* don't check error code */
250         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
251     } else {
252         strcpy(oldName, fileName);
253         strcat(oldName, ".old");
254
255         /* don't check error */
256         renamefile(fileName, oldName);
257         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
258     }
259
260     if (tempfd < 0) {
261         printf("Unable to open log file %s\n", fileName);
262         return -1;
263     }
264 #if defined(AFS_PTHREAD_ENV)
265     /* redirect stdout and stderr so random printf's don't write to data */
266     assert(freopen(NULLDEV, "w", stdout) != NULL);
267     assert(freopen(NULLDEV, "w", stderr) != NULL);
268
269     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
270
271     serverLogFD = tempfd;
272 #else
273     close(tempfd);              /* just checking.... */
274     (void)freopen(fileName, "w", stdout);
275     (void)freopen(fileName, "w", stderr);
276     serverLogFD = fileno(stdout);
277 #endif /* AFS_PTHREAD_ENV */
278
279     return 0;
280 }                               /*OpenLog */
281
282 int
283 ReOpenLog(const char *fileName)
284 {
285 #if !defined(AFS_PTHREAD_ENV)
286     int tempfd;
287 #endif
288
289     if (access(fileName, F_OK) == 0)
290         return 0;               /* exists, no need to reopen. */
291
292 #if !defined(AFS_NT40_ENV)
293     if (serverLogSyslog) {
294         return 0;
295     }
296 #endif
297
298 #if defined(AFS_PTHREAD_ENV)
299     LOCK_SERVERLOG();
300     if (serverLogFD > 0)
301         close(serverLogFD);
302     serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT, 0666);
303     UNLOCK_SERVERLOG();
304     return serverLogFD < 0 ? -1 : 0;
305 #else
306
307     tempfd = open(fileName, O_WRONLY | O_APPEND | O_CREAT, 0666);
308     if (tempfd < 0) {
309         printf("Unable to open log file %s\n", fileName);
310         return -1;
311     }
312     close(tempfd);
313
314     (void)freopen(fileName, "a", stdout);
315     (void)freopen(fileName, "a", stderr);
316     serverLogFD = fileno(stdout);
317
318
319     return 0;
320 #endif /* AFS_PTHREAD_ENV */
321 }