| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | #include <afsconfig.h> |
| 20 | #include <afs/param.h> |
| 21 | #include <afs/stds.h> |
| 22 | |
| 23 | #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */ |
| 24 | |
| 25 | #include <roken.h> /* Must come after procmgmt.h */ |
| 26 | |
| 27 | #include "afsutil.h" |
| 28 | #include "fileutil.h" |
| 29 | #include <lwp.h> |
| 30 | |
| 31 | #if defined(AFS_PTHREAD_ENV) |
| 32 | #include <afs/afs_assert.h> |
| 33 | |
| 34 | #include <rx/rx.h> |
| 35 | #include <pthread.h> |
| 36 | static pthread_mutex_t serverLogMutex; |
| 37 | #define LOCK_SERVERLOG() MUTEX_ENTER(&serverLogMutex) |
| 38 | #define UNLOCK_SERVERLOG() MUTEX_EXIT(&serverLogMutex) |
| 39 | |
| 40 | #ifdef AFS_NT40_ENV |
| 41 | #define NULLDEV "NUL" |
| 42 | #else |
| 43 | #define NULLDEV "/dev/null" |
| 44 | #endif |
| 45 | |
| 46 | #else /* AFS_PTHREAD_ENV */ |
| 47 | #define LOCK_SERVERLOG() |
| 48 | #define UNLOCK_SERVERLOG() |
| 49 | #endif /* AFS_PTHREAD_ENV */ |
| 50 | |
| 51 | #ifdef AFS_NT40_ENV |
| 52 | #define F_OK0 0 |
| 53 | #define O_NONBLOCK0x0004 0 |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | dummyThreadNum(void) |
| 58 | { |
| 59 | return -1; |
| 60 | } |
| 61 | static int (*threadNumProgram) (void) = dummyThreadNum; |
| 62 | |
| 63 | static int serverLogFD = -1; |
| 64 | |
| 65 | #ifndef AFS_NT40_ENV |
| 66 | int serverLogSyslog = 0; |
| 67 | int serverLogSyslogFacility = LOG_DAEMON(3<<3); |
| 68 | char *serverLogSyslogTag = 0; |
| 69 | #endif |
| 70 | |
| 71 | #include <stdarg.h> |
| 72 | int LogLevel; |
| 73 | int mrafsStyleLogs = 0; |
| 74 | static int threadIdLogs = 0; |
| 75 | int printLocks = 0; |
| 76 | static char ourName[MAXPATHLEN1024]; |
| 77 | |
| 78 | void |
| 79 | SetLogThreadNumProgram(int (*func) (void) ) |
| 80 | { |
| 81 | threadNumProgram = func; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | WriteLogBuffer(char *buf, afs_uint32 len) |
| 86 | { |
| 87 | LOCK_SERVERLOG(); |
| 88 | if (serverLogFD > 0) |
| 89 | (void)write(serverLogFD, buf, len); |
| 90 | UNLOCK_SERVERLOG(); |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | LogThreadNum(void) |
| 95 | { |
| 96 | return (*threadNumProgram) (); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | vFSLog(const char *format, va_list args) |
| 101 | { |
| 102 | time_t currenttime; |
| 103 | char tbuffer[1024]; |
| 104 | char *info; |
| 105 | size_t len; |
| 106 | struct tm tm; |
| 107 | int num; |
| 108 | |
| 109 | currenttime = time(NULL((void *)0)); |
| 110 | len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y ", |
| 111 | localtime_r(¤ttime, &tm)); |
| 112 | info = &tbuffer[len]; |
| 113 | |
| 114 | if (mrafsStyleLogs || threadIdLogs) { |
| 115 | num = (*threadNumProgram) (); |
| 116 | if (num > -1) { |
| 117 | snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ", |
| 118 | num); |
| 119 | info += strlen(info); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args); |
| 124 | |
| 125 | len = strlen(tbuffer); |
| 126 | LOCK_SERVERLOG(); |
| 127 | #ifndef AFS_NT40_ENV |
| 128 | if (serverLogSyslog) { |
| 129 | syslog(LOG_INFO6, "%s", info); |
| 130 | } else |
| 131 | #endif |
| 132 | if (serverLogFD > 0) |
| 133 | (void)write(serverLogFD, tbuffer, len); |
| 134 | UNLOCK_SERVERLOG(); |
| 135 | |
| 136 | #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV) |
| 137 | if (!serverLogSyslog) { |
| 138 | fflush(stdout__stdoutp); |
| 139 | fflush(stderr__stderrp); |
| 140 | } |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | void |
| 147 | FSLog(const char *format, ...) |
| 148 | { |
| 149 | va_list args; |
| 150 | |
| 151 | va_start(args, format)__builtin_va_start((args), (format)); |
| 152 | vFSLog(format, args); |
| 153 | va_end(args)__builtin_va_end(args); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | LogCommandLine(int argc, char **argv, const char *progname, |
| 158 | const char *version, const char *logstring, |
| 159 | void (*log) (const char *format, ...)) |
| 160 | { |
| 161 | int i, l; |
| 162 | char *commandLine, *cx; |
| 163 | |
| 164 | for (l = i = 0; i < argc; i++) |
| 1 | Loop condition is false. Execution continues on line 166 |
|
| 165 | l += strlen(argv[i]) + 1; |
| 166 | if ((commandLine = malloc(l))) { |
| 2 | Call to 'malloc' has an allocation size of 0 bytes |
|
| 167 | for (cx = commandLine, i = 0; i < argc; i++) { |
| 168 | strcpy(cx, argv[i]); |
| 169 | cx += strlen(cx); |
| 170 | *(cx++) = ' '; |
| 171 | } |
| 172 | commandLine[l-1] = '\0'; |
| 173 | (*log)("%s %s %s%s(%s)\n", logstring, progname, |
| 174 | version, strlen(version)>0?" ":"", commandLine); |
| 175 | free(commandLine); |
| 176 | } else { |
| 177 | |
| 178 | (*log)("%s %s%s%s\n", logstring, |
| 179 | progname, strlen(version)>0?" ":"", version); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void* |
| 184 | DebugOn(void *param) |
| 185 | { |
| 186 | int loglevel = (intptr_t)param; |
| 187 | if (loglevel == 0) { |
| 188 | ViceLog(0, ("Reset Debug levels to 0\n"))do { if ((0) <= LogLevel) (FSLog ("Reset Debug levels to 0\n" )); } while (0); |
| 189 | } else { |
| 190 | ViceLog(0, ("Set Debug On level = %d\n", loglevel))do { if ((0) <= LogLevel) (FSLog ("Set Debug On level = %d\n" , loglevel)); } while (0); |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | void |
| 198 | SetDebug_Signal(int signo) |
| 199 | { |
| 200 | if (LogLevel > 0) { |
| 201 | LogLevel *= 5; |
| 202 | |
| 203 | #if defined(AFS_PTHREAD_ENV) |
| 204 | if (LogLevel > 1 && threadNumProgram != NULL((void *)0) && |
| 205 | threadIdLogs == 0) { |
| 206 | threadIdLogs = 1; |
| 207 | } |
| 208 | #endif |
| 209 | } else { |
| 210 | LogLevel = 1; |
| 211 | |
| 212 | #if defined(AFS_PTHREAD_ENV) |
| 213 | if (threadIdLogs == 1) |
| 214 | threadIdLogs = 0; |
| 215 | #endif |
| 216 | } |
| 217 | printLocks = 2; |
| 218 | #if defined(AFS_PTHREAD_ENV) |
| 219 | DebugOn((void *)(intptr_t)LogLevel); |
| 220 | #else /* AFS_PTHREAD_ENV */ |
| 221 | IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel); |
| 222 | #endif /* AFS_PTHREAD_ENV */ |
| 223 | |
| 224 | (void)signal(signo, SetDebug_Signal); |
| 225 | |
| 226 | |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | ResetDebug_Signal(int signo) |
| 231 | { |
| 232 | LogLevel = 0; |
| 233 | |
| 234 | if (printLocks > 0) |
| 235 | --printLocks; |
| 236 | #if defined(AFS_PTHREAD_ENV) |
| 237 | DebugOn((void *)(intptr_t)LogLevel); |
| 238 | #else /* AFS_PTHREAD_ENV */ |
| 239 | IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel); |
| 240 | #endif /* AFS_PTHREAD_ENV */ |
| 241 | |
| 242 | (void)signal(signo, ResetDebug_Signal); |
| 243 | |
| 244 | |
| 245 | |
| 246 | #if defined(AFS_PTHREAD_ENV) |
| 247 | if (threadIdLogs == 1) |
| 248 | threadIdLogs = 0; |
| 249 | #endif |
| 250 | if (mrafsStyleLogs) |
| 251 | OpenLog((char *)&ourName); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void |
| 256 | SetupLogSignals(void) |
| 257 | { |
| 258 | (void)signal(SIGHUP1, ResetDebug_Signal); |
| 259 | |
| 260 | (void)signal(SIGTSTP18, SetDebug_Signal); |
| 261 | #ifndef AFS_NT40_ENV |
| 262 | (void)signal(SIGPIPE13, SIG_IGN((__sighandler_t *)1)); |
| 263 | #endif |
| 264 | } |
| 265 | |
| 266 | int |
| 267 | OpenLog(const char *fileName) |
| 268 | { |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | int tempfd, isfifo = 0; |
| 274 | char oldName[MAXPATHLEN1024]; |
| 275 | struct timeval Start; |
| 276 | struct tm *TimeFields; |
| 277 | char FileName[MAXPATHLEN1024]; |
| 278 | |
| 279 | #ifndef AFS_NT40_ENV |
| 280 | struct stat statbuf; |
| 281 | |
| 282 | if (serverLogSyslog) { |
| 283 | openlog(serverLogSyslogTag, LOG_PID0x01, serverLogSyslogFacility); |
| 284 | return (0); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode)(((statbuf.st_mode) & 0170000) == 0010000))) { |
| 289 | isfifo = 1; |
| 290 | } |
| 291 | #endif |
| 292 | |
| 293 | if (mrafsStyleLogs) { |
| 294 | time_t t; |
| 295 | struct stat buf; |
| 296 | gettimeofday(&Start, NULL((void *)0)); |
| 297 | t = Start.tv_sec; |
| 298 | TimeFields = localtime(&t); |
| 299 | if (fileName) { |
| 300 | if (strncmp(fileName, (char *)&ourName, strlen(fileName))) |
| 301 | strcpy((char *)&ourName, (char *)fileName); |
| 302 | } |
| 303 | makefilename: |
| 304 | snprintf(FileName, MAXPATHLEN1024, "%s.%d%02d%02d%02d%02d%02d", |
| 305 | ourName, TimeFields->tm_year + 1900, |
| 306 | TimeFields->tm_mon + 1, TimeFields->tm_mday, |
| 307 | TimeFields->tm_hour, TimeFields->tm_min, |
| 308 | TimeFields->tm_sec); |
| 309 | if(lstat(FileName, &buf) == 0) { |
| 310 | |
| 311 | TimeFields->tm_sec++; |
| 312 | goto makefilename; |
| 313 | } |
| 314 | if (!isfifo) |
| 315 | renamefile(fileName, FileName); |
| 316 | tempfd = open(fileName, O_WRONLY0x0001 | O_TRUNC0x0400 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666); |
| 317 | } else { |
| 318 | strcpy(oldName, fileName); |
| 319 | strcat(oldName, ".old"); |
| 320 | |
| 321 | |
| 322 | if (!isfifo) |
| 323 | renamefile(fileName, oldName); |
| 324 | tempfd = open(fileName, O_WRONLY0x0001 | O_TRUNC0x0400 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666); |
| 325 | } |
| 326 | |
| 327 | if (tempfd < 0) { |
| 328 | printf("Unable to open log file %s\n", fileName); |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | (void)freopen(fileName, "a", stdout__stdoutp); |
| 333 | (void)freopen(fileName, "a", stderr__stderrp); |
| 334 | #ifdef HAVE_SETVBUF1 |
| 335 | setvbuf(stderr__stderrp, NULL((void *)0), _IONBF2, 0); |
| 336 | #else |
| 337 | setbuf(stderr__stderrp, NULL((void *)0)); |
| 338 | #endif |
| 339 | |
| 340 | #if defined(AFS_PTHREAD_ENV) |
| 341 | MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0); |
| 342 | #endif /* AFS_PTHREAD_ENV */ |
| 343 | |
| 344 | serverLogFD = tempfd; |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | int |
| 350 | ReOpenLog(const char *fileName) |
| 351 | { |
| 352 | int isfifo = 0; |
| 353 | #if !defined(AFS_NT40_ENV) |
| 354 | struct stat statbuf; |
| 355 | #endif |
| 356 | |
| 357 | if (access(fileName, F_OK0) == 0) |
| 358 | return 0; |
| 359 | |
| 360 | #if !defined(AFS_NT40_ENV) |
| 361 | if (serverLogSyslog) { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode)(((statbuf.st_mode) & 0170000) == 0010000))) { |
| 367 | isfifo = 1; |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | LOCK_SERVERLOG(); |
| 372 | if (serverLogFD > 0) |
| 373 | close(serverLogFD); |
| 374 | serverLogFD = open(fileName, O_WRONLY0x0001 | O_APPEND0x0008 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666); |
| 375 | if (serverLogFD > 0) { |
| 376 | (void)freopen(fileName, "a", stdout__stdoutp); |
| 377 | (void)freopen(fileName, "a", stderr__stderrp); |
| 378 | #ifdef HAVE_SETVBUF1 |
| 379 | #ifdef SETVBUF_REVERSED |
| 380 | setvbuf(stderr__stderrp, _IONBF2, NULL((void *)0), 0); |
| 381 | #else |
| 382 | setvbuf(stderr__stderrp, NULL((void *)0), _IONBF2, 0); |
| 383 | #endif |
| 384 | #else |
| 385 | setbuf(stderr__stderrp, NULL((void *)0)); |
| 386 | #endif |
| 387 | |
| 388 | } |
| 389 | UNLOCK_SERVERLOG(); |
| 390 | return serverLogFD < 0 ? -1 : 0; |
| 391 | } |