syslog-tags-20030515
[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("$Header$");
23
24 #include <stdio.h>
25 #ifdef AFS_NT40_ENV
26 #include <io.h>
27 #include <time.h>
28 #else
29 #ifdef AFS_AIX_ENV
30 #include <time.h>
31 #endif
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <syslog.h>
35 #endif
36 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
37 #include <fcntl.h>
38 #include <afs/stds.h>
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46 #include "afsutil.h"
47 #include "fileutil.h"
48 #if defined(AFS_PTHREAD_ENV)
49 #include <assert.h>
50 #include <pthread.h>
51 static pthread_mutex_t serverLogMutex;
52 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
53 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
54
55 #ifdef AFS_NT40_ENV
56 #define NULLDEV "NUL"
57 #else
58 #define NULLDEV "/dev/null"
59 #endif
60
61 #else /* AFS_PTHREAD_ENV */
62 #define LOCK_SERVERLOG() 
63 #define UNLOCK_SERVERLOG() 
64 #endif  /* AFS_PTHREAD_ENV */
65
66 #ifdef AFS_NT40_ENV
67 #define F_OK 0
68 #endif
69
70 char *(*threadNameProgram)();
71
72 static int serverLogFD = -1;
73
74 #ifndef AFS_NT40_ENV
75 int serverLogSyslog = 0;
76 int serverLogSyslogFacility = LOG_DAEMON;
77 char *serverLogSyslogTag = 0;
78 #endif
79
80 #include <stdarg.h>
81 int LogLevel;
82 int mrafsStyleLogs = 0;
83 int printLocks = 0;
84 static char ourName[MAXPATHLEN];
85
86 void WriteLogBuffer(char *buf, afs_uint32 len)
87 {
88     LOCK_SERVERLOG();
89     if (serverLogFD > 0)
90       write(serverLogFD, buf, len);
91     UNLOCK_SERVERLOG();
92 }
93
94 /* VARARGS1 */
95 void FSLog (const char *format, ...)
96 {
97     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        sprintf(info, "[%s] ", name);
114        info += strlen(info);
115     }
116
117     va_start(args, format);
118     (void) vsprintf(info, format, args);
119     va_end(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             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 }
139
140 static int DebugOn(int loglevel)
141 {
142     if (loglevel == 0) {
143         ViceLog(0, ("Reset Debug levels to 0\n"));
144     } else {
145         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
146     }
147     return 0;
148 } /*DebugOn*/
149
150
151
152 void SetDebug_Signal(int signo)
153 {
154     extern int IOMGR_SoftSig();
155
156     if (LogLevel > 0) {
157         LogLevel *= 5;
158     }
159     else {
160         LogLevel = 1;
161     }
162     printLocks = 2;
163 #if defined(AFS_PTHREAD_ENV)
164     DebugOn(LogLevel);
165 #else /* AFS_PTHREAD_ENV */
166     IOMGR_SoftSig(DebugOn, LogLevel);
167 #endif /* AFS_PTHREAD_ENV */
168
169     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
170                                       /* handler needs to be set again */
171 } /*SetDebug_Signal*/
172
173 void ResetDebug_Signal(int signo)
174 {
175     LogLevel = 0;
176
177     if (printLocks >0) --printLocks;
178 #if defined(AFS_PTHREAD_ENV)
179     DebugOn(LogLevel);
180 #else /* AFS_PTHREAD_ENV */
181     IOMGR_SoftSig(DebugOn, (void *)LogLevel);
182 #endif /* AFS_PTHREAD_ENV */
183
184     signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
185                                         /* handler needs to be set again */
186     if (mrafsStyleLogs)
187         OpenLog((char *)&ourName);
188 } /*ResetDebug_Signal*/
189
190
191 void SetupLogSignals(void)
192 {
193     signal(SIGHUP, ResetDebug_Signal);
194     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
195     signal(SIGTSTP, SetDebug_Signal);
196 }
197
198 int OpenLog(const char *fileName) 
199 {
200     /*
201      * This function should allow various libraries that inconsistently
202      * use stdout/stderr to all go to the same place
203      */
204     int tempfd;
205     char oldName[MAXPATHLEN];
206     struct timeval Start;
207     struct tm *TimeFields;
208     char FileName[MAXPATHLEN]; 
209
210 #ifndef AFS_NT40_ENV
211     if ( serverLogSyslog ) {
212         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
213         return(0);
214     }
215 #endif
216
217     if (mrafsStyleLogs) {
218         TM_GetTimeOfDay(&Start, 0);
219         TimeFields = localtime(&Start.tv_sec);
220         if (fileName) {
221             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
222             strcpy((char *)&ourName, (char *) fileName);
223         }
224         sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
225                 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, 
226                 TimeFields->tm_mday, TimeFields->tm_hour, 
227                 TimeFields->tm_min, TimeFields->tm_sec);
228         rename (fileName, FileName); /* don't check error code */
229         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666); 
230     } else {
231         strcpy(oldName, fileName);
232         strcat(oldName, ".old");
233
234         /* don't check error */
235         renamefile(fileName, oldName);
236         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
237     }
238
239     if(tempfd < 0)
240     {
241         printf("Unable to open log file %s\n", fileName);
242         return -1;
243     }
244
245 #if defined(AFS_PTHREAD_ENV)
246     /* redirect stdout and stderr so random printf's don't write to data */
247     assert(freopen(NULLDEV, "w", stdout) != NULL);
248     assert(freopen(NULLDEV, "w", stderr) != NULL);
249
250     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
251
252     serverLogFD = tempfd;
253 #else
254     close(tempfd); /* just checking.... */
255     freopen(fileName, "w", stdout);
256     freopen(fileName, "w", stderr);
257     serverLogFD = fileno(stdout);
258 #endif /* AFS_PTHREAD_ENV */
259
260     return 0;
261 } /*OpenLog*/
262
263 int ReOpenLog(const char *fileName) 
264 {
265 #if !defined(AFS_PTHREAD_ENV)
266     int tempfd;
267 #endif
268
269     if (access(fileName, F_OK)==0)
270         return 0; /* exists, no need to reopen. */
271
272 #if !defined(AFS_NT40_ENV)
273     if ( serverLogSyslog ) {
274         return 0;
275     }
276 #endif
277
278 #if defined(AFS_PTHREAD_ENV)
279     LOCK_SERVERLOG();
280     if (serverLogFD > 0)
281         close(serverLogFD);
282     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
283     UNLOCK_SERVERLOG();
284     return serverLogFD < 0 ? -1 : 0;
285 #else
286
287     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
288     if(tempfd < 0)
289     {
290         printf("Unable to open log file %s\n", fileName);
291         return -1;
292     }
293     close(tempfd);
294
295     freopen(fileName, "a", stdout);
296     freopen(fileName, "a", stderr);
297     serverLogFD = fileno(stdout);
298   
299
300     return 0;
301 #endif /* AFS_PTHREAD_ENV */
302 }