xref: /freebsd/contrib/tcsh/dotlock.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*	NetBSD: dotlock.c,v 1.11 2009/10/21 01:07:46 snj 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "sh.h"
27 
28 #include <stdio.h>
29 #ifndef O_SYNC
30 #define O_SYNC	0
31 #endif
32 
33 #include "dotlock.h"
34 
35 static int create_exclusive(const char *);
36 /*
37  * Create a unique file. O_EXCL does not really work over NFS so we follow
38  * the following trick: [Inspired by  S.R. van den Berg]
39  *
40  * - make a mostly unique filename and try to create it.
41  * - link the unique filename to our target
42  * - get the link count of the target
43  * - unlink the mostly unique filename
44  * - if the link count was 2, then we are ok; else we've failed.
45  */
46 static int
47 create_exclusive(const char *fname)
48 {
49 	char path[MAXPATHLEN], hostname[MAXHOSTNAMELEN + 1];
50 	const char *ptr;
51 	struct timeval tv;
52 	pid_t pid;
53 	size_t ntries, cookie;
54 	int fd, serrno;
55 	struct stat st;
56 
57 	(void)gettimeofday(&tv, NULL);
58 	(void)gethostname(hostname, sizeof(hostname));
59 	hostname[sizeof(hostname) - 1] = '\0';
60 	pid = getpid();
61 
62 	cookie = pid ^ tv.tv_usec;
63 
64 	/*
65 	 * We generate a semi-unique filename, from hostname.(pid ^ usec)
66 	 */
67 	if ((ptr = strrchr(fname, '/')) == NULL)
68 		ptr = fname;
69 	else
70 		ptr++;
71 
72 	(void)snprintf(path, sizeof(path), "%.*s.%s.%lx",
73 	    (int)(ptr - fname), fname, hostname, (u_long)cookie);
74 
75 	/*
76 	 * We try to create the unique filename.
77 	 */
78 	for (ntries = 0; ntries < 5; ntries++) {
79 		fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_SYNC, 0);
80 		if (fd != -1) {
81 			(void)close(fd);
82 			break;
83 		}
84 		else if (errno == EEXIST)
85 			continue;
86 		else
87 			return -1;
88 	}
89 
90 	/*
91 	 * We link the path to the name
92 	 */
93 	if (link(path, fname) == -1)
94 		goto bad;
95 
96 	/*
97 	 * Note that we stat our own exclusively created name, not the
98 	 * destination, since the destination can be affected by others.
99 	 */
100 	if (stat(path, &st) == -1)
101 		goto bad;
102 
103 	(void)unlink(path);
104 
105 	/*
106 	 * If the number of links was two (one for the unique file and one
107 	 * for the lock), we've won the race
108 	 */
109 	if (st.st_nlink != 2) {
110 		errno = EEXIST;
111 		return -1;
112 	}
113 	return 0;
114 
115 bad:
116 	serrno = errno;
117 	(void)unlink(path);
118 	errno = serrno;
119 	return -1;
120 }
121 
122 /*
123  * fname -- Pathname to lock
124  * pollinterval -- Interval (miliseconds) to check for lock, -1 return
125  */
126 int
127 dot_lock(const char *fname, int pollinterval)
128 {
129 	char path[MAXPATHLEN];
130 	sigset_t nset, oset;
131 	int retval;
132 
133 	(void)sigemptyset(&nset);
134 	(void)sigaddset(&nset, SIGHUP);
135 	(void)sigaddset(&nset, SIGINT);
136 	(void)sigaddset(&nset, SIGQUIT);
137 	(void)sigaddset(&nset, SIGTERM);
138 	(void)sigaddset(&nset, SIGTTIN);
139 	(void)sigaddset(&nset, SIGTTOU);
140 	(void)sigaddset(&nset, SIGTSTP);
141 	(void)sigaddset(&nset, SIGCHLD);
142 
143 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
144 
145 	retval = -1;
146 	for (;;) {
147 		handle_pending_signals();
148 		(void)sigprocmask(SIG_BLOCK, &nset, &oset);
149 		if (create_exclusive(path) != -1) {
150 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
151 			retval = 0;
152 			break;
153 		}
154 		else
155 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
156 
157 		if (errno != EEXIST)
158 			break;
159 
160 		if (pollinterval) {
161 			if (pollinterval == -1) {
162 				errno = EEXIST;
163 				break;
164 			}
165 			(void)usleep((unsigned int)pollinterval * 1000);
166 		}
167 	}
168 	handle_pending_signals();
169 	return retval;
170 }
171 
172 void
173 dot_unlock(const char *fname)
174 {
175 	char path[MAXPATHLEN];
176 
177 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
178 	(void)unlink(path);
179 }
180