2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christos Zoulas.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 static char sccsid
[] = "@(#)dotlock.c 2.9 (gritter) 3/20/06";
46 #include <sys/utsname.h>
55 static int maildir_access(const char *fname
);
56 static int perhaps_setgid(const char *name
, gid_t gid
);
57 static int create_exclusive(const char *fname
);
59 /* Check if we can write a lock file at all */
61 maildir_access(const char *fname
)
67 path
= ac_alloc(strlen(fname
) + 2);
69 p
= strrchr(path
, '/');
72 if (p
== NULL
|| *path
== '\0')
74 i
= access(path
, R_OK
|W_OK
|X_OK
);
80 * Set the gid if the path is in the normal mail spool
83 perhaps_setgid(const char *name
, gid_t gid
)
85 char safepath
[]= MAILSPOOL
;
87 if (strncmp(name
, safepath
, sizeof (safepath
)-1) ||
88 strchr(name
+ sizeof (safepath
), '/'))
90 return (setgid (gid
));
94 #define APID_SZ 40 /* sufficient for storign 128 bits pids */
96 * Create a unique file. O_EXCL does not really work over NFS so we follow
97 * the following trick: [Inspired by S.R. van den Berg]
99 * - make a mostly unique filename and try to create it.
100 * - link the unique filename to our target
101 * - get the link count of the target
102 * - unlink the mostly unique filename
103 * - if the link count was 2, then we are ok; else we've failed.
106 create_exclusive(const char *fname
)
108 char path
[MAXPATHLEN
];
114 size_t ntries
, cookie
;
121 hostname
= ut
.nodename
;
124 cookie
= (int)pid
^ (int)t
;
127 * We generate a semi-unique filename, from hostname.(pid ^ usec)
129 if ((ptr
= strrchr(fname
, '/')) == NULL
)
134 snprintf(path
, sizeof path
, "%.*s.%s.%x",
135 (int) (ptr
- fname
), fname
, hostname
, (unsigned int) cookie
);
138 * We try to create the unique filename.
140 for (ntries
= 0; ntries
< 5; ntries
++) {
141 perhaps_setgid(path
, effectivegid
);
142 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
|O_SYNC
, 0);
145 snprintf(apid
, APID_SZ
, "%d", (int)getpid());
146 write(fd
, apid
, strlen(apid
));
150 else if (errno
== EEXIST
)
156 * We link the path to the name
158 perhaps_setgid(fname
, effectivegid
);
159 cc
= link(path
, fname
);
166 * Note that we stat our own exclusively created name, not the
167 * destination, since the destination can be affected by others.
169 if (stat(path
, &st
) == -1)
172 perhaps_setgid(fname
, effectivegid
);
177 * If the number of links was two (one for the unique file and one
178 * for the lock), we've won the race
180 if (st
.st_nlink
!= 2) {
194 fcntl_lock(int fd
, int type
)
200 flp
.l_whence
= SEEK_SET
;
202 return fcntl(fd
, F_SETLKW
, &flp
);
206 dot_lock(const char *fname
, int fd
, int pollinterval
, FILE *fp
, const char *msg
)
208 const char *fname
; /* Pathname to lock */
209 int fd
; /* File descriptor for fname, for fcntl lock */
210 int pollinterval
; /* Interval to check for lock, -1 return */
211 FILE *fp
; /* File to print message */
212 const char *msg
; /* Message to print */
215 char path
[MAXPATHLEN
];
219 if (maildir_access(fname
) != 0)
223 sigaddset(&nset
, SIGHUP
);
224 sigaddset(&nset
, SIGINT
);
225 sigaddset(&nset
, SIGQUIT
);
226 sigaddset(&nset
, SIGTERM
);
227 sigaddset(&nset
, SIGTTIN
);
228 sigaddset(&nset
, SIGTTOU
);
229 sigaddset(&nset
, SIGTSTP
);
230 sigaddset(&nset
, SIGCHLD
);
232 snprintf(path
, sizeof(path
), "%s.lock", fname
);
235 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
236 if (create_exclusive(path
) != -1) {
237 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
242 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
245 fcntl_lock(fd
, F_UNLCK
);
246 if (olderrno
!= EEXIST
)
253 if (pollinterval
== -1) {
259 fcntl_lock(fd
, F_WRLCK
);
261 fprintf(stderr
, catgets(catd
, CATSET
, 71,
262 "%s seems a stale lock? Need to be removed by hand?\n"), path
);
267 dot_unlock(const char *fname
)
269 char path
[MAXPATHLEN
];
271 if (maildir_access(fname
) != 0)
274 snprintf(path
, sizeof(path
), "%s.lock", fname
);
275 perhaps_setgid(path
, effectivegid
);