aboutsummaryrefslogtreecommitdiff
path: root/httpd.c
diff options
context:
space:
mode:
authordefault <nobody@localhost>2024-12-16 19:47:27 +0100
committerdefault <nobody@localhost>2024-12-16 19:47:27 +0100
commitb9e0c6e74205ee43bea54fe91a9151053f107235 (patch)
tree31e3c34671d0648c9a26f7f3e7bc9b8326381f32 /httpd.c
parent1aab3c5739a689f5288d576fa5148d001a9a56c6 (diff)
Added pidfile locking.
Diffstat (limited to 'httpd.c')
-rw-r--r--httpd.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/httpd.c b/httpd.c
index 0eff657..11e4d17 100644
--- a/httpd.c
+++ b/httpd.c
@@ -778,6 +778,26 @@ void httpd(void)
xs *shm_name = NULL;
sem_t anon_job_sem;
xs *pidfile = xs_fmt("%s/server.pid", srv_basedir);
+ int pidfd;
+
+ {
+ /* do some pidfile locking acrobatics */
+ if ((pidfd = open(pidfile, O_RDWR | O_CREAT, 0660)) == -1) {
+ srv_log(xs_fmt("Cannot create pidfile %s -- cannot continue", pidfile));
+ return;
+ }
+
+ if (lockf(pidfd, F_TLOCK, 1) == -1) {
+ srv_log(xs_fmt("Cannot lock pidfile %s -- server already running?", pidfile));
+ close(pidfd);
+ return;
+ }
+
+ ftruncate(pidfd, 0);
+
+ xs *s = xs_fmt("%d\n", (int)getpid());
+ write(pidfd, s, strlen(s));
+ }
address = xs_dict_get(srv_config, "address");
@@ -813,17 +833,6 @@ void httpd(void)
srv_log(xs_fmt("httpd%s start %s %s", p_state->use_fcgi ? " (FastCGI)" : "",
full_address, USER_AGENT));
- {
- FILE *f;
-
- if ((f = fopen(pidfile, "w")) != NULL) {
- fprintf(f, "%d\n", getpid());
- fclose(f);
- }
- else
- srv_log(xs_fmt("Cannot create %s: %s", pidfile, strerror(errno)));
- }
-
/* show the number of usable file descriptors */
struct rlimit r;
getrlimit(RLIMIT_NOFILE, &r);