thermferm/lock.c

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

mercurial