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