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