"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "logrotate.c" between
logrotate-3.18.0.tar.xz and logrotate-3.18.1.tar.xz

About: logrotate rotates, compresses, and mails system logs.

logrotate.c  (logrotate-3.18.0.tar.xz):logrotate.c  (logrotate-3.18.1.tar.xz)
skipping to change at line 585 skipping to change at line 585
return -1; return -1;
} }
if (fstat(fd, &sb_create)) { if (fstat(fd, &sb_create)) {
message(MESS_ERROR, "fstat of %s failed: %s\n", fileName, message(MESS_ERROR, "fstat of %s failed: %s\n", fileName,
strerror(errno)); strerror(errno));
close(fd); close(fd);
return -1; return -1;
} }
if ((sb_create.st_uid != sb->st_uid || sb_create.st_gid != sb->st_gid) && /* Only attempt to set user/group if running as root */
fchown(fd, sb->st_uid, sb->st_gid)) { if (
ROOT_UID == geteuid() &&
(sb_create.st_uid != sb->st_uid || sb_create.st_gid != sb->st_gid) &&
fchown(fd, sb->st_uid, sb->st_gid)
) {
message(MESS_ERROR, "error setting owner of %s to uid %u and gid %u: %s\ n", message(MESS_ERROR, "error setting owner of %s to uid %u and gid %u: %s\ n",
fileName, (unsigned) sb->st_uid, (unsigned) sb->st_gid, strerror (errno)); fileName, (unsigned) sb->st_uid, (unsigned) sb->st_gid, strerror (errno));
close(fd); close(fd);
return -1; return -1;
} }
#ifdef WITH_ACL #ifdef WITH_ACL
if (!force_mode && acl) { if (!force_mode && acl) {
if (acl_set_fd(fd, acl) == -1) { if (acl_set_fd(fd, acl) == -1) {
if (is_acl_well_supported(errno)) { if (is_acl_well_supported(errno)) {
skipping to change at line 773 skipping to change at line 777
int inFile; int inFile;
int outFile; int outFile;
int i; int i;
int status; int status;
int compressPipe[2]; int compressPipe[2];
char buff[4092]; char buff[4092];
ssize_t n_read; ssize_t n_read;
int error_printed = 0; int error_printed = 0;
char *prevCtx; char *prevCtx;
pid_t pid; pid_t pid;
int in_flags;
const char *in_how;
message(MESS_DEBUG, "compressing log with: %s\n", log->compress_prog); message(MESS_DEBUG, "compressing log with: %s\n", log->compress_prog);
if (debug) if (debug)
return 0; return 0;
fullCommand = alloca(sizeof(*fullCommand) * fullCommand = alloca(sizeof(*fullCommand) *
((unsigned)log->compress_options_count + 2)); ((unsigned)log->compress_options_count + 2));
fullCommand[0] = log->compress_prog; fullCommand[0] = log->compress_prog;
for (i = 0; i < log->compress_options_count; i++) for (i = 0; i < log->compress_options_count; i++)
fullCommand[i + 1] = log->compress_options_list[i]; fullCommand[i + 1] = log->compress_options_list[i];
fullCommand[log->compress_options_count + 1] = NULL; fullCommand[log->compress_options_count + 1] = NULL;
compressedName = alloca(strlen(name) + strlen(log->compress_ext) + 2); compressedName = alloca(strlen(name) + strlen(log->compress_ext) + 2);
sprintf(compressedName, "%s%s", name, log->compress_ext); sprintf(compressedName, "%s%s", name, log->compress_ext);
if ((inFile = open(name, O_RDWR | O_NOFOLLOW)) < 0) { in_flags = O_NOFOLLOW;
message(MESS_ERROR, "unable to open %s for compression: %s\n", name, str if (log->flags & LOG_FLAG_SHRED) {
error(errno)); /* need write access for shredding */
in_flags |= O_RDWR;
in_how = "read-write";
} else {
in_flags |= O_RDONLY;
in_how = "read-only";
}
if ((inFile = open(name, in_flags)) < 0) {
message(MESS_ERROR, "unable to open %s (%s) for compression: %s\n",
name, in_how, strerror(errno));
return 1; return 1;
} }
if (setSecCtx(inFile, name, &prevCtx) != 0) { if (setSecCtx(inFile, name, &prevCtx) != 0) {
/* error msg already printed */ /* error msg already printed */
close(inFile); close(inFile);
return 1; return 1;
} }
#ifdef WITH_ACL #ifdef WITH_ACL
skipping to change at line 1532 skipping to change at line 1548
} }
globfree(&globResult); globfree(&globResult);
return last; return last;
} }
static int prerotateSingleLog(const struct logInfo *log, unsigned logNum, static int prerotateSingleLog(const struct logInfo *log, unsigned logNum,
struct logState *state, struct logNames *rotNames) struct logState *state, struct logNames *rotNames)
{ {
struct tm now; struct tm now;
char *oldName = NULL;
const char *compext = ""; const char *compext = "";
const char *fileext = ""; const char *fileext = "";
int hasErrors = 0; int hasErrors = 0;
char *glob_pattern; char *glob_pattern;
glob_t globResult; glob_t globResult;
int rc; int rc;
int rotateCount = log->rotateCount ? log->rotateCount : 1; int rotateCount = log->rotateCount ? log->rotateCount : 1;
int logStart = (log->logStart == -1) ? 1 : log->logStart; int logStart = (log->logStart == -1) ? 1 : log->logStart;
#define DATEEXT_LEN 64 #define DATEEXT_LEN 64
#define PATTERN_LEN (DATEEXT_LEN * 2) #define PATTERN_LEN (DATEEXT_LEN * 2)
skipping to change at line 1570 skipping to change at line 1585
return 1; return 1;
} }
compext = log->compress_ext; compext = log->compress_ext;
} }
localtime_r(&nowSecs, &now); localtime_r(&nowSecs, &now);
state->lastRotated = now; state->lastRotated = now;
{ {
char *ld; const char *ld;
char *logpath = strdup(log->files[logNum]); char *logpath = strdup(log->files[logNum]);
if (logpath == NULL) { if (logpath == NULL) {
message_OOM(); message_OOM();
return 1; return 1;
} }
ld = dirname(logpath); ld = dirname(logpath);
if (log->oldDir) { if (log->oldDir) {
if (log->oldDir[0] != '/') { if (log->oldDir[0] != '/') {
rotNames->dirName = if (asprintf(&rotNames->dirName, "%s/%s", ld, log->oldDir) < 0)
malloc(strlen(ld) + strlen(log->oldDir) + 2); {
sprintf(rotNames->dirName, "%s/%s", ld, log->oldDir); rotNames->dirName = NULL;
}
} else } else
rotNames->dirName = strdup(log->oldDir); rotNames->dirName = strdup(log->oldDir);
} else } else
rotNames->dirName = strdup(ld); rotNames->dirName = strdup(ld);
free(logpath); free(logpath);
if (rotNames->dirName == NULL) { if (rotNames->dirName == NULL) {
message_OOM(); message_OOM();
return 1; return 1;
} }
skipping to change at line 1773 skipping to change at line 1788
rotNames->baseName, dext_pattern, fileext) < 0) { rotNames->baseName, dext_pattern, fileext) < 0) {
message_OOM(); message_OOM();
return 1; return 1;
} }
rc = glob(glob_pattern, 0, globerr, &globResult); rc = glob(glob_pattern, 0, globerr, &globResult);
if (!rc && globResult.gl_pathc > 0) { if (!rc && globResult.gl_pathc > 0) {
size_t glob_count; size_t glob_count;
sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strl en(rotNames->baseName), dformat); sortGlobResult(&globResult, strlen(rotNames->dirName) + 1 + strl en(rotNames->baseName), dformat);
for (glob_count = 0; glob_count < globResult.gl_pathc && !hasErr ors; glob_count++) { for (glob_count = 0; glob_count < globResult.gl_pathc && !hasErr ors; glob_count++) {
struct stat sbprev; struct stat sbprev;
const char *oldName = globResult.gl_pathv[glob_count];
if (asprintf(&oldName, "%s", (globResult.gl_pathv)[glob_coun
t]) < 0) {
message_OOM();
return 1;
}
if (stat(oldName, &sbprev)) { if (stat(oldName, &sbprev)) {
if (errno == ENOENT) if (errno == ENOENT)
message(MESS_DEBUG, "previous log %s does not exist\ n", oldName); message(MESS_DEBUG, "previous log %s does not exist\ n", oldName);
else else
message(MESS_ERROR, "cannot stat %s: %s\n", oldName, strerror(errno)); message(MESS_ERROR, "cannot stat %s: %s\n", oldName, strerror(errno));
} else { } else {
hasErrors = compressLogFile(oldName, log, &sbprev); hasErrors = compressLogFile(oldName, log, &sbprev);
} }
free(oldName);
} }
} else { } else {
message(MESS_DEBUG, message(MESS_DEBUG,
"glob finding logs to compress failed\n"); "glob finding logs to compress failed\n");
} }
globfree(&globResult); globfree(&globResult);
free(glob_pattern); free(glob_pattern);
} else { } else {
struct stat sbprev; struct stat sbprev;
char *oldName;
if (asprintf(&oldName, "%s/%s.%d%s", rotNames->dirName, if (asprintf(&oldName, "%s/%s.%d%s", rotNames->dirName,
rotNames->baseName, logStart, fileext) < 0) { rotNames->baseName, logStart, fileext) < 0) {
message_OOM(); message_OOM();
return 1; return 1;
} }
if (stat(oldName, &sbprev)) { if (stat(oldName, &sbprev)) {
if (errno == ENOENT) if (errno == ENOENT)
message(MESS_DEBUG, "previous log %s does not exist\n", oldN ame); message(MESS_DEBUG, "previous log %s does not exist\n", oldN ame);
else else
message(MESS_ERROR, "cannot stat %s: %s\n", oldName, strerro r(errno)); message(MESS_ERROR, "cannot stat %s: %s\n", oldName, strerro r(errno));
} else { } else {
hasErrors = compressLogFile(oldName, log, &sbprev); hasErrors = compressLogFile(oldName, log, &sbprev);
} }
free(oldName); free(oldName);
} }
} }
/* adding 2 due to / and \0 being added by snprintf */
rotNames->firstRotated =
malloc(strlen(rotNames->dirName) + strlen(rotNames->baseName) +
strlen(fileext) + strlen(compext) + DATEEXT_LEN + 2 );
if (rotNames->firstRotated == NULL) {
message_OOM();
return 1;
}
if (log->flags & LOG_FLAG_DATEEXT) { if (log->flags & LOG_FLAG_DATEEXT) {
/* glob for compressed files with our pattern /* glob for compressed files with our pattern
* and compress ext */ * and compress ext */
if (asprintf(&glob_pattern, "%s/%s%s%s%s", rotNames->dirName, if (asprintf(&glob_pattern, "%s/%s%s%s%s", rotNames->dirName,
rotNames->baseName, dext_pattern, fileext, compext) < 0) { rotNames->baseName, dext_pattern, fileext, compext) < 0) {
message_OOM(); message_OOM();
return 1; return 1;
} }
rc = glob(glob_pattern, 0, globerr, &globResult); rc = glob(glob_pattern, 0, globerr, &globResult);
if (!rc) { if (!rc) {
skipping to change at line 1865 skipping to change at line 1868
message(MESS_DEBUG, "removing %s\n", mailFilenam e); message(MESS_DEBUG, "removing %s\n", mailFilenam e);
hasErrors = removeLogFile(mailFilename, log); hasErrors = removeLogFile(mailFilename, log);
} }
} }
mail_out = glob_count; mail_out = glob_count;
} }
} }
} }
if (mail_out != (size_t)-1) { if (mail_out != (size_t)-1) {
/* oldName is oldest Backup found (for unlink later) */ /* oldName is oldest Backup found (for unlink later) */
if (asprintf(&oldName, "%s", (globResult.gl_pathv)[mail_out]) < const char *oldName = globResult.gl_pathv[mail_out];
0) {
message_OOM();
return 1;
}
rotNames->disposeName = strdup(oldName); rotNames->disposeName = strdup(oldName);
if (rotNames->disposeName == NULL) { if (rotNames->disposeName == NULL) {
message_OOM(); message_OOM();
globfree(&globResult);
free(glob_pattern);
return 1; return 1;
} }
free(oldName);
} else { } else {
free(rotNames->disposeName); free(rotNames->disposeName);
rotNames->disposeName = NULL; rotNames->disposeName = NULL;
} }
} else { } else {
message(MESS_DEBUG, "glob finding old rotated logs failed\n"); message(MESS_DEBUG, "glob finding old rotated logs failed\n");
free(rotNames->disposeName); free(rotNames->disposeName);
rotNames->disposeName = NULL; rotNames->disposeName = NULL;
} }
/* firstRotated is most recently created/compressed rotated log */ /* firstRotated is most recently created/compressed rotated log */
sprintf(rotNames->firstRotated, "%s/%s%s%s%s", if (asprintf(&rotNames->firstRotated, "%s/%s%s%s%s",
rotNames->dirName, rotNames->baseName, dext_str, fileext, rotNames->dirName, rotNames->baseName, dext_str, fileext,
(log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext); (log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext) < 0) {
message_OOM();
rotNames->firstRotated = NULL;
globfree(&globResult);
free(glob_pattern);
return 1;
}
globfree(&globResult); globfree(&globResult);
free(glob_pattern); free(glob_pattern);
} else { } else {
int i; int i;
char *newName = NULL; char *newName = NULL;
char *oldName;
if (rotateCount == -1) { if (rotateCount == -1) {
rotateCount = findLastRotated(rotNames, fileext, compext); rotateCount = findLastRotated(rotNames, fileext, compext);
if (rotateCount < 0) { if (rotateCount < 0) {
message(MESS_ERROR, "could not find last rotated file: %s/%s.*%s %s\n", message(MESS_ERROR, "could not find last rotated file: %s/%s.*%s %s\n",
rotNames->dirName, rotNames->baseName, fileext, compext) ; rotNames->dirName, rotNames->baseName, fileext, compext) ;
return 1; return 1;
} }
} }
skipping to change at line 1914 skipping to change at line 1922
rotNames->baseName, logStart + rotateCount, fileext, rotNames->baseName, logStart + rotateCount, fileext,
compext) < 0) { compext) < 0) {
message_OOM(); message_OOM();
return 1; return 1;
} }
if (log->rotateCount != -1) { if (log->rotateCount != -1) {
rotNames->disposeName = strdup(oldName); rotNames->disposeName = strdup(oldName);
if (rotNames->disposeName == NULL) { if (rotNames->disposeName == NULL) {
message_OOM(); message_OOM();
free(oldName);
return 1; return 1;
} }
} }
sprintf(rotNames->firstRotated, "%s/%s.%d%s%s", rotNames->dirName, if (asprintf(&rotNames->firstRotated, "%s/%s.%d%s%s", rotNames->dirName,
rotNames->baseName, logStart, fileext, rotNames->baseName, logStart, fileext,
(log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext); (log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext) < 0) {
message_OOM();
free(oldName);
rotNames->firstRotated = NULL;
return 1;
}
for (i = rotateCount + logStart - 1; (i >= 0) && !hasErrors; i--) { for (i = rotateCount + logStart - 1; (i >= 0) && !hasErrors; i--) {
free(newName); free(newName);
newName = oldName; newName = oldName;
if (asprintf(&oldName, "%s/%s.%d%s%s", rotNames->dirName, if (asprintf(&oldName, "%s/%s.%d%s%s", rotNames->dirName,
rotNames->baseName, i, fileext, compext) < 0) { rotNames->baseName, i, fileext, compext) < 0) {
message_OOM(); message_OOM();
oldName = NULL; oldName = NULL;
break; break;
} }
skipping to change at line 1986 skipping to change at line 2000
free(oldName); free(oldName);
} /* !LOG_FLAG_DATEEXT */ } /* !LOG_FLAG_DATEEXT */
if (log->flags & LOG_FLAG_DATEEXT) { if (log->flags & LOG_FLAG_DATEEXT) {
char *destFile; char *destFile;
struct stat fst_buf; struct stat fst_buf;
if (asprintf(&(rotNames->finalName), "%s/%s%s%s", rotNames->dirName, if (asprintf(&(rotNames->finalName), "%s/%s%s%s", rotNames->dirName,
rotNames->baseName, dext_str, fileext) < 0) { rotNames->baseName, dext_str, fileext) < 0) {
message_OOM(); message_OOM();
rotNames->finalName = NULL;
return 1; return 1;
} }
if (asprintf(&destFile, "%s%s", rotNames->finalName, compext) < 0) { if (asprintf(&destFile, "%s%s", rotNames->finalName, compext) < 0) {
message_OOM(); message_OOM();
return 1; return 1;
} }
if (!stat(destFile, &fst_buf)) { if (!stat(destFile, &fst_buf)) {
message(MESS_ERROR, message(MESS_ERROR,
"destination %s already exists, skipping rotation\n", "destination %s already exists, skipping rotation\n",
rotNames->firstRotated); rotNames->firstRotated);
hasErrors = 1; hasErrors = 1;
} }
free(destFile); free(destFile);
} else { } else {
/* note: the gzip extension is *not* used here! */ /* note: the gzip extension is *not* used here! */
if (asprintf(&(rotNames->finalName), "%s/%s.%d%s", rotNames->dirName, if (asprintf(&(rotNames->finalName), "%s/%s.%d%s", rotNames->dirName,
rotNames->baseName, logStart, fileext) < 0) { rotNames->baseName, logStart, fileext) < 0) {
message_OOM(); message_OOM();
rotNames->finalName = NULL;
} }
} }
/* if the last rotation doesn't exist, that's okay */ /* if the last rotation doesn't exist, that's okay */
if (rotNames->disposeName && access(rotNames->disposeName, F_OK)) { if (rotNames->disposeName && access(rotNames->disposeName, F_OK)) {
message(MESS_DEBUG, message(MESS_DEBUG,
"log %s doesn't exist -- won't try to dispose of it\n", "log %s doesn't exist -- won't try to dispose of it\n",
rotNames->disposeName); rotNames->disposeName);
free(rotNames->disposeName); free(rotNames->disposeName);
rotNames->disposeName = NULL; rotNames->disposeName = NULL;
skipping to change at line 2087 skipping to change at line 2103
if (!log->rotateCount) { if (!log->rotateCount) {
const char *ext = ""; const char *ext = "";
if (log->compress_ext if (log->compress_ext
&& (log->flags & LOG_FLAG_COMPRESS) && (log->flags & LOG_FLAG_COMPRESS)
&& !(log->flags & LOG_FLAG_DELAYCOMPRESS)) && !(log->flags & LOG_FLAG_DELAYCOMPRESS))
ext = log->compress_ext; ext = log->compress_ext;
free(rotNames->disposeName); free(rotNames->disposeName);
if (asprintf(&rotNames->disposeName, "%s%s", rotNames->finalName , ext) < 0) { if (asprintf(&rotNames->disposeName, "%s%s", rotNames->finalName , ext) < 0) {
message_OOM(); message_OOM();
rotNames->disposeName = NULL;
return 1; return 1;
} }
message(MESS_DEBUG, "disposeName will be %s\n", rotNames->dispos eName); message(MESS_DEBUG, "disposeName will be %s\n", rotNames->dispos eName);
} }
} }
if (!hasErrors && (log->flags & LOG_FLAG_CREATE) && if (!hasErrors && (log->flags & LOG_FLAG_CREATE) &&
!(log->flags & (LOG_FLAG_COPYTRUNCATE | LOG_FLAG_COPY))) { !(log->flags & (LOG_FLAG_COPYTRUNCATE | LOG_FLAG_COPY))) {
int have_create_mode = 0; int have_create_mode = 0;
skipping to change at line 2215 skipping to change at line 2232
static int rotateLogSet(const struct logInfo *log, int force) static int rotateLogSet(const struct logInfo *log, int force)
{ {
unsigned i, j; unsigned i, j;
int hasErrors = 0; int hasErrors = 0;
int *logHasErrors; int *logHasErrors;
int numRotated = 0; int numRotated = 0;
struct logState **state; struct logState **state;
struct logNames **rotNames; struct logNames **rotNames;
logHasErrors = calloc(log->numFiles, sizeof(int));
if (!logHasErrors) {
message_OOM();
return 1;
}
message(MESS_DEBUG, "\nrotating pattern: %s ", log->pattern); message(MESS_DEBUG, "\nrotating pattern: %s ", log->pattern);
if (force) { if (force) {
message(MESS_DEBUG, "forced from command line "); message(MESS_DEBUG, "forced from command line ");
} }
else { else {
switch (log->criterium) { switch (log->criterium) {
case ROT_HOURLY: case ROT_HOURLY:
message(MESS_DEBUG, "hourly "); message(MESS_DEBUG, "hourly ");
break; break;
case ROT_DAYS: case ROT_DAYS:
skipping to change at line 2280 skipping to change at line 2292
message(MESS_DEBUG, "only log files older than %d days are rotated, ", l og->rotateMinAge); message(MESS_DEBUG, "only log files older than %d days are rotated, ", l og->rotateMinAge);
if (log->logAddress) { if (log->logAddress) {
message(MESS_DEBUG, "old logs mailed to %s\n", log->logAddress); message(MESS_DEBUG, "old logs mailed to %s\n", log->logAddress);
} else { } else {
message(MESS_DEBUG, "old logs are removed\n"); message(MESS_DEBUG, "old logs are removed\n");
} }
if (log->numFiles == 0) { if (log->numFiles == 0) {
message(MESS_DEBUG, "No logs found. Rotation not needed.\n"); message(MESS_DEBUG, "No logs found. Rotation not needed.\n");
free(logHasErrors);
return 0; return 0;
} }
logHasErrors = calloc(log->numFiles, sizeof(int));
if (!logHasErrors) {
message_OOM();
return 1;
}
if (log->flags & LOG_FLAG_SU) { if (log->flags & LOG_FLAG_SU) {
if (switch_user(log->suUid, log->suGid) != 0) { if (switch_user(log->suUid, log->suGid) != 0) {
free(logHasErrors); free(logHasErrors);
return 1; return 1;
} }
} }
for (i = 0; i < log->numFiles; i++) { for (i = 0; i < log->numFiles; i++) {
struct logState *logState; struct logState *logState;
logHasErrors[i] = findNeedRotating(log, i, force); logHasErrors[i] = findNeedRotating(log, i, force);
 End of changes. 26 change blocks. 
41 lines changed or deleted 56 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)