static void message(char *fmt, ...) { int fd; struct flock fl; va_list arguments; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 1; #ifdef CONFIG_FEATURE_IPC_SYSLOG if ((circular_logging == TRUE) && (buf != NULL)) { char b[1024]; va_start(arguments, fmt); vsnprintf(b, sizeof(b) - 1, fmt, arguments); va_end(arguments); circ_message(b); } else #endif if ((fd = device_open(logFilePath, O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | O_NONBLOCK)) >= 0) { fl.l_type = F_WRLCK; fcntl(fd, F_SETLKW, &fl); #ifdef CONFIG_FEATURE_ROTATE_LOGFILE if ( logFileSize > 0 ) { struct stat statf; int r = fstat(fd, &statf); if( !r && (statf.st_mode & S_IFREG) && (lseek(fd,0,SEEK_END) > logFileSize) ) { if(logFileRotate > 0) { int i; char oldFile[(strlen(logFilePath)+3)], newFile[(strlen(logFilePath)+3)]; for(i=logFileRotate-1;i>0;i--) { sprintf(oldFile, "%s.%d", logFilePath, i-1); sprintf(newFile, "%s.%d", logFilePath, i); rename(oldFile, newFile); } sprintf(newFile, "%s.%d", logFilePath, 0); fl.l_type = F_UNLCK; fcntl (fd, F_SETLKW, &fl); close(fd); rename(logFilePath, newFile); fd = device_open (logFilePath, O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | O_NONBLOCK); fl.l_type = F_WRLCK; fcntl (fd, F_SETLKW, &fl); } else { ftruncate( fd, 0 ); } } } #endif va_start(arguments, fmt); vdprintf(fd, fmt, arguments); va_end(arguments); fl.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &fl); close(fd); } else { /* Always send console messages to /dev/console so people will see them. */ if ((fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { va_start(arguments, fmt); vdprintf(fd, fmt, arguments); va_end(arguments); close(fd); } else { fprintf(stderr, "Bummer, can't print: "); va_start(arguments, fmt); vfprintf(stderr, fmt, arguments); fflush(stderr); va_end(arguments); } } }
void log_local_message(syslogd_local_config_t *local, const char *msg) { int fd; struct flock fl; debug_printf("log_local_message: local=%p, msg=%s", local, msg); fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 1; #ifdef CONFIG_FEATURE_IPC_SYSLOG if (local->circular_logging && (buf != NULL)) { circ_message(msg); } else #endif debug_printf("logfile=%s", local->logfile); if ((fd = device_open(local->logfile, O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | O_NONBLOCK)) >= 0) { fl.l_type = F_WRLCK; fcntl(fd, F_SETLKW, &fl); #ifdef CONFIG_FEATURE_ROTATE_LOGFILE if ( local->maxsize > 0 ) { struct stat statf; int r = fstat(fd, &statf); if( !r && (statf.st_mode & S_IFREG) && (lseek(fd,0,SEEK_END) > local->maxsize * 1024) ) { if(local->numfiles > 0) { int i; char oldFile[(strlen(local->logfile)+3)], newFile[(strlen(local->logfile)+3)]; debug_printf("rotating"); for(i=local->numfiles-1;i>0;i--) { sprintf(oldFile, "%s.%d", local->logfile, i-1); sprintf(newFile, "%s.%d", local->logfile, i); rename(oldFile, newFile); } sprintf(newFile, "%s.%d", local->logfile, 0); fl.l_type = F_UNLCK; fcntl (fd, F_SETLKW, &fl); close(fd); rename(local->logfile, newFile); fd = device_open (local->logfile, O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | O_NONBLOCK); fl.l_type = F_WRLCK; fcntl (fd, F_SETLKW, &fl); } else { ftruncate( fd, 0 ); } } } #endif debug_printf("writing"); write(fd, msg, strlen(msg)); fl.l_type = F_UNLCK; fcntl(fd, F_SETLKW, &fl); close(fd); } else { /* Always send console messages to /dev/console so people will see them. */ debug_printf("sending to console"); if ((fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { debug_printf("writing to console"); write(fd, msg, strlen(msg)); close(fd); } else { debug_printf("writing to stderr"); fprintf(stderr, "Bummer, can't print: %s", msg); fflush(stderr); } } }