include-afsconfig-before-param-h-20010712
[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_STRINGS_H
40 #include <strings.h>
41 #else
42 #ifdef HAVE_STRING_H
43 #include <string.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 #endif
78
79 #include <stdarg.h>
80 int LogLevel;
81 int mrafsStyleLogs = 0;
82 int printLocks = 0;
83 static char ourName[MAXPATHLEN];
84
85 void WriteLogBuffer(buf,len)
86     char *buf;
87     afs_uint32 len;
88 {
89     LOCK_SERVERLOG();
90     if (serverLogFD > 0)
91       write(serverLogFD, buf, len);
92     UNLOCK_SERVERLOG();
93 }
94
95 /* VARARGS1 */
96 void FSLog (const char *format, ...)
97 {
98     va_list args;
99
100     time_t currenttime;
101     char *timeStamp;
102     char tbuffer[1024];
103     char *info;
104     int len;
105     char *name;
106
107     currenttime = time(0);
108     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
109     timeStamp[24] = ' ';  /* ts[24] is the newline, 25 is the null */
110     info = &timeStamp[25];
111
112     if (mrafsStyleLogs) {
113        name = (*threadNameProgram)();
114        sprintf(info, "[%s] ", name);
115        info += strlen(info);
116     }
117
118     va_start(args, format);
119     (void) vsprintf(info, format, args);
120     va_end(args);
121
122     len = strlen(tbuffer);
123     LOCK_SERVERLOG();
124 #ifndef AFS_NT40_ENV
125     if ( serverLogSyslog ){
126         syslog(LOG_INFO, "%s", info);
127     } else 
128 #endif
129         if (serverLogFD > 0)
130             write(serverLogFD, tbuffer, len);
131     UNLOCK_SERVERLOG();
132
133 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
134     if ( ! serverLogSyslog ) {
135         fflush(stdout);
136         fflush(stderr);     /* in case they're sharing the same FD */
137     }
138 #endif
139 }
140
141 static int DebugOn(int loglevel)
142 {
143     if (loglevel == 0) {
144         ViceLog(0, ("Reset Debug levels to 0\n"));
145     } else {
146         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
147     }
148     return 0;
149 } /*DebugOn*/
150
151
152
153 void SetDebug_Signal(int signo)
154 {
155     extern int IOMGR_SoftSig();
156
157     if (LogLevel > 0) {
158         LogLevel *= 5;
159     }
160     else {
161         LogLevel = 1;
162     }
163     printLocks = 2;
164 #if defined(AFS_PTHREAD_ENV)
165     DebugOn(LogLevel);
166 #else /* AFS_PTHREAD_ENV */
167     IOMGR_SoftSig(DebugOn, LogLevel);
168 #endif /* AFS_PTHREAD_ENV */
169
170     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
171                                       /* handler needs to be set again */
172 } /*SetDebug_Signal*/
173
174 void ResetDebug_Signal(int signo)
175 {
176     LogLevel = 0;
177
178     if (printLocks >0) --printLocks;
179 #if defined(AFS_PTHREAD_ENV)
180     DebugOn(LogLevel);
181 #else /* AFS_PTHREAD_ENV */
182     IOMGR_SoftSig(DebugOn, (void *)LogLevel);
183 #endif /* AFS_PTHREAD_ENV */
184
185     signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
186                                         /* handler needs to be set again */
187     if (mrafsStyleLogs)
188         OpenLog((char *)&ourName);
189 } /*ResetDebug_Signal*/
190
191
192 void SetupLogSignals(void)
193 {
194     signal(SIGHUP, ResetDebug_Signal);
195     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
196     signal(SIGTSTP, SetDebug_Signal);
197 }
198
199 int OpenLog(const char *fileName) 
200 {
201     /*
202      * This function should allow various libraries that inconsistently
203      * use stdout/stderr to all go to the same place
204      */
205     int tempfd;
206     char oldName[MAXPATHLEN];
207     struct timeval Start;
208     struct tm *TimeFields;
209     char FileName[MAXPATHLEN]; 
210
211 #ifndef AFS_NT40_ENV
212     if ( serverLogSyslog ) {
213         openlog(NULL, LOG_PID, serverLogSyslogFacility);
214         return(0);
215     }
216 #endif
217
218     if (mrafsStyleLogs) {
219         TM_GetTimeOfDay(&Start, 0);
220         TimeFields = localtime(&Start.tv_sec);
221         if (fileName) {
222             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
223             strcpy((char *)&ourName, (char *) fileName);
224         }
225         sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
226                 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, 
227                 TimeFields->tm_mday, TimeFields->tm_hour, 
228                 TimeFields->tm_min, TimeFields->tm_sec);
229         rename (fileName, FileName); /* don't check error code */
230         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666); 
231     } else {
232         strcpy(oldName, fileName);
233         strcat(oldName, ".old");
234
235         /* don't check error */
236         renamefile(fileName, oldName);
237         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
238     }
239
240     if(tempfd < 0)
241     {
242         printf("Unable to open log file %s\n", fileName);
243         return -1;
244     }
245
246 #if defined(AFS_PTHREAD_ENV)
247     /* redirect stdout and stderr so random printf's don't write to data */
248     assert(freopen(NULLDEV, "w", stdout) != NULL);
249     assert(freopen(NULLDEV, "w", stderr) != NULL);
250
251     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
252
253     serverLogFD = tempfd;
254 #else
255     close(tempfd); /* just checking.... */
256     freopen(fileName, "w", stdout);
257     freopen(fileName, "w", stderr);
258     serverLogFD = fileno(stdout);
259 #endif /* AFS_PTHREAD_ENV */
260
261     return 0;
262 } /*OpenLog*/
263
264 int ReOpenLog(const char *fileName) 
265 {
266 #if !defined(AFS_PTHREAD_ENV)
267     int tempfd;
268 #endif
269
270     if (access(fileName, F_OK)==0)
271         return 0; /* exists, no need to reopen. */
272
273 #if !defined(AFS_NT40_ENV)
274     if ( serverLogSyslog ) {
275         return 0;
276     }
277 #endif
278
279 #if defined(AFS_PTHREAD_ENV)
280     LOCK_SERVERLOG();
281     if (serverLogFD > 0)
282         close(serverLogFD);
283     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
284     UNLOCK_SERVERLOG();
285     return serverLogFD < 0 ? -1 : 0;
286 #else
287
288     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
289     if(tempfd < 0)
290     {
291         printf("Unable to open log file %s\n", fileName);
292         return -1;
293     }
294     close(tempfd);
295
296     freopen(fileName, "a", stdout);
297     freopen(fileName, "a", stderr);
298     serverLogFD = fileno(stdout);
299   
300
301     return 0;
302 #endif /* AFS_PTHREAD_ENV */
303 }