xref: /illumos-gate/usr/src/cmd/fs.d/nfs/statd/sm_proc.c (revision 7c478bd9)
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 2000 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <rpc/rpc.h>
49 #include <rpcsvc/sm_inter.h>
50 #include <rpcsvc/nsm_addr.h>
51 #include <memory.h>
52 #include <net/if.h>
53 #include <sys/sockio.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <netdb.h>
58 #include <netdir.h>
59 #include <synch.h>
60 #include <thread.h>
61 #include <assert.h>
62 #include "sm_statd.h"
63 
64 static int local_state;		/* fake local sm state */
65 				/* client name-to-address translation table */
66 static name_addr_entry_t *name_addr = NULL;
67 
68 
69 #define	LOGHOST "loghost"
70 
71 static void delete_mon(char *mon_name, my_id *my_idp);
72 static void insert_mon(mon *monp);
73 static void pr_mon(char *);
74 static int statd_call_lockd(mon *monp, int state);
75 static int hostname_eq(char *host1, char *host2);
76 static char *get_system_id(char *hostname);
77 static void add_aliases(struct hostent *phost);
78 static void *thr_send_notice(void *);
79 static void delete_onemon(char *mon_name, my_id *my_idp,
80 				mon_entry **monitor_q);
81 static void send_notice(char *mon_name, int state);
82 static void add_to_host_array(char *host);
83 static int in_host_array(char *host);
84 static void pr_name_addr(name_addr_entry_t *name_addr);
85 
86 extern int self_check(char *hostname);
87 extern struct lifconf *getmyaddrs(void);
88 
89 /* ARGSUSED */
90 void
91 sm_status(namep, resp)
92 	sm_name *namep;
93 	sm_stat_res *resp;
94 {
95 
96 	if (debug)
97 		(void) printf("proc sm_stat: mon_name = %s\n",
98 				namep->mon_name);
99 
100 	resp->res_stat = stat_succ;
101 	resp->state = LOCAL_STATE;
102 }
103 
104 /* ARGSUSED */
105 void
106 sm_mon(monp, resp)
107 	mon *monp;
108 	sm_stat_res *resp;
109 {
110 	mon_id *monidp;
111 	monidp = &monp->mon_id;
112 
113 	rw_rdlock(&thr_rwlock);
114 	if (debug) {
115 		(void) printf("proc sm_mon: mon_name = %s, id = %d\n",
116 		monidp->mon_name, * ((int *)monp->priv));
117 		pr_mon(monp->mon_id.mon_name);
118 	}
119 
120 	/* only monitor other hosts */
121 	if (self_check(monp->mon_id.mon_name) == 0) {
122 		/* store monitor request into monitor_q */
123 		insert_mon(monp);
124 	}
125 
126 	pr_mon(monp->mon_id.mon_name);
127 	resp->res_stat = stat_succ;
128 	resp->state = local_state;
129 	rw_unlock(&thr_rwlock);
130 }
131 
132 /* ARGSUSED */
133 void
134 sm_unmon(monidp, resp)
135 	mon_id *monidp;
136 	sm_stat *resp;
137 {
138 	rw_rdlock(&thr_rwlock);
139 	if (debug) {
140 		(void) printf(
141 			"proc sm_unmon: mon_name = %s, [%s, %d, %d, %d]\n",
142 			monidp->mon_name, monidp->my_id.my_name,
143 			monidp->my_id.my_prog, monidp->my_id.my_vers,
144 			monidp->my_id.my_proc);
145 		pr_mon(monidp->mon_name);
146 	}
147 
148 	delete_mon(monidp->mon_name, &monidp->my_id);
149 	pr_mon(monidp->mon_name);
150 	resp->state = local_state;
151 	rw_unlock(&thr_rwlock);
152 }
153 
154 /* ARGSUSED */
155 void
156 sm_unmon_all(myidp, resp)
157 	my_id *myidp;
158 	sm_stat *resp;
159 {
160 	rw_rdlock(&thr_rwlock);
161 	if (debug)
162 		(void) printf("proc sm_unmon_all: [%s, %d, %d, %d]\n",
163 		myidp->my_name,
164 		myidp->my_prog, myidp->my_vers,
165 		myidp->my_proc);
166 	delete_mon((char *)NULL, myidp);
167 	pr_mon(NULL);
168 	resp->state = local_state;
169 	rw_unlock(&thr_rwlock);
170 }
171 
172 /*
173  * Notifies lockd specified by name that state has changed for this server.
174  */
175 void
176 sm_notify(ntfp)
177 	stat_chge *ntfp;
178 {
179 	rw_rdlock(&thr_rwlock);
180 	if (debug)
181 		(void) printf("sm_notify: %s state =%d\n",
182 			ntfp->mon_name, ntfp->state);
183 	send_notice(ntfp->mon_name, ntfp->state);
184 	rw_unlock(&thr_rwlock);
185 }
186 
187 /* ARGSUSED */
188 void
189 sm_simu_crash(myidp)
190 	void *myidp;
191 {
192 	int i;
193 	struct mon_entry *monitor_q;
194 	int found = 0;
195 
196 	/* Only one crash should be running at a time. */
197 	mutex_lock(&crash_lock);
198 	if (debug)
199 		(void) printf("proc sm_simu_crash\n");
200 	if (in_crash) {
201 		cond_wait(&crash_finish, &crash_lock);
202 		mutex_unlock(&crash_lock);
203 		return;
204 	} else {
205 		in_crash = 1;
206 	}
207 	mutex_unlock(&crash_lock);
208 
209 	for (i = 0; i < MAX_HASHSIZE; i++) {
210 		mutex_lock(&mon_table[i].lock);
211 		monitor_q = mon_table[i].sm_monhdp;
212 		if (monitor_q != (struct mon_entry *)NULL) {
213 			mutex_unlock(&mon_table[i].lock);
214 			found = 1;
215 			break;
216 		}
217 		mutex_unlock(&mon_table[i].lock);
218 	}
219 	/*
220 	 * If there are entries found in the monitor table,
221 	 * initiate a crash, else zero out the in_crash variable.
222 	 */
223 	if (found) {
224 		mutex_lock(&crash_lock);
225 		die = 1;
226 		/* Signal sm_retry() thread if sleeping. */
227 		cond_signal(&retrywait);
228 		mutex_unlock(&crash_lock);
229 		rw_wrlock(&thr_rwlock);
230 		sm_crash();
231 		rw_unlock(&thr_rwlock);
232 	} else {
233 		mutex_lock(&crash_lock);
234 		in_crash = 0;
235 		mutex_unlock(&crash_lock);
236 	}
237 }
238 
239 /* ARGSUSED */
240 void
241 nsmaddrproc1_reg(regargs, regresp)
242 	reg1args *regargs;
243 	reg1res  *regresp;
244 {
245 	nsm_addr_res status;
246 	name_addr_entry_t *entry;
247 	char *tmp_n_bytes;
248 	addr_entry_t *addr;
249 
250 	rw_rdlock(&thr_rwlock);
251 	if (debug) {
252 		int i;
253 
254 		(void) printf("nap1_reg: fam= %d, name= %s, len= %d\n",
255 				regargs->family,
256 				regargs->name,
257 				regargs->address.n_len);
258 		(void) printf("address is: ");
259 		for (i = 0; i < regargs->address.n_len; i++) {
260 			(void) printf("%d.",
261 				(unsigned char)regargs->address.n_bytes[i]);
262 		}
263 		(void) printf("\n");
264 	}
265 
266 	/*
267 	 * Locate the entry with the name in the NSM_ADDR_REG request if
268 	 * it exists.  If it doesn't, create a new entry to hold this name.
269 	 * The first time through this code, name_addr starts out as NULL.
270 	 */
271 	mutex_lock(&name_addrlock);
272 	for (entry = name_addr; entry; entry = entry->next) {
273 		if (strcmp(regargs->name, entry->name) == 0) {
274 			if (debug) {
275 				(void) printf("nap1_reg: matched name %s\n",
276 						entry->name);
277 			}
278 			break;
279 		}
280 	}
281 
282 	if (entry == NULL) {
283 		entry = (name_addr_entry_t *)malloc(sizeof (*entry));
284 		if (entry == NULL) {
285 			if (debug) {
286 				(void) printf(
287 				"nsmaddrproc1_reg: no memory for entry\n");
288 			}
289 			status = nsm_addr_fail;
290 			goto done;
291 		}
292 
293 		entry->name = strdup(regargs->name);
294 		if (entry->name == NULL) {
295 			if (debug) {
296 				(void) printf(
297 				"nsmaddrproc1_reg: no memory for name\n");
298 			}
299 			free(entry);
300 			status = nsm_addr_fail;
301 			goto done;
302 		}
303 		entry->addresses = NULL;
304 
305 		/*
306 		 * Link the new entry onto the *head* of the name_addr
307 		 * table.
308 		 *
309 		 * Note: there is code below in the address maintenance
310 		 * section that assumes this behavior.
311 		 */
312 		entry->next = name_addr;
313 		name_addr = entry;
314 	}
315 
316 	/*
317 	 * Try to match the address in the request; if it doesn't match,
318 	 * add it to the entry's address list.
319 	 */
320 	for (addr = entry->addresses; addr; addr = addr->next) {
321 		if (addr->family == (sa_family_t)regargs->family &&
322 		    addr->ah.n_len == regargs->address.n_len &&
323 		    memcmp(addr->ah.n_bytes, regargs->address.n_bytes,
324 			addr->ah.n_len) == 0) {
325 			if (debug) {
326 				int i;
327 
328 				(void) printf("nap1_reg: matched addr ");
329 				for (i = 0; i < addr->ah.n_len; i++) {
330 					(void) printf("%d.",
331 					(unsigned char)addr->ah.n_bytes[i]);
332 				}
333 				(void) printf(" family %d for name %s\n",
334 						addr->family,
335 						entry->name);
336 			}
337 			break;
338 		}
339 	}
340 
341 	if (addr == NULL) {
342 		addr = (addr_entry_t *)malloc(sizeof (*addr));
343 		tmp_n_bytes = (char *)malloc(regargs->address.n_len);
344 		if (addr == NULL || tmp_n_bytes == NULL) {
345 			if (debug) {
346 				(void) printf(
347 					"nap1_reg: no memory for addr\n");
348 			}
349 
350 			/*
351 			 * If this name entry was just newly made in the
352 			 * table, back it out now that we can't register
353 			 * an address with it anyway.
354 			 *
355 			 * Note: we are making an assumption about how
356 			 * names are added to (the head of) name_addr here.
357 			 */
358 			if (entry == name_addr && entry->addresses == NULL) {
359 				name_addr = name_addr->next;
360 				free(entry->name);
361 				free(entry);
362 				if (tmp_n_bytes)
363 					free(tmp_n_bytes);
364 				if (addr)
365 					free(addr);
366 				status = nsm_addr_fail;
367 				goto done;
368 			}
369 		}
370 
371 		/*
372 		 * Note:  this check for address family assumes that we
373 		 *	  will get something different here someday for
374 		 *	  other supported address types, such as IPv6.
375 		 */
376 		addr->ah.n_len = regargs->address.n_len;
377 		addr->ah.n_bytes = tmp_n_bytes;
378 		addr->family = regargs->family;
379 		if (debug) {
380 			if ((addr->family != AF_INET) && \
381 				(addr->family != AF_INET6)) {
382 				(void) printf(
383 					"nap1_reg: unknown addr family %d\n",
384 					addr->family);
385 			}
386 		}
387 		(void) memcpy(addr->ah.n_bytes, regargs->address.n_bytes,
388 				addr->ah.n_len);
389 
390 		addr->next = entry->addresses;
391 		entry->addresses = addr;
392 	}
393 
394 	status = nsm_addr_succ;
395 
396 done:
397 	regresp->status = status;
398 	if (debug) {
399 		pr_name_addr(name_addr);
400 	}
401 	mutex_unlock(&name_addrlock);
402 	rw_unlock(&thr_rwlock);
403 }
404 
405 /*
406  * Insert an entry into the monitor_q.  Space for the entry is allocated
407  * here.  It is then filled in from the information passed in.
408  */
409 static void
410 insert_mon(monp)
411 	mon *monp;
412 {
413 	mon_entry *new, *found;
414 	my_id *my_idp, *nl_idp;
415 	mon_entry *monitor_q;
416 	unsigned int hash;
417 	name_addr_entry_t *entry;
418 	addr_entry_t *addr;
419 
420 	/* Allocate entry for new */
421 	if ((new = (mon_entry *) malloc(sizeof (mon_entry))) == 0) {
422 		syslog(LOG_ERR,
423 			"statd: insert_mon: malloc error on mon %s (id=%d)\n",
424 			monp->mon_id.mon_name, * ((int *)monp->priv));
425 		return;
426 	}
427 
428 	/* Initialize and copy contents of monp to new */
429 	(void) memset(new, 0, sizeof (mon_entry));
430 	(void) memcpy(&new->id, monp, sizeof (mon));
431 
432 	/* Allocate entry for new mon_name */
433 	if ((new->id.mon_id.mon_name = strdup(monp->mon_id.mon_name)) == 0) {
434 		syslog(LOG_ERR,
435 			"statd: insert_mon: malloc error on mon %s (id=%d)\n",
436 			monp->mon_id.mon_name, * ((int *)monp->priv));
437 		free(new);
438 		return;
439 	}
440 
441 
442 	/* Allocate entry for new my_name */
443 	if ((new->id.mon_id.my_id.my_name =
444 		strdup(monp->mon_id.my_id.my_name)) == 0) {
445 		syslog(LOG_ERR,
446 			"statd: insert_mon: malloc error on mon %s (id=%d)\n",
447 			monp->mon_id.mon_name, * ((int *)monp->priv));
448 		free(new->id.mon_id.mon_name);
449 		free(new);
450 		return;
451 	}
452 
453 	if (debug)
454 		(void) printf("add_mon(%x) %s (id=%d)\n",
455 		(int)new, new->id.mon_id.mon_name, * ((int *)new->id.priv));
456 
457 	/*
458 	 * Record the name, and all addresses which have been registered
459 	 * for this name, in the filesystem name space.
460 	 */
461 	record_name(new->id.mon_id.mon_name, 1);
462 	if (regfiles_only == 0) {
463 		mutex_lock(&name_addrlock);
464 		for (entry = name_addr; entry; entry = entry->next) {
465 			if (strcmp(new->id.mon_id.mon_name, entry->name) != 0) {
466 				continue;
467 			}
468 
469 			for (addr = entry->addresses; addr; addr = addr->next) {
470 				record_addr(new->id.mon_id.mon_name,
471 						addr->family, &addr->ah);
472 			}
473 			break;
474 		}
475 		mutex_unlock(&name_addrlock);
476 	}
477 
478 	SMHASH(new->id.mon_id.mon_name, hash);
479 	mutex_lock(&mon_table[hash].lock);
480 	monitor_q = mon_table[hash].sm_monhdp;
481 
482 	/* If mon_table hash list is empty. */
483 	if (monitor_q == (struct mon_entry *)NULL) {
484 		if (debug)
485 			(void) printf("\nAdding to monitor_q hash %d\n", hash);
486 		new->nxt = new->prev = (mon_entry *)NULL;
487 		mon_table[hash].sm_monhdp = new;
488 		mutex_unlock(&mon_table[hash].lock);
489 		return;
490 	} else {
491 		found = 0;
492 		my_idp = &new->id.mon_id.my_id;
493 		while (monitor_q != (mon_entry *)NULL)  {
494 			/*
495 			 * This list is searched sequentially for the
496 			 * tuple (hostname, prog, vers, proc). The tuples
497 			 * are inserted in the beginning of the monitor_q,
498 			 * if the hostname is not already present in the list.
499 			 * If the hostname is found in the list, the incoming
500 			 * tuple is inserted just after all the tuples with the
501 			 * same hostname. However, if the tuple matches exactly
502 			 * with an entry in the list, space allocated for the
503 			 * new entry is released and nothing is inserted in the
504 			 * list.
505 			 */
506 
507 			if (str_cmp_unqual_hostname(
508 				monitor_q->id.mon_id.mon_name,
509 				new->id.mon_id.mon_name) == 0) {
510 				/* found */
511 				nl_idp = &monitor_q->id.mon_id.my_id;
512 				if ((str_cmp_unqual_hostname(my_idp->my_name,
513 					nl_idp->my_name) == 0) &&
514 					my_idp->my_prog == nl_idp->my_prog &&
515 					my_idp->my_vers == nl_idp->my_vers &&
516 					my_idp->my_proc == nl_idp->my_proc) {
517 					/*
518 					 * already exists an identical one,
519 					 * release the space allocated for the
520 					 * mon_entry
521 					 */
522 					free(new->id.mon_id.mon_name);
523 					free(new->id.mon_id.my_id.my_name);
524 					free(new);
525 					mutex_unlock(&mon_table[hash].lock);
526 					return;
527 				} else {
528 					/*
529 					 * mark the last callback that is
530 					 * not matching; new is inserted
531 					 * after this
532 					 */
533 					found = monitor_q;
534 				}
535 			} else if (found)
536 				break;
537 			monitor_q = monitor_q->nxt;
538 		}
539 		if (found) {
540 			/*
541 			 * insert just after the entry having matching tuple.
542 			 */
543 			new->nxt = found->nxt;
544 			new->prev = found;
545 			if (found->nxt != (mon_entry *)NULL)
546 				found->nxt->prev = new;
547 			found->nxt = new;
548 		} else {
549 			/*
550 			 * not found, insert in front of list.
551 			 */
552 			new->nxt = mon_table[hash].sm_monhdp;
553 			new->prev = (mon_entry *) NULL;
554 			if (new->nxt != (mon_entry *) NULL)
555 				new->nxt->prev = new;
556 			mon_table[hash].sm_monhdp = new;
557 		}
558 		mutex_unlock(&mon_table[hash].lock);
559 		return;
560 	}
561 }
562 
563 /*
564  * Deletes a specific monitor name or deletes all monitors with same id
565  * in hash table.
566  */
567 static void
568 delete_mon(mon_name, my_idp)
569 	char *mon_name;
570 	my_id *my_idp;
571 {
572 	unsigned int hash;
573 
574 	if (mon_name != (char *)NULL) {
575 		record_name(mon_name, 0);
576 		SMHASH(mon_name, hash);
577 		mutex_lock(&mon_table[hash].lock);
578 		delete_onemon(mon_name, my_idp, &mon_table[hash].sm_monhdp);
579 		mutex_unlock(&mon_table[hash].lock);
580 	} else {
581 		for (hash = 0; hash < MAX_HASHSIZE; hash++) {
582 			mutex_lock(&mon_table[hash].lock);
583 			delete_onemon(mon_name, my_idp,
584 					&mon_table[hash].sm_monhdp);
585 			mutex_unlock(&mon_table[hash].lock);
586 		}
587 	}
588 }
589 
590 /*
591  * Deletes a monitor in list.
592  * IF mon_name is NULL, delete all mon_names that have the same id,
593  * else delete specific monitor.
594  */
595 void
596 delete_onemon(mon_name, my_idp, monitor_q)
597 	char *mon_name;
598 	my_id *my_idp;
599 	mon_entry **monitor_q;
600 {
601 
602 	mon_entry *next, *nl;
603 	my_id *nl_idp;
604 
605 	next = *monitor_q;
606 	while ((nl = next) != (struct mon_entry *)NULL) {
607 		next = next->nxt;
608 		if (mon_name == (char *)NULL || (mon_name != (char *)NULL &&
609 			str_cmp_unqual_hostname(nl->id.mon_id.mon_name,
610 			mon_name) == 0)) {
611 			nl_idp = &nl->id.mon_id.my_id;
612 			if ((str_cmp_unqual_hostname(my_idp->my_name,
613 					nl_idp->my_name) == 0) &&
614 				my_idp->my_prog == nl_idp->my_prog &&
615 				my_idp->my_vers == nl_idp->my_vers &&
616 				my_idp->my_proc == nl_idp->my_proc) {
617 				/* found */
618 				if (debug)
619 					(void) printf("delete_mon(%x): %s\n",
620 							(int)nl, mon_name ?
621 							mon_name : "<NULL>");
622 				/*
623 				 * Remove the monitor name from the
624 				 * record_q, if id matches.
625 				 */
626 				record_name(nl->id.mon_id.mon_name, 0);
627 				/* if nl is not the first entry on list */
628 				if (nl->prev != (struct mon_entry *)NULL)
629 					nl->prev->nxt = nl->nxt;
630 				else {
631 					*monitor_q = nl->nxt;
632 				}
633 				if (nl->nxt != (struct mon_entry *)NULL)
634 					nl->nxt->prev = nl->prev;
635 				free(nl->id.mon_id.mon_name);
636 				free(nl_idp->my_name);
637 				free(nl);
638 			}
639 		} /* end of if mon */
640 	}
641 
642 }
643 /*
644  * Notify lockd of host specified by mon_name that the specified state
645  * has changed.
646  */
647 static void
648 send_notice(mon_name, state)
649 	char *mon_name;
650 	int state;
651 {
652 	struct mon_entry *next;
653 	mon_entry *monitor_q;
654 	unsigned int hash;
655 	moninfo_t *minfop;
656 	mon *monp;
657 
658 	SMHASH(mon_name, hash);
659 	mutex_lock(&mon_table[hash].lock);
660 	monitor_q = mon_table[hash].sm_monhdp;
661 
662 	next = monitor_q;
663 	while (next != (struct mon_entry *)NULL) {
664 		if (hostname_eq(next->id.mon_id.mon_name, mon_name)) {
665 			monp = &next->id;
666 			/*
667 			 * Prepare the minfop structure to pass to
668 			 * thr_create(). This structure is a copy of
669 			 * mon info and state.
670 			 */
671 			if ((minfop =
672 				(moninfo_t *)xmalloc(sizeof (moninfo_t))) !=
673 				(moninfo_t *)NULL) {
674 				(void) memcpy(&minfop->id, monp, sizeof (mon));
675 				/* Allocate entry for mon_name */
676 				if ((minfop->id.mon_id.mon_name =
677 					strdup(monp->mon_id.mon_name)) == 0) {
678 					syslog(LOG_ERR,
679 			"statd: send_notice: malloc error on mon %s (id=%d)\n",
680 						monp->mon_id.mon_name,
681 						* ((int *)monp->priv));
682 					free(minfop);
683 					continue;
684 				}
685 				/* Allocate entry for my_name */
686 				if ((minfop->id.mon_id.my_id.my_name =
687 				strdup(monp->mon_id.my_id.my_name)) == 0) {
688 					syslog(LOG_ERR,
689 			"statd: send_notice: malloc error on mon %s (id=%d)\n",
690 						monp->mon_id.mon_name,
691 						* ((int *)monp->priv));
692 					free(minfop->id.mon_id.mon_name);
693 					free(minfop);
694 					continue;
695 				}
696 				minfop->state = state;
697 				/*
698 				 * Create detached threads to process each host
699 				 * to notify.  If error, print out msg, free
700 				 * resources and continue.
701 				 */
702 				if (thr_create(NULL, NULL, thr_send_notice,
703 						(void *)minfop, THR_DETACHED,
704 						NULL)) {
705 				    syslog(LOG_ERR,
706 		"statd: unable to create thread to send_notice to %s.\n",
707 					mon_name);
708 				    free(minfop->id.mon_id.mon_name);
709 				    free(minfop->id.mon_id.my_id.my_name);
710 				    free(minfop);
711 				    continue;
712 				}
713 			}
714 		}
715 		next = next->nxt;
716 	}
717 	mutex_unlock(&mon_table[hash].lock);
718 }
719 
720 /*
721  * Work thread created to do the actual statd_call_lockd
722  */
723 static void *
724 thr_send_notice(void *arg)
725 {
726 	moninfo_t *minfop;
727 
728 	minfop = (moninfo_t *)arg;
729 
730 	if (statd_call_lockd(&minfop->id, minfop->state) == -1) {
731 		if (debug && minfop->id.mon_id.mon_name)
732 			(void) printf(
733 		"problem with notifying %s failure, give up\n",
734 			minfop->id.mon_id.mon_name);
735 	} else {
736 		if (debug)
737 			(void) printf(
738 		"send_notice: %s, %d notified.\n",
739 		minfop->id.mon_id.mon_name, minfop->state);
740 	}
741 
742 	free(minfop->id.mon_id.mon_name);
743 	free(minfop->id.mon_id.my_id.my_name);
744 	free(minfop);
745 
746 	thr_exit((void *) 0);
747 #ifdef lint
748 	/*NOTREACHED*/
749 	return ((void *)0);
750 #endif
751 }
752 
753 /*
754  * Contact lockd specified by monp.
755  */
756 static int
757 statd_call_lockd(monp, state)
758 	mon *monp;
759 	int state;
760 {
761 	enum clnt_stat clnt_stat;
762 	struct timeval tottimeout;
763 	struct status stat;
764 	my_id *my_idp;
765 	char *mon_name;
766 	int i;
767 	int rc = 0;
768 	CLIENT *clnt;
769 
770 	mon_name = monp->mon_id.mon_name;
771 	my_idp = &monp->mon_id.my_id;
772 	(void) memset(&stat, 0, sizeof (struct status));
773 	stat.mon_name = mon_name;
774 	stat.state = state;
775 	for (i = 0; i < 16; i++) {
776 		stat.priv[i] = monp->priv[i];
777 	}
778 	if (debug)
779 		(void) printf("statd_call_lockd: %s state = %d\n",
780 			stat.mon_name, stat.state);
781 
782 	if ((clnt = create_client(my_idp->my_name, my_idp->my_prog,
783 		my_idp->my_vers)) == (CLIENT *) NULL) {
784 			return (-1);
785 	}
786 
787 	tottimeout.tv_sec = SM_RPC_TIMEOUT;
788 	tottimeout.tv_usec = 0;
789 	clnt_stat = clnt_call(clnt, my_idp->my_proc, xdr_status, (char *)&stat,
790 				xdr_void, NULL, tottimeout);
791 	if (debug) {
792 		(void) printf("clnt_stat=%s(%d)\n",
793 			clnt_sperrno(clnt_stat), clnt_stat);
794 	}
795 	if (clnt_stat != (int)RPC_SUCCESS) {
796 		syslog(LOG_WARNING,
797 			"statd: cannot talk to lockd at %s, %s(%d)\n",
798 			my_idp->my_name, clnt_sperrno(clnt_stat), clnt_stat);
799 		rc = -1;
800 	}
801 
802 	clnt_destroy(clnt);
803 	return (rc);
804 
805 }
806 
807 /*
808  * Client handle created.
809  */
810 CLIENT *
811 create_client(host, prognum, versnum)
812 	char	*host;
813 	int	prognum;
814 	int	versnum;
815 {
816 	int		fd;
817 	struct timeval	timeout;
818 	CLIENT		*client;
819 	struct t_info	tinfo;
820 
821 	if ((client = clnt_create(host, prognum, versnum,
822 			"netpath")) == NULL) {
823 		return (NULL);
824 	}
825 	(void) CLNT_CONTROL(client, CLGET_FD, (caddr_t)&fd);
826 	if (t_getinfo(fd, &tinfo) != -1) {
827 		if (tinfo.servtype == T_CLTS) {
828 			/*
829 			 * Set time outs for connectionless case
830 			 */
831 			timeout.tv_usec = 0;
832 			timeout.tv_sec = SM_CLTS_TIMEOUT;
833 			(void) CLNT_CONTROL(client,
834 				CLSET_RETRY_TIMEOUT, (caddr_t)&timeout);
835 		}
836 	} else
837 		return (NULL);
838 
839 	return (client);
840 }
841 
842 /*
843  * ONLY for debugging.
844  * Debug messages which prints out the monitor table information.
845  * If name is specified, just print out the hash list corresponding
846  * to name, otherwise print out the entire monitor table.
847  */
848 static void
849 pr_mon(name)
850 	char *name;
851 {
852 	mon_entry *nl;
853 	int hash;
854 
855 	if (!debug)
856 		return;
857 
858 	/* print all */
859 	if (name == NULL) {
860 		for (hash = 0; hash < MAX_HASHSIZE; hash++) {
861 			mutex_lock(&mon_table[hash].lock);
862 			nl = mon_table[hash].sm_monhdp;
863 			if (nl == (struct mon_entry *)NULL) {
864 				(void) printf(
865 					"*****monitor_q = NULL hash %d\n",
866 					hash);
867 				mutex_unlock(&mon_table[hash].lock);
868 				continue;
869 			}
870 			(void) printf("*****monitor_q:\n ");
871 			while (nl != (mon_entry *)NULL) {
872 				(void) printf("%s:(%x), ",
873 					nl->id.mon_id.mon_name, (int)nl);
874 				nl = nl->nxt;
875 			}
876 			mutex_unlock(&mon_table[hash].lock);
877 			(void) printf("\n");
878 		}
879 	} else { /* print one hash list */
880 		SMHASH(name, hash);
881 		mutex_lock(&mon_table[hash].lock);
882 		nl = mon_table[hash].sm_monhdp;
883 		if (nl == (struct mon_entry *)NULL) {
884 			(void) printf("*****monitor_q = NULL hash %d\n", hash);
885 		} else {
886 			(void) printf("*****monitor_q:\n ");
887 			while (nl != (mon_entry *)NULL) {
888 				(void) printf("%s:(%x), ",
889 					nl->id.mon_id.mon_name, (int)nl);
890 				nl = nl->nxt;
891 			}
892 			(void) printf("\n");
893 		}
894 		mutex_unlock(&mon_table[hash].lock);
895 	}
896 }
897 
898 /*
899  * Only for debugging.
900  * Dump the host name-to-address translation table passed in `name_addr'.
901  */
902 static void
903 pr_name_addr(name_addr_entry_t *name_addr)
904 {
905 	name_addr_entry_t *entry;
906 	addr_entry_t *addr;
907 	struct in_addr ipv4_addr;
908 	char *ipv6_addr;
909 	char abuf[INET6_ADDRSTRLEN];
910 
911 	assert(MUTEX_HELD(&name_addrlock));
912 	(void) printf("name-to-address translation table:\n");
913 	for (entry = name_addr; entry != NULL; entry = entry->next) {
914 		(void) printf("\t%s: ",
915 				(entry->name ? entry->name : "(null)"));
916 		for (addr = entry->addresses; addr; addr = addr->next) {
917 			switch (addr->family) {
918 				case AF_INET:
919 					ipv4_addr = *(struct in_addr *)addr->ah.
920 n_bytes;
921 					(void) printf(" %s (fam %d)",
922 							inet_ntoa(ipv4_addr),
923 							addr->family);
924 					break;
925 				case AF_INET6:
926 					ipv6_addr = (char *)addr->ah.n_bytes;
927 					(void) printf(" %s (fam %d)",
928 							inet_ntop(addr->family,
929 ipv6_addr, abuf, sizeof (abuf)), addr->family);
930 					break;
931 				default:
932 					return;
933 			}
934 		}
935 		printf("\n");
936 	}
937 }
938 
939 /*
940  * Statd has trouble dealing with hostname aliases because two
941  * different aliases for the same machine don't match each other
942  * when using strcmp.  To deal with this, the hostnames must be
943  * translated into some sort of universal identifier.  These
944  * identifiers can be compared.  Universal network addresses are
945  * currently used for this identifier because it is general and
946  * easy to do.  Other schemes are possible and this routine
947  * could be converted if required.
948  *
949  * If it can't find an address for some reason, 0 is returned.
950  */
951 static int
952 hostname_eq(char *host1, char *host2)
953 {
954 	char *sysid1;
955 	char *sysid2;
956 	int rv;
957 
958 	sysid1 = get_system_id(host1);
959 	sysid2 = get_system_id(host2);
960 	if ((sysid1 == NULL) || (sysid2 == NULL))
961 		rv = 0;
962 	else
963 		rv = (strcmp(sysid1, sysid2) == 0);
964 	free(sysid1);
965 	free(sysid2);
966 	return (rv);
967 }
968 
969 /*
970  * Convert a hostname character string into its network address.
971  * A network address is found by searching through all the entries
972  * in /etc/netconfig and doing a netdir_getbyname() for each inet
973  * entry found.  The netbuf structure returned is converted into
974  * a universal address format.
975  *
976  * If a NULL hostname is given, then the name of the current host
977  * is used.  If the hostname doesn't map to an address, a NULL
978  * pointer is returned.
979  *
980  * N.B. the character string returned is allocated in taddr2uaddr()
981  * and should be freed by the caller using free().
982  */
983 static char *
984 get_system_id(char *hostname)
985 {
986 	void *hp;
987 	struct netconfig *ncp;
988 	struct nd_hostserv service;
989 	struct nd_addrlist *addrs;
990 	char *uaddr;
991 	int rv;
992 
993 	if (hostname == NULL)
994 		service.h_host = HOST_SELF;
995 	else
996 		service.h_host = hostname;
997 	service.h_serv = NULL;
998 	hp = setnetconfig();
999 	if (hp == (void *) NULL) {
1000 		return (NULL);
1001 	}
1002 	while ((ncp = getnetconfig(hp)) != (struct netconfig *)NULL) {
1003 		if ((strcmp(ncp->nc_protofmly, NC_INET) == 0) ||
1004 			(strcmp(ncp->nc_protofmly, NC_INET6) == 0)) {
1005 			addrs = NULL;
1006 			rv = netdir_getbyname(ncp, &service, &addrs);
1007 			if (rv != 0) {
1008 				continue;
1009 			}
1010 			if (addrs) {
1011 				uaddr = taddr2uaddr(ncp, addrs->n_addrs);
1012 				netdir_free(addrs, ND_ADDRLIST);
1013 				endnetconfig(hp);
1014 				return (uaddr);
1015 			}
1016 		}
1017 		else
1018 			continue;
1019 	}
1020 	endnetconfig(hp);
1021 	return (NULL);
1022 }
1023 
1024 void
1025 merge_hosts(void)
1026 {
1027 	struct lifconf *lifc = NULL;
1028 	int sock = -1;
1029 	struct lifreq *lifrp;
1030 	struct lifreq lifr;
1031 	int n;
1032 	struct sockaddr_in *sin;
1033 	struct sockaddr_in6 *sin6;
1034 	struct sockaddr_storage *sa;
1035 	int af;
1036 	struct hostent *phost;
1037 	char *addr;
1038 	size_t alen;
1039 	int errnum;
1040 
1041 	/*
1042 	 * This function will enumerate all the interfaces for
1043 	 * this platform, then get the hostent for each i/f.
1044 	 * With the hostent structure, we can get all of the
1045 	 * aliases for the i/f. Then we'll merge all the aliases
1046 	 * with the existing host_name[] list to come up with
1047 	 * all of the known names for each interface. This solves
1048 	 * the problem of a multi-homed host not knowing which
1049 	 * name to publish when statd is started. All the aliases
1050 	 * will be stored in the array, host_name.
1051 	 *
1052 	 * NOTE: Even though we will use all of the aliases we
1053 	 * can get from the i/f hostent, the receiving statd
1054 	 * will still need to handle aliases with hostname_eq.
1055 	 * This is because the sender's aliases may not match
1056 	 * those of the receiver.
1057 	 */
1058 	lifc = getmyaddrs();
1059 	if (lifc == (struct lifconf *)NULL) {
1060 		goto finish;
1061 	}
1062 	lifrp = lifc->lifc_req;
1063 	for (n = lifc->lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
1064 
1065 		(void) strncpy(lifr.lifr_name, lifrp->lifr_name,
1066 				sizeof (lifr.lifr_name));
1067 
1068 		af = lifrp->lifr_addr.ss_family;
1069 		sock = socket(af, SOCK_DGRAM, 0);
1070 		if (sock == -1) {
1071 			syslog(LOG_ERR, "statd: socket failed\n");
1072 			goto finish;
1073 		}
1074 
1075 		/* If it's the loopback interface, ignore */
1076 		if (ioctl(sock, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
1077 			syslog(LOG_ERR,
1078 				"statd: SIOCGLIFFLAGS failed, error: %m\n");
1079 			goto finish;
1080 		}
1081 		if (lifr.lifr_flags & IFF_LOOPBACK)
1082 			continue;
1083 
1084 		if (ioctl(sock, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1085 			syslog(LOG_ERR,
1086 				"statd: SIOCGLIFADDR failed, error: %m\n");
1087 			goto finish;
1088 		}
1089 		sa = (struct sockaddr_storage *)&(lifr.lifr_addr);
1090 
1091 		if (sa->ss_family == AF_INET) {
1092 			sin = (struct sockaddr_in *)&lifr.lifr_addr;
1093 			addr = (char *)(&sin->sin_addr);
1094 			alen = sizeof (struct in_addr);
1095 		} else if (sa->ss_family == AF_INET6) {
1096 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
1097 			addr = (char *)(&sin6->sin6_addr);
1098 			alen = sizeof (struct in6_addr);
1099 		} else {
1100 			syslog(LOG_WARNING,
1101 			    "unexpected address family (%d)",
1102 			    sa->ss_family);
1103 			continue;
1104 		}
1105 
1106 		phost = getipnodebyaddr(addr, alen, sa->ss_family, &errnum);
1107 
1108 		if (phost)
1109 			add_aliases(phost);
1110 	}
1111 	/*
1112 	 * Now, just in case we didn't get them all byaddr,
1113 	 * let's look by name.
1114 	 */
1115 	phost = getipnodebyname(hostname, AF_INET6, AI_ALL, &errnum);
1116 
1117 	if (phost)
1118 		add_aliases(phost);
1119 
1120 finish:
1121 	if (sock != -1)
1122 		(void) close(sock);
1123 	if (lifc) {
1124 		free(lifc->lifc_buf);
1125 		free(lifc);
1126 	}
1127 }
1128 
1129 /*
1130  * add_aliases traverses a hostent alias list, compares
1131  * the aliases to the contents of host_name, and if an
1132  * alias is not already present, adds it to host_name[].
1133  */
1134 
1135 static void
1136 add_aliases(struct hostent *phost)
1137 {
1138 	char **aliases;
1139 
1140 	if (!in_host_array(phost->h_name)) {
1141 		add_to_host_array(phost->h_name);
1142 	}
1143 
1144 	if (phost->h_aliases == NULL)
1145 		return;			/* no aliases to register */
1146 
1147 	for (aliases = phost->h_aliases; *aliases != NULL; aliases++) {
1148 		if (!in_host_array(*aliases)) {
1149 			add_to_host_array(*aliases);
1150 		}
1151 	}
1152 }
1153 
1154 /*
1155  * in_host_array checks if the given hostname exists in the host_name
1156  * array. Returns 0 if the host doesn't exist, and 1 if it does exist
1157  */
1158 static int
1159 in_host_array(char *host)
1160 {
1161 	int i;
1162 
1163 	if (debug)
1164 		(void) printf("%s ", host);
1165 
1166 	if ((strcmp(hostname, host) == 0) || (strcmp(LOGHOST, host) == 0))
1167 		return (1);
1168 
1169 	for (i = 0; i < addrix; i++) {
1170 		if (strcmp(host_name[i], host) == 0)
1171 			return (1);
1172 	}
1173 
1174 	return (0);
1175 }
1176 
1177 /*
1178  * add_to_host_array adds a hostname to the host_name array. But if
1179  * the array is already full, then it first reallocates the array with
1180  * HOST_NAME_INCR extra elements. If the realloc fails, then it does
1181  * nothing and leaves host_name the way it was previous to the call.
1182  */
1183 static void
1184 add_to_host_array(char *host) {
1185 
1186 	void *new_block = NULL;
1187 
1188 	/* Make sure we don't overrun host_name. */
1189 	if (addrix >= host_name_count) {
1190 		host_name_count += HOST_NAME_INCR;
1191 		new_block = realloc((void *)host_name,
1192 				    host_name_count*sizeof (char *));
1193 		if (new_block != NULL)
1194 			host_name = new_block;
1195 		else {
1196 			host_name_count -= HOST_NAME_INCR;
1197 			return;
1198 		}
1199 	}
1200 
1201 	if ((host_name[addrix] = strdup(host)) != NULL)
1202 		addrix++;
1203 }
1204 
1205 /*
1206  * Compares the unqualified hostnames for hosts. Returns 0 if the
1207  * names match, and 1 if the names fail to match.
1208  */
1209 int
1210 str_cmp_unqual_hostname(char *rawname1, char *rawname2)
1211 {
1212 	size_t unq_len1, unq_len2;
1213 	char *domain;
1214 
1215 	if (debug) {
1216 		(void) printf("str_cmp_unqual: rawname1= %s, rawname2= %s\n",
1217 				rawname1, rawname2);
1218 	}
1219 
1220 	unq_len1 = strcspn(rawname1, ".");
1221 	unq_len2 = strcspn(rawname2, ".");
1222 	domain = strchr(rawname1, '.');
1223 	if (domain != NULL) {
1224 		if ((strncmp(rawname1, SM_ADDR_IPV4, unq_len1) == 0) ||
1225 			(strncmp(rawname1, SM_ADDR_IPV6, unq_len1) == 0))
1226 		return (1);
1227 	}
1228 
1229 	if ((unq_len1 == unq_len2) &&
1230 			(strncmp(rawname1, rawname2, unq_len1) == 0)) {
1231 		return (0);
1232 	}
1233 
1234 	return (1);
1235 }
1236 
1237 /*
1238  * Compares <family>.<address-specifier> ASCII names for hosts.  Returns
1239  * 0 if the addresses match, and 1 if the addresses fail to match.
1240  * If the args are indeed specifiers, they should look like this:
1241  *
1242  *	ipv4.192.9.200.1 or ipv6.::C009:C801
1243  */
1244 int
1245 str_cmp_address_specifier(char *specifier1, char *specifier2)
1246 {
1247 	size_t unq_len1, unq_len2;
1248 	char *rawaddr1, *rawaddr2;
1249 	int af1, af2, len;
1250 
1251 	if (debug) {
1252 		(void) printf("str_cmp_addr: specifier1= %s, specifier2= %s\n",
1253 				specifier1, specifier2);
1254 	}
1255 
1256 	/*
1257 	 * Verify that:
1258 	 *	1. The family tokens match;
1259 	 *	2. The IP addresses following the `.' are legal; and
1260 	 *	3. These addresses match.
1261 	 */
1262 	unq_len1 = strcspn(specifier1, ".");
1263 	unq_len2 = strcspn(specifier2, ".");
1264 	rawaddr1 = strchr(specifier1, '.');
1265 	rawaddr2 = strchr(specifier2, '.');
1266 
1267 	if (strncmp(specifier1, SM_ADDR_IPV4, unq_len1) == 0) {
1268 		af1 = AF_INET;
1269 		len = 4;
1270 	} else if (strncmp(specifier1, SM_ADDR_IPV6, unq_len1) == 0) {
1271 		af1 = AF_INET6;
1272 		len = 16;
1273 	}
1274 	else
1275 		return (1);
1276 
1277 	if (strncmp(specifier2, SM_ADDR_IPV4, unq_len2) == 0)
1278 		af2 = AF_INET;
1279 	else if (strncmp(specifier2, SM_ADDR_IPV6, unq_len2) == 0)
1280 		af2 = AF_INET6;
1281 	else
1282 		return (1);
1283 
1284 	if (af1 != af2)
1285 		return (1);
1286 
1287 	if (rawaddr1 != NULL && rawaddr2 != NULL) {
1288 		char dst1[16];
1289 		char dst2[16];
1290 		++rawaddr1;
1291 		++rawaddr2;
1292 
1293 		if (inet_pton(af1, rawaddr1, dst1) == 1 &&
1294 			inet_pton(af2, rawaddr1, dst2) == 1 &&
1295 			memcmp(dst1, dst2, len) == 0) {
1296 			return (0);
1297 		}
1298 	}
1299 	return (1);
1300 }
1301