aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-09-06 11:50:22 -0400
committerNick Mathewson <nickm@torproject.org>2017-09-06 11:50:22 -0400
commit1098893e4f3753bf66248abf81702175dca980ee (patch)
tree65fba3d76ebd6bccb05a4dd5dd2661dfc1034244 /src
parent342712b9ef12c0234def9c2b4eb5c63f54c8bb5b (diff)
downloadtor-1098893e4f3753bf66248abf81702175dca980ee.tar.gz
tor-1098893e4f3753bf66248abf81702175dca980ee.zip
Exit when we can't write to a configured pid file
This is probably what the user wants, according to 20119.
Diffstat (limited to 'src')
-rw-r--r--src/common/util.c15
-rw-r--r--src/common/util.h2
-rw-r--r--src/or/config.c8
3 files changed, 18 insertions, 7 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 31d42a3e5c..36d0f4d068 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3691,8 +3691,9 @@ finish_daemon(const char *cp)
#endif
/** Write the current process ID, followed by NL, into <b>filename</b>.
+ * Return 0 on success, -1 on failure.
*/
-void
+int
write_pidfile(const char *filename)
{
FILE *pidfile;
@@ -3700,13 +3701,19 @@ write_pidfile(const char *filename)
if ((pidfile = fopen(filename, "w")) == NULL) {
log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
strerror(errno));
+ return -1;
} else {
#ifdef _WIN32
- fprintf(pidfile, "%d\n", (int)_getpid());
+ int pid = (int)_getpid();
#else
- fprintf(pidfile, "%d\n", (int)getpid());
+ int pid = (int)getpid();
#endif
- fclose(pidfile);
+ int rv = 0;
+ if (fprintf(pidfile, "%d\n", pid) < 0)
+ rv = -1;
+ if (fclose(pidfile) < 0)
+ rv = -1;
+ return rv;
}
}
diff --git a/src/common/util.h b/src/common/util.h
index f50cf043a4..073fb82aed 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -389,7 +389,7 @@ int path_is_relative(const char *filename);
/* Process helpers */
void start_daemon(void);
void finish_daemon(const char *desired_cwd);
-void write_pidfile(const char *filename);
+int write_pidfile(const char *filename);
/* Port forwarding */
void tor_check_port_forwarding(const char *filename,
diff --git a/src/or/config.c b/src/or/config.c
index eb89d6f5ee..872bbbd646 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1772,9 +1772,13 @@ options_act(const or_options_t *old_options)
}
/* Write our PID to the PID file. If we do not have write permissions we
- * will log a warning */
+ * will log a warning and exit. */
if (options->PidFile && !sandbox_is_active()) {
- write_pidfile(options->PidFile);
+ if (write_pidfile(options->PidFile) < 0) {
+ log_err(LD_CONFIG, "Unable to write PIDFile %s",
+ escaped(options->PidFile));
+ return -1;
+ }
}
/* Register addressmap directives */