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