Standardize License information
[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 #endif
31 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
32 #include <fcntl.h>
33 #include <afs/stds.h>
34 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39 #include "afsutil.h"
40 #include "fileutil.h"
41 #if defined(AFS_PTHREAD_ENV)
42 #include <assert.h>
43 #include <pthread.h>
44 static pthread_mutex_t serverLogMutex;
45 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
46 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
47
48 #ifdef AFS_NT40_ENV
49 #define NULLDEV "NUL"
50 #else
51 #define NULLDEV "/dev/null"
52 #endif
53
54 #else /* AFS_PTHREAD_ENV */
55 #define LOCK_SERVERLOG() 
56 #define UNLOCK_SERVERLOG() 
57 #endif  /* AFS_PTHREAD_ENV */
58
59 #ifdef AFS_NT40_ENV
60 #define F_OK 0
61 #endif
62
63 static int serverLogFD = -1;
64
65 #include <stdarg.h>
66 int LogLevel;
67
68 /* VARARGS1 */
69 void FSLog (const char *format, ...)
70 {
71     va_list args;
72
73     time_t currenttime;
74     char *timeStamp;
75     char tbuffer[1024];
76     char *info;
77     int len;
78
79     currenttime = time(0);
80     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
81     timeStamp[24] = ' ';  /* ts[24] is the newline, 25 is the null */
82     info = &timeStamp[25];
83
84     va_start(args, format);
85     (void) vsprintf(info, format, args);
86     va_end(args);
87
88     len = strlen(tbuffer);
89     LOCK_SERVERLOG();
90     if (serverLogFD > 0)
91         write(serverLogFD, tbuffer, len);
92     UNLOCK_SERVERLOG();
93
94 #if !defined(AFS_PTHREAD_ENV)
95     fflush(stdout);
96     fflush(stderr);     /* in case they're sharing the same FD */
97 #endif
98 }
99
100 static void DebugOn(int loglevel)
101 {
102     if (loglevel == 0) {
103         ViceLog(0, ("Reset Debug levels to 0\n"));
104     } else {
105         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
106     }
107 } /*DebugOn*/
108
109
110
111 void SetDebug_Signal(int signo)
112 {
113     extern int IOMGR_SoftSig();
114
115     if (LogLevel > 0) {
116         LogLevel *= 5;
117     }
118     else {
119         LogLevel = 1;
120     }
121 #if defined(AFS_PTHREAD_ENV)
122     DebugOn(LogLevel);
123 #else /* AFS_PTHREAD_ENV */
124     IOMGR_SoftSig(DebugOn, LogLevel);
125 #endif /* AFS_PTHREAD_ENV */
126
127     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
128                                       /* handler needs to be set again */
129 } /*SetDebug_Signal*/
130
131 void ResetDebug_Signal(int signo)
132 {
133     LogLevel = 0;
134
135 #if defined(AFS_PTHREAD_ENV)
136     DebugOn(LogLevel);
137 #else /* AFS_PTHREAD_ENV */
138     IOMGR_SoftSig(DebugOn, LogLevel);
139 #endif /* AFS_PTHREAD_ENV */
140
141     signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
142                                         /* handler needs to be set again */
143 } /*ResetDebug_Signal*/
144
145
146 void SetupLogSignals(void)
147 {
148     signal(SIGHUP, ResetDebug_Signal);
149     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
150     signal(SIGTSTP, SetDebug_Signal);
151 }
152
153 int OpenLog(const char *fileName) 
154 {
155     /*
156      * This function should allow various libraries that inconsistently
157      * use stdout/stderr to all go to the same place
158      */
159     int tempfd;
160     char oldName[MAXPATHLEN];
161
162     strcpy(oldName, fileName);
163     strcat(oldName, ".old");
164
165     /* don't check error */
166     renamefile(fileName, oldName);
167     tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
168     if(tempfd < 0)
169     {
170         printf("Unable to open log file %s\n", fileName);
171         return -1;
172     }
173
174 #if defined(AFS_PTHREAD_ENV)
175     /* redirect stdout and stderr so random printf's don't write to data */
176     assert(freopen(NULLDEV, "w", stdout) != NULL);
177     assert(freopen(NULLDEV, "w", stderr) != NULL);
178
179     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
180
181     serverLogFD = tempfd;
182 #else
183     close(tempfd); /* just checking.... */
184     freopen(fileName, "w", stdout);
185     freopen(fileName, "w", stderr);
186     serverLogFD = fileno(stdout);
187 #endif /* AFS_PTHREAD_ENV */
188
189     return 0;
190 } /*OpenLog*/
191
192 int ReOpenLog(const char *fileName) 
193 {
194 #if !defined(AFS_PTHREAD_ENV)
195     int tempfd;
196 #endif
197
198     if (access(fileName, F_OK)==0)
199         return 0; /* exists, no need to reopen. */
200
201 #if defined(AFS_PTHREAD_ENV)
202     LOCK_SERVERLOG();
203     if (serverLogFD > 0)
204         close(serverLogFD);
205     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
206     UNLOCK_SERVERLOG();
207     return serverLogFD < 0 ? -1 : 0;
208 #else
209
210     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
211     if(tempfd < 0)
212     {
213         printf("Unable to open log file %s\n", fileName);
214         return -1;
215     }
216     close(tempfd);
217
218     freopen(fileName, "a", stdout);
219     freopen(fileName, "a", stderr);
220     serverLogFD = fileno(stdout);
221   
222
223     return 0;
224 #endif /* AFS_PTHREAD_ENV */
225 }