windows-compile-fixes-20010411
[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 *(*threadNameProgram)();
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 = (*threadNameProgram)();
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) && !defined(AFS_NT40_ENV)
119     if ( ! serverLogSyslog ) {
120         fflush(stdout);
121         fflush(stderr);     /* in case they're sharing the same FD */
122     }
123 #endif
124 }
125
126 static void DebugOn(int loglevel)
127 {
128     if (loglevel == 0) {
129         ViceLog(0, ("Reset Debug levels to 0\n"));
130     } else {
131         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
132     }
133 } /*DebugOn*/
134
135
136
137 void SetDebug_Signal(int signo)
138 {
139     extern int IOMGR_SoftSig();
140
141     if (LogLevel > 0) {
142         LogLevel *= 5;
143     }
144     else {
145         LogLevel = 1;
146     }
147     printLocks = 2;
148 #if defined(AFS_PTHREAD_ENV)
149     DebugOn(LogLevel);
150 #else /* AFS_PTHREAD_ENV */
151     IOMGR_SoftSig(DebugOn, LogLevel);
152 #endif /* AFS_PTHREAD_ENV */
153
154     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
155                                       /* handler needs to be set again */
156 } /*SetDebug_Signal*/
157
158 void ResetDebug_Signal(int signo)
159 {
160     LogLevel = 0;
161
162     if (printLocks >0) --printLocks;
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, ResetDebug_Signal);   /* on some platforms, this signal */
170                                         /* handler needs to be set again */
171     if (mrafsStyleLogs)
172         OpenLog((char *)&ourName);
173 } /*ResetDebug_Signal*/
174
175
176 void SetupLogSignals(void)
177 {
178     signal(SIGHUP, ResetDebug_Signal);
179     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
180     signal(SIGTSTP, SetDebug_Signal);
181 }
182
183 int OpenLog(const char *fileName) 
184 {
185     /*
186      * This function should allow various libraries that inconsistently
187      * use stdout/stderr to all go to the same place
188      */
189     int tempfd;
190     char oldName[MAXPATHLEN];
191     struct timeval Start;
192     struct tm *TimeFields;
193     char FileName[MAXPATHLEN]; 
194
195 #ifndef AFS_NT40_ENV
196     if ( serverLogSyslog ) {
197         openlog(NULL, LOG_PID, serverLogSyslogFacility);
198         return;
199     }
200 #endif
201
202     if (mrafsStyleLogs) {
203         TM_GetTimeOfDay(&Start, 0);
204         TimeFields = localtime(&Start.tv_sec);
205         if (fileName) {
206             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
207             strcpy((char *)&ourName, (char *) fileName);
208         }
209         sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
210                 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, 
211                 TimeFields->tm_mday, TimeFields->tm_hour, 
212                 TimeFields->tm_min, TimeFields->tm_sec);
213         rename (fileName, FileName); /* don't check error code */
214         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666); 
215     } else {
216         strcpy(oldName, fileName);
217         strcat(oldName, ".old");
218
219         /* don't check error */
220         renamefile(fileName, oldName);
221         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
222     }
223
224     if(tempfd < 0)
225     {
226         printf("Unable to open log file %s\n", fileName);
227         return -1;
228     }
229
230 #if defined(AFS_PTHREAD_ENV)
231     /* redirect stdout and stderr so random printf's don't write to data */
232     assert(freopen(NULLDEV, "w", stdout) != NULL);
233     assert(freopen(NULLDEV, "w", stderr) != NULL);
234
235     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
236
237     serverLogFD = tempfd;
238 #else
239     close(tempfd); /* just checking.... */
240     freopen(fileName, "w", stdout);
241     freopen(fileName, "w", stderr);
242     serverLogFD = fileno(stdout);
243 #endif /* AFS_PTHREAD_ENV */
244
245     return 0;
246 } /*OpenLog*/
247
248 int ReOpenLog(const char *fileName) 
249 {
250 #if !defined(AFS_PTHREAD_ENV)
251     int tempfd;
252 #endif
253
254     if (access(fileName, F_OK)==0)
255         return 0; /* exists, no need to reopen. */
256
257 #if !defined(AFS_NT40_ENV)
258     if ( serverLogSyslog ) {
259         return 0;
260     }
261 #endif
262
263 #if defined(AFS_PTHREAD_ENV)
264     LOCK_SERVERLOG();
265     if (serverLogFD > 0)
266         close(serverLogFD);
267     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
268     UNLOCK_SERVERLOG();
269     return serverLogFD < 0 ? -1 : 0;
270 #else
271
272     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
273     if(tempfd < 0)
274     {
275         printf("Unable to open log file %s\n", fileName);
276         return -1;
277     }
278     close(tempfd);
279
280     freopen(fileName, "a", stdout);
281     freopen(fileName, "a", stderr);
282     serverLogFD = fileno(stdout);
283   
284
285     return 0;
286 #endif /* AFS_PTHREAD_ENV */
287 }