1 /***************************************************************************** |
|
2 * Copyright (C) 2015 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
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 * mbsePi-apps 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 "brewco.h" |
|
25 |
|
26 extern char *varpath; |
|
27 |
|
28 |
|
29 /* |
|
30 * Put a lock on this program. |
|
31 */ |
|
32 int lockprog(char *name) |
|
33 { |
|
34 char *tempfile, *lockfile; |
|
35 FILE *fp; |
|
36 pid_t oldpid; |
|
37 |
|
38 tempfile = calloc(PATH_MAX, sizeof(char)); |
|
39 lockfile = calloc(PATH_MAX, sizeof(char)); |
|
40 |
|
41 snprintf(tempfile, PATH_MAX, "%s%s.tmp", varpath, name); |
|
42 snprintf(lockfile, PATH_MAX, "%s%s.pid", varpath, name); |
|
43 |
|
44 if ((fp = fopen(tempfile, "w")) == NULL) { |
|
45 perror(name); |
|
46 printf("Can't create lockfile \"%s\"\n", tempfile); |
|
47 free(tempfile); |
|
48 free(lockfile); |
|
49 return 1; |
|
50 } |
|
51 fprintf(fp, "%10u\n", getpid()); |
|
52 fclose(fp); |
|
53 |
|
54 while (TRUE) { |
|
55 if (link(tempfile, lockfile) == 0) { |
|
56 unlink(tempfile); |
|
57 free(tempfile); |
|
58 free(lockfile); |
|
59 return 0; |
|
60 } |
|
61 if ((fp = fopen(lockfile, "r")) == NULL) { |
|
62 perror(name); |
|
63 printf("Can't open lockfile \"%s\"\n", tempfile); |
|
64 unlink(tempfile); |
|
65 free(tempfile); |
|
66 free(lockfile); |
|
67 return 1; |
|
68 } |
|
69 if (fscanf(fp, "%u", &oldpid) != 1) { |
|
70 perror(name); |
|
71 printf("Can't read old pid from \"%s\"\n", tempfile); |
|
72 fclose(fp); |
|
73 unlink(tempfile); |
|
74 free(tempfile); |
|
75 free(lockfile); |
|
76 return 1; |
|
77 } |
|
78 fclose(fp); |
|
79 if (kill(oldpid,0) == -1) { |
|
80 if (errno == ESRCH) { |
|
81 printf("Stale lock found for pid %u\n", oldpid); |
|
82 unlink(lockfile); |
|
83 /* no return, try lock again */ |
|
84 } else { |
|
85 perror(name); |
|
86 printf("Kill for %u failed\n",oldpid); |
|
87 unlink(tempfile); |
|
88 free(tempfile); |
|
89 free(lockfile); |
|
90 return 1; |
|
91 } |
|
92 } else { |
|
93 printf("Another %s is already running, pid=%u\n", name, oldpid); |
|
94 unlink(tempfile); |
|
95 free(tempfile); |
|
96 free(lockfile); |
|
97 return 1; |
|
98 } |
|
99 } |
|
100 } |
|
101 |
|
102 |
|
103 |
|
104 void ulockprog(char *name) |
|
105 { |
|
106 char *lockfile; |
|
107 pid_t oldpid; |
|
108 FILE *fp; |
|
109 |
|
110 lockfile = calloc(PATH_MAX, sizeof(char)); |
|
111 snprintf(lockfile, PATH_MAX, "%s%s.pid", varpath, name); |
|
112 |
|
113 if ((fp = fopen(lockfile, "r")) == NULL) { |
|
114 syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile); |
|
115 free(lockfile); |
|
116 return; |
|
117 } |
|
118 |
|
119 if (fscanf(fp, "%u", &oldpid) != 1) { |
|
120 syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile); |
|
121 fclose(fp); |
|
122 unlink(lockfile); |
|
123 free(lockfile); |
|
124 return; |
|
125 } |
|
126 |
|
127 fclose(fp); |
|
128 |
|
129 if (oldpid == getpid()) { |
|
130 (void)unlink(lockfile); |
|
131 } |
|
132 |
|
133 free(lockfile); |
|
134 } |
|
135 |
|
136 |
|