Home | History | Annotate | Line # | Download | only in mail
      1 /*	$NetBSD: dotlock.c,v 1.14 2021/02/17 21:09:39 christos 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 
     27 #include <sys/cdefs.h>
     28 #ifndef lint
     29 __RCSID("$NetBSD: dotlock.c,v 1.14 2021/02/17 21:09:39 christos Exp $");
     30 #endif
     31 
     32 #include "rcv.h"
     33 #include "extern.h"
     34 #include "sig.h"
     35 
     36 #ifndef O_SYNC
     37 #define O_SYNC	0
     38 #endif
     39 
     40 static int create_exclusive(const char *);
     41 /*
     42  * Create a unique file. O_EXCL does not really work over NFS so we follow
     43  * the following trick: [Inspired by  S.R. van den Berg]
     44  *
     45  * - make a mostly unique filename and try to create it.
     46  * - link the unique filename to our target
     47  * - get the link count of the target
     48  * - unlink the mostly unique filename
     49  * - if the link count was 2, then we are ok; else we've failed.
     50  */
     51 static int
     52 create_exclusive(const char *fname)
     53 {
     54 	char path[MAXPATHLEN], hostname[MAXHOSTNAMELEN + 1];
     55 	const char *ptr;
     56 	struct timeval tv;
     57 	pid_t pid;
     58 	size_t ntries, cookie;
     59 	int fd, serrno;
     60 	struct stat st;
     61 
     62 	(void)gettimeofday(&tv, NULL);
     63 	(void)gethostname(hostname, sizeof(hostname));
     64 	hostname[sizeof(hostname) - 1] = '\0';
     65 	pid = getpid();
     66 
     67 	cookie = pid ^ tv.tv_usec;
     68 
     69 	/*
     70 	 * We generate a semi-unique filename, from hostname.(pid ^ usec)
     71 	 */
     72 	if ((ptr = strrchr(fname, '/')) == NULL)
     73 		ptr = fname;
     74 	else
     75 		ptr++;
     76 
     77 	(void)snprintf(path, sizeof(path), "%.*s.%s.%lx",
     78 	    (int)(ptr - fname), fname, hostname, (u_long)cookie);
     79 
     80 	/*
     81 	 * We try to create the unique filename.
     82 	 */
     83 	for (ntries = 0; ; ntries++) {
     84 		fd = open(path,
     85 		    O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_SYNC|O_CLOEXEC, 0);
     86 		if (fd != -1) {
     87 			(void)close(fd);
     88 			break;
     89 		}
     90 		else if (errno == EEXIST && ntries < 5)
     91 			continue;
     92 		else
     93 			return -1;
     94 	}
     95 
     96 	/*
     97 	 * We link the path to the name
     98 	 */
     99 	if (link(path, fname) == -1)
    100 		goto bad;
    101 
    102 	/*
    103 	 * Note that we stat our own exclusively created name, not the
    104 	 * destination, since the destination can be affected by others.
    105 	 */
    106 	if (stat(path, &st) == -1)
    107 		goto bad;
    108 
    109 	(void)unlink(path);
    110 
    111 	/*
    112 	 * If the number of links was two (one for the unique file and one
    113 	 * for the lock), we've won the race
    114 	 */
    115 	if (st.st_nlink != 2) {
    116 		errno = EEXIST;
    117 		return -1;
    118 	}
    119 	return 0;
    120 
    121 bad:
    122 	serrno = errno;
    123 	(void)unlink(path);
    124 	errno = serrno;
    125 	return -1;
    126 }
    127 
    128 /*
    129  * fname -- Pathname to lock
    130  * pollinterval -- Interval to check for lock, -1 return
    131  * fp -- File to print message
    132  * msg -- Message to print
    133  */
    134 PUBLIC int
    135 dot_lock(const char *fname, int pollinterval, FILE *fp, const char *msg)
    136 {
    137 	char path[MAXPATHLEN];
    138 	sigset_t nset, oset;
    139 	int retval;
    140 
    141 	(void)sigemptyset(&nset);
    142 	(void)sigaddset(&nset, SIGHUP);
    143 	(void)sigaddset(&nset, SIGINT);
    144 	(void)sigaddset(&nset, SIGQUIT);
    145 	(void)sigaddset(&nset, SIGTERM);
    146 	(void)sigaddset(&nset, SIGTTIN);
    147 	(void)sigaddset(&nset, SIGTTOU);
    148 	(void)sigaddset(&nset, SIGTSTP);
    149 	(void)sigaddset(&nset, SIGCHLD);
    150 
    151 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
    152 
    153 	retval = -1;
    154 	for (;;) {
    155 		sig_check();
    156 		(void)sigprocmask(SIG_BLOCK, &nset, &oset);
    157 		if (create_exclusive(path) != -1) {
    158 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    159 			retval = 0;
    160 			break;
    161 		}
    162 		else
    163 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    164 
    165 		if (errno != EEXIST)
    166 			break;
    167 
    168 		if (fp && msg)
    169 		    (void)fputs(msg, fp);
    170 
    171 		if (pollinterval) {
    172 			if (pollinterval == -1) {
    173 				errno = EEXIST;
    174 				break;
    175 			}
    176 			(void)sleep((unsigned int)pollinterval);
    177 		}
    178 	}
    179 	sig_check();
    180 	return retval;
    181 }
    182 
    183 PUBLIC void
    184 dot_unlock(const char *fname)
    185 {
    186 	char path[MAXPATHLEN];
    187 
    188 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
    189 	(void)unlink(path);
    190 }
    191