dotlock.c revision 1.1 1 1.1 christos /* $NetBSD: dotlock.c,v 1.1 1996/06/08 19:48:19 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos * 3. All advertising materials mentioning features or use of this software
15 1.1 christos * must display the following acknowledgement:
16 1.1 christos * This product includes software developed by Christos Zoulas.
17 1.1 christos * 4. The name of the author may not be used to endorse or promote products
18 1.1 christos * derived from this software without specific prior written permission.
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #ifndef lint
33 1.1 christos static char rcsid[] = "$NetBSD: dotlock.c,v 1.1 1996/06/08 19:48:19 christos Exp $";
34 1.1 christos #endif
35 1.1 christos
36 1.1 christos #include <sys/types.h>
37 1.1 christos #include <sys/param.h>
38 1.1 christos #include <sys/stat.h>
39 1.1 christos #include <sys/time.h>
40 1.1 christos
41 1.1 christos #include <stdio.h>
42 1.1 christos #include <string.h>
43 1.1 christos #include <fcntl.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <unistd.h>
46 1.1 christos #include <errno.h>
47 1.1 christos #include <signal.h>
48 1.1 christos
49 1.1 christos #include "extern.h"
50 1.1 christos
51 1.1 christos #ifndef O_SYNC
52 1.1 christos #define O_SYNC 0
53 1.1 christos #endif
54 1.1 christos
55 1.1 christos static int create_exclusive __P((const char *));
56 1.1 christos /*
57 1.1 christos * Create a unique file. O_EXCL does not really work over NFS so we follow
58 1.1 christos * the following trick: [Inspired by S.R. van den Berg]
59 1.1 christos *
60 1.1 christos * - make a mostly unique filename and try to create it.
61 1.1 christos * - link the unique filename to our target
62 1.1 christos * - get the link count of the target
63 1.1 christos * - unlink the mostly unique filename
64 1.1 christos * - if the link count was 2, then we are ok; else we've failed.
65 1.1 christos */
66 1.1 christos static int
67 1.1 christos create_exclusive(fname)
68 1.1 christos const char *fname;
69 1.1 christos {
70 1.1 christos char path[MAXPATHLEN], hostname[MAXHOSTNAMELEN];
71 1.1 christos const char *ptr;
72 1.1 christos struct timeval tv;
73 1.1 christos pid_t pid;
74 1.1 christos size_t ntries, cookie;
75 1.1 christos int fd, serrno;
76 1.1 christos struct stat st;
77 1.1 christos
78 1.1 christos (void) gettimeofday(&tv, NULL);
79 1.1 christos (void) gethostname(hostname, MAXHOSTNAMELEN);
80 1.1 christos pid = getpid();
81 1.1 christos
82 1.1 christos cookie = pid ^ tv.tv_usec;
83 1.1 christos
84 1.1 christos /*
85 1.1 christos * We generate a semi-unique filename, from hostname.(pid ^ usec)
86 1.1 christos */
87 1.1 christos if ((ptr = strrchr(fname, '/')) == NULL)
88 1.1 christos ptr = fname;
89 1.1 christos else
90 1.1 christos ptr++;
91 1.1 christos
92 1.1 christos (void) snprintf(path, sizeof(path), "%.*s.%s.%x",
93 1.1 christos ptr - fname, fname, hostname, cookie);
94 1.1 christos
95 1.1 christos /*
96 1.1 christos * We try to create the unique filename.
97 1.1 christos */
98 1.1 christos for (ntries = 0; ntries < 5; ntries++) {
99 1.1 christos fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_SYNC, 0);
100 1.1 christos if (fd != -1) {
101 1.1 christos (void) close(fd);
102 1.1 christos break;
103 1.1 christos }
104 1.1 christos else if (errno == EEXIST)
105 1.1 christos continue;
106 1.1 christos else
107 1.1 christos return -1;
108 1.1 christos }
109 1.1 christos
110 1.1 christos /*
111 1.1 christos * We link the path to the name
112 1.1 christos */
113 1.1 christos if (link(path, fname) == -1)
114 1.1 christos goto bad;
115 1.1 christos
116 1.1 christos /*
117 1.1 christos * Note that we stat our own exclusively created name, not the
118 1.1 christos * destination, since the destination can be affected by others.
119 1.1 christos */
120 1.1 christos if (stat(path, &st) == -1)
121 1.1 christos goto bad;
122 1.1 christos
123 1.1 christos (void) unlink(path);
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * If the number of links was two (one for the unique file and one
127 1.1 christos * for the lock), we've won the race
128 1.1 christos */
129 1.1 christos if (st.st_nlink != 2) {
130 1.1 christos errno = EEXIST;
131 1.1 christos return -1;
132 1.1 christos }
133 1.1 christos return 0;
134 1.1 christos
135 1.1 christos bad:
136 1.1 christos serrno = errno;
137 1.1 christos (void) unlink(path);
138 1.1 christos errno = serrno;
139 1.1 christos return -1;
140 1.1 christos }
141 1.1 christos
142 1.1 christos int
143 1.1 christos dot_lock(fname, pollinterval, fp, msg)
144 1.1 christos const char *fname; /* Pathname to lock */
145 1.1 christos int pollinterval; /* Interval to check for lock, -1 return */
146 1.1 christos FILE *fp; /* File to print message */
147 1.1 christos const char *msg; /* Message to print */
148 1.1 christos {
149 1.1 christos char path[MAXPATHLEN];
150 1.1 christos sigset_t nset, oset;
151 1.1 christos
152 1.1 christos sigemptyset(&nset);
153 1.1 christos sigaddset(&nset, SIGHUP);
154 1.1 christos sigaddset(&nset, SIGINT);
155 1.1 christos sigaddset(&nset, SIGQUIT);
156 1.1 christos sigaddset(&nset, SIGTERM);
157 1.1 christos sigaddset(&nset, SIGTTIN);
158 1.1 christos sigaddset(&nset, SIGTTOU);
159 1.1 christos sigaddset(&nset, SIGTSTP);
160 1.1 christos sigaddset(&nset, SIGCHLD);
161 1.1 christos
162 1.1 christos (void) snprintf(path, sizeof(path), "%s.lock", fname);
163 1.1 christos
164 1.1 christos for (;;) {
165 1.1 christos (void) sigprocmask(SIG_BLOCK, &nset, &oset);
166 1.1 christos if (create_exclusive(path) != -1) {
167 1.1 christos (void) sigprocmask(SIG_SETMASK, &oset, NULL);
168 1.1 christos return 0;
169 1.1 christos }
170 1.1 christos else
171 1.1 christos (void) sigprocmask(SIG_SETMASK, &oset, NULL);
172 1.1 christos
173 1.1 christos if (errno != EEXIST)
174 1.1 christos return -1;
175 1.1 christos
176 1.1 christos if (fp && msg)
177 1.1 christos (void) fputs(msg, fp);
178 1.1 christos
179 1.1 christos if (pollinterval) {
180 1.1 christos if (pollinterval == -1) {
181 1.1 christos errno = EEXIST;
182 1.1 christos return -1;
183 1.1 christos }
184 1.1 christos sleep(pollinterval);
185 1.1 christos }
186 1.1 christos }
187 1.1 christos }
188 1.1 christos
189 1.1 christos void
190 1.1 christos dot_unlock(fname)
191 1.1 christos const char *fname;
192 1.1 christos {
193 1.1 christos char path[MAXPATHLEN];
194 1.1 christos
195 1.1 christos (void) snprintf(path, sizeof(path), "%s.lock", fname);
196 1.1 christos (void) unlink(path);
197 1.1 christos }
198