Wed, 18 Oct 2023 10:06:11 +0200
Version 0.3.45. Removed all writing to ascii logfiles in the webserver environment, only log to MySQL.
0 | 1 | /***************************************************************************** |
745
3addb8cfcc3e
More and better error loging during daemon startup
Michiel Broek <mbroek@mbse.eu>
parents:
704
diff
changeset
|
2 | * Copyright (C) 2017-2021 |
0 | 3 | * |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the bms (Brewery Management System) | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * bms is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "lock.h" | |
24 | #include "bms.h" | |
25 | #include "futil.h" | |
26 | ||
27 | ||
28 | extern char *Private_Path; | |
29 | ||
30 | ||
31 | /* | |
32 | * Put a lock on this program. | |
33 | */ | |
34 | int lockprog(char *name) | |
35 | { | |
36 | char *tempfile, *lockfile; | |
37 | FILE *fp; | |
38 | pid_t oldpid; | |
39 | ||
40 | tempfile = calloc(PATH_MAX, sizeof(char)); | |
41 | lockfile = calloc(PATH_MAX, sizeof(char)); | |
42 | ||
43 | snprintf(tempfile, PATH_MAX, "%s/run/%s.tmp", Private_Path, name); | |
44 | snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name); | |
45 | ||
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
46 | if (mkdirs(tempfile, 0755) == false) { |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
47 | syslog(LOG_NOTICE, "Can't create path `%s': %s", tempfile, strerror(errno)); |
0 | 48 | free(tempfile); |
49 | free(lockfile); | |
50 | return 1; | |
51 | } | |
52 | ||
53 | if ((fp = fopen(tempfile, "w")) == NULL) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
54 | syslog(LOG_NOTICE, "Can't create lockfile `%s': %s", tempfile, strerror(errno)); |
0 | 55 | free(tempfile); |
56 | free(lockfile); | |
57 | return 1; | |
58 | } | |
746
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
59 | fprintf(fp, "%u\n", getpid()); |
0 | 60 | fclose(fp); |
61 | ||
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
62 | while (true) { |
0 | 63 | if (link(tempfile, lockfile) == 0) { |
64 | unlink(tempfile); | |
65 | free(tempfile); | |
66 | free(lockfile); | |
67 | return 0; | |
68 | } | |
69 | if ((fp = fopen(lockfile, "r")) == NULL) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
70 | syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", tempfile, strerror(errno)); |
0 | 71 | unlink(tempfile); |
72 | free(tempfile); | |
73 | free(lockfile); | |
74 | return 1; | |
75 | } | |
76 | if (fscanf(fp, "%u", &oldpid) != 1) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
77 | syslog(LOG_NOTICE, "Can't read old pid from `%s'", tempfile); |
0 | 78 | fclose(fp); |
79 | unlink(tempfile); | |
80 | free(tempfile); | |
81 | free(lockfile); | |
82 | return 1; | |
83 | } | |
84 | fclose(fp); | |
85 | if (kill(oldpid,0) == -1) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
86 | if (errno == ESRCH || errno == EPERM) { |
745
3addb8cfcc3e
More and better error loging during daemon startup
Michiel Broek <mbroek@mbse.eu>
parents:
704
diff
changeset
|
87 | syslog(LOG_NOTICE, "Stale lock found for pid %u, try lock again", oldpid); |
0 | 88 | unlink(lockfile); |
89 | /* no return, try lock again */ | |
90 | } else { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
91 | syslog(LOG_NOTICE, "Kill for %u failed: %s",oldpid, strerror(errno)); |
0 | 92 | unlink(tempfile); |
93 | free(tempfile); | |
94 | free(lockfile); | |
95 | return 1; | |
96 | } | |
97 | } else { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
98 | syslog(LOG_NOTICE, "Another %s is already running, pid=%u", name, oldpid); |
0 | 99 | unlink(tempfile); |
100 | free(tempfile); | |
101 | free(lockfile); | |
102 | return 1; | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | ||
108 | ||
109 | void ulockprog(char *name) | |
110 | { | |
111 | char *lockfile; | |
112 | pid_t oldpid; | |
113 | FILE *fp; | |
114 | ||
115 | lockfile = calloc(PATH_MAX, sizeof(char)); | |
116 | snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name); | |
117 | ||
118 | if ((fp = fopen(lockfile, "r")) == NULL) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
119 | syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", lockfile, strerror(errno)); |
0 | 120 | free(lockfile); |
121 | return; | |
122 | } | |
123 | ||
124 | if (fscanf(fp, "%u", &oldpid) != 1) { | |
704
62a1d2baec75
Better logging for locking the daemon and fixed stale lock recovery.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
125 | syslog(LOG_NOTICE, "Can't read old pid from `%s'", lockfile); |
0 | 126 | fclose(fp); |
127 | unlink(lockfile); | |
128 | free(lockfile); | |
129 | return; | |
130 | } | |
131 | ||
132 | fclose(fp); | |
133 | ||
134 | if (oldpid == getpid()) { | |
135 | (void)unlink(lockfile); | |
136 | } | |
137 | ||
138 | free(lockfile); | |
139 | } | |
140 | ||
141 |