xref: /titanic_52/usr/src/lib/libc/port/gen/lckpwdf.c (revision 10db1377dafab8ba3feedef26db9c5d8539a5cd1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #pragma weak lckpwdf = _lckpwdf
34 #pragma weak ulckpwdf = _ulckpwdf
35 
36 #include "synonyms.h"
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <signal.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <shadow.h>
43 #include <errno.h>
44 #include <thread.h>
45 #include "mtlib.h"
46 
47 #define	LOCKFILE	"/etc/.pwd.lock"
48 #define	S_WAITTIME	15
49 
50 static struct flock flock =	{
51 			0,	/* l_type */
52 			0,	/* l_whence */
53 			0,	/* l_start */
54 			0,	/* l_len */
55 			0,	/* l_sysid */
56 			0	/* l_pid */
57 			};
58 
59 /*
60  * lckpwdf() returns a 0 for a successful lock within W_WAITTIME
61  * seconds and -1 otherwise.  We stand on our head to make it MT-safe.
62  */
63 
64 static pid_t lck_pid = 0;	/* process's pid at last lock */
65 static thread_t lck_tid = 0;	/* thread that holds the lock */
66 static int fildes = -1;
67 static mutex_t lck_lock = DEFAULTMUTEX;
68 
69 int
70 lckpwdf(void)
71 {
72 	int seconds = 0;
73 
74 	lmutex_lock(&lck_lock);
75 	for (;;) {
76 		if (lck_pid != 0 && lck_pid != getpid()) {
77 			/* somebody forked */
78 			lck_pid = 0;
79 			lck_tid = 0;
80 		}
81 		if (lck_tid == 0) {
82 			if ((fildes = creat(LOCKFILE, 0600)) == -1)
83 				break;
84 			flock.l_type = F_WRLCK;
85 			if (fcntl(fildes, F_SETLK, &flock) != -1) {
86 				lck_pid = getpid();
87 				lck_tid = thr_self();
88 				lmutex_unlock(&lck_lock);
89 				return (0);
90 			}
91 			(void) close(fildes);
92 			fildes = -1;
93 		}
94 		if (seconds++ >= S_WAITTIME) {
95 			/*
96 			 * For compatibility with the past, pretend
97 			 * that we were interrupted by SIGALRM.
98 			 */
99 			errno = EINTR;
100 			break;
101 		}
102 		lmutex_unlock(&lck_lock);
103 		(void) sleep(1);
104 		lmutex_lock(&lck_lock);
105 	}
106 	lmutex_unlock(&lck_lock);
107 	return (-1);
108 }
109 
110 /*
111  * ulckpwdf() returns 0 for a successful unlock and -1 otherwise
112  */
113 int
114 ulckpwdf(void)
115 {
116 	lmutex_lock(&lck_lock);
117 	if (lck_tid == thr_self() && fildes >= 0) {
118 		flock.l_type = F_UNLCK;
119 		(void) fcntl(fildes, F_SETLK, &flock);
120 		(void) close(fildes);
121 		fildes = -1;
122 		lck_pid = 0;
123 		lck_tid = 0;
124 		lmutex_unlock(&lck_lock);
125 		return (0);
126 	}
127 	lmutex_unlock(&lck_lock);
128 	return (-1);
129 }
130