diff options
Diffstat (limited to 'src/pidfile.c')
-rw-r--r-- | src/pidfile.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/pidfile.c b/src/pidfile.c new file mode 100644 index 0000000..484cabd --- /dev/null +++ b/src/pidfile.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* wydawca - automatic release submission daemon | ||
2 | Copyright (C) 2007, 2009 Sergey Poznyakoff | ||
3 | |||
4 | Wydawca is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU General Public License as published by the | ||
6 | Free Software Foundation; either version 3 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | Wydawca is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with wydawca. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include "wydawca.h" | ||
18 | |||
19 | char *pidfile = LOCALSTATEDIR "/run/wydawca.pid"; | ||
20 | int force_startup; | ||
21 | |||
22 | |||
23 | void | ||
24 | remove_pidfile (void) | ||
25 | { | ||
26 | unlink (pidfile); | ||
27 | } | ||
28 | |||
29 | void | ||
30 | check_pidfile () | ||
31 | { | ||
32 | FILE *fp = fopen (pidfile, "r+"); | ||
33 | |||
34 | if (fp) | ||
35 | { | ||
36 | unsigned long pid; | ||
37 | if (fscanf (fp, "%ul\n", &pid) != 1) | ||
38 | { | ||
39 | logmsg (LOG_ERR, _("malformed pidfile %s"), pidfile); | ||
40 | if (!force_startup) | ||
41 | exit (1); | ||
42 | } | ||
43 | else | ||
44 | { | ||
45 | if (kill (pid, 0)) | ||
46 | { | ||
47 | if (errno != ESRCH) | ||
48 | { | ||
49 | logmsg (LOG_ERR, | ||
50 | _("cannot verify if PID %lu is running: %s"), | ||
51 | pid, strerror (errno)); | ||
52 | if (!force_startup) | ||
53 | exit (1); | ||
54 | } | ||
55 | } | ||
56 | else if (!force_startup) | ||
57 | { | ||
58 | logmsg (LOG_ERR, | ||
59 | _("another wydawca instance may be running (PID %lu)"), | ||
60 | pid); | ||
61 | exit (1); | ||
62 | } | ||
63 | } | ||
64 | logmsg (LOG_NOTICE, _("replacing pidfile %s (PID %lu)"), | ||
65 | pidfile, pid); | ||
66 | fseek (fp, 0, SEEK_SET); | ||
67 | } | ||
68 | else if (errno != ENOENT) | ||
69 | { | ||
70 | logmsg (LOG_ERR, _("cannot open pidfile %s: %s"), pidfile, | ||
71 | strerror (errno)); | ||
72 | exit (1); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | fp = fopen (pidfile, "w"); | ||
77 | if (!fp) | ||
78 | { | ||
79 | logmsg (LOG_ERR, | ||
80 | _("cannot open pidfile %s for writing: %s"), | ||
81 | pidfile, strerror (errno)); | ||
82 | exit (1); | ||
83 | } | ||
84 | } | ||
85 | fprintf (fp, "%lu\n", getpid ()); | ||
86 | fclose (fp); | ||
87 | atexit (remove_pidfile); | ||
88 | } | ||
89 | |||
90 | |||