Home | History | Annotate | Line # | Download | only in mail
dotlock.c revision 1.9
      1 /*	$NetBSD: dotlock.c,v 1.9 2007/10/29 23:20:38 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  * 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.9 2007/10/29 23:20:38 christos 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(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(const char *fname)
     57 {
     58 	char path[MAXPATHLEN], hostname[MAXHOSTNAMELEN + 1];
     59 	const char *ptr;
     60 	struct timeval tv;
     61 	pid_t pid;
     62 	size_t ntries, cookie;
     63 	int fd, serrno;
     64 	struct stat st;
     65 
     66 	(void)gettimeofday(&tv, NULL);
     67 	(void)gethostname(hostname, sizeof(hostname));
     68 	hostname[sizeof(hostname) - 1] = '\0';
     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 /*
    132  * fname -- Pathname to lock
    133  * pollinterval -- Interval to check for lock, -1 return
    134  * fp -- File to print message
    135  * msg -- Message to print
    136  */
    137 PUBLIC int
    138 dot_lock(const char *fname, int pollinterval, FILE *fp, const char *msg)
    139 {
    140 	char path[MAXPATHLEN];
    141 	sigset_t nset, oset;
    142 
    143 	(void)sigemptyset(&nset);
    144 	(void)sigaddset(&nset, SIGHUP);
    145 	(void)sigaddset(&nset, SIGINT);
    146 	(void)sigaddset(&nset, SIGQUIT);
    147 	(void)sigaddset(&nset, SIGTERM);
    148 	(void)sigaddset(&nset, SIGTTIN);
    149 	(void)sigaddset(&nset, SIGTTOU);
    150 	(void)sigaddset(&nset, SIGTSTP);
    151 	(void)sigaddset(&nset, SIGCHLD);
    152 
    153 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
    154 
    155 	for (;;) {
    156 		(void)sigprocmask(SIG_BLOCK, &nset, &oset);
    157 		if (create_exclusive(path) != -1) {
    158 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    159 			return 0;
    160 		}
    161 		else
    162 			(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    163 
    164 		if (errno != EEXIST)
    165 			return -1;
    166 
    167 		if (fp && msg)
    168 		    (void)fputs(msg, fp);
    169 
    170 		if (pollinterval) {
    171 			if (pollinterval == -1) {
    172 				errno = EEXIST;
    173 				return -1;
    174 			}
    175 			(void)sleep((unsigned int)pollinterval);
    176 		}
    177 	}
    178 }
    179 
    180 PUBLIC void
    181 dot_unlock(const char *fname)
    182 {
    183 	char path[MAXPATHLEN];
    184 
    185 	(void)snprintf(path, sizeof(path), "%s.lock", fname);
    186 	(void)unlink(path);
    187 }
    188