summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert James Kaes <rjkaes@users.sourceforge.net>2002-05-31 18:26:30 +0000
committerRobert James Kaes <rjkaes@users.sourceforge.net>2002-05-31 18:26:30 +0000
commit833186b7be118380f3955644d40ab7b0e1e685cf (patch)
tree8229814aceff7dd2a8d6e93c70c9a31e621820d3 /src
parente340b3371ba4e9301427273830d9b2964ec53237 (diff)
downloadtinyproxy-833186b7be118380f3955644d40ab7b0e1e685cf.tar.gz
tinyproxy-833186b7be118380f3955644d40ab7b0e1e685cf.zip
(create_file_safely): Added the "truncate_file" boolean flag. This is needed since I use this function for both the log file and the pid file, and they behave differently when tinyproxy is started.
(pidfile_create): Call create_file_safely() with a TRUE "truncate_file" flag so that the file is emptied whenever the pid file is needed.
Diffstat (limited to 'src')
-rw-r--r--src/utils.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/utils.c b/src/utils.c
index 8d5a432..fdc6d4a 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,4 +1,4 @@
-/* $Id: utils.c,v 1.31 2002-05-24 04:45:32 rjkaes Exp $
+/* $Id: utils.c,v 1.32 2002-05-31 18:26:30 rjkaes Exp $
*
* 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,
@@ -138,7 +138,7 @@ indicate_http_error(struct conn_s* connptr, int number, const char* string)
* Safely creates filename and returns the low-level file descriptor.
*/
int
-create_file_safely(const char *filename)
+create_file_safely(const char *filename, bool_t truncate_file)
{
struct stat lstatinfo;
int fildes;
@@ -155,7 +155,7 @@ create_file_safely(const char *filename)
*/
if (errno != ENOENT) {
log_message(LOG_ERR,
- "create_file_safely: Error checking PID file %s: %s.",
+ "create_file_safely: Error checking file %s: %s.",
filename, strerror(errno));
return -1;
}
@@ -168,19 +168,24 @@ create_file_safely(const char *filename)
if ((fildes =
open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
log_message(LOG_ERR,
- "create_file_safely: Could not create PID file %s: %s.",
+ "create_file_safely: Could not create file %s: %s.",
filename, strerror(errno));
return -1;
}
} else {
struct stat fstatinfo;
+ int flags;
+
+ flags = O_RDWR;
+ if (!truncate_file)
+ flags |= O_APPEND;
/*
* Open an existing file.
*/
- if ((fildes = open(filename, O_RDWR)) < 0) {
+ if ((fildes = open(filename, flags)) < 0) {
log_message(LOG_ERR,
- "create_file_safely: Could not open PID file %s: %s.",
+ "create_file_safely: Could not open file %s: %s.",
filename, strerror(errno));
return -1;
}
@@ -194,7 +199,7 @@ create_file_safely(const char *filename)
|| lstatinfo.st_ino != fstatinfo.st_ino
|| lstatinfo.st_dev != fstatinfo.st_dev) {
log_message(LOG_ERR,
- "create_file_safely: The PID file %s has been changed before it could be opened.",
+ "create_file_safely: The file %s has been changed before it could be opened.",
filename);
close(fildes);
return -1;
@@ -209,13 +214,20 @@ create_file_safely(const char *filename)
*/
if (fstatinfo.st_nlink > 1 || !S_ISREG(lstatinfo.st_mode)) {
log_message(LOG_ERR,
- "create_file_safely: The PID file %s has too many links, or is not a regular file: %s.",
+ "create_file_safely: The file %s has too many links, or is not a regular file: %s.",
filename, strerror(errno));
close(fildes);
return -1;
}
/*
+ * Just return the file descriptor if we _don't_ want the file
+ * truncated.
+ */
+ if (!truncate_file)
+ return fildes;
+
+ /*
* On systems which don't support ftruncate() the best we can
* do is to close the file and reopen it in create mode, which
* unfortunately leads to a race condition, however "systems
@@ -230,7 +242,7 @@ create_file_safely(const char *filename)
if ((fildes =
open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
log_message(LOG_ERR,
- "create_file_safely: Could not open PID file %s: %s.",
+ "create_file_safely: Could not open file %s: %s.",
filename, strerror(errno));
return -1;
}
@@ -252,7 +264,7 @@ pidfile_create(const char *filename)
/*
* Create a new file
*/
- if ((fildes = create_file_safely(filename)) < 0)
+ if ((fildes = create_file_safely(filename, TRUE)) < 0)
exit(1);
/*
@@ -306,7 +318,7 @@ rotate_log_files(void)
rename(config.logf_name, rename_file);
- log_file_des = create_file_safely(config.logf_name);
+ log_file_des = create_file_safely(config.logf_name, TRUE);
if (log_file_des < 0) {
log_message(LOG_CRIT,
"Could not properly create the new log file; therefore the log has not been rotated.");