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