xref: /illumos-gate/usr/src/cmd/fs.d/nfs/statd/sm_svc.c (revision 064d1627)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2198573c19SMarcel Telka 
227c478bd9Sstevel@tonic-gate /*
239ff75adeSSurya Prakki  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24e72ff08dSGordon Ross  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
25e72ff08dSGordon Ross  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*064d1627SToomas Soome /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
42004388ebScasper #include <stdio_ext.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <ftw.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
487c478bd9Sstevel@tonic-gate #include <netconfig.h>
49e72ff08dSGordon Ross #include <netdir.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <netdb.h>
527c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
53e72ff08dSGordon Ross #include <rpc/svc.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <sys/param.h>
567c478bd9Sstevel@tonic-gate #include <sys/resource.h>
577c478bd9Sstevel@tonic-gate #include <sys/file.h>
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
617c478bd9Sstevel@tonic-gate #include <dirent.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
637c478bd9Sstevel@tonic-gate #include <rpcsvc/sm_inter.h>
647c478bd9Sstevel@tonic-gate #include <rpcsvc/nsm_addr.h>
657c478bd9Sstevel@tonic-gate #include <thread.h>
667c478bd9Sstevel@tonic-gate #include <synch.h>
677c478bd9Sstevel@tonic-gate #include <net/if.h>
687c478bd9Sstevel@tonic-gate #include <limits.h>
697c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
707c478bd9Sstevel@tonic-gate #include <priv_utils.h>
71e72ff08dSGordon Ross #include "smfcfg.h"
727c478bd9Sstevel@tonic-gate #include "sm_statd.h"
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	home0		"/var/statmon"
767c478bd9Sstevel@tonic-gate #define	current0	"/var/statmon/sm"
777c478bd9Sstevel@tonic-gate #define	backup0		"/var/statmon/sm.bak"
787c478bd9Sstevel@tonic-gate #define	state0		"/var/statmon/state"
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	home1		"statmon"
817c478bd9Sstevel@tonic-gate #define	current1	"statmon/sm/"
827c478bd9Sstevel@tonic-gate #define	backup1		"statmon/sm.bak/"
837c478bd9Sstevel@tonic-gate #define	state1		"statmon/state"
847c478bd9Sstevel@tonic-gate 
8598573c19SMarcel Telka extern int	daemonize_init(void);
8698573c19SMarcel Telka extern void	daemonize_fini(int fd);
8798573c19SMarcel Telka 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * User and group IDs to run as.  These are hardwired, rather than looked
907c478bd9Sstevel@tonic-gate  * up at runtime, because they are very unlikely to change and because they
917c478bd9Sstevel@tonic-gate  * provide some protection against bogus changes to the passwd and group
927c478bd9Sstevel@tonic-gate  * files.
937c478bd9Sstevel@tonic-gate  */
949acbbeafSnn uid_t	daemon_uid = DAEMON_UID;
959acbbeafSnn gid_t	daemon_gid = DAEMON_GID;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate char STATE[MAXPATHLEN], CURRENT[MAXPATHLEN], BACKUP[MAXPATHLEN];
987c478bd9Sstevel@tonic-gate static char statd_home[MAXPATHLEN];
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int debug;
1017c478bd9Sstevel@tonic-gate int regfiles_only = 0;		/* 1 => use symlinks in statmon, 0 => don't */
102e72ff08dSGordon Ross int statd_port = 0;
1037c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * These variables will be used to store all the
1077c478bd9Sstevel@tonic-gate  * alias names for the host, as well as the -a
1087c478bd9Sstevel@tonic-gate  * command line hostnames.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate int host_name_count;
1117c478bd9Sstevel@tonic-gate char **host_name; /* store -a opts */
1127c478bd9Sstevel@tonic-gate int  addrix; /* # of -a entries */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * The following 2 variables are meaningful
1177c478bd9Sstevel@tonic-gate  * only under a HA configuration.
1187c478bd9Sstevel@tonic-gate  * The path_name array is dynamically allocated in main() during
1197c478bd9Sstevel@tonic-gate  * command line argument processing for the -p options.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate char **path_name = NULL;  /* store -p opts */
1227c478bd9Sstevel@tonic-gate int  pathix = 0;  /* # of -p entries */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* Global variables.  Refer to sm_statd.h for description */
1257c478bd9Sstevel@tonic-gate mutex_t crash_lock;
1267c478bd9Sstevel@tonic-gate int die;
1277c478bd9Sstevel@tonic-gate int in_crash;
1287c478bd9Sstevel@tonic-gate mutex_t sm_trylock;
1297c478bd9Sstevel@tonic-gate rwlock_t thr_rwlock;
1307c478bd9Sstevel@tonic-gate cond_t retrywait;
1317c478bd9Sstevel@tonic-gate mutex_t name_addrlock;
1327c478bd9Sstevel@tonic-gate 
13398573c19SMarcel Telka mutex_t merges_lock;
13498573c19SMarcel Telka cond_t merges_cond;
13598573c19SMarcel Telka boolean_t in_merges;
13698573c19SMarcel Telka 
1377c478bd9Sstevel@tonic-gate /* forward references */
1387c478bd9Sstevel@tonic-gate static void set_statmon_owner(void);
1397c478bd9Sstevel@tonic-gate static void copy_client_names(void);
1407c478bd9Sstevel@tonic-gate static void one_statmon_owner(const char *);
1417c478bd9Sstevel@tonic-gate static int nftw_owner(const char *, const struct stat *, int, struct FTW *);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * statd protocol
145*064d1627SToomas Soome  *	commands:
146*064d1627SToomas Soome  *		SM_STAT
147*064d1627SToomas Soome  *			returns stat_fail to caller
148*064d1627SToomas Soome  *		SM_MON
149*064d1627SToomas Soome  *			adds an entry to the monitor_q and the record_q.
1507c478bd9Sstevel@tonic-gate  *			This message is sent by the server lockd to the server
1517c478bd9Sstevel@tonic-gate  *			statd, to indicate that a new client is to be monitored.
1527c478bd9Sstevel@tonic-gate  *			It is also sent by the server lockd to the client statd
1537c478bd9Sstevel@tonic-gate  *			to indicate that a new server is to be monitored.
154*064d1627SToomas Soome  *		SM_UNMON
155*064d1627SToomas Soome  *			removes an entry from the monitor_q and the record_q
156*064d1627SToomas Soome  *		SM_UNMON_ALL
157*064d1627SToomas Soome  *			removes all entries from a particular host from the
158*064d1627SToomas Soome  *			monitor_q and the record_q.  Our statd has this
159*064d1627SToomas Soome  *			disabled.
160*064d1627SToomas Soome  *		SM_SIMU_CRASH
161*064d1627SToomas Soome  *			simulate a crash.  Removes everything from the
162*064d1627SToomas Soome  *			record_q and the recovery_q, then calls statd_init()
163*064d1627SToomas Soome  *			to restart things.  This message is sent by the server
1647c478bd9Sstevel@tonic-gate  *			lockd to the server statd to have all clients notified
1657c478bd9Sstevel@tonic-gate  *			that they should reclaim locks.
166*064d1627SToomas Soome  *		SM_NOTIFY
1677c478bd9Sstevel@tonic-gate  *			Sent by statd on server to statd on client during
1687c478bd9Sstevel@tonic-gate  *			crash recovery.  The client statd passes the info
1697c478bd9Sstevel@tonic-gate  *			to its lockd so it can attempt to reclaim the locks
1707c478bd9Sstevel@tonic-gate  *			held on the server.
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  * There are three main hash tables used to keep track of things.
173*064d1627SToomas Soome  *	mon_table
174*064d1627SToomas Soome  *		table that keeps track hosts statd must watch.  If one of
175*064d1627SToomas Soome  *		these hosts crashes, then any locks held by that host must
176*064d1627SToomas Soome  *		be released.
177*064d1627SToomas Soome  *	record_table
178*064d1627SToomas Soome  *		used to keep track of all the hostname files stored in
179*064d1627SToomas Soome  *		the directory /var/statmon/sm.  These are client hosts who
1807c478bd9Sstevel@tonic-gate  *		are holding or have held a lock at some point.  Needed
1817c478bd9Sstevel@tonic-gate  *		to determine if a file needs to be created for host in
1827c478bd9Sstevel@tonic-gate  *		/var/statmon/sm.
1837c478bd9Sstevel@tonic-gate  *	recov_q
1847c478bd9Sstevel@tonic-gate  *		used to keep track hostnames during a recovery
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * The entries are hashed based upon the name.
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  * There is a directory /var/statmon/sm which holds a file named
1897c478bd9Sstevel@tonic-gate  * for each host that is holding (or has held) a lock.  This is
1907c478bd9Sstevel@tonic-gate  * used during initialization on startup, or after a simulated
1917c478bd9Sstevel@tonic-gate  * crash.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static void
sm_prog_1(struct svc_req * rqstp,SVCXPRT * transp)19598573c19SMarcel Telka sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	union {
1987c478bd9Sstevel@tonic-gate 		struct sm_name sm_stat_1_arg;
1997c478bd9Sstevel@tonic-gate 		struct mon sm_mon_1_arg;
2007c478bd9Sstevel@tonic-gate 		struct mon_id sm_unmon_1_arg;
2017c478bd9Sstevel@tonic-gate 		struct my_id sm_unmon_all_1_arg;
2027c478bd9Sstevel@tonic-gate 		struct stat_chge ntf_arg;
2037c478bd9Sstevel@tonic-gate 		struct reg1args reg1_arg;
2047c478bd9Sstevel@tonic-gate 	} argument;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	union {
2077c478bd9Sstevel@tonic-gate 		sm_stat_res stat_resp;
2087c478bd9Sstevel@tonic-gate 		sm_stat	mon_resp;
2097c478bd9Sstevel@tonic-gate 		struct reg1res reg1_resp;
2107c478bd9Sstevel@tonic-gate 	} result;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	bool_t (*xdr_argument)(), (*xdr_result)();
213*064d1627SToomas Soome 	void (*local)(void *, void *);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Dispatch according to which protocol is being used:
2177c478bd9Sstevel@tonic-gate 	 *	NSM_ADDR_PROGRAM is the private lockd address
2187c478bd9Sstevel@tonic-gate 	 *		registration protocol.
2197c478bd9Sstevel@tonic-gate 	 *	SM_PROG is the normal statd (NSM) protocol.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	if (rqstp->rq_prog == NSM_ADDR_PROGRAM) {
2227c478bd9Sstevel@tonic-gate 		switch (rqstp->rq_proc) {
2237c478bd9Sstevel@tonic-gate 		case NULLPROC:
2247c478bd9Sstevel@tonic-gate 			svc_sendreply(transp, xdr_void, (caddr_t)NULL);
2257c478bd9Sstevel@tonic-gate 			return;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		case NSMADDRPROC1_REG:
2287c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_reg1args;
2297c478bd9Sstevel@tonic-gate 			xdr_result = xdr_reg1res;
230*064d1627SToomas Soome 			local = nsmaddrproc1_reg;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
233bbaa8b60SDan Kruchinin 		case NSMADDRPROC1_UNREG: /* Not impl. */
2347c478bd9Sstevel@tonic-gate 		default:
2357c478bd9Sstevel@tonic-gate 			svcerr_noproc(transp);
2367c478bd9Sstevel@tonic-gate 			return;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 	} else {
239bbaa8b60SDan Kruchinin 		/* Must be SM_PROG */
2407c478bd9Sstevel@tonic-gate 		switch (rqstp->rq_proc) {
2417c478bd9Sstevel@tonic-gate 		case NULLPROC:
2427c478bd9Sstevel@tonic-gate 			svc_sendreply(transp, xdr_void, (caddr_t)NULL);
2437c478bd9Sstevel@tonic-gate 			return;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		case SM_STAT:
2467c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_sm_name;
2477c478bd9Sstevel@tonic-gate 			xdr_result = xdr_sm_stat_res;
248*064d1627SToomas Soome 			local = sm_stat_svc;
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		case SM_MON:
2527c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_mon;
2537c478bd9Sstevel@tonic-gate 			xdr_result = xdr_sm_stat_res;
254*064d1627SToomas Soome 			local = sm_mon_svc;
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		case SM_UNMON:
2587c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_mon_id;
2597c478bd9Sstevel@tonic-gate 			xdr_result = xdr_sm_stat;
260*064d1627SToomas Soome 			local = sm_unmon_svc;
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		case SM_UNMON_ALL:
2647c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_my_id;
2657c478bd9Sstevel@tonic-gate 			xdr_result = xdr_sm_stat;
266*064d1627SToomas Soome 			local = sm_unmon_all_svc;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		case SM_SIMU_CRASH:
2707c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_void;
2717c478bd9Sstevel@tonic-gate 			xdr_result = xdr_void;
272*064d1627SToomas Soome 			local = sm_simu_crash_svc;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		case SM_NOTIFY:
2767c478bd9Sstevel@tonic-gate 			xdr_argument = xdr_stat_chge;
2777c478bd9Sstevel@tonic-gate 			xdr_result = xdr_void;
278*064d1627SToomas Soome 			local = sm_notify_svc;
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		default:
2827c478bd9Sstevel@tonic-gate 			svcerr_noproc(transp);
2837c478bd9Sstevel@tonic-gate 			return;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	(void) memset(&argument, 0, sizeof (argument));
2887c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
2897c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
2907c478bd9Sstevel@tonic-gate 		return;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	(void) memset(&result, 0, sizeof (result));
2947c478bd9Sstevel@tonic-gate 	(*local)(&argument, &result);
2957c478bd9Sstevel@tonic-gate 	if (!svc_sendreply(transp, xdr_result, (caddr_t)&result)) {
2967c478bd9Sstevel@tonic-gate 		svcerr_systemerr(transp);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
300bbaa8b60SDan Kruchinin 		syslog(LOG_ERR, "statd: unable to free arguments\n");
301bbaa8b60SDan Kruchinin 	}
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * Remove all files under directory path_dir.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate static int
remove_dir(char * path_dir)30898573c19SMarcel Telka remove_dir(char *path_dir)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	DIR	*dp;
3114bc0a2efScasper 	struct dirent   *dirp;
3127c478bd9Sstevel@tonic-gate 	char tmp_path[MAXPATHLEN];
3137c478bd9Sstevel@tonic-gate 
31498573c19SMarcel Telka 	if ((dp = opendir(path_dir)) == NULL) {
3157c478bd9Sstevel@tonic-gate 		if (debug)
31698573c19SMarcel Telka 			syslog(LOG_ERR,
31798573c19SMarcel Telka 			    "warning: open directory %s failed: %m\n",
31898573c19SMarcel Telka 			    path_dir);
3197c478bd9Sstevel@tonic-gate 		return (1);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3224bc0a2efScasper 	while ((dirp = readdir(dp)) != NULL) {
3237c478bd9Sstevel@tonic-gate 		if (strcmp(dirp->d_name, ".") != 0 &&
32498573c19SMarcel Telka 		    strcmp(dirp->d_name, "..") != 0) {
3257c478bd9Sstevel@tonic-gate 			if (strlen(path_dir) + strlen(dirp->d_name) +2 >
32698573c19SMarcel Telka 			    MAXPATHLEN) {
3277c478bd9Sstevel@tonic-gate 
32898573c19SMarcel Telka 				syslog(LOG_ERR, "statd: remove dir %s/%s "
32998573c19SMarcel Telka 				    "failed.  Pathname too long.\n", path_dir,
33098573c19SMarcel Telka 				    dirp->d_name);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 				continue;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 			(void) strcpy(tmp_path, path_dir);
3357c478bd9Sstevel@tonic-gate 			(void) strcat(tmp_path, "/");
3367c478bd9Sstevel@tonic-gate 			(void) strcat(tmp_path, dirp->d_name);
3377c478bd9Sstevel@tonic-gate 			delete_file(tmp_path);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	(void) closedir(dp);
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * Copy all files from directory `from_dir' to directory `to_dir'.
3477c478bd9Sstevel@tonic-gate  * Symlinks, if any, are preserved.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate void
copydir_from_to(char * from_dir,char * to_dir)35098573c19SMarcel Telka copydir_from_to(char *from_dir, char *to_dir)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	int	n;
3537c478bd9Sstevel@tonic-gate 	DIR	*dp;
3544bc0a2efScasper 	struct dirent   *dirp;
3557c478bd9Sstevel@tonic-gate 	char rname[MAXNAMELEN + 1];
3567c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN+MAXNAMELEN+2];
3577c478bd9Sstevel@tonic-gate 
35898573c19SMarcel Telka 	if ((dp = opendir(from_dir)) == NULL) {
3597c478bd9Sstevel@tonic-gate 		if (debug)
36098573c19SMarcel Telka 			syslog(LOG_ERR,
36198573c19SMarcel Telka 			    "warning: open directory %s failed: %m\n",
36298573c19SMarcel Telka 			    from_dir);
3637c478bd9Sstevel@tonic-gate 		return;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3664bc0a2efScasper 	while ((dirp = readdir(dp)) != NULL) {
3677c478bd9Sstevel@tonic-gate 		if (strcmp(dirp->d_name, ".") == 0 ||
36898573c19SMarcel Telka 		    strcmp(dirp->d_name, "..") == 0) {
3697c478bd9Sstevel@tonic-gate 			continue;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		(void) strcpy(path, from_dir);
3737c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
3747c478bd9Sstevel@tonic-gate 		(void) strcat(path, dirp->d_name);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		if (is_symlink(path)) {
3777c478bd9Sstevel@tonic-gate 			/*
3787c478bd9Sstevel@tonic-gate 			 * Follow the link to get the referenced file name
3797c478bd9Sstevel@tonic-gate 			 * and make a new link for that file in to_dir.
3807c478bd9Sstevel@tonic-gate 			 */
3817c478bd9Sstevel@tonic-gate 			n = readlink(path, rname, MAXNAMELEN);
3827c478bd9Sstevel@tonic-gate 			if (n <= 0) {
3837c478bd9Sstevel@tonic-gate 				if (debug >= 2) {
38498573c19SMarcel Telka 					(void) printf("copydir_from_to: can't "
38598573c19SMarcel Telka 					    "read link %s\n", path);
3867c478bd9Sstevel@tonic-gate 				}
3877c478bd9Sstevel@tonic-gate 				continue;
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 			rname[n] = '\0';
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 			(void) create_symlink(to_dir, rname, dirp->d_name);
3927c478bd9Sstevel@tonic-gate 		} else {
3937c478bd9Sstevel@tonic-gate 			/*
3947c478bd9Sstevel@tonic-gate 			 * Simply copy regular files to to_dir.
3957c478bd9Sstevel@tonic-gate 			 */
3967c478bd9Sstevel@tonic-gate 			(void) strcpy(path, to_dir);
3977c478bd9Sstevel@tonic-gate 			(void) strcat(path, "/");
3987c478bd9Sstevel@tonic-gate 			(void) strcat(path, dirp->d_name);
3997c478bd9Sstevel@tonic-gate 			(void) create_file(path);
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	(void) closedir(dp);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static int
init_hostname(void)4077c478bd9Sstevel@tonic-gate init_hostname(void)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
4107c478bd9Sstevel@tonic-gate 	int sock;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
4137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "statd:init_hostname, socket: %m");
4147c478bd9Sstevel@tonic-gate 		return (-1);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
4187c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = 0;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
4217c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4227c478bd9Sstevel@tonic-gate 		"statd:init_hostname, get number of interfaces, error: %m");
4237c478bd9Sstevel@tonic-gate 		close(sock);
4247c478bd9Sstevel@tonic-gate 		return (-1);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	host_name_count = lifn.lifn_count;
4287c478bd9Sstevel@tonic-gate 
42998573c19SMarcel Telka 	host_name = malloc(host_name_count * sizeof (char *));
4307c478bd9Sstevel@tonic-gate 	if (host_name == NULL) {
4317c478bd9Sstevel@tonic-gate 		perror("statd -a can't get ip configuration\n");
4327c478bd9Sstevel@tonic-gate 		close(sock);
4337c478bd9Sstevel@tonic-gate 		return (-1);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	close(sock);
4367c478bd9Sstevel@tonic-gate 	return (0);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
43998573c19SMarcel Telka static void
thr_statd_merges(void)44098573c19SMarcel Telka thr_statd_merges(void)
44198573c19SMarcel Telka {
44298573c19SMarcel Telka 	/*
44398573c19SMarcel Telka 	 * Get other aliases from each interface.
44498573c19SMarcel Telka 	 */
44598573c19SMarcel Telka 	merge_hosts();
44698573c19SMarcel Telka 
44798573c19SMarcel Telka 	/*
44898573c19SMarcel Telka 	 * Get all of the configured IP addresses.
44998573c19SMarcel Telka 	 */
45098573c19SMarcel Telka 	merge_ips();
45198573c19SMarcel Telka 
45298573c19SMarcel Telka 	/*
45398573c19SMarcel Telka 	 * Notify the waiters.
45498573c19SMarcel Telka 	 */
45598573c19SMarcel Telka 	(void) mutex_lock(&merges_lock);
45698573c19SMarcel Telka 	in_merges = B_FALSE;
45798573c19SMarcel Telka 	(void) cond_broadcast(&merges_cond);
45898573c19SMarcel Telka 	(void) mutex_unlock(&merges_lock);
45998573c19SMarcel Telka }
46098573c19SMarcel Telka 
461e72ff08dSGordon Ross /*
462e72ff08dSGordon Ross  * This function is called for each configured network type to
463e72ff08dSGordon Ross  * bind and register our RPC service programs.
464e72ff08dSGordon Ross  *
465e72ff08dSGordon Ross  * On TCP or UDP, we may want to bind SM_PROG on a specific port
466e72ff08dSGordon Ross  * (when statd_port is specified) in which case we'll use the
467e72ff08dSGordon Ross  * variant of svc_tp_create() that lets us pass a bind address.
468e72ff08dSGordon Ross  */
469e72ff08dSGordon Ross static void
sm_svc_tp_create(struct netconfig * nconf)470e72ff08dSGordon Ross sm_svc_tp_create(struct netconfig *nconf)
471e72ff08dSGordon Ross {
472e72ff08dSGordon Ross 	char port_str[8];
473e72ff08dSGordon Ross 	struct nd_hostserv hs;
474e72ff08dSGordon Ross 	struct nd_addrlist *al = NULL;
475e72ff08dSGordon Ross 	SVCXPRT *xprt = NULL;
476e72ff08dSGordon Ross 
477e72ff08dSGordon Ross 	/*
478e72ff08dSGordon Ross 	 * If statd_port is set and this is an inet transport,
479e72ff08dSGordon Ross 	 * bind this service on the specified port.  The TLI way
480e72ff08dSGordon Ross 	 * to create such a bind address is netdir_getbyname()
481e72ff08dSGordon Ross 	 * with the special "host" HOST_SELF_BIND.  This builds
482e72ff08dSGordon Ross 	 * an all-zeros IP address with the specified port.
483e72ff08dSGordon Ross 	 */
484e72ff08dSGordon Ross 	if (statd_port != 0 &&
485e72ff08dSGordon Ross 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
486e72ff08dSGordon Ross 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
487e72ff08dSGordon Ross 		int err;
488e72ff08dSGordon Ross 
489e72ff08dSGordon Ross 		snprintf(port_str, sizeof (port_str), "%u",
490e72ff08dSGordon Ross 		    (unsigned short)statd_port);
491e72ff08dSGordon Ross 
492e72ff08dSGordon Ross 		hs.h_host = HOST_SELF_BIND;
493e72ff08dSGordon Ross 		hs.h_serv = port_str;
494e72ff08dSGordon Ross 		err = netdir_getbyname((struct netconfig *)nconf, &hs, &al);
495e72ff08dSGordon Ross 		if (err == 0 && al != NULL) {
496e72ff08dSGordon Ross 			xprt = svc_tp_create_addr(sm_prog_1, SM_PROG, SM_VERS,
497e72ff08dSGordon Ross 			    nconf, al->n_addrs);
498e72ff08dSGordon Ross 			netdir_free(al, ND_ADDRLIST);
499e72ff08dSGordon Ross 		}
500e72ff08dSGordon Ross 		if (xprt == NULL) {
501e72ff08dSGordon Ross 			syslog(LOG_ERR, "statd: unable to create "
502e72ff08dSGordon Ross 			    "(SM_PROG, SM_VERS) on transport %s (port %d)",
503e72ff08dSGordon Ross 			    nconf->nc_netid, statd_port);
504e72ff08dSGordon Ross 		}
505e72ff08dSGordon Ross 		/* fall-back to default bind */
506e72ff08dSGordon Ross 	}
507e72ff08dSGordon Ross 	if (xprt == NULL) {
508e72ff08dSGordon Ross 		/*
509e72ff08dSGordon Ross 		 * Had statd_port=0, or non-inet transport,
510e72ff08dSGordon Ross 		 * or the bind to a specific port failed.
511e72ff08dSGordon Ross 		 * Do a default bind.
512e72ff08dSGordon Ross 		 */
513e72ff08dSGordon Ross 		xprt = svc_tp_create(sm_prog_1, SM_PROG, SM_VERS, nconf);
514e72ff08dSGordon Ross 	}
515e72ff08dSGordon Ross 	if (xprt == NULL) {
516e72ff08dSGordon Ross 		syslog(LOG_ERR, "statd: unable to create "
517e72ff08dSGordon Ross 		    "(SM_PROG, SM_VERS) for transport %s",
518e72ff08dSGordon Ross 		    nconf->nc_netid);
519e72ff08dSGordon Ross 		return;
520e72ff08dSGordon Ross 	}
521e72ff08dSGordon Ross 
522e72ff08dSGordon Ross 	/*
523e72ff08dSGordon Ross 	 * Also register the NSM_ADDR program on this
524e72ff08dSGordon Ross 	 * transport handle (same dispatch function).
525e72ff08dSGordon Ross 	 */
526e72ff08dSGordon Ross 	if (!svc_reg(xprt, NSM_ADDR_PROGRAM, NSM_ADDR_V1, sm_prog_1, nconf)) {
527e72ff08dSGordon Ross 		syslog(LOG_ERR, "statd: failed to register "
528e72ff08dSGordon Ross 		    "(NSM_ADDR_PROGRAM, NSM_ADDR_V1) for "
529e72ff08dSGordon Ross 		    "netconfig %s", nconf->nc_netid);
530e72ff08dSGordon Ross 	}
531e72ff08dSGordon Ross }
532e72ff08dSGordon Ross 
53311606941Sjwahlig int
main(int argc,char * argv[])53411606941Sjwahlig main(int argc, char *argv[])
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	int c;
5377c478bd9Sstevel@tonic-gate 	int ppid;
5387c478bd9Sstevel@tonic-gate 	extern char *optarg;
5397c478bd9Sstevel@tonic-gate 	int choice = 0;
5407c478bd9Sstevel@tonic-gate 	struct rlimit rl;
5417c478bd9Sstevel@tonic-gate 	int mode;
5427c478bd9Sstevel@tonic-gate 	int sz;
54398573c19SMarcel Telka 	int pipe_fd = -1;
544e72ff08dSGordon Ross 	int ret;
5457c478bd9Sstevel@tonic-gate 	int connmaxrec = RPC_MAXDATASIZE;
546e72ff08dSGordon Ross 	struct netconfig *nconf;
547e72ff08dSGordon Ross 	NCONF_HANDLE *nc;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	addrix = 0;
5507c478bd9Sstevel@tonic-gate 	pathix = 0;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	(void) gethostname(hostname, MAXHOSTNAMELEN);
5537c478bd9Sstevel@tonic-gate 	if (init_hostname() < 0)
5547c478bd9Sstevel@tonic-gate 		exit(1);
5557c478bd9Sstevel@tonic-gate 
556e72ff08dSGordon Ross 	ret = nfs_smf_get_iprop("statd_port", &statd_port,
557e72ff08dSGordon Ross 	    DEFAULT_INSTANCE, SCF_TYPE_INTEGER, STATD);
558e72ff08dSGordon Ross 	if (ret != SA_OK) {
559e72ff08dSGordon Ross 		syslog(LOG_ERR, "Reading of statd_port from SMF "
560e72ff08dSGordon Ross 		    "failed, using default value");
561e72ff08dSGordon Ross 	}
562e72ff08dSGordon Ross 
563e72ff08dSGordon Ross 	while ((c = getopt(argc, argv, "Dd:a:G:p:P:rU:")) != EOF)
5647c478bd9Sstevel@tonic-gate 		switch (c) {
5657c478bd9Sstevel@tonic-gate 		case 'd':
5667c478bd9Sstevel@tonic-gate 			(void) sscanf(optarg, "%d", &debug);
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 		case 'D':
5697c478bd9Sstevel@tonic-gate 			choice = 1;
5707c478bd9Sstevel@tonic-gate 			break;
5717c478bd9Sstevel@tonic-gate 		case 'a':
5727c478bd9Sstevel@tonic-gate 			if (addrix < host_name_count) {
5737c478bd9Sstevel@tonic-gate 				if (strcmp(hostname, optarg) != 0) {
5747c478bd9Sstevel@tonic-gate 					sz = strlen(optarg);
5757c478bd9Sstevel@tonic-gate 					if (sz < MAXHOSTNAMELEN) {
5767c478bd9Sstevel@tonic-gate 						host_name[addrix] =
5775c6b0397SPavel Filipensky 						    (char *)xmalloc(sz+1);
5787c478bd9Sstevel@tonic-gate 						if (host_name[addrix] !=
5797c478bd9Sstevel@tonic-gate 						    NULL) {
5807c478bd9Sstevel@tonic-gate 						(void) sscanf(optarg, "%s",
5815c6b0397SPavel Filipensky 						    host_name[addrix]);
5827c478bd9Sstevel@tonic-gate 							addrix++;
5837c478bd9Sstevel@tonic-gate 						}
5847c478bd9Sstevel@tonic-gate 					} else
5857c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5867c478bd9Sstevel@tonic-gate 				    "statd: -a name of host is too long.\n");
5877c478bd9Sstevel@tonic-gate 				}
5887c478bd9Sstevel@tonic-gate 			} else
5897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5907c478bd9Sstevel@tonic-gate 				    "statd: -a exceeding maximum hostnames\n");
5917c478bd9Sstevel@tonic-gate 			break;
5929acbbeafSnn 		case 'U':
5939acbbeafSnn 			(void) sscanf(optarg, "%d", &daemon_uid);
5949acbbeafSnn 			break;
5959acbbeafSnn 		case 'G':
5969acbbeafSnn 			(void) sscanf(optarg, "%d", &daemon_gid);
5979acbbeafSnn 			break;
5987c478bd9Sstevel@tonic-gate 		case 'p':
5997c478bd9Sstevel@tonic-gate 			if (strlen(optarg) < MAXPATHLEN) {
6007c478bd9Sstevel@tonic-gate 				/* If the path_name array has not yet	   */
6017c478bd9Sstevel@tonic-gate 				/* been malloc'ed, do that.  The array	   */
6027c478bd9Sstevel@tonic-gate 				/* should be big enough to hold all of the */
6037c478bd9Sstevel@tonic-gate 				/* -p options we might have.  An upper	   */
6047c478bd9Sstevel@tonic-gate 				/* bound on the number of -p options is	   */
6057c478bd9Sstevel@tonic-gate 				/* argc/2, because each -p option consumes */
6067c478bd9Sstevel@tonic-gate 				/* two arguments.  Here the upper bound	   */
6077c478bd9Sstevel@tonic-gate 				/* is supposing that all the command line  */
6087c478bd9Sstevel@tonic-gate 				/* arguments are -p options, which would   */
6097c478bd9Sstevel@tonic-gate 				/* actually never be the case.		   */
6107c478bd9Sstevel@tonic-gate 				if (path_name == NULL) {
6117c478bd9Sstevel@tonic-gate 					size_t sz = (argc/2) * sizeof (char *);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 					path_name = (char **)malloc(sz);
6147c478bd9Sstevel@tonic-gate 					if (path_name == NULL) {
6157c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6167c478bd9Sstevel@tonic-gate 						"statd: malloc failed\n");
6177c478bd9Sstevel@tonic-gate 						exit(1);
6187c478bd9Sstevel@tonic-gate 					}
6197c478bd9Sstevel@tonic-gate 					(void) memset(path_name, 0, sz);
6207c478bd9Sstevel@tonic-gate 				}
6217c478bd9Sstevel@tonic-gate 				path_name[pathix] = optarg;
6227c478bd9Sstevel@tonic-gate 				pathix++;
6237c478bd9Sstevel@tonic-gate 			} else {
6247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
625e72ff08dSGordon Ross 				    "statd: -p pathname is too long.\n");
626e72ff08dSGordon Ross 			}
627e72ff08dSGordon Ross 			break;
628e72ff08dSGordon Ross 		case 'P':
629e72ff08dSGordon Ross 			(void) sscanf(optarg, "%d", &statd_port);
630e72ff08dSGordon Ross 			if (statd_port < 1 || statd_port > UINT16_MAX) {
631e72ff08dSGordon Ross 				(void) fprintf(stderr,
632e72ff08dSGordon Ross 				    "statd: -P port invalid.\n");
633e72ff08dSGordon Ross 				statd_port = 0;
6347c478bd9Sstevel@tonic-gate 			}
6357c478bd9Sstevel@tonic-gate 			break;
6367c478bd9Sstevel@tonic-gate 		case 'r':
6377c478bd9Sstevel@tonic-gate 			regfiles_only = 1;
6387c478bd9Sstevel@tonic-gate 			break;
6397c478bd9Sstevel@tonic-gate 		default:
6407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
641e72ff08dSGordon Ross 			    "statd [-d level] [-D]\n");
6427c478bd9Sstevel@tonic-gate 			return (1);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (choice == 0) {
6467c478bd9Sstevel@tonic-gate 		(void) strcpy(statd_home, home0);
6477c478bd9Sstevel@tonic-gate 		(void) strcpy(CURRENT, current0);
6487c478bd9Sstevel@tonic-gate 		(void) strcpy(BACKUP, backup0);
6497c478bd9Sstevel@tonic-gate 		(void) strcpy(STATE, state0);
6507c478bd9Sstevel@tonic-gate 	} else {
6517c478bd9Sstevel@tonic-gate 		(void) strcpy(statd_home, home1);
6527c478bd9Sstevel@tonic-gate 		(void) strcpy(CURRENT, current1);
6537c478bd9Sstevel@tonic-gate 		(void) strcpy(BACKUP, backup1);
6547c478bd9Sstevel@tonic-gate 		(void) strcpy(STATE, state1);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	if (debug)
6577c478bd9Sstevel@tonic-gate 		(void) printf("debug is on, create entry: %s, %s, %s\n",
6585c6b0397SPavel Filipensky 		    CURRENT, BACKUP, STATE);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl))
6617c478bd9Sstevel@tonic-gate 		(void) printf("statd: getrlimit failed. \n");
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/* Set maxfdlimit current soft limit */
6645c6b0397SPavel Filipensky 	rl.rlim_cur = rl.rlim_max;
6657c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
6667c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "statd: unable to set RLIMIT_NOFILE to %d\n",
6675c6b0397SPavel Filipensky 		    rl.rlim_cur);
6687c478bd9Sstevel@tonic-gate 
669004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
670004388ebScasper 
6717c478bd9Sstevel@tonic-gate 	if (!debug) {
67298573c19SMarcel Telka 		pipe_fd = daemonize_init();
67398573c19SMarcel Telka 
6747c478bd9Sstevel@tonic-gate 		openlog("statd", LOG_PID, LOG_DAEMON);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6779acbbeafSnn 	(void) _create_daemon_lock(STATD, daemon_uid, daemon_gid);
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * establish our lock on the lock file and write our pid to it.
6807c478bd9Sstevel@tonic-gate 	 * exit if some other process holds the lock, or if there's any
6817c478bd9Sstevel@tonic-gate 	 * error in writing/locking the file.
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	ppid = _enter_daemon_lock(STATD);
6847c478bd9Sstevel@tonic-gate 	switch (ppid) {
6857c478bd9Sstevel@tonic-gate 	case 0:
6867c478bd9Sstevel@tonic-gate 		break;
6877c478bd9Sstevel@tonic-gate 	case -1:
6887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "error locking for %s: %s", STATD,
6897c478bd9Sstevel@tonic-gate 		    strerror(errno));
6907c478bd9Sstevel@tonic-gate 		exit(2);
6917c478bd9Sstevel@tonic-gate 	default:
6927c478bd9Sstevel@tonic-gate 		/* daemon was already running */
6937c478bd9Sstevel@tonic-gate 		exit(0);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
69698573c19SMarcel Telka 	mutex_init(&merges_lock, USYNC_THREAD, NULL);
69798573c19SMarcel Telka 	cond_init(&merges_cond, USYNC_THREAD, NULL);
69898573c19SMarcel Telka 	in_merges = B_TRUE;
6997c478bd9Sstevel@tonic-gate 
70098573c19SMarcel Telka 	/*
70198573c19SMarcel Telka 	 * Create thr_statd_merges() thread to populate the host_name list
70298573c19SMarcel Telka 	 * asynchronously.
70398573c19SMarcel Telka 	 */
70498573c19SMarcel Telka 	if (thr_create(NULL, 0, (void *(*)(void *))thr_statd_merges, NULL,
70598573c19SMarcel Telka 	    THR_DETACHED, NULL) != 0) {
70698573c19SMarcel Telka 		syslog(LOG_ERR, "statd: unable to create thread for "
70798573c19SMarcel Telka 		    "thr_statd_merges().");
70898573c19SMarcel Telka 		exit(1);
70998573c19SMarcel Telka 	}
710bbaa8b60SDan Kruchinin 
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * Set to automatic mode such that threads are automatically
7137c478bd9Sstevel@tonic-gate 	 * created
7147c478bd9Sstevel@tonic-gate 	 */
7157c478bd9Sstevel@tonic-gate 	mode = RPC_SVC_MT_AUTO;
7167c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {
7177c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
7185c6b0397SPavel Filipensky 		    "statd:unable to set automatic MT mode.");
7197c478bd9Sstevel@tonic-gate 		exit(1);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Set non-blocking mode and maximum record size for
7247c478bd9Sstevel@tonic-gate 	 * connection oriented RPC transports.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
7277c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "unable to set maximum RPC record size");
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
730e72ff08dSGordon Ross 	/*
731e72ff08dSGordon Ross 	 * Enumerate network transports and create service listeners
732e72ff08dSGordon Ross 	 * as appropriate for each.
733e72ff08dSGordon Ross 	 */
734e72ff08dSGordon Ross 	if ((nc = setnetconfig()) == NULL) {
735e72ff08dSGordon Ross 		syslog(LOG_ERR, "setnetconfig failed: %m");
736e72ff08dSGordon Ross 		return (-1);
7377c478bd9Sstevel@tonic-gate 	}
738e72ff08dSGordon Ross 	while ((nconf = getnetconfig(nc)) != NULL) {
739e72ff08dSGordon Ross 
740e72ff08dSGordon Ross 		/*
741e72ff08dSGordon Ross 		 * Skip things like tpi_raw, invisible...
742e72ff08dSGordon Ross 		 */
743e72ff08dSGordon Ross 		if ((nconf->nc_flag & NC_VISIBLE) == 0)
744e72ff08dSGordon Ross 			continue;
745e72ff08dSGordon Ross 		if (nconf->nc_semantics != NC_TPI_CLTS &&
746e72ff08dSGordon Ross 		    nconf->nc_semantics != NC_TPI_COTS &&
747e72ff08dSGordon Ross 		    nconf->nc_semantics != NC_TPI_COTS_ORD)
748e72ff08dSGordon Ross 			continue;
7497c478bd9Sstevel@tonic-gate 
750e72ff08dSGordon Ross 		sm_svc_tp_create(nconf);
7517c478bd9Sstevel@tonic-gate 	}
752e72ff08dSGordon Ross 	(void) endnetconfig(nc);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Make sure /var/statmon and any alternate (-p) statmon
7567c478bd9Sstevel@tonic-gate 	 * directories exist and are owned by daemon.  Then change our uid
7577c478bd9Sstevel@tonic-gate 	 * to daemon.  The uid change is to prevent attacks against local
7587c478bd9Sstevel@tonic-gate 	 * daemons that trust any call from a local root process.
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	set_statmon_owner();
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 *
7657c478bd9Sstevel@tonic-gate 	 * statd now runs as a daemon rather than root and can not
7667c478bd9Sstevel@tonic-gate 	 * dump core under / because of the permission. It is
7677c478bd9Sstevel@tonic-gate 	 * important that current working directory of statd be
7687c478bd9Sstevel@tonic-gate 	 * changed to writable directory /var/statmon so that it
7697c478bd9Sstevel@tonic-gate 	 * can dump the core upon the receipt of the signal.
7707c478bd9Sstevel@tonic-gate 	 * One still need to set allow_setid_core to non-zero in
7717c478bd9Sstevel@tonic-gate 	 * /etc/system to get the core dump.
7727c478bd9Sstevel@tonic-gate 	 *
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (chdir(statd_home) < 0) {
7767c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "can't chdir %s: %m", statd_home);
7777c478bd9Sstevel@tonic-gate 		exit(1);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	copy_client_names();
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	rwlock_init(&thr_rwlock, USYNC_THREAD, NULL);
7837c478bd9Sstevel@tonic-gate 	mutex_init(&crash_lock, USYNC_THREAD, NULL);
7847c478bd9Sstevel@tonic-gate 	mutex_init(&name_addrlock, USYNC_THREAD, NULL);
7857c478bd9Sstevel@tonic-gate 	cond_init(&retrywait, USYNC_THREAD, NULL);
7867c478bd9Sstevel@tonic-gate 	sm_inithash();
7877c478bd9Sstevel@tonic-gate 	die = 0;
7887c478bd9Sstevel@tonic-gate 	/*
7897c478bd9Sstevel@tonic-gate 	 * This variable is set to ensure that an sm_crash
7907c478bd9Sstevel@tonic-gate 	 * request will not be done at the same time
7917c478bd9Sstevel@tonic-gate 	 * when a statd_init is being done, since sm_crash
7927c478bd9Sstevel@tonic-gate 	 * can reset some variables that statd_init will be using.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	in_crash = 1;
7957c478bd9Sstevel@tonic-gate 	statd_init();
7967c478bd9Sstevel@tonic-gate 
79798573c19SMarcel Telka 	/*
79898573c19SMarcel Telka 	 * statd is up and running as far as we are concerned.
79998573c19SMarcel Telka 	 */
80098573c19SMarcel Telka 	daemonize_fini(pipe_fd);
80198573c19SMarcel Telka 
8027c478bd9Sstevel@tonic-gate 	if (debug)
8037c478bd9Sstevel@tonic-gate 		(void) printf("Starting svc_run\n");
8047c478bd9Sstevel@tonic-gate 	svc_run();
8057c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "statd: svc_run returned\n");
8067c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
80798573c19SMarcel Telka 	thr_exit((void *)1);
80811606941Sjwahlig 	return (0);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Make sure the ownership of the statmon directories is correct, then
8147c478bd9Sstevel@tonic-gate  * change our uid to match.  If the top-level directories (/var/statmon, -p
8157c478bd9Sstevel@tonic-gate  * arguments) don't exist, they are created first.  The sm and sm.bak
8167c478bd9Sstevel@tonic-gate  * directories are not created here, but if they already exist, they are
8177c478bd9Sstevel@tonic-gate  * chowned to the correct uid, along with anything else in the
8187c478bd9Sstevel@tonic-gate  * directories.
8197c478bd9Sstevel@tonic-gate  */
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate static void
set_statmon_owner(void)82245916cd2Sjpk set_statmon_owner(void)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	int i;
82545916cd2Sjpk 	boolean_t can_do_mlp;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	/*
8287c478bd9Sstevel@tonic-gate 	 * Recursively chown/chgrp /var/statmon and the alternate paths,
8297c478bd9Sstevel@tonic-gate 	 * creating them if necessary.
8307c478bd9Sstevel@tonic-gate 	 */
8317c478bd9Sstevel@tonic-gate 	one_statmon_owner(statd_home);
8327c478bd9Sstevel@tonic-gate 	for (i = 0; i < pathix; i++) {
8337c478bd9Sstevel@tonic-gate 		char alt_path[MAXPATHLEN];
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		snprintf(alt_path, MAXPATHLEN, "%s/statmon", path_name[i]);
8367c478bd9Sstevel@tonic-gate 		one_statmon_owner(alt_path);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
83945916cd2Sjpk 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
8407c478bd9Sstevel@tonic-gate 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
8419acbbeafSnn 	    daemon_uid, daemon_gid, can_do_mlp ? PRIV_NET_BINDMLP : NULL,
84245916cd2Sjpk 	    NULL) == -1) {
8437c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "can't run unprivileged: %m");
8447c478bd9Sstevel@tonic-gate 		exit(1);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
84898573c19SMarcel Telka 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate  * Copy client names from the alternate statmon directories into
8537c478bd9Sstevel@tonic-gate  * /var/statmon.  The top-level (statmon) directories should already
8547c478bd9Sstevel@tonic-gate  * exist, though the sm and sm.bak directories might not.
8557c478bd9Sstevel@tonic-gate  */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate static void
copy_client_names(void)85898573c19SMarcel Telka copy_client_names(void)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	int i;
8617c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN+SM_MAXPATHLEN];
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * Copy all clients from alternate paths to /var/statmon/sm
8657c478bd9Sstevel@tonic-gate 	 * Remove the files in alternate directory when copying is done.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	for (i = 0; i < pathix; i++) {
8687c478bd9Sstevel@tonic-gate 		/*
8697c478bd9Sstevel@tonic-gate 		 * If the alternate directories do not exist, create it.
8707c478bd9Sstevel@tonic-gate 		 * If they do exist, just do the copy.
8717c478bd9Sstevel@tonic-gate 		 */
8727c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "%s/statmon/sm", path_name[i]);
8737c478bd9Sstevel@tonic-gate 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
8747c478bd9Sstevel@tonic-gate 			if (errno != EEXIST) {
8757c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
8765c6b0397SPavel Filipensky 				    "can't mkdir %s: %m\n", buf);
8777c478bd9Sstevel@tonic-gate 				continue;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 			copydir_from_to(buf, CURRENT);
8807c478bd9Sstevel@tonic-gate 			(void) remove_dir(buf);
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s/statmon/sm.bak",
8845c6b0397SPavel Filipensky 		    path_name[i]);
8857c478bd9Sstevel@tonic-gate 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
8867c478bd9Sstevel@tonic-gate 			if (errno != EEXIST) {
8877c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
8885c6b0397SPavel Filipensky 				    "can't mkdir %s: %m\n", buf);
8897c478bd9Sstevel@tonic-gate 				continue;
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 			copydir_from_to(buf, BACKUP);
8927c478bd9Sstevel@tonic-gate 			(void) remove_dir(buf);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * Create the given directory if it doesn't already exist.  Set the user
8997c478bd9Sstevel@tonic-gate  * and group to daemon for the directory and anything under it.
9007c478bd9Sstevel@tonic-gate  */
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate static void
one_statmon_owner(const char * dir)9037c478bd9Sstevel@tonic-gate one_statmon_owner(const char *dir)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate 	if ((mkdir(dir, SM_DIRECTORY_MODE)) == -1) {
9067c478bd9Sstevel@tonic-gate 		if (errno != EEXIST) {
9077c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "can't mkdir %s: %m",
9087c478bd9Sstevel@tonic-gate 			    dir);
9097c478bd9Sstevel@tonic-gate 			return;
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (debug)
9147c478bd9Sstevel@tonic-gate 		printf("Setting owner for %s\n", dir);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (nftw(dir, nftw_owner, MAX_FDS, FTW_PHYS) != 0) {
9177c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "error setting owner for %s: %m",
9187c478bd9Sstevel@tonic-gate 		    dir);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate /*
9237c478bd9Sstevel@tonic-gate  * Set the user and group to daemon for the given file or directory.  If
9247c478bd9Sstevel@tonic-gate  * it's a directory, also makes sure that it is mode 755.
9257c478bd9Sstevel@tonic-gate  * Generates a syslog message but does not return an error if there were
9267c478bd9Sstevel@tonic-gate  * problems.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
9307c478bd9Sstevel@tonic-gate static int
nftw_owner(const char * path,const struct stat * statp,int info,struct FTW * ftw)9317c478bd9Sstevel@tonic-gate nftw_owner(const char *path, const struct stat *statp, int info,
932e72ff08dSGordon Ross     struct FTW *ftw)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	if (!(info == FTW_F || info == FTW_D))
9357c478bd9Sstevel@tonic-gate 		return (0);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Some older systems might have mode 777 directories.  Fix that.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (info == FTW_D && (statp->st_mode & (S_IWGRP | S_IWOTH)) != 0) {
9427c478bd9Sstevel@tonic-gate 		mode_t newmode = (statp->st_mode & ~(S_IWGRP | S_IWOTH)) &
9435c6b0397SPavel Filipensky 		    S_IAMB;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		if (debug)
9467c478bd9Sstevel@tonic-gate 			printf("chmod %03o %s\n", newmode, path);
9477c478bd9Sstevel@tonic-gate 		if (chmod(path, newmode) < 0) {
9487c478bd9Sstevel@tonic-gate 			int error = errno;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "can't chmod %s to %03o: %m",
9517c478bd9Sstevel@tonic-gate 			    path, newmode);
9527c478bd9Sstevel@tonic-gate 			if (debug)
9537c478bd9Sstevel@tonic-gate 				printf("  FAILED: %s\n", strerror(error));
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/* If already owned by daemon, don't bother changing. */
9589acbbeafSnn 	if (statp->st_uid == daemon_uid &&
9599acbbeafSnn 	    statp->st_gid == daemon_gid)
9607c478bd9Sstevel@tonic-gate 		return (0);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (debug)
9637c478bd9Sstevel@tonic-gate 		printf("lchown %s daemon:daemon\n", path);
9649acbbeafSnn 	if (lchown(path, daemon_uid, daemon_gid) < 0) {
9657c478bd9Sstevel@tonic-gate 		int error = errno;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "can't chown %s to daemon: %m",
9687c478bd9Sstevel@tonic-gate 		    path);
9697c478bd9Sstevel@tonic-gate 		if (debug)
9707c478bd9Sstevel@tonic-gate 			printf("  FAILED: %s\n", strerror(error));
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	return (0);
9747c478bd9Sstevel@tonic-gate }
975