xref: /onnv-gate/usr/src/lib/libc/port/gen/lckpwdf.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <signal.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <shadow.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <thread.h>
410Sstevel@tonic-gate #include "mtlib.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	LOCKFILE	"/etc/.pwd.lock"
440Sstevel@tonic-gate #define	S_WAITTIME	15
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static struct flock flock =	{
470Sstevel@tonic-gate 			0,	/* l_type */
480Sstevel@tonic-gate 			0,	/* l_whence */
490Sstevel@tonic-gate 			0,	/* l_start */
500Sstevel@tonic-gate 			0,	/* l_len */
510Sstevel@tonic-gate 			0,	/* l_sysid */
520Sstevel@tonic-gate 			0	/* l_pid */
530Sstevel@tonic-gate 			};
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * lckpwdf() returns a 0 for a successful lock within W_WAITTIME
570Sstevel@tonic-gate  * seconds and -1 otherwise.  We stand on our head to make it MT-safe.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static pid_t lck_pid = 0;	/* process's pid at last lock */
610Sstevel@tonic-gate static thread_t lck_tid = 0;	/* thread that holds the lock */
620Sstevel@tonic-gate static int fildes = -1;
630Sstevel@tonic-gate static mutex_t lck_lock = DEFAULTMUTEX;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int
lckpwdf(void)660Sstevel@tonic-gate lckpwdf(void)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	int seconds = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	lmutex_lock(&lck_lock);
710Sstevel@tonic-gate 	for (;;) {
720Sstevel@tonic-gate 		if (lck_pid != 0 && lck_pid != getpid()) {
730Sstevel@tonic-gate 			/* somebody forked */
740Sstevel@tonic-gate 			lck_pid = 0;
750Sstevel@tonic-gate 			lck_tid = 0;
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 		if (lck_tid == 0) {
780Sstevel@tonic-gate 			if ((fildes = creat(LOCKFILE, 0600)) == -1)
790Sstevel@tonic-gate 				break;
800Sstevel@tonic-gate 			flock.l_type = F_WRLCK;
810Sstevel@tonic-gate 			if (fcntl(fildes, F_SETLK, &flock) != -1) {
820Sstevel@tonic-gate 				lck_pid = getpid();
830Sstevel@tonic-gate 				lck_tid = thr_self();
840Sstevel@tonic-gate 				lmutex_unlock(&lck_lock);
850Sstevel@tonic-gate 				return (0);
860Sstevel@tonic-gate 			}
870Sstevel@tonic-gate 			(void) close(fildes);
880Sstevel@tonic-gate 			fildes = -1;
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 		if (seconds++ >= S_WAITTIME) {
910Sstevel@tonic-gate 			/*
920Sstevel@tonic-gate 			 * For compatibility with the past, pretend
930Sstevel@tonic-gate 			 * that we were interrupted by SIGALRM.
940Sstevel@tonic-gate 			 */
950Sstevel@tonic-gate 			errno = EINTR;
960Sstevel@tonic-gate 			break;
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		lmutex_unlock(&lck_lock);
990Sstevel@tonic-gate 		(void) sleep(1);
1000Sstevel@tonic-gate 		lmutex_lock(&lck_lock);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	lmutex_unlock(&lck_lock);
1030Sstevel@tonic-gate 	return (-1);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * ulckpwdf() returns 0 for a successful unlock and -1 otherwise
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate int
ulckpwdf(void)1100Sstevel@tonic-gate ulckpwdf(void)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	lmutex_lock(&lck_lock);
1130Sstevel@tonic-gate 	if (lck_tid == thr_self() && fildes >= 0) {
1140Sstevel@tonic-gate 		flock.l_type = F_UNLCK;
1150Sstevel@tonic-gate 		(void) fcntl(fildes, F_SETLK, &flock);
1160Sstevel@tonic-gate 		(void) close(fildes);
1170Sstevel@tonic-gate 		fildes = -1;
1180Sstevel@tonic-gate 		lck_pid = 0;
1190Sstevel@tonic-gate 		lck_tid = 0;
1200Sstevel@tonic-gate 		lmutex_unlock(&lck_lock);
1210Sstevel@tonic-gate 		return (0);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	lmutex_unlock(&lck_lock);
1240Sstevel@tonic-gate 	return (-1);
1250Sstevel@tonic-gate }
126