pidlock.c revision 1.1 1 /* $NetBSD: pidlock.c,v 1.1 1997/10/11 02:56:23 cjs Exp $ */
2
3 /*
4 * Copyright 1996, 1997 by Curt Sampson <cjs (at) netbsd.org>.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22 * SUCH DAMAGE.
23 */
24
25 #include <sys/cdefs.h>
26 #if defined(LIBC_SCCS) && !defined(lint)
27 __RCSID("$NetBSD: pidlock.c,v 1.1 1997/10/11 02:56:23 cjs Exp $");
28 #endif /* LIBC_SCCS and not lint */
29
30 #include <sys/errno.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <util.h>
42
43 /*
44 * Create a lockfile. Return 0 when locked, -1 on error.
45 */
46 int
47 pidlock(lockfile, flags, locker, info)
48 const char *lockfile;
49 int flags;
50 pid_t *locker;
51 const char *info;
52 {
53 char tempfile[MAXPATHLEN];
54 char hostname[256];
55 pid_t pid2 = -1;
56 int f;
57 char s[256];
58 char *p;
59
60 /* Build a path to the temporary file. */
61 snprintf(s, sizeof(s), "%d", getpid());
62 /* XXX This breaks if PIDs can be longer than ten decimal digits. */
63 /* XXX But then again, so does the lockfile format. :-) */
64 strncpy(tempfile, lockfile, sizeof(tempfile)-26);
65 if (strlen(tempfile) < strlen(lockfile)) {
66 errno = ENAMETOOLONG;
67 return -1;
68 }
69 strcat(tempfile, ".tmp.");
70 strcat(tempfile, s);
71
72 /* Open it, write pid, hostname, info. */
73 if ( (f = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1 ) {
74 unlink(tempfile);
75 return -1;
76 }
77 snprintf(s, sizeof(s), "%10d\n", getpid()); /* pid */
78 if (write(f, s, 11) != 11) {
79 close(f); unlink(tempfile); return -1;
80 }
81 if (gethostname(hostname, sizeof(hostname))) /* hostname */
82 return -1;
83 if ((flags & PIDLOCK_USEHOSTNAME)) {
84 if ((write(f, hostname, strlen(hostname)) != strlen(hostname))
85 || (write(f, "\n", 1) != 1)) {
86 close(f); unlink(tempfile); return -1;
87 }
88 }
89 if (info) { /* info */
90 if (!(flags & PIDLOCK_USEHOSTNAME)) {
91 /* write blank line because there's no hostname */
92 if (write(f, "\n", 1) != 1) {
93 close(f); unlink(tempfile); return -1;
94 }
95 }
96 if (write(f, info, strlen(info)) != strlen(info) ||
97 (write(f, "\n", 1) != 1)) {
98 close(f); unlink(tempfile); return -1;
99 }
100 }
101 close(f);
102
103 /* Hard link the temporary file to the real lock file. */
104 /* This is an atomic operation. */
105 while (link(tempfile, lockfile) != 0) {
106 if (errno != EEXIST) {
107 unlink(tempfile);
108 return -1;
109 }
110 /* Find out who has this lockfile. */
111 if (!(f = open(lockfile, O_RDONLY, 0)))
112 goto retry;
113 read(f, s, 11);
114 pid2 = atoi(s);
115 read(f, s, sizeof(s)-2);
116 s[sizeof(s)-1] = '\0';
117 if ((p=strchr(s, '\n')))
118 *p = '\0';
119 close(f);
120
121 if (!((flags & PIDLOCK_USEHOSTNAME) && strcmp(s, hostname))) {
122 if ((kill(pid2, 0) != 0) && (errno == ESRCH)) {
123 /* process doesn't exist */
124 unlink(lockfile);
125 continue;
126 }
127 }
128 retry:
129 if (flags & PIDLOCK_NONBLOCK) {
130 if (locker)
131 *locker = pid2;
132 unlink(tempfile);
133 errno = EWOULDBLOCK;
134 return -1;
135 } else
136 sleep(5);
137 }
138
139 /* return this process's PID on lock */
140 if (locker)
141 *locker = getpid();
142 unlink(tempfile);
143
144 return 0;
145 }
146
147 #define LOCKPATH "/var/spool/lock/LCK.."
148 #define DEVPATH "/dev/"
149
150 int
151 ttylock(tty, flags, locker)
152 const char *tty;
153 int flags;
154 pid_t *locker;
155 {
156 char lockfile[MAXPATHLEN];
157 char ttyfile[MAXPATHLEN];
158 struct stat sb;
159
160 /* make sure the tty exists */
161 strcpy(ttyfile, DEVPATH);
162 strncat(ttyfile, tty, MAXPATHLEN-strlen(DEVPATH));
163 if (stat(ttyfile, &sb)) {
164 errno = ENOENT; return -1;
165 }
166 if (!(sb.st_mode & S_IFCHR)) {
167 errno = ENOENT; return -1;
168 }
169
170 /* do the lock */
171 strcpy(lockfile, LOCKPATH);
172 strncat(lockfile, tty, MAXPATHLEN-strlen(LOCKPATH));
173 return pidlock(lockfile, 0, 0, 0);
174 }
175
176 int
177 ttyunlock(tty)
178 const char *tty;
179 {
180 char lockfile[MAXPATHLEN];
181 char ttyfile[MAXPATHLEN];
182 struct stat sb;
183
184 /* make sure the tty exists */
185 strcpy(ttyfile, DEVPATH);
186 strncat(ttyfile, tty, MAXPATHLEN-strlen(DEVPATH));
187 if (stat(ttyfile, &sb)) {
188 errno = ENOENT; return -1;
189 }
190 if (!(sb.st_mode & S_IFCHR)) {
191 errno = ENOENT; return -1;
192 }
193
194 /* undo the lock */
195 strcpy(lockfile, LOCKPATH);
196 strncat(lockfile, tty, MAXPATHLEN-strlen(LOCKPATH));
197 return unlink(lockfile);
198 }
199