mash/lock.c

Sat, 12 Jul 2014 21:59:19 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Jul 2014 21:59:19 +0200
changeset 103
99c47a8a61cb
permissions
-rw-r--r--

Added the mash sourcecode, this does nothing useful yet.

103
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Copyright (C) 2014
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with EC-65K; see the file COPYING. If not, write to the Free
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 #include "mash.h"
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "lock.h"
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 /*
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 * Put a lock on this program.
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 */
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 int lockprog(char *name)
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 char *tempfile, *lockfile;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 FILE *fp;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 pid_t oldpid;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 tempfile = calloc(PATH_MAX, sizeof(char));
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 lockfile = calloc(PATH_MAX, sizeof(char));
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 snprintf(tempfile, PATH_MAX, "/var/run/%s.tmp", name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 if ((fp = fopen(tempfile, "w")) == NULL) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 perror(name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 printf("Can't create lockfile \"%s\"\n", tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 return 1;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 fprintf(fp, "%10u\n", getpid());
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 fclose(fp);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 while (TRUE) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 if (link(tempfile, lockfile) == 0) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 unlink(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 return 0;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 if ((fp = fopen(lockfile, "r")) == NULL) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 perror(name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 printf("Can't open lockfile \"%s\"\n", tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 unlink(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 return 1;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 if (fscanf(fp, "%u", &oldpid) != 1) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 perror(name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 printf("Can't read old pid from \"%s\"\n", tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 fclose(fp);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 unlink(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 return 1;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 fclose(fp);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 if (kill(oldpid,0) == -1) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 if (errno == ESRCH) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 printf("Stale lock found for pid %u\n", oldpid);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 unlink(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 /* no return, try lock again */
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 } else {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 perror(name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 printf("Kill for %u failed\n",oldpid);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 unlink(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 return 1;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 } else {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 printf("Another %s is already running, pid=%u\n", name, oldpid);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 unlink(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 free(tempfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 return 1;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 void ulockprog(char *name)
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 char *lockfile;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 pid_t oldpid;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 FILE *fp;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 lockfile = calloc(PATH_MAX, sizeof(char));
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 if ((fp = fopen(lockfile, "r")) == NULL) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 return;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 if (fscanf(fp, "%u", &oldpid) != 1) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 fclose(fp);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 unlink(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 return;
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 fclose(fp);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 if (oldpid == getpid()) {
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 (void)unlink(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 free(lockfile);
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 }
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133
99c47a8a61cb Added the mash sourcecode, this does nothing useful yet.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134

mercurial