Relax strict checking of log file.

Add a strict_check parameter to create_file_safely() and disable the
strict file type checking when opening the log file. This allows the log
file to be a device file, fifo, etc.
This commit is contained in:
Bob Showalter 2017-06-02 17:50:49 +00:00
parent cb6f868739
commit 14523bb1f1
3 changed files with 36 additions and 32 deletions

View File

@ -70,7 +70,7 @@ static unsigned int logging_initialized = FALSE; /* boolean */
*/ */
int open_log_file (const char *log_file_name) int open_log_file (const char *log_file_name)
{ {
log_file_fd = create_file_safely (log_file_name, FALSE); log_file_fd = create_file_safely (log_file_name, FALSE, FALSE);
return log_file_fd; return log_file_fd;
} }

View File

@ -61,7 +61,8 @@ send_http_message (struct conn_s *connptr, int http_code,
/* /*
* Safely creates filename and returns the low-level file descriptor. * Safely creates filename and returns the low-level file descriptor.
*/ */
int create_file_safely (const char *filename, unsigned int truncate_file) int create_file_safely (const char *filename, unsigned int truncate_file,
unsigned int strict_check)
{ {
struct stat lstatinfo; struct stat lstatinfo;
int fildes; int fildes;
@ -112,35 +113,37 @@ int create_file_safely (const char *filename, unsigned int truncate_file)
return fildes; return fildes;
} }
/* if (strict_check) {
* fstat() the opened file and check that the file mode bits, /*
* inode, and device match. * fstat() the opened file and check that the file mode bits,
*/ * inode, and device match.
if (fstat (fildes, &fstatinfo) < 0 */
|| lstatinfo.st_mode != fstatinfo.st_mode if (fstat (fildes, &fstatinfo) < 0
|| lstatinfo.st_ino != fstatinfo.st_ino || lstatinfo.st_mode != fstatinfo.st_mode
|| lstatinfo.st_dev != fstatinfo.st_dev) { || lstatinfo.st_ino != fstatinfo.st_ino
fprintf (stderr, || lstatinfo.st_dev != fstatinfo.st_dev) {
"%s: The file %s has been changed before it could be opened\n", fprintf (stderr,
PACKAGE, filename); "%s: The file %s has been changed before it could be opened\n",
close (fildes); PACKAGE, filename);
return -EIO; close (fildes);
} return -EIO;
}
/* /*
* If the above check was passed, we know that the lstat() * If the above check was passed, we know that the lstat()
* and fstat() were done on the same file. Now we check that * and fstat() were done on the same file. Now we check that
* there's only one link, and that it's a normal file (this * there's only one link, and that it's a normal file (this
* isn't strictly necessary because the fstat() vs lstat() * isn't strictly necessary because the fstat() vs lstat()
* st_mode check would also find this) * st_mode check would also find this)
*/ */
if (fstatinfo.st_nlink > 1 || !S_ISREG (lstatinfo.st_mode)) { if (fstatinfo.st_nlink > 1 || !S_ISREG (lstatinfo.st_mode)) {
fprintf (stderr, fprintf (stderr,
"%s: The file %s has too many links, " "%s: The file %s has too many links, "
"or is not a regular file: %s\n", "or is not a regular file: %s\n",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
close (fildes); close (fildes);
return -EMLINK; return -EMLINK;
}
} }
/* /*
@ -194,7 +197,7 @@ pidfile_create (const char *filename)
/* /*
* Create a new file * Create a new file
*/ */
if ((fildes = create_file_safely (filename, TRUE)) < 0) if ((fildes = create_file_safely (filename, TRUE, TRUE)) < 0)
return fildes; return fildes;
/* /*

View File

@ -32,6 +32,7 @@ extern int send_http_message (struct conn_s *connptr, int http_code,
extern int pidfile_create (const char *path); extern int pidfile_create (const char *path);
extern int create_file_safely (const char *filename, extern int create_file_safely (const char *filename,
unsigned int truncate_file); unsigned int truncate_file,
unsigned int strict_check);
#endif #endif