Sat, 23 Feb 2019 22:56:35 +0100
Fixed save empty dates
0 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2017 | |
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 | ||
46 | if (mkdirs(tempfile, 0755) == FALSE) { | |
47 | printf("Can't create path \"%s\"\n", tempfile); | |
48 | free(tempfile); | |
49 | free(lockfile); | |
50 | return 1; | |
51 | } | |
52 | ||
53 | if ((fp = fopen(tempfile, "w")) == NULL) { | |
54 | perror(name); | |
55 | printf("Can't create lockfile \"%s\"\n", tempfile); | |
56 | free(tempfile); | |
57 | free(lockfile); | |
58 | return 1; | |
59 | } | |
60 | fprintf(fp, "%10u\n", getpid()); | |
61 | fclose(fp); | |
62 | ||
63 | while (TRUE) { | |
64 | if (link(tempfile, lockfile) == 0) { | |
65 | unlink(tempfile); | |
66 | free(tempfile); | |
67 | free(lockfile); | |
68 | return 0; | |
69 | } | |
70 | if ((fp = fopen(lockfile, "r")) == NULL) { | |
71 | perror(name); | |
72 | printf("Can't open lockfile \"%s\"\n", tempfile); | |
73 | unlink(tempfile); | |
74 | free(tempfile); | |
75 | free(lockfile); | |
76 | return 1; | |
77 | } | |
78 | if (fscanf(fp, "%u", &oldpid) != 1) { | |
79 | perror(name); | |
80 | printf("Can't read old pid from \"%s\"\n", tempfile); | |
81 | fclose(fp); | |
82 | unlink(tempfile); | |
83 | free(tempfile); | |
84 | free(lockfile); | |
85 | return 1; | |
86 | } | |
87 | fclose(fp); | |
88 | if (kill(oldpid,0) == -1) { | |
89 | if (errno == ESRCH) { | |
90 | printf("Stale lock found for pid %u\n", oldpid); | |
91 | unlink(lockfile); | |
92 | /* no return, try lock again */ | |
93 | } else { | |
94 | perror(name); | |
95 | printf("Kill for %u failed\n",oldpid); | |
96 | unlink(tempfile); | |
97 | free(tempfile); | |
98 | free(lockfile); | |
99 | return 1; | |
100 | } | |
101 | } else { | |
102 | printf("Another %s is already running, pid=%u\n", name, oldpid); | |
103 | unlink(tempfile); | |
104 | free(tempfile); | |
105 | free(lockfile); | |
106 | return 1; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | ||
112 | ||
113 | void ulockprog(char *name) | |
114 | { | |
115 | char *lockfile; | |
116 | pid_t oldpid; | |
117 | FILE *fp; | |
118 | ||
119 | lockfile = calloc(PATH_MAX, sizeof(char)); | |
120 | snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name); | |
121 | ||
122 | if ((fp = fopen(lockfile, "r")) == NULL) { | |
123 | syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile); | |
124 | free(lockfile); | |
125 | return; | |
126 | } | |
127 | ||
128 | if (fscanf(fp, "%u", &oldpid) != 1) { | |
129 | syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile); | |
130 | fclose(fp); | |
131 | unlink(lockfile); | |
132 | free(lockfile); | |
133 | return; | |
134 | } | |
135 | ||
136 | fclose(fp); | |
137 | ||
138 | if (oldpid == getpid()) { | |
139 | (void)unlink(lockfile); | |
140 | } | |
141 | ||
142 | free(lockfile); | |
143 | } | |
144 | ||
145 |