1 /* $NetBSD: dotlock.c,v 1.11 2009/10/21 01:07:46 snj Exp $ */
4 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 RCSID("$tcsh: dotlock.c,v 3.3 2014/03/09 00:11:54 christos Exp $");
36 static int create_exclusive(const char *);
38 * Create a unique file. O_EXCL does not really work over NFS so we follow
39 * the following trick: [Inspired by S.R. van den Berg]
41 * - make a mostly unique filename and try to create it.
42 * - link the unique filename to our target
43 * - get the link count of the target
44 * - unlink the mostly unique filename
45 * - if the link count was 2, then we are ok; else we've failed.
48 create_exclusive(const char *fname
)
50 char path
[MAXPATHLEN
], hostname
[MAXHOSTNAMELEN
+ 1];
54 size_t ntries
, cookie
;
58 (void)gettimeofday(&tv
, NULL
);
59 (void)gethostname(hostname
, sizeof(hostname
));
60 hostname
[sizeof(hostname
) - 1] = '\0';
63 cookie
= pid
^ tv
.tv_usec
;
66 * We generate a semi-unique filename, from hostname.(pid ^ usec)
68 if ((ptr
= strrchr(fname
, '/')) == NULL
)
73 (void)snprintf(path
, sizeof(path
), "%.*s.%s.%lx",
74 (int)(ptr
- fname
), fname
, hostname
, (u_long
)cookie
);
77 * We try to create the unique filename.
79 for (ntries
= 0; ntries
< 5; ntries
++) {
80 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
|O_SYNC
, 0);
85 else if (errno
== EEXIST
)
92 * We link the path to the name
94 if (link(path
, fname
) == -1)
98 * Note that we stat our own exclusively created name, not the
99 * destination, since the destination can be affected by others.
101 if (stat(path
, &st
) == -1)
107 * If the number of links was two (one for the unique file and one
108 * for the lock), we've won the race
110 if (st
.st_nlink
!= 2) {
124 * fname -- Pathname to lock
125 * pollinterval -- Interval (miliseconds) to check for lock, -1 return
128 dot_lock(const char *fname
, int pollinterval
)
130 char path
[MAXPATHLEN
];
134 (void)sigemptyset(&nset
);
135 (void)sigaddset(&nset
, SIGHUP
);
136 (void)sigaddset(&nset
, SIGINT
);
137 (void)sigaddset(&nset
, SIGQUIT
);
138 (void)sigaddset(&nset
, SIGTERM
);
139 (void)sigaddset(&nset
, SIGTTIN
);
140 (void)sigaddset(&nset
, SIGTTOU
);
141 (void)sigaddset(&nset
, SIGTSTP
);
142 (void)sigaddset(&nset
, SIGCHLD
);
144 (void)snprintf(path
, sizeof(path
), "%s.lock", fname
);
148 handle_pending_signals();
149 (void)sigprocmask(SIG_BLOCK
, &nset
, &oset
);
150 if (create_exclusive(path
) != -1) {
151 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
156 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
162 if (pollinterval
== -1) {
166 (void)usleep((unsigned int)pollinterval
* 1000);
169 handle_pending_signals();
174 dot_unlock(const char *fname
)
176 char path
[MAXPATHLEN
];
178 (void)snprintf(path
, sizeof(path
), "%s.lock", fname
);