diff options
| author | 2024-12-16 19:47:27 +0100 | |
|---|---|---|
| committer | 2024-12-16 19:47:27 +0100 | |
| commit | b9e0c6e74205ee43bea54fe91a9151053f107235 (patch) | |
| tree | 31e3c34671d0648c9a26f7f3e7bc9b8326381f32 | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | penes-snac2-b9e0c6e74205ee43bea54fe91a9151053f107235.tar.gz penes-snac2-b9e0c6e74205ee43bea54fe91a9151053f107235.tar.xz penes-snac2-b9e0c6e74205ee43bea54fe91a9151053f107235.zip | |
Added pidfile locking.
| -rw-r--r-- | httpd.c | 31 |
1 files changed, 20 insertions, 11 deletions
| @@ -778,6 +778,26 @@ void httpd(void) | |||
| 778 | xs *shm_name = NULL; | 778 | xs *shm_name = NULL; |
| 779 | sem_t anon_job_sem; | 779 | sem_t anon_job_sem; |
| 780 | xs *pidfile = xs_fmt("%s/server.pid", srv_basedir); | 780 | xs *pidfile = xs_fmt("%s/server.pid", srv_basedir); |
| 781 | int pidfd; | ||
| 782 | |||
| 783 | { | ||
| 784 | /* do some pidfile locking acrobatics */ | ||
| 785 | if ((pidfd = open(pidfile, O_RDWR | O_CREAT, 0660)) == -1) { | ||
| 786 | srv_log(xs_fmt("Cannot create pidfile %s -- cannot continue", pidfile)); | ||
| 787 | return; | ||
| 788 | } | ||
| 789 | |||
| 790 | if (lockf(pidfd, F_TLOCK, 1) == -1) { | ||
| 791 | srv_log(xs_fmt("Cannot lock pidfile %s -- server already running?", pidfile)); | ||
| 792 | close(pidfd); | ||
| 793 | return; | ||
| 794 | } | ||
| 795 | |||
| 796 | ftruncate(pidfd, 0); | ||
| 797 | |||
| 798 | xs *s = xs_fmt("%d\n", (int)getpid()); | ||
| 799 | write(pidfd, s, strlen(s)); | ||
| 800 | } | ||
| 781 | 801 | ||
| 782 | address = xs_dict_get(srv_config, "address"); | 802 | address = xs_dict_get(srv_config, "address"); |
| 783 | 803 | ||
| @@ -813,17 +833,6 @@ void httpd(void) | |||
| 813 | srv_log(xs_fmt("httpd%s start %s %s", p_state->use_fcgi ? " (FastCGI)" : "", | 833 | srv_log(xs_fmt("httpd%s start %s %s", p_state->use_fcgi ? " (FastCGI)" : "", |
| 814 | full_address, USER_AGENT)); | 834 | full_address, USER_AGENT)); |
| 815 | 835 | ||
| 816 | { | ||
| 817 | FILE *f; | ||
| 818 | |||
| 819 | if ((f = fopen(pidfile, "w")) != NULL) { | ||
| 820 | fprintf(f, "%d\n", getpid()); | ||
| 821 | fclose(f); | ||
| 822 | } | ||
| 823 | else | ||
| 824 | srv_log(xs_fmt("Cannot create %s: %s", pidfile, strerror(errno))); | ||
| 825 | } | ||
| 826 | |||
| 827 | /* show the number of usable file descriptors */ | 836 | /* show the number of usable file descriptors */ |
| 828 | struct rlimit r; | 837 | struct rlimit r; |
| 829 | getrlimit(RLIMIT_NOFILE, &r); | 838 | getrlimit(RLIMIT_NOFILE, &r); |