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.
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
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.
41 #include <sys/utsname.h>
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 */
56 maildir_access(const char *fname
)
62 path
= ac_alloc(strlen(fname
) + 2);
64 p
= strrchr(path
, '/');
67 if (p
== NULL
|| *path
== '\0')
69 i
= access(path
, R_OK
|W_OK
|X_OK
);
75 * Set the gid if the path is in the normal mail spool
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
), '/'))
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.
101 create_exclusive(const char *fname
)
103 char path
[MAXPATHLEN
];
109 size_t ntries
, cookie
;
116 hostname
= ut
.nodename
;
119 cookie
= (int)pid
^ (int)t
;
122 * We generate a semi-unique filename, from hostname.(pid ^ usec)
124 if ((ptr
= strrchr(fname
, '/')) == NULL
)
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);
140 snprintf(apid
, APID_SZ
, "%d", (int)getpid());
141 write(fd
, apid
, strlen(apid
));
145 else if (errno
== EEXIST
)
151 * We link the path to the name
153 perhaps_setgid(fname
, effectivegid
);
154 cc
= link(path
, fname
);
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)
167 perhaps_setgid(fname
, effectivegid
);
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) {
189 fcntl_lock(int fd
, int type
)
195 flp
.l_whence
= SEEK_SET
;
197 return fcntl(fd
, F_SETLKW
, &flp
);
201 dot_lock(const char *fname
, int fd
, int pollinterval
, FILE *fp
, const char *msg
)
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 */
210 char path
[MAXPATHLEN
];
214 if (maildir_access(fname
) != 0)
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
);
230 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
231 if (create_exclusive(path
) != -1) {
232 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
237 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
240 fcntl_lock(fd
, F_UNLCK
);
241 if (olderrno
!= EEXIST
)
248 if (pollinterval
== -1) {
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
);
262 dot_unlock(const char *fname
)
264 char path
[MAXPATHLEN
];
266 if (maildir_access(fname
) != 0)
269 snprintf(path
, sizeof(path
), "%s.lock", fname
);
270 perhaps_setgid(path
, effectivegid
);