2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* serverLog.c - Server logging */
12 /* Information Technology Center */
15 /* Function - These routiens implement logging from the servers */
17 /* ********************************************************************** */
19 #include <afs/param.h>
28 #include <sys/param.h>
32 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
35 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
42 #if defined(AFS_PTHREAD_ENV)
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)
52 #define NULLDEV "/dev/null"
55 #else /* AFS_PTHREAD_ENV */
56 #define LOCK_SERVERLOG()
57 #define UNLOCK_SERVERLOG()
58 #endif /* AFS_PTHREAD_ENV */
66 static int serverLogFD = -1;
69 int serverLogSyslog = 0;
70 int serverLogSyslogFacility = LOG_DAEMON;
75 int mrafsStyleLogs = 0;
77 static char ourName[MAXPATHLEN];
80 void FSLog (const char *format, ...)
92 currenttime = time(0);
93 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
94 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
95 info = &timeStamp[25];
99 sprintf(info, "[%s] ", name);
100 info += strlen(info);
103 va_start(args, format);
104 (void) vsprintf(info, format, args);
107 len = strlen(tbuffer);
110 if ( serverLogSyslog ){
111 syslog(LOG_INFO, "%s", info);
115 write(serverLogFD, tbuffer, len);
118 #if !defined(AFS_PTHREAD_ENV)
120 fflush(stderr); /* in case they're sharing the same FD */
124 static void DebugOn(int loglevel)
127 ViceLog(0, ("Reset Debug levels to 0\n"));
129 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
135 void SetDebug_Signal(int signo)
137 extern int IOMGR_SoftSig();
146 #if defined(AFS_PTHREAD_ENV)
148 #else /* AFS_PTHREAD_ENV */
149 IOMGR_SoftSig(DebugOn, LogLevel);
150 #endif /* AFS_PTHREAD_ENV */
152 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
153 /* handler needs to be set again */
154 } /*SetDebug_Signal*/
156 void ResetDebug_Signal(int signo)
160 if (printLocks >0) --printLocks;
161 #if defined(AFS_PTHREAD_ENV)
163 #else /* AFS_PTHREAD_ENV */
164 IOMGR_SoftSig(DebugOn, LogLevel);
165 #endif /* AFS_PTHREAD_ENV */
167 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
168 /* handler needs to be set again */
170 OpenLog((char *)&ourName);
171 } /*ResetDebug_Signal*/
174 void SetupLogSignals(void)
176 signal(SIGHUP, ResetDebug_Signal);
177 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
178 signal(SIGTSTP, SetDebug_Signal);
181 int OpenLog(const char *fileName)
184 * This function should allow various libraries that inconsistently
185 * use stdout/stderr to all go to the same place
188 char oldName[MAXPATHLEN];
189 struct timeval Start;
190 struct tm *TimeFields;
191 char FileName[MAXPATHLEN];
193 if ( serverLogSyslog ) {
195 openlog(NULL, LOG_PID, serverLogSyslogFacility);
200 if (mrafsStyleLogs) {
201 TM_GetTimeOfDay(&Start, 0);
202 TimeFields = localtime(&Start.tv_sec);
204 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
205 strcpy((char *)&ourName, (char *) fileName);
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);
214 strcpy(oldName, fileName);
215 strcat(oldName, ".old");
217 /* don't check error */
218 renamefile(fileName, oldName);
219 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
224 printf("Unable to open log file %s\n", fileName);
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);
233 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
235 serverLogFD = tempfd;
237 close(tempfd); /* just checking.... */
238 freopen(fileName, "w", stdout);
239 freopen(fileName, "w", stderr);
240 serverLogFD = fileno(stdout);
241 #endif /* AFS_PTHREAD_ENV */
246 int ReOpenLog(const char *fileName)
248 #if !defined(AFS_PTHREAD_ENV)
252 if (access(fileName, F_OK)==0)
253 return 0; /* exists, no need to reopen. */
255 if ( serverLogSyslog ) {
259 #if defined(AFS_PTHREAD_ENV)
263 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
265 return serverLogFD < 0 ? -1 : 0;
268 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
271 printf("Unable to open log file %s\n", fileName);
276 freopen(fileName, "a", stdout);
277 freopen(fileName, "a", stderr);
278 serverLogFD = fileno(stdout);
282 #endif /* AFS_PTHREAD_ENV */