(create_file_safely):

(pidfile_create): Changed all the error logging to write to standard error and then exit the program.  This will prevent segmentation fault problems from occurring because the log file could not be created properly.
This commit is contained in:
Robert James Kaes 2002-07-09 19:02:57 +00:00
parent 4932b87fc9
commit ab574cbec0

View File

@ -1,4 +1,4 @@
/* $Id: utils.c,v 1.33 2002-06-15 17:28:19 rjkaes Exp $ /* $Id: utils.c,v 1.34 2002-07-09 19:02:57 rjkaes Exp $
* *
* Misc. routines which are used by the various functions to handle strings * Misc. routines which are used by the various functions to handle strings
* and memory allocation and pretty much anything else we can think of. Also, * and memory allocation and pretty much anything else we can think of. Also,
@ -154,10 +154,10 @@ create_file_safely(const char *filename, bool_t truncate_file)
* existing", exit. * existing", exit.
*/ */
if (errno != ENOENT) { if (errno != ENOENT) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: Error checking file %s: %s.", "%s: Error checking file %s: %s\n",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
return -1; exit(EX_IOERR);
} }
/* /*
@ -167,10 +167,10 @@ create_file_safely(const char *filename, bool_t truncate_file)
*/ */
if ((fildes = if ((fildes =
open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) { open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: Could not create file %s: %s.", "%s: Could not create file %s: %s\n",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
return -1; exit(EX_CANTCREAT);
} }
} else { } else {
struct stat fstatinfo; struct stat fstatinfo;
@ -184,10 +184,10 @@ create_file_safely(const char *filename, bool_t truncate_file)
* Open an existing file. * Open an existing file.
*/ */
if ((fildes = open(filename, flags)) < 0) { if ((fildes = open(filename, flags)) < 0) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: Could not open file %s: %s.", "%s: Could not open file %s: %s\n",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
return -1; exit(EX_IOERR);
} }
/* /*
@ -198,11 +198,11 @@ create_file_safely(const char *filename, bool_t truncate_file)
|| lstatinfo.st_mode != fstatinfo.st_mode || lstatinfo.st_mode != fstatinfo.st_mode
|| lstatinfo.st_ino != fstatinfo.st_ino || lstatinfo.st_ino != fstatinfo.st_ino
|| lstatinfo.st_dev != fstatinfo.st_dev) { || lstatinfo.st_dev != fstatinfo.st_dev) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: The file %s has been changed before it could be opened.", "%s: The file %s has been changed before it could be opened\n",
filename); PACKAGE, filename);
close(fildes); close(fildes);
return -1; exit(EX_IOERR);
} }
/* /*
@ -213,11 +213,11 @@ create_file_safely(const char *filename, bool_t truncate_file)
* 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)) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: The file %s has too many links, or is not a regular file: %s.", "%s: The file %s has too many links, or is not a regular file: %s\n",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
close(fildes); close(fildes);
return -1; exit(EX_IOERR);
} }
/* /*
@ -241,10 +241,10 @@ create_file_safely(const char *filename, bool_t truncate_file)
close(fildes); close(fildes);
if ((fildes = if ((fildes =
open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) { open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
log_message(LOG_ERR, fprintf(stderr,
"create_file_safely: Could not open file %s: %s.", "%s: Could not open file %s: %s.",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
return -1; exit(EX_IOERR);
} }
#endif /* HAVE_FTRUNCATE */ #endif /* HAVE_FTRUNCATE */
} }
@ -271,12 +271,12 @@ pidfile_create(const char *filename)
* Open a stdio file over the low-level one. * Open a stdio file over the low-level one.
*/ */
if ((fd = fdopen(fildes, "w")) == NULL) { if ((fd = fdopen(fildes, "w")) == NULL) {
log_message(LOG_ERR, fprintf(stderr,
"pidfile_create: fdopen() error on PID file %s: %s.", "%s: Could not write PID file %s: %s.",
filename, strerror(errno)); PACKAGE, filename, strerror(errno));
close(fildes); close(fildes);
unlink(filename); unlink(filename);
exit(1); exit(EX_IOERR);
} }
fprintf(fd, "%ld\n", (long) getpid()); fprintf(fd, "%ld\n", (long) getpid());