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