Resort options, exact usage hint..
[s-mailx.git] / dotlock.c
blobeb04a9293a518c78b93549f9f65c5da7678e8f2b
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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 "rcv.h"
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <time.h>
41 #include <sys/utsname.h>
42 #include <errno.h>
44 #include "extern.h"
46 #ifndef O_SYNC
47 #define O_SYNC 0
48 #endif
50 static int maildir_access(const char *fname);
51 static int perhaps_setgid(const char *name, gid_t gid);
52 static int create_exclusive(const char *fname);
54 /* Check if we can write a lock file at all */
55 static int
56 maildir_access(const char *fname)
58 char *path;
59 char *p;
60 int i;
62 path = ac_alloc(strlen(fname) + 2);
63 strcpy(path, fname);
64 p = strrchr(path, '/');
65 if (p != NULL)
66 *p = '\0';
67 if (p == NULL || *path == '\0')
68 strcpy(path, ".");
69 i = access(path, R_OK|W_OK|X_OK);
70 ac_free(path);
71 return i;
75 * Set the gid if the path is in the normal mail spool
77 static int
78 perhaps_setgid(const char *name, gid_t gid)
80 char safepath[]= MAILSPOOL;
82 if (strncmp(name, safepath, sizeof (safepath)-1) ||
83 strchr(name + sizeof (safepath), '/'))
84 return 0;
85 return (setgid (gid));
89 #define APID_SZ 40 /* sufficient for storign 128 bits pids */
91 * Create a unique file. O_EXCL does not really work over NFS so we follow
92 * the following trick: [Inspired by S.R. van den Berg]
94 * - make a mostly unique filename and try to create it.
95 * - link the unique filename to our target
96 * - get the link count of the target
97 * - unlink the mostly unique filename
98 * - if the link count was 2, then we are ok; else we've failed.
100 static int
101 create_exclusive(const char *fname)
103 char path[MAXPATHLEN];
104 char *hostname;
105 char apid[APID_SZ];
106 const char *ptr;
107 time_t t;
108 pid_t pid;
109 size_t ntries, cookie;
110 int fd, serrno, cc;
111 struct stat st;
112 struct utsname ut;
114 time(&t);
115 uname(&ut);
116 hostname = ut.nodename;
117 pid = getpid();
119 cookie = (int)pid ^ (int)t;
122 * We generate a semi-unique filename, from hostname.(pid ^ usec)
124 if ((ptr = strrchr(fname, '/')) == NULL)
125 ptr = fname;
126 else
127 ptr++;
129 snprintf(path, sizeof path, "%.*s.%s.%x",
130 (int) (ptr - fname), fname, hostname, (unsigned int) cookie);
133 * We try to create the unique filename.
135 for (ntries = 0; ntries < 5; ntries++) {
136 perhaps_setgid(path, effectivegid);
137 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_SYNC, 0);
138 setgid(realgid);
139 if (fd != -1) {
140 snprintf(apid, APID_SZ, "%d", (int)getpid());
141 write(fd, apid, strlen(apid));
142 close(fd);
143 break;
145 else if (errno == EEXIST)
146 continue;
147 else
148 return -1;
151 * We link the path to the name
153 perhaps_setgid(fname, effectivegid);
154 cc = link(path, fname);
155 setgid(realgid);
157 if (cc == -1)
158 goto bad;
161 * Note that we stat our own exclusively created name, not the
162 * destination, since the destination can be affected by others.
164 if (stat(path, &st) == -1)
165 goto bad;
167 perhaps_setgid(fname, effectivegid);
168 unlink(path);
169 setgid(realgid);
172 * If the number of links was two (one for the unique file and one
173 * for the lock), we've won the race
175 if (st.st_nlink != 2) {
176 errno = EEXIST;
177 return -1;
179 return 0;
181 bad:
182 serrno = errno;
183 unlink(path);
184 errno = serrno;
185 return -1;
188 int
189 fcntl_lock(int fd, int type)
191 struct flock flp;
193 flp.l_type = type;
194 flp.l_start = 0;
195 flp.l_whence = SEEK_SET;
196 flp.l_len = 0;
197 return fcntl(fd, F_SETLKW, &flp);
201 dot_lock(const char *fname, int fd, int pollinterval, FILE *fp, const char *msg)
202 #ifdef notdef
203 const char *fname; /* Pathname to lock */
204 int fd; /* File descriptor for fname, for fcntl lock */
205 int pollinterval; /* Interval to check for lock, -1 return */
206 FILE *fp; /* File to print message */
207 const char *msg; /* Message to print */
208 #endif
210 char path[MAXPATHLEN];
211 sigset_t nset, oset;
212 int i, olderrno;
214 if (maildir_access(fname) != 0)
215 return 0;
217 sigemptyset(&nset);
218 sigaddset(&nset, SIGHUP);
219 sigaddset(&nset, SIGINT);
220 sigaddset(&nset, SIGQUIT);
221 sigaddset(&nset, SIGTERM);
222 sigaddset(&nset, SIGTTIN);
223 sigaddset(&nset, SIGTTOU);
224 sigaddset(&nset, SIGTSTP);
225 sigaddset(&nset, SIGCHLD);
227 snprintf(path, sizeof(path), "%s.lock", fname);
229 for (i=0;i<15;i++) {
230 sigprocmask(SIG_BLOCK, &nset, &oset);
231 if (create_exclusive(path) != -1) {
232 sigprocmask(SIG_SETMASK, &oset, NULL);
233 return 0;
235 else {
236 olderrno = errno;
237 sigprocmask(SIG_SETMASK, &oset, NULL);
240 fcntl_lock(fd, F_UNLCK);
241 if (olderrno != EEXIST)
242 return -1;
244 if (fp && msg)
245 fputs(msg, fp);
247 if (pollinterval) {
248 if (pollinterval == -1) {
249 errno = EEXIST;
250 return -1;
252 sleep(pollinterval);
254 fcntl_lock(fd, F_WRLCK);
256 fprintf(stderr, catgets(catd, CATSET, 71,
257 "%s seems a stale lock? Need to be removed by hand?\n"), path);
258 return -1;
261 void
262 dot_unlock(const char *fname)
264 char path[MAXPATHLEN];
266 if (maildir_access(fname) != 0)
267 return;
269 snprintf(path, sizeof(path), "%s.lock", fname);
270 perhaps_setgid(path, effectivegid);
271 unlink(path);
272 setgid(realgid);