Many: use srelax() is some places
[s-mailx.git] / dotlock.c
blob365e07f78ca16b8dd05c698be2447ba8f68f2058
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Mailbox file locking.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Christos Zoulas.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "nail.h"
38 #include <sys/utsname.h>
40 #include <fcntl.h>
42 #ifndef O_SYNC
43 # define O_SYNC 0
44 #endif
46 static int maildir_access(const char *fname);
47 static int perhaps_setgid(const char *name, gid_t gid);
48 static int create_exclusive(const char *fname);
50 /* Check if we can write a lock file at all */
51 static int
52 maildir_access(const char *fname)
54 char *path;
55 char *p;
56 int i;
58 i = (int)strlen(fname);
59 path = ac_alloc(i + 2);
60 memcpy(path, fname, i + 1);
61 p = strrchr(path, '/');
62 if (p != NULL)
63 *p = '\0';
64 if (p == NULL || *path == '\0') {
65 path[0] = '.';
66 path[1] = '\0';
68 i = access(path, R_OK|W_OK|X_OK);
69 ac_free(path);
70 return i;
74 * Set the gid if the path is in the normal mail spool
76 static int
77 perhaps_setgid(const char *name, gid_t gid)
79 char safepath[]= MAILSPOOL;
81 if (strncmp(name, safepath, sizeof (safepath)-1) ||
82 strchr(name + sizeof (safepath), '/'))
83 return 0;
84 return (setgid (gid));
88 #define APID_SZ 40 /* sufficient for storign 128 bits pids */
90 * Create a unique file. O_EXCL does not really work over NFS so we follow
91 * the following trick: [Inspired by S.R. van den Berg]
93 * - make a mostly unique filename and try to create it.
94 * - link the unique filename to our target
95 * - get the link count of the target
96 * - unlink the mostly unique filename
97 * - if the link count was 2, then we are ok; else we've failed.
99 static int
100 create_exclusive(const char *fname)
102 char path[MAXPATHLEN];
103 char *hostname;
104 char apid[APID_SZ];
105 const char *ptr;
106 time_t t;
107 pid_t pid;
108 size_t ntries, cookie;
109 int fd, serrno, cc;
110 struct stat st;
111 struct utsname ut;
113 time(&t);
114 uname(&ut);
115 hostname = ut.nodename;
116 pid = getpid();
118 cookie = (int)pid ^ (int)t;
121 * We generate a semi-unique filename, from hostname.(pid ^ usec)
123 if ((ptr = strrchr(fname, '/')) == NULL)
124 ptr = fname;
125 else
126 ptr++;
128 snprintf(path, sizeof path, "%.*s.%s.%x",
129 (int) (ptr - fname), fname, hostname, (unsigned int) cookie);
132 * We try to create the unique filename.
134 for (ntries = 0; ntries < 5; ntries++) {
135 perhaps_setgid(path, effectivegid);
136 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_SYNC, 0);
137 (void)setgid(realgid); /* XXX */
138 if (fd != -1) {
139 snprintf(apid, APID_SZ, "%d", (int)getpid());
140 write(fd, apid, strlen(apid));
141 close(fd);
142 break;
144 else if (errno == EEXIST)
145 continue;
146 else
147 return -1;
150 * We link the path to the name
152 perhaps_setgid(fname, effectivegid);
153 cc = link(path, fname);
154 (void)setgid(realgid); /* XXX */
156 if (cc == -1)
157 goto bad;
160 * Note that we stat our own exclusively created name, not the
161 * destination, since the destination can be affected by others.
163 if (stat(path, &st) == -1)
164 goto bad;
166 perhaps_setgid(fname, effectivegid);
167 unlink(path);
168 (void)setgid(realgid); /* XXX */
171 * If the number of links was two (one for the unique file and one
172 * for the lock), we've won the race
174 if (st.st_nlink != 2) {
175 errno = EEXIST;
176 return -1;
178 return 0;
180 bad:
181 serrno = errno;
182 unlink(path);
183 errno = serrno;
184 return -1;
187 int
188 fcntl_lock(int fd, int ltype) /* TODO check callees for EINTR etc.!!! */
190 struct flock flp;
192 flp.l_type = ltype;
193 flp.l_start = 0;
194 flp.l_whence = SEEK_SET;
195 flp.l_len = 0;
196 return fcntl(fd, F_SETLKW, &flp);
200 dot_lock(const char *fname, int fd, int pollinterval, FILE *fp, const char *msg)
201 #ifdef notdef
202 const char *fname; /* Pathname to lock */
203 int fd; /* File descriptor for fname, for fcntl lock */
204 int pollinterval; /* Interval to check for lock, -1 return */
205 FILE *fp; /* File to print message */
206 const char *msg; /* Message to print */
207 #endif
209 char path[MAXPATHLEN];
210 sigset_t nset, oset;
211 int i, olderrno;
213 if (maildir_access(fname) != 0)
214 return 0;
216 sigemptyset(&nset);
217 sigaddset(&nset, SIGHUP);
218 sigaddset(&nset, SIGINT);
219 sigaddset(&nset, SIGQUIT);
220 sigaddset(&nset, SIGTERM);
221 sigaddset(&nset, SIGTTIN);
222 sigaddset(&nset, SIGTTOU);
223 sigaddset(&nset, SIGTSTP);
224 sigaddset(&nset, SIGCHLD);
226 snprintf(path, sizeof(path), "%s.lock", fname);
228 for (i=0;i<15;i++) {
229 sigprocmask(SIG_BLOCK, &nset, &oset);
230 if (create_exclusive(path) != -1) {
231 sigprocmask(SIG_SETMASK, &oset, NULL);
232 return 0;
234 else {
235 olderrno = errno;
236 sigprocmask(SIG_SETMASK, &oset, NULL);
239 fcntl_lock(fd, F_UNLCK);
240 if (olderrno != EEXIST)
241 return -1;
243 if (fp && msg)
244 fputs(msg, fp);
246 if (pollinterval) {
247 if (pollinterval == -1) {
248 errno = EEXIST;
249 return -1;
251 sleep(pollinterval);
253 (void)fcntl_lock(fd, F_WRLCK);
255 fprintf(stderr, tr(71,
256 "%s seems a stale lock? Need to be removed by hand?\n"), path);
257 return -1;
260 void
261 dot_unlock(const char *fname)
263 char path[MAXPATHLEN];
265 if (maildir_access(fname) != 0)
266 return;
268 snprintf(path, sizeof(path), "%s.lock", fname);
269 perhaps_setgid(path, effectivegid);
270 unlink(path);
271 (void)setgid(realgid); /* XXX */