1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <unistd.h>
30*0Sstevel@tonic-gate #include <syslog.h>
31*0Sstevel@tonic-gate #include <sys/mman.h>
32*0Sstevel@tonic-gate #include <thread.h>
33*0Sstevel@tonic-gate #include <synch.h>
34*0Sstevel@tonic-gate #include <ndbm.h>
35*0Sstevel@tonic-gate #include "../ypsym.h"
36*0Sstevel@tonic-gate #include "../ypdefs.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  *  These routines provide mutual exclusion between ypserv and ypxfr.
40*0Sstevel@tonic-gate  *  Mutual exclusion is needed so that ypxfr doesn't try to rename
41*0Sstevel@tonic-gate  *  dbm files while ypserv is trying to open them.  After ypserv has
42*0Sstevel@tonic-gate  *  opened a dbm file, it is safe to rename it because ypserv still
43*0Sstevel@tonic-gate  *  has access to the file through its file descriptor.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	LOCKFILE "/var/run/yp_maplock"
47*0Sstevel@tonic-gate struct lockarray {
48*0Sstevel@tonic-gate 	mutex_t		locknode[MAXHASH];
49*0Sstevel@tonic-gate };
50*0Sstevel@tonic-gate typedef struct lockarray lockarray;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * Cross-process robust mutex locks.
54*0Sstevel@tonic-gate  * Provide synchronization between YP processes
55*0Sstevel@tonic-gate  * by implementing an exclusive locking mechanism
56*0Sstevel@tonic-gate  * via a memory-mapped file.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate static struct lockarray	*shmlockarray;
59*0Sstevel@tonic-gate static int	lockfile;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate int
62*0Sstevel@tonic-gate hash(char *s)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	int n = 0;
65*0Sstevel@tonic-gate 	int i;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	for (i = 1; *s; i += 10, s++) {
68*0Sstevel@tonic-gate 		n += i * (*s);
69*0Sstevel@tonic-gate 	}
70*0Sstevel@tonic-gate 	n %= MAXHASH;
71*0Sstevel@tonic-gate 	return (n);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate bool
75*0Sstevel@tonic-gate init_locks_mem()
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	int iiter, rc;
78*0Sstevel@tonic-gate 	int ebusy_cnt = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/*
81*0Sstevel@tonic-gate 	 * Initialize cross-process locks in memory-mapped file.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	for (iiter = 0; iiter < MAXHASH; iiter++) {
84*0Sstevel@tonic-gate 		if (rc = mutex_init(&(shmlockarray->locknode[iiter]),
85*0Sstevel@tonic-gate 		    USYNC_PROCESS_ROBUST, 0)) {
86*0Sstevel@tonic-gate 			if (rc == EBUSY) {
87*0Sstevel@tonic-gate 				ebusy_cnt++;
88*0Sstevel@tonic-gate 			} else {
89*0Sstevel@tonic-gate 				syslog(LOG_ERR,
90*0Sstevel@tonic-gate 				    "init_locks_mem():mutex_init():error=%d",
91*0Sstevel@tonic-gate 				    rc);
92*0Sstevel@tonic-gate 				return (FALSE);
93*0Sstevel@tonic-gate 			}
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/*
98*0Sstevel@tonic-gate 	 * EBUSY for all locks OK, it means another process
99*0Sstevel@tonic-gate 	 * has already initialized locks.
100*0Sstevel@tonic-gate 	 */
101*0Sstevel@tonic-gate 	if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) {
102*0Sstevel@tonic-gate 		syslog(LOG_ERR,
103*0Sstevel@tonic-gate 		    "%s inconsistent. Remove and restart NIS (YP).", LOCKFILE);
104*0Sstevel@tonic-gate 		return (FALSE);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 	return (TRUE);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate bool
110*0Sstevel@tonic-gate init_lock_map()
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	char buff[ sizeof (lockarray) ];
113*0Sstevel@tonic-gate 	int write_cnt, lf_size;
114*0Sstevel@tonic-gate 	struct stat fdata;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/*
117*0Sstevel@tonic-gate 	 * Locking file initialization algorithm, with recovery mechanism.
118*0Sstevel@tonic-gate 	 * This mechanism has been devised to ensure proper creation
119*0Sstevel@tonic-gate 	 * of a memory-mapped lock file containing mutexes for robust,
120*0Sstevel@tonic-gate 	 * inter-process communication.
121*0Sstevel@tonic-gate 	 * File name is /var/run/yp_maplock (LOCKFILE).  It might or might
122*0Sstevel@tonic-gate 	 * not exist.
123*0Sstevel@tonic-gate 	 *
124*0Sstevel@tonic-gate 	 * Algorithm:
125*0Sstevel@tonic-gate 	 * Try to open the file. If file doesn't exist, or size is too small,
126*0Sstevel@tonic-gate 	 * create/rewrite the file, m-map it into memory and initialize the
127*0Sstevel@tonic-gate 	 * mutexes in it.
128*0Sstevel@tonic-gate 	 * If file exists and size is at least large enough, assume it's a
129*0Sstevel@tonic-gate 	 * good file, and m-map the lock structure directly to it.
130*0Sstevel@tonic-gate 	 *
131*0Sstevel@tonic-gate 	 * Recovery from inconsistent state is easy - simply delete the file
132*0Sstevel@tonic-gate 	 * and restart NIS (YP).
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600);
136*0Sstevel@tonic-gate 	if (lockfile != -1) {
137*0Sstevel@tonic-gate 		if (lockf(lockfile, F_LOCK, 0) == 0) {
138*0Sstevel@tonic-gate 			if (fstat(lockfile, &fdata) == 0) {
139*0Sstevel@tonic-gate 				lf_size = fdata.st_size;
140*0Sstevel@tonic-gate 				if (lf_size < sizeof (lockarray)) {
141*0Sstevel@tonic-gate 					bzero(buff, sizeof (buff));
142*0Sstevel@tonic-gate 					if ((write_cnt = write(lockfile, buff,
143*0Sstevel@tonic-gate 					    sizeof (buff)) != sizeof (buff))) {
144*0Sstevel@tonic-gate 						if (write_cnt < 0) {
145*0Sstevel@tonic-gate 							syslog(LOG_ERR,
146*0Sstevel@tonic-gate 						    "write(%s) => errno=%d",
147*0Sstevel@tonic-gate 							    LOCKFILE, errno);
148*0Sstevel@tonic-gate 						} else {
149*0Sstevel@tonic-gate 							syslog(LOG_ERR,
150*0Sstevel@tonic-gate 		    "write(%s) => %d!=%d: wrong number of bytes written.",
151*0Sstevel@tonic-gate 							    LOCKFILE,
152*0Sstevel@tonic-gate 							    write_cnt,
153*0Sstevel@tonic-gate 							    sizeof (buff));
154*0Sstevel@tonic-gate 						}
155*0Sstevel@tonic-gate 						lockf(lockfile, F_ULOCK, 0);
156*0Sstevel@tonic-gate 						close(lockfile);
157*0Sstevel@tonic-gate 						return (FALSE);
158*0Sstevel@tonic-gate 					}
159*0Sstevel@tonic-gate 				}
160*0Sstevel@tonic-gate 			} else {
161*0Sstevel@tonic-gate 				syslog(LOG_ERR,
162*0Sstevel@tonic-gate 				    "fstat(%s) => errno=%d", LOCKFILE, errno);
163*0Sstevel@tonic-gate 				lockf(lockfile, F_ULOCK, 0);
164*0Sstevel@tonic-gate 				close(lockfile);
165*0Sstevel@tonic-gate 				return (FALSE);
166*0Sstevel@tonic-gate 			}
167*0Sstevel@tonic-gate 		} else {
168*0Sstevel@tonic-gate 			syslog(LOG_ERR,
169*0Sstevel@tonic-gate 			    "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno);
170*0Sstevel@tonic-gate 			close(lockfile);
171*0Sstevel@tonic-gate 			return (FALSE);
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	} else {
174*0Sstevel@tonic-gate 		syslog(LOG_ERR,
175*0Sstevel@tonic-gate 		    "open(%s) => errno=%d", LOCKFILE, errno);
176*0Sstevel@tonic-gate 		return (FALSE);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	/*
180*0Sstevel@tonic-gate 	 * File exists with correct size, is open, and we're holding
181*0Sstevel@tonic-gate 	 * the file lock.
182*0Sstevel@tonic-gate 	 */
183*0Sstevel@tonic-gate 	shmlockarray = (lockarray *)mmap((caddr_t) 0, sizeof (lockarray),
184*0Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0);
185*0Sstevel@tonic-gate 	if (shmlockarray == MAP_FAILED) {
186*0Sstevel@tonic-gate 		syslog(LOG_ERR, "mmap(%s) => errno=%d", LOCKFILE, errno);
187*0Sstevel@tonic-gate 		lockf(lockfile, F_ULOCK, 0);
188*0Sstevel@tonic-gate 		close(lockfile);
189*0Sstevel@tonic-gate 		return (FALSE);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/*
193*0Sstevel@tonic-gate 	 * If we wrote zeroes to the file, we also need to initialize
194*0Sstevel@tonic-gate 	 * the mutex locks.
195*0Sstevel@tonic-gate 	 */
196*0Sstevel@tonic-gate 	if (lf_size < sizeof (lockarray)) {
197*0Sstevel@tonic-gate 		if (init_locks_mem() == FALSE) {
198*0Sstevel@tonic-gate 			lockf(lockfile, F_ULOCK, 0);
199*0Sstevel@tonic-gate 			close(lockfile);
200*0Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
201*0Sstevel@tonic-gate 				syslog(LOG_ERR,
202*0Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
203*0Sstevel@tonic-gate 				    LOCKFILE, errno);
204*0Sstevel@tonic-gate 			}
205*0Sstevel@tonic-gate 			return (FALSE);
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (lockf(lockfile, F_ULOCK, 0) != 0) {
210*0Sstevel@tonic-gate 		syslog(LOG_ERR,
211*0Sstevel@tonic-gate 		    "lockf(%s,F_ULOCK) => errno=%d",
212*0Sstevel@tonic-gate 		    LOCKFILE, errno);
213*0Sstevel@tonic-gate 		close(lockfile);
214*0Sstevel@tonic-gate 		return (FALSE);
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (close(lockfile) == 0) {
218*0Sstevel@tonic-gate 		return (TRUE);
219*0Sstevel@tonic-gate 	} else {
220*0Sstevel@tonic-gate 		syslog(LOG_ERR,
221*0Sstevel@tonic-gate 		    "close(%s) => errno=%d", LOCKFILE, errno);
222*0Sstevel@tonic-gate 		return (FALSE);
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate  * FUNCTION : 	lock_map()
228*0Sstevel@tonic-gate  *
229*0Sstevel@tonic-gate  * DESCRIPTION: Front end to the lock routine taking map name as argument.
230*0Sstevel@tonic-gate  *
231*0Sstevel@tonic-gate  * GIVEN :	Map name.
232*0Sstevel@tonic-gate  *
233*0Sstevel@tonic-gate  * RETURNS :	Same as lock_core
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate int
236*0Sstevel@tonic-gate lock_map(char *mapname)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	int hashval;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	hashval = hash(mapname);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	return( lock_core(hashval));
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate /*
246*0Sstevel@tonic-gate  * FUNCTION : 	lock_core()
247*0Sstevel@tonic-gate  *
248*0Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
249*0Sstevel@tonic-gate  *
250*0Sstevel@tonic-gate  * GIVEN :	Map hash value
251*0Sstevel@tonic-gate  *
252*0Sstevel@tonic-gate  * RETURNS :	0 = Failure
253*0Sstevel@tonic-gate  *		1 = Success
254*0Sstevel@tonic-gate  */
255*0Sstevel@tonic-gate int
256*0Sstevel@tonic-gate lock_core(int hashval)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	int rc;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/*
261*0Sstevel@tonic-gate 	 *Robust, cross-process lock implementation
262*0Sstevel@tonic-gate 	 */
263*0Sstevel@tonic-gate 	rc = mutex_lock(&(shmlockarray->locknode[hashval]));
264*0Sstevel@tonic-gate 	while (rc != 0) {
265*0Sstevel@tonic-gate 		switch (rc) {
266*0Sstevel@tonic-gate 		case EOWNERDEAD:
267*0Sstevel@tonic-gate 			/*
268*0Sstevel@tonic-gate 			 * Previows lock owner died, resetting lock
269*0Sstevel@tonic-gate 			 * to recover from error.
270*0Sstevel@tonic-gate 			 */
271*0Sstevel@tonic-gate 			rc = mutex_init(&(shmlockarray->locknode[hashval]),
272*0Sstevel@tonic-gate 			    USYNC_PROCESS_ROBUST, 0);
273*0Sstevel@tonic-gate 			if (rc != 0) {
274*0Sstevel@tonic-gate 				syslog(LOG_ERR,
275*0Sstevel@tonic-gate 				    "mutex_init(): error=%d", rc);
276*0Sstevel@tonic-gate 				return (0);
277*0Sstevel@tonic-gate 			}
278*0Sstevel@tonic-gate 			rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
279*0Sstevel@tonic-gate 			if (rc != 0) {
280*0Sstevel@tonic-gate 				syslog(LOG_ERR,
281*0Sstevel@tonic-gate 				    "mutex_unlock(): error=%d", rc);
282*0Sstevel@tonic-gate 				return (0);
283*0Sstevel@tonic-gate 			}
284*0Sstevel@tonic-gate 			break;
285*0Sstevel@tonic-gate 		default:
286*0Sstevel@tonic-gate 			/*
287*0Sstevel@tonic-gate 			 * Unrecoverable problem - nothing to do
288*0Sstevel@tonic-gate 			 * but exit YP and delete lock file.
289*0Sstevel@tonic-gate 			 */
290*0Sstevel@tonic-gate 			syslog(LOG_ERR,
291*0Sstevel@tonic-gate 			    "mutex_lock(): error=%d", rc);
292*0Sstevel@tonic-gate 			syslog(LOG_ERR,
293*0Sstevel@tonic-gate 			    "Please restart NIS (ypstop/ypstart).");
294*0Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
295*0Sstevel@tonic-gate 				syslog(LOG_ERR,
296*0Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
297*0Sstevel@tonic-gate 				    LOCKFILE, errno);
298*0Sstevel@tonic-gate 			}
299*0Sstevel@tonic-gate 			return (0);
300*0Sstevel@tonic-gate 		}
301*0Sstevel@tonic-gate 		rc = mutex_lock(&(shmlockarray->locknode[hashval]));
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	/* Success */
305*0Sstevel@tonic-gate 	return (1);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate  * FUNCTION : 	unlock_map()
311*0Sstevel@tonic-gate  *
312*0Sstevel@tonic-gate  * DESCRIPTION: Front end to the unlock routine taking map name as argument.
313*0Sstevel@tonic-gate  *
314*0Sstevel@tonic-gate  * GIVEN :	Map name.
315*0Sstevel@tonic-gate  *
316*0Sstevel@tonic-gate  * RETURNS :	Same as unlock_core
317*0Sstevel@tonic-gate  */
318*0Sstevel@tonic-gate int
319*0Sstevel@tonic-gate unlock_map(char *mapname)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	int hashval;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	hashval = hash(mapname);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	return( unlock_core( hashval ));
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate /*
329*0Sstevel@tonic-gate  * FUNCTION : 	unlock_core()
330*0Sstevel@tonic-gate  *
331*0Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
332*0Sstevel@tonic-gate  *
333*0Sstevel@tonic-gate  * GIVEN :	Map hash value
334*0Sstevel@tonic-gate  *
335*0Sstevel@tonic-gate  * RETURNS :	0 = Failure
336*0Sstevel@tonic-gate  *		1 = Success
337*0Sstevel@tonic-gate  */
338*0Sstevel@tonic-gate int
339*0Sstevel@tonic-gate unlock_core(int hashval)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	int rc;
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
344*0Sstevel@tonic-gate 	if (rc != 0) {
345*0Sstevel@tonic-gate 		syslog(LOG_ERR,
346*0Sstevel@tonic-gate 		    "mutex_unlock(): error=%d", rc);
347*0Sstevel@tonic-gate 		syslog(LOG_ERR,
348*0Sstevel@tonic-gate 		    "Please restart NIS (ypstop/ypstart).");
349*0Sstevel@tonic-gate 		if (remove(LOCKFILE) != 0) {
350*0Sstevel@tonic-gate 			syslog(LOG_ERR,
351*0Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
352*0Sstevel@tonic-gate 			    LOCKFILE, errno);
353*0Sstevel@tonic-gate 		}
354*0Sstevel@tonic-gate 		return (0);
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/* Success */
358*0Sstevel@tonic-gate 	return (1);
359*0Sstevel@tonic-gate }
360