From: mike Date: Fri, 15 Apr 2011 04:47:29 +0000 (+0000) Subject: Make sure the log files get created when cupsd runs. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=faac60be0da22aa0882958d15d5beb3051836eb0;p=thirdparty%2Fcups.git Make sure the log files get created when cupsd runs. git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@9686 7a7537e8-13f0-0310-91df-b6672ffda945 --- diff --git a/scheduler/conf.c b/scheduler/conf.c index c65b960494..14eef1d9d4 100644 --- a/scheduler/conf.c +++ b/scheduler/conf.c @@ -843,6 +843,19 @@ cupsdReadConfiguration(void) openlog("cupsd", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LPR); #endif /* HAVE_VSYSLOG */ + /* + * Make sure each of the log files exists and gets rotated as neccessary... + */ + + if (!strcmp(AccessLog, "syslog")) + cupsdCheckLogFile(&AccessFile, AccessLog); + + if (!strcmp(ErrorLog, "syslog")) + cupsdCheckLogFile(&ErrorFile, ErrorLog); + + if (!strcmp(PageLog, "syslog")) + cupsdCheckLogFile(&PageFile, PageLog); + /* * Log the configuration file that was used... */ diff --git a/scheduler/conf.h b/scheduler/conf.h index 990084fc56..c3479e1720 100644 --- a/scheduler/conf.h +++ b/scheduler/conf.h @@ -263,6 +263,7 @@ VAR char *SystemGroupAuthKey VALUE(NULL); */ extern void cupsdAddAlias(cups_array_t *aliases, const char *name); +extern int cupsdCheckLogFile(cups_file_t **lf, const char *logname); extern int cupsdCheckPermissions(const char *filename, const char *suffix, int mode, int user, int group, int is_dir, diff --git a/scheduler/log.c b/scheduler/log.c index 412ede907d..e982f7a50a 100644 --- a/scheduler/log.c +++ b/scheduler/log.c @@ -14,6 +14,7 @@ * * Contents: * + * cupsdCheckLogFile() - Open/rotate a log file if it needs it. * cupsdGetDateTime() - Returns a pointer to a date/time string. * cupsdLogGSSMessage() - Log a GSSAPI error... * cupsdLogJob() - Log a job message. @@ -21,7 +22,6 @@ * cupsdLogPage() - Log a page to the page log file. * cupsdLogRequest() - Log an HTTP request in Common Log Format. * cupsdWriteErrorLog() - Write a line to the ErrorLog. - * check_log_file() - Open/rotate a log file if it needs it. * format_log_line() - Format a line for a log file. */ @@ -46,10 +46,187 @@ static char *log_line = NULL; /* Line for output file */ * Local functions... */ -static int check_log_file(cups_file_t **lf, const char *logname); static int format_log_line(const char *message, va_list ap); +/* + * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it. + */ + +int /* O - 1 if log file open */ +cupsdCheckLogFile(cups_file_t **lf, /* IO - Log file */ + const char *logname) /* I - Log filename */ +{ + char backname[1024], /* Backup log filename */ + filename[1024], /* Formatted log filename */ + *ptr; /* Pointer into filename */ + const char *logptr; /* Pointer into log filename */ + + + /* + * See if we have a log file to check... + */ + + if (!lf || !logname || !logname[0]) + return (1); + + /* + * Format the filename as needed... + */ + + if (!*lf || + (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && + MaxLogSize > 0)) + { + /* + * Handle format strings... + */ + + filename[sizeof(filename) - 1] = '\0'; + + if (logname[0] != '/') + { + strlcpy(filename, ServerRoot, sizeof(filename)); + strlcat(filename, "/", sizeof(filename)); + } + else + filename[0] = '\0'; + + for (logptr = logname, ptr = filename + strlen(filename); + *logptr && ptr < (filename + sizeof(filename) - 1); + logptr ++) + if (*logptr == '%') + { + /* + * Format spec... + */ + + logptr ++; + if (*logptr == 's') + { + /* + * Insert the server name... + */ + + strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename)); + ptr += strlen(ptr); + } + else + { + /* + * Otherwise just insert the character... + */ + + *ptr++ = *logptr; + } + } + else + *ptr++ = *logptr; + + *ptr = '\0'; + } + + /* + * See if the log file is open... + */ + + if (!*lf) + { + /* + * Nope, open the log file... + */ + + if ((*lf = cupsFileOpen(filename, "a")) == NULL) + { + /* + * If the file is in CUPS_LOGDIR then try to create a missing directory... + */ + + if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR))) + { + /* + * Try updating the permissions of the containing log directory, using + * the log file permissions as a basis... + */ + + int log_dir_perm = 0300 | LogFilePerm; + /* LogFilePerm + owner write/search */ + if (log_dir_perm & 0040) + log_dir_perm |= 0010; /* Add group search */ + if (log_dir_perm & 0004) + log_dir_perm |= 0001; /* Add other search */ + + cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group, + 1, -1); + + *lf = cupsFileOpen(filename, "a"); + } + + if (*lf == NULL) + { + syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, + strerror(errno)); + + if (FatalErrors & CUPSD_FATAL_LOG) + cupsdEndProcess(getpid(), 0); + + return (0); + } + } + + if (strncmp(filename, "/dev/", 5)) + { + /* + * Change ownership and permissions of non-device logs... + */ + + fchown(cupsFileNumber(*lf), RunUser, Group); + fchmod(cupsFileNumber(*lf), LogFilePerm); + } + } + + /* + * Do we need to rotate the log? + */ + + if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && + MaxLogSize > 0) + { + /* + * Rotate log file... + */ + + cupsFileClose(*lf); + + strcpy(backname, filename); + strlcat(backname, ".O", sizeof(backname)); + + unlink(backname); + rename(filename, backname); + + if ((*lf = cupsFileOpen(filename, "a")) == NULL) + { + syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, + strerror(errno)); + + if (FatalErrors & CUPSD_FATAL_LOG) + cupsdEndProcess(getpid(), 0); + + return (0); + } + + /* + * Change ownership and permissions of non-device logs... + */ + + fchown(cupsFileNumber(*lf), RunUser, Group); + fchmod(cupsFileNumber(*lf), LogFilePerm); + } + + return (1); +} + + /* * 'cupsdGetDateTime()' - Returns a pointer to a date/time string. */ @@ -508,7 +685,7 @@ cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */ * Not using syslog; check the log file... */ - if (!check_log_file(&PageFile, PageLog)) + if (!cupsdCheckLogFile(&PageFile, PageLog)) return (0); /* @@ -687,7 +864,7 @@ cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */ * Not using syslog; check the log file... */ - if (!check_log_file(&AccessFile, AccessLog)) + if (!cupsdCheckLogFile(&AccessFile, AccessLog)) return (0); /* @@ -769,7 +946,7 @@ cupsdWriteErrorLog(int level, /* I - Log level */ * Not using syslog; check the log file... */ - if (!check_log_file(&ErrorFile, ErrorLog)) + if (!cupsdCheckLogFile(&ErrorFile, ErrorLog)) return (0); /* @@ -784,184 +961,6 @@ cupsdWriteErrorLog(int level, /* I - Log level */ } -/* - * 'check_log_file()' - Open/rotate a log file if it needs it. - */ - -static int /* O - 1 if log file open */ -check_log_file(cups_file_t **lf, /* IO - Log file */ - const char *logname) /* I - Log filename */ -{ - char backname[1024], /* Backup log filename */ - filename[1024], /* Formatted log filename */ - *ptr; /* Pointer into filename */ - const char *logptr; /* Pointer into log filename */ - - - /* - * See if we have a log file to check... - */ - - if (!lf || !logname || !logname[0]) - return (1); - - /* - * Format the filename as needed... - */ - - if (!*lf || - (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && - MaxLogSize > 0)) - { - /* - * Handle format strings... - */ - - filename[sizeof(filename) - 1] = '\0'; - - if (logname[0] != '/') - { - strlcpy(filename, ServerRoot, sizeof(filename)); - strlcat(filename, "/", sizeof(filename)); - } - else - filename[0] = '\0'; - - for (logptr = logname, ptr = filename + strlen(filename); - *logptr && ptr < (filename + sizeof(filename) - 1); - logptr ++) - if (*logptr == '%') - { - /* - * Format spec... - */ - - logptr ++; - if (*logptr == 's') - { - /* - * Insert the server name... - */ - - strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename)); - ptr += strlen(ptr); - } - else - { - /* - * Otherwise just insert the character... - */ - - *ptr++ = *logptr; - } - } - else - *ptr++ = *logptr; - - *ptr = '\0'; - } - - /* - * See if the log file is open... - */ - - if (!*lf) - { - /* - * Nope, open the log file... - */ - - if ((*lf = cupsFileOpen(filename, "a")) == NULL) - { - /* - * If the file is in CUPS_LOGDIR then try to create a missing directory... - */ - - if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR))) - { - /* - * Try updating the permissions of the containing log directory, using - * the log file permissions as a basis... - */ - - int log_dir_perm = 0300 | LogFilePerm; - /* LogFilePerm + owner write/search */ - if (log_dir_perm & 0040) - log_dir_perm |= 0010; /* Add group search */ - if (log_dir_perm & 0004) - log_dir_perm |= 0001; /* Add other search */ - - cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group, - 1, -1); - - *lf = cupsFileOpen(filename, "a"); - } - - if (*lf == NULL) - { - syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, - strerror(errno)); - - if (FatalErrors & CUPSD_FATAL_LOG) - cupsdEndProcess(getpid(), 0); - - return (0); - } - } - - if (strncmp(filename, "/dev/", 5)) - { - /* - * Change ownership and permissions of non-device logs... - */ - - fchown(cupsFileNumber(*lf), RunUser, Group); - fchmod(cupsFileNumber(*lf), LogFilePerm); - } - } - - /* - * Do we need to rotate the log? - */ - - if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && - MaxLogSize > 0) - { - /* - * Rotate log file... - */ - - cupsFileClose(*lf); - - strcpy(backname, filename); - strlcat(backname, ".O", sizeof(backname)); - - unlink(backname); - rename(filename, backname); - - if ((*lf = cupsFileOpen(filename, "a")) == NULL) - { - syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, - strerror(errno)); - - if (FatalErrors & CUPSD_FATAL_LOG) - cupsdEndProcess(getpid(), 0); - - return (0); - } - - /* - * Change ownership and permissions of non-device logs... - */ - - fchown(cupsFileNumber(*lf), RunUser, Group); - fchmod(cupsFileNumber(*lf), LogFilePerm); - } - - return (1); -} - - /* * 'format_log_line()' - Format a line for a log file. *