1bbaa8b6Dan Kruchinin/*
2bbaa8b6Dan Kruchinin * CDDL HEADER START
3bbaa8b6Dan Kruchinin *
4bbaa8b6Dan Kruchinin * The contents of this file are subject to the terms of the
5bbaa8b6Dan Kruchinin * Common Development and Distribution License (the "License").
6bbaa8b6Dan Kruchinin * You may not use this file except in compliance with the License.
7bbaa8b6Dan Kruchinin *
8bbaa8b6Dan Kruchinin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9bbaa8b6Dan Kruchinin * or http://www.opensolaris.org/os/licensing.
10bbaa8b6Dan Kruchinin * See the License for the specific language governing permissions
11bbaa8b6Dan Kruchinin * and limitations under the License.
12bbaa8b6Dan Kruchinin *
13bbaa8b6Dan Kruchinin * When distributing Covered Code, include this CDDL HEADER in each
14bbaa8b6Dan Kruchinin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15bbaa8b6Dan Kruchinin * If applicable, add the following below this CDDL HEADER, with the
16bbaa8b6Dan Kruchinin * fields enclosed by brackets "[]" replaced with your own identifying
17bbaa8b6Dan Kruchinin * information: Portions Copyright [yyyy] [name of copyright owner]
18bbaa8b6Dan Kruchinin *
19bbaa8b6Dan Kruchinin * CDDL HEADER END
20bbaa8b6Dan Kruchinin */
21bbaa8b6Dan Kruchinin
22bbaa8b6Dan Kruchinin/*
23bbaa8b6Dan Kruchinin * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24bbaa8b6Dan Kruchinin * Copyright (c) 2012 by Delphix. All rights reserved.
2542cdb25Marcel Telka * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26bbaa8b6Dan Kruchinin */
27bbaa8b6Dan Kruchinin
28bbaa8b6Dan Kruchinin/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T		*/
29bbaa8b6Dan Kruchinin/*	  All Rights Reserved  	*/
30bbaa8b6Dan Kruchinin
31bbaa8b6Dan Kruchinin/*
32bbaa8b6Dan Kruchinin * University Copyright- Copyright (c) 1982, 1986, 1988
33bbaa8b6Dan Kruchinin * The Regents of the University of California
34bbaa8b6Dan Kruchinin * All Rights Reserved
35bbaa8b6Dan Kruchinin *
36bbaa8b6Dan Kruchinin * University Acknowledgment- Portions of this document are derived from
37bbaa8b6Dan Kruchinin * software developed by the University of California, Berkeley, and its
38bbaa8b6Dan Kruchinin * contributors.
39bbaa8b6Dan Kruchinin */
40bbaa8b6Dan Kruchinin
41bbaa8b6Dan Kruchinin/* LINTLIBRARY */
42bbaa8b6Dan Kruchinin/* PROTOLIB1 */
43bbaa8b6Dan Kruchinin
44bbaa8b6Dan Kruchinin/*
45bbaa8b6Dan Kruchinin * NLM server
46bbaa8b6Dan Kruchinin *
47bbaa8b6Dan Kruchinin * Most of this copied from ../nfsd/nfsd.c
48bbaa8b6Dan Kruchinin * and then s:NFS:NLM: applied, etc.
49bbaa8b6Dan Kruchinin */
50bbaa8b6Dan Kruchinin
51bbaa8b6Dan Kruchinin#include <sys/param.h>
52bbaa8b6Dan Kruchinin#include <sys/types.h>
53bbaa8b6Dan Kruchinin#include <sys/stat.h>
54bbaa8b6Dan Kruchinin#include <syslog.h>
55bbaa8b6Dan Kruchinin#include <tiuser.h>
56bbaa8b6Dan Kruchinin#include <rpc/rpc.h>
57bbaa8b6Dan Kruchinin#include <errno.h>
58bbaa8b6Dan Kruchinin#include <thread.h>
59bbaa8b6Dan Kruchinin#include <sys/time.h>
60bbaa8b6Dan Kruchinin#include <sys/file.h>
61bbaa8b6Dan Kruchinin#include <nfs/nfs.h>
62bbaa8b6Dan Kruchinin#include <nfs/nfssys.h>
63bbaa8b6Dan Kruchinin#include <stdio.h>
64bbaa8b6Dan Kruchinin#include <stdio_ext.h>
65bbaa8b6Dan Kruchinin#include <stdlib.h>
66bbaa8b6Dan Kruchinin#include <signal.h>
67bbaa8b6Dan Kruchinin#include <netconfig.h>
68bbaa8b6Dan Kruchinin#include <netdir.h>
69bbaa8b6Dan Kruchinin#include <string.h>
70bbaa8b6Dan Kruchinin#include <unistd.h>
71bbaa8b6Dan Kruchinin#include <stropts.h>
72bbaa8b6Dan Kruchinin#include <sys/tihdr.h>
73bbaa8b6Dan Kruchinin#include <poll.h>
74bbaa8b6Dan Kruchinin#include <priv_utils.h>
75bbaa8b6Dan Kruchinin#include <sys/tiuser.h>
76bbaa8b6Dan Kruchinin#include <netinet/tcp.h>
77bbaa8b6Dan Kruchinin#include <deflt.h>
78bbaa8b6Dan Kruchinin#include <rpcsvc/daemon_utils.h>
79bbaa8b6Dan Kruchinin#include <rpcsvc/nlm_prot.h>
80bbaa8b6Dan Kruchinin#include <libintl.h>
81bbaa8b6Dan Kruchinin#include <libscf.h>
82bbaa8b6Dan Kruchinin#include <libshare.h>
83bbaa8b6Dan Kruchinin#include "nfs_tbind.h"
84bbaa8b6Dan Kruchinin#include "thrpool.h"
85bbaa8b6Dan Kruchinin#include "smfcfg.h"
86bbaa8b6Dan Kruchinin
87bbaa8b6Dan Kruchinin/* Option defaults.  See nfssys.h */
88bbaa8b6Dan Kruchininstruct lm_svc_args lmargs = {
89bbaa8b6Dan Kruchinin	.version = LM_SVC_CUR_VERS,
90bbaa8b6Dan Kruchinin	/* fd, n_fmly, n_proto, n_rdev (below) */
91bbaa8b6Dan Kruchinin	.debug = 0,
92bbaa8b6Dan Kruchinin	.timout = 5 * 60,
9342cdb25Marcel Telka	.grace = 90,
9442cdb25Marcel Telka	.retransmittimeout = 5
95bbaa8b6Dan Kruchinin};
96201f5ebSebastien Royint max_servers = 256;
97bbaa8b6Dan Kruchinin
98bbaa8b6Dan Kruchinin
99bbaa8b6Dan Kruchinin#define	RET_OK		0	/* return code for no error */
100bbaa8b6Dan Kruchinin#define	RET_ERR		33	/* return code for error(s) */
101bbaa8b6Dan Kruchinin
102bbaa8b6Dan Kruchininstatic	int	nlmsvc(int fd, struct netbuf addrmask,
103bbaa8b6Dan Kruchinin			struct netconfig *nconf);
104bbaa8b6Dan Kruchininstatic int nlmsvcpool(int max_servers);
105bbaa8b6Dan Kruchininstatic	void	usage(void);
106bbaa8b6Dan Kruchinin
107bbaa8b6Dan Kruchininextern	int	_nfssys(int, void *);
108bbaa8b6Dan Kruchininstatic void sigterm_handler(void);
109bbaa8b6Dan Kruchininstatic void shutdown_lockd(void);
110bbaa8b6Dan Kruchinin
111bbaa8b6Dan Kruchininextern int	daemonize_init(void);
112bbaa8b6Dan Kruchininextern void	daemonize_fini(int fd);
113bbaa8b6Dan Kruchinin
114bbaa8b6Dan Kruchininstatic	char	*MyName;
115bbaa8b6Dan Kruchinin
116bbaa8b6Dan Kruchinin/*
117bbaa8b6Dan Kruchinin * We want to bind to these TLI providers, and in this order,
118bbaa8b6Dan Kruchinin * because the kernel NLM needs the loopback first for its
119bbaa8b6Dan Kruchinin * initialization. (It uses it to talk to statd.)
120bbaa8b6Dan Kruchinin */
121bbaa8b6Dan Kruchininstatic  NETSELDECL(defaultproviders)[] = {
122bbaa8b6Dan Kruchinin	"/dev/ticotsord",
123bbaa8b6Dan Kruchinin	"/dev/tcp",
124bbaa8b6Dan Kruchinin	"/dev/udp",
125bbaa8b6Dan Kruchinin	"/dev/tcp6",
126bbaa8b6Dan Kruchinin	"/dev/udp6",
127bbaa8b6Dan Kruchinin	NULL
128bbaa8b6Dan Kruchinin};
129bbaa8b6Dan Kruchinin
130bbaa8b6Dan Kruchinin/*
131bbaa8b6Dan Kruchinin * The following are all globals used by routines in nfs_tbind.c.
132bbaa8b6Dan Kruchinin */
133bbaa8b6Dan Kruchininsize_t	end_listen_fds;		/* used by conn_close_oldest() */
134bbaa8b6Dan Kruchininsize_t	num_fds = 0;		/* used by multiple routines */
135bbaa8b6Dan Kruchininint	listen_backlog = 32;	/* used by bind_to_{provider,proto}() */
136bbaa8b6Dan Kruchininint	(*Mysvc)(int, struct netbuf, struct netconfig *) = nlmsvc;
137bbaa8b6Dan Kruchinin				/* used by cots_listen_event() */
138bbaa8b6Dan Kruchininint	max_conns_allowed = -1;	/* used by cots_listen_event() */
139bbaa8b6Dan Kruchinin
140bbaa8b6Dan Kruchininint
141bbaa8b6Dan Kruchininmain(int ac, char *av[])
142bbaa8b6Dan Kruchinin{
143bbaa8b6Dan Kruchinin	char *propname = NULL;
144bbaa8b6Dan Kruchinin	char *dir = "/";
145bbaa8b6Dan Kruchinin	char *provider = (char *)NULL;
146bbaa8b6Dan Kruchinin	struct protob *protobp;
147bbaa8b6Dan Kruchinin	NETSELPDECL(providerp);
148bbaa8b6Dan Kruchinin	sigset_t sgset;
149bbaa8b6Dan Kruchinin	int i, c, pid, ret, val;
150bbaa8b6Dan Kruchinin	int pipe_fd = -1;
151bbaa8b6Dan Kruchinin	struct sigaction act;
152bbaa8b6Dan Kruchinin
153bbaa8b6Dan Kruchinin	MyName = *av;
154bbaa8b6Dan Kruchinin
155bbaa8b6Dan Kruchinin	/*
156bbaa8b6Dan Kruchinin	 * Initializations that require more privileges than we need to run.
157bbaa8b6Dan Kruchinin	 */
158bbaa8b6Dan Kruchinin	(void) _create_daemon_lock(LOCKD, DAEMON_UID, DAEMON_GID);
159bbaa8b6Dan Kruchinin	svcsetprio();
160bbaa8b6Dan Kruchinin
161bbaa8b6Dan Kruchinin	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
162bbaa8b6Dan Kruchinin	    DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, NULL) == -1) {
163bbaa8b6Dan Kruchinin		(void) fprintf(stderr, "%s should be run with"
164bbaa8b6Dan Kruchinin		    " sufficient privileges\n", av[0]);
165bbaa8b6Dan Kruchinin		exit(1);
166bbaa8b6Dan Kruchinin	}
167bbaa8b6Dan Kruchinin
168bbaa8b6Dan Kruchinin	(void) enable_extended_FILE_stdio(-1, -1);
169bbaa8b6Dan Kruchinin
170bbaa8b6Dan Kruchinin	/*
171bbaa8b6Dan Kruchinin	 * Read in the values from SMF first before we check
172bbaa8b6Dan Kruchinin	 * command line options so the options override SMF values.
173bbaa8b6Dan Kruchinin	 */
174bbaa8b6Dan Kruchinin
175bbaa8b6Dan Kruchinin	/* How long to wait for clients to re-establish locks. */
176bbaa8b6Dan Kruchinin	propname = "grace_period"; /* also -g */
177bbaa8b6Dan Kruchinin	ret = nfs_smf_get_iprop(propname, &val,
179bbaa8b6Dan Kruchinin	if (ret == SA_OK) {
180bbaa8b6Dan Kruchinin		if (val <= 0)
181bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
182bbaa8b6Dan Kruchinin			    "Invalid %s from SMF"), propname);
183bbaa8b6Dan Kruchinin		else
184bbaa8b6Dan Kruchinin			lmargs.grace = val;
18542cdb25Marcel Telka	} else {
18642cdb25Marcel Telka		syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
18742cdb25Marcel Telka		    "value", propname);
188bbaa8b6Dan Kruchinin	}
189bbaa8b6Dan Kruchinin
19042cdb25Marcel Telka	propname = "lockd_listen_backlog"; /* also -l */
191bbaa8b6Dan Kruchinin	ret = nfs_smf_get_iprop(propname, &val,
193bbaa8b6Dan Kruchinin	if (ret == SA_OK) {
194bbaa8b6Dan Kruchinin		if (val <= 0)
195bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
196bbaa8b6Dan Kruchinin			    "Invalid %s from SMF"), propname);
197bbaa8b6Dan Kruchinin		else
198bbaa8b6Dan Kruchinin			listen_backlog = val;
19942cdb25Marcel Telka	} else {
20042cdb25Marcel Telka		syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
20142cdb25Marcel Telka		    "value", propname);
202bbaa8b6Dan Kruchinin	}
203bbaa8b6Dan Kruchinin
20442cdb25Marcel Telka	propname = "lockd_servers"; /* also argv[1] */
205bbaa8b6Dan Kruchinin	ret = nfs_smf_get_iprop(propname, &val,
207bbaa8b6Dan Kruchinin	if (ret == SA_OK) {
208bbaa8b6Dan Kruchinin		if (val <= 0)
209bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
210bbaa8b6Dan Kruchinin			    "Invalid %s from SMF"), propname);
211bbaa8b6Dan Kruchinin		else
212bbaa8b6Dan Kruchinin			max_servers = val;
21342cdb25Marcel Telka	} else {
21442cdb25Marcel Telka		syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
21542cdb25Marcel Telka		    "value", propname);
216bbaa8b6Dan Kruchinin	}
217bbaa8b6Dan Kruchinin
21842cdb25Marcel Telka	propname = "lockd_retransmit_timeout"; /* also -t */
219bbaa8b6Dan Kruchinin	ret = nfs_smf_get_iprop(propname, &val,
221bbaa8b6Dan Kruchinin	if (ret == SA_OK) {
222bbaa8b6Dan Kruchinin		if (val <= 0)
223bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
224bbaa8b6Dan Kruchinin			    "Invalid %s from SMF"), propname);
225bbaa8b6Dan Kruchinin		else
226bbaa8b6Dan Kruchinin			lmargs.retransmittimeout = val;
22742cdb25Marcel Telka	} else {
22842cdb25Marcel Telka		syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
22942cdb25Marcel Telka		    "value", propname);
230bbaa8b6Dan Kruchinin	}
231bbaa8b6Dan Kruchinin
23242cdb25Marcel Telka	while ((c = getopt(ac, av, "c:d:g:l:t:")) != EOF)
233bbaa8b6Dan Kruchinin		switch (c) {
234bbaa8b6Dan Kruchinin		case 'c': /* max_connections */
235bbaa8b6Dan Kruchinin			if ((val = atoi(optarg)) <= 0)
236bbaa8b6Dan Kruchinin				goto badval;
237bbaa8b6Dan Kruchinin			max_conns_allowed = val;
238bbaa8b6Dan Kruchinin			break;
239bbaa8b6Dan Kruchinin
240bbaa8b6Dan Kruchinin		case 'd': /* debug */
241bbaa8b6Dan Kruchinin			lmargs.debug = atoi(optarg);
242bbaa8b6Dan Kruchinin			break;
243bbaa8b6Dan Kruchinin
244bbaa8b6Dan Kruchinin		case 'g': /* grace_period */
245bbaa8b6Dan Kruchinin			if ((val = atoi(optarg)) <= 0)
246bbaa8b6Dan Kruchinin				goto badval;
247bbaa8b6Dan Kruchinin			lmargs.grace = val;
248bbaa8b6Dan Kruchinin			break;
249bbaa8b6Dan Kruchinin
250bbaa8b6Dan Kruchinin		case 'l': /* listen_backlog */
251bbaa8b6Dan Kruchinin			if ((val = atoi(optarg)) <= 0)
252bbaa8b6Dan Kruchinin				goto badval;
253bbaa8b6Dan Kruchinin			listen_backlog = val;
254bbaa8b6Dan Kruchinin			break;
255bbaa8b6Dan Kruchinin
25642cdb25Marcel Telka		case 't': /* retrans_timeout */
257bbaa8b6Dan Kruchinin			if ((val = atoi(optarg)) <= 0)
258bbaa8b6Dan Kruchinin				goto badval;
259bbaa8b6Dan Kruchinin			lmargs.retransmittimeout = val;
260bbaa8b6Dan Kruchinin			break;
261bbaa8b6Dan Kruchinin
262bbaa8b6Dan Kruchinin		badval:
263bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
264bbaa8b6Dan Kruchinin			    "Invalid -%c option value"), c);
265bbaa8b6Dan Kruchinin			/* FALLTHROUGH */
266bbaa8b6Dan Kruchinin		default:
267bbaa8b6Dan Kruchinin			usage();
268bbaa8b6Dan Kruchinin			/* NOTREACHED */
269bbaa8b6Dan Kruchinin		}
270bbaa8b6Dan Kruchinin
271bbaa8b6Dan Kruchinin	/*
272bbaa8b6Dan Kruchinin	 * If there is exactly one more argument, it is the number of
273bbaa8b6Dan Kruchinin	 * servers.
274bbaa8b6Dan Kruchinin	 */
275bbaa8b6Dan Kruchinin	if (optind < ac) {
276bbaa8b6Dan Kruchinin		val = atoi(av[optind]);
277bbaa8b6Dan Kruchinin		if (val <= 0) {
278bbaa8b6Dan Kruchinin			fprintf(stderr, gettext(
279bbaa8b6Dan Kruchinin			    "Invalid max_servers argument"));
280bbaa8b6Dan Kruchinin			usage();
281bbaa8b6Dan Kruchinin		}
282bbaa8b6Dan Kruchinin		max_servers = val;
283bbaa8b6Dan Kruchinin		optind++;
284bbaa8b6Dan Kruchinin	}
285bbaa8b6Dan Kruchinin	/*
286bbaa8b6Dan Kruchinin	 * If there are two or more arguments, then this is a usage error.
287bbaa8b6Dan Kruchinin	 */
288bbaa8b6Dan Kruchinin	if (optind != ac)
289bbaa8b6Dan Kruchinin		usage();
290bbaa8b6Dan Kruchinin
291bbaa8b6Dan Kruchinin	if (lmargs.debug) {
292bbaa8b6Dan Kruchinin		printf("%s: debug= %d, conn_idle_timout= %d,"
293bbaa8b6Dan Kruchinin		    " grace_period= %d, listen_backlog= %d,"
294bbaa8b6Dan Kruchinin		    " max_connections= %d, max_servers= %d,"
295bbaa8b6Dan Kruchinin		    " retrans_timeout= %d\n",
296bbaa8b6Dan Kruchinin		    MyName, lmargs.debug, lmargs.timout,
297bbaa8b6Dan Kruchinin		    lmargs.grace, listen_backlog,
298bbaa8b6Dan Kruchinin		    max_conns_allowed, max_servers,
299bbaa8b6Dan Kruchinin		    lmargs.retransmittimeout);
300bbaa8b6Dan Kruchinin	}
301bbaa8b6Dan Kruchinin
302bbaa8b6Dan Kruchinin	/*
303bbaa8b6Dan Kruchinin	 * Set current dir to server root
304bbaa8b6Dan Kruchinin	 */
305bbaa8b6Dan Kruchinin	if (chdir(dir) < 0) {
306bbaa8b6Dan Kruchinin		(void) fprintf(stderr, "%s:  ", MyName);
307bbaa8b6Dan Kruchinin		perror(dir);
308bbaa8b6Dan Kruchinin		exit(1);
309bbaa8b6Dan Kruchinin	}
310bbaa8b6Dan Kruchinin
311bbaa8b6Dan Kruchinin	/* Daemonize, if not debug. */
312bbaa8b6Dan Kruchinin	if (lmargs.debug == 0)
313bbaa8b6Dan Kruchinin		pipe_fd = daemonize_init();
314bbaa8b6Dan Kruchinin
315bbaa8b6Dan Kruchinin	openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);
316bbaa8b6Dan Kruchinin
317bbaa8b6Dan Kruchinin	/*
318bbaa8b6Dan Kruchinin	 * establish our lock on the lock file and write our pid to it.
319bbaa8b6Dan Kruchinin	 * exit if some other process holds the lock, or if there's any
320bbaa8b6Dan Kruchinin	 * error in writing/locking the file.
321bbaa8b6Dan Kruchinin	 */
322bbaa8b6Dan Kruchinin	pid = _enter_daemon_lock(LOCKD);
323bbaa8b6Dan Kruchinin	switch (pid) {
324bbaa8b6Dan Kruchinin	case 0:
325bbaa8b6Dan Kruchinin		break;
326bbaa8b6Dan Kruchinin	case -1:
327bbaa8b6Dan Kruchinin		fprintf(stderr, "error locking for %s: %s", LOCKD,
328bbaa8b6Dan Kruchinin		    strerror(errno));
329bbaa8b6Dan Kruchinin		exit(2);
330bbaa8b6Dan Kruchinin	default:
331bbaa8b6Dan Kruchinin		/* daemon was already running */
332bbaa8b6Dan Kruchinin		exit(0);
333bbaa8b6Dan Kruchinin	}
334bbaa8b6Dan Kruchinin
335bbaa8b6Dan Kruchinin	/*
336bbaa8b6Dan Kruchinin	 * Block all signals till we spawn other
337bbaa8b6Dan Kruchinin	 * threads.
338bbaa8b6Dan Kruchinin	 */
339bbaa8b6Dan Kruchinin	(void) sigfillset(&sgset);
340bbaa8b6Dan Kruchinin	(void) thr_sigsetmask(SIG_BLOCK, &sgset, NULL);
341bbaa8b6Dan Kruchinin
342bbaa8b6Dan Kruchinin	/* Unregister any previous versions. */
343bbaa8b6Dan Kruchinin	for (i = NLM_VERS; i < NLM4_VERS; i++) {
344bbaa8b6Dan Kruchinin		svc_unreg(NLM_PROG, i);
345bbaa8b6Dan Kruchinin	}
346bbaa8b6Dan Kruchinin
347bbaa8b6Dan Kruchinin	/*
348bbaa8b6Dan Kruchinin	 * Set up kernel RPC thread pool for the NLM server.
349bbaa8b6Dan Kruchinin	 */
350bbaa8b6Dan Kruchinin	if (nlmsvcpool(max_servers)) {
351bbaa8b6Dan Kruchinin		fprintf(stderr, "Can't set up kernel NLM service: %s. Exiting",
352bbaa8b6Dan Kruchinin		    strerror(errno));
353bbaa8b6Dan Kruchinin		exit(1);
354bbaa8b6Dan Kruchinin	}
355bbaa8b6Dan Kruchinin
356bbaa8b6Dan Kruchinin	/*
357bbaa8b6Dan Kruchinin	 * Set up blocked thread to do LWP creation on behalf of the kernel.
358bbaa8b6Dan Kruchinin	 */
359bbaa8b6Dan Kruchinin	if (svcwait(NLM_SVCPOOL_ID)) {
360bbaa8b6Dan Kruchinin		fprintf(stderr, "Can't set up NLM pool creator: %s. Exiting",
361bbaa8b6Dan Kruchinin		    strerror(errno));
362bbaa8b6Dan Kruchinin		exit(1);
363bbaa8b6Dan Kruchinin	}
364bbaa8b6Dan Kruchinin
365bbaa8b6Dan Kruchinin	/*
366bbaa8b6Dan Kruchinin	 * Install atexit and sigterm handlers
367bbaa8b6Dan Kruchinin	 */
368bbaa8b6Dan Kruchinin	act.sa_handler = sigterm_handler;
369bbaa8b6Dan Kruchinin	act.sa_flags = 0;
370bbaa8b6Dan Kruchinin
371bbaa8b6Dan Kruchinin	(void) sigaction(SIGTERM, &act, NULL);
372bbaa8b6Dan Kruchinin	(void) atexit(shutdown_lockd);
373bbaa8b6Dan Kruchinin
374bbaa8b6Dan Kruchinin	/*
375bbaa8b6Dan Kruchinin	 * Now open up for signal delivery
376bbaa8b6Dan Kruchinin	 */
377bbaa8b6Dan Kruchinin	(void) thr_sigsetmask(SIG_UNBLOCK, &sgset, NULL);
378bbaa8b6Dan Kruchinin
379bbaa8b6Dan Kruchinin	/*
380bbaa8b6Dan Kruchinin	 * Build a protocol block list for registration.
381bbaa8b6Dan Kruchinin	 */
382bbaa8b6Dan Kruchinin	protobp = (struct protob *)malloc(sizeof (struct protob));
383bbaa8b6Dan Kruchinin	protobp->serv = "NLM";
384bbaa8b6Dan Kruchinin	protobp->versmin = NLM_VERS;
385bbaa8b6Dan Kruchinin	protobp->versmax = NLM4_VERS;
386bbaa8b6Dan Kruchinin	protobp->program = NLM_PROG;
387bbaa8b6Dan Kruchinin	protobp->next = (struct protob *)NULL;
388bbaa8b6Dan Kruchinin
389bbaa8b6Dan Kruchinin	for (providerp = defaultproviders;
390bbaa8b6Dan Kruchinin	    *providerp != NULL; providerp++) {
391bbaa8b6Dan Kruchinin		provider = *providerp;
392bbaa8b6Dan Kruchinin		do_one(provider, NULL, protobp, nlmsvc);
393bbaa8b6Dan Kruchinin	}
394bbaa8b6Dan Kruchinin
395bbaa8b6Dan Kruchinin	free(protobp);
396bbaa8b6Dan Kruchinin
397bbaa8b6Dan Kruchinin	if (num_fds == 0) {
398bbaa8b6Dan Kruchinin		fprintf(stderr, "Could not start NLM service for any protocol."
399bbaa8b6Dan Kruchinin		    " Exiting");
400bbaa8b6Dan Kruchinin		exit(1);
401bbaa8b6Dan Kruchinin	}
402bbaa8b6Dan Kruchinin
403bbaa8b6Dan Kruchinin	end_listen_fds = num_fds;
404bbaa8b6Dan Kruchinin
405bbaa8b6Dan Kruchinin	/*
406bbaa8b6Dan Kruchinin	 * lockd is up and running as far as we are concerned.
407bbaa8b6Dan Kruchinin	 */
408bbaa8b6Dan Kruchinin	if (lmargs.debug == 0)
409bbaa8b6Dan Kruchinin		daemonize_fini(pipe_fd);
410bbaa8b6Dan Kruchinin
411bbaa8b6Dan Kruchinin	/*
412bbaa8b6Dan Kruchinin	 * Get rid of unneeded privileges.
413bbaa8b6Dan Kruchinin	 */
414bbaa8b6Dan Kruchinin	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
415bbaa8b6Dan Kruchinin	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
416bbaa8b6Dan Kruchinin
417bbaa8b6Dan Kruchinin	/*
418bbaa8b6Dan Kruchinin	 * Poll for non-data control events on the transport descriptors.
419bbaa8b6Dan Kruchinin	 */
420bbaa8b6Dan Kruchinin	poll_for_action();
421bbaa8b6Dan Kruchinin
422bbaa8b6Dan Kruchinin	/*
423bbaa8b6Dan Kruchinin	 * If we get here, something failed in poll_for_action().
424bbaa8b6Dan Kruchinin	 */
425bbaa8b6Dan Kruchinin	return (1);
426bbaa8b6Dan Kruchinin}
427bbaa8b6Dan Kruchinin
428bbaa8b6Dan Kruchininstatic int
429bbaa8b6Dan Kruchininnlmsvcpool(int maxservers)
430bbaa8b6Dan Kruchinin{
431bbaa8b6Dan Kruchinin	struct svcpool_args npa;
432bbaa8b6Dan Kruchinin
433bbaa8b6Dan Kruchinin	npa.id = NLM_SVCPOOL_ID;
434bbaa8b6Dan Kruchinin	npa.maxthreads = maxservers;
435bbaa8b6Dan Kruchinin	npa.redline = 0;
436bbaa8b6Dan Kruchinin	npa.qsize = 0;
437bbaa8b6Dan Kruchinin	npa.timeout = 0;
438bbaa8b6Dan Kruchinin	npa.stksize = 0;
439bbaa8b6Dan Kruchinin	npa.max_same_xprt = 0;
440bbaa8b6Dan Kruchinin	return (_nfssys(SVCPOOL_CREATE, &npa));
441bbaa8b6Dan Kruchinin}
442bbaa8b6Dan Kruchinin
443bbaa8b6Dan Kruchininstatic int
444bbaa8b6Dan Kruchininncfmly_to_lmfmly(const char *ncfmly)
445bbaa8b6Dan Kruchinin{
446bbaa8b6Dan Kruchinin	if (0 == strcmp(ncfmly, NC_INET))
447bbaa8b6Dan Kruchinin		return (LM_INET);
448bbaa8b6Dan Kruchinin	if (0 == strcmp(ncfmly, NC_INET6))
449bbaa8b6Dan Kruchinin		return (LM_INET6);
450bbaa8b6Dan Kruchinin	if (0 == strcmp(ncfmly, NC_LOOPBACK))
451bbaa8b6Dan Kruchinin		return (LM_LOOPBACK);
452bbaa8b6Dan Kruchinin	return (-1);
453bbaa8b6Dan Kruchinin}
454bbaa8b6Dan Kruchinin
455bbaa8b6Dan Kruchininstatic int
456bbaa8b6Dan Kruchininnctype_to_lmprot(uint_t semantics)
457bbaa8b6Dan Kruchinin{
458bbaa8b6Dan Kruchinin	switch (semantics) {
459bbaa8b6Dan Kruchinin	case NC_TPI_CLTS:
460bbaa8b6Dan Kruchinin		return (LM_UDP);
461bbaa8b6Dan Kruchinin	case NC_TPI_COTS_ORD:
462bbaa8b6Dan Kruchinin		return (LM_TCP);
463bbaa8b6Dan Kruchinin	}
464bbaa8b6Dan Kruchinin	return (-1);
465bbaa8b6Dan Kruchinin}
466bbaa8b6Dan Kruchinin
467bbaa8b6Dan Kruchininstatic dev_t
468bbaa8b6Dan Kruchininncdev_to_rdev(const char *ncdev)
469bbaa8b6Dan Kruchinin{
470bbaa8b6Dan Kruchinin	struct stat st;
471bbaa8b6Dan Kruchinin
472bbaa8b6Dan Kruchinin	if (stat(ncdev, &st) < 0)
473bbaa8b6Dan Kruchinin		return (NODEV);
474bbaa8b6Dan Kruchinin	return (st.st_rdev);
475bbaa8b6Dan Kruchinin}
476bbaa8b6Dan Kruchinin
477bbaa8b6Dan Kruchininstatic void
478bbaa8b6Dan Kruchininsigterm_handler(void)
479bbaa8b6Dan Kruchinin{
480bbaa8b6Dan Kruchinin	/* to call atexit handler */
481bbaa8b6Dan Kruchinin	exit(0);
482bbaa8b6Dan Kruchinin}
483bbaa8b6Dan Kruchinin
484bbaa8b6Dan Kruchininstatic void
485bbaa8b6Dan Kruchininshutdown_lockd(void)
486bbaa8b6Dan Kruchinin{
487bbaa8b6Dan Kruchinin	(void) _nfssys(KILL_LOCKMGR, NULL);
488bbaa8b6Dan Kruchinin}
489bbaa8b6Dan Kruchinin
490bbaa8b6Dan Kruchinin
491bbaa8b6Dan Kruchinin/*
492bbaa8b6Dan Kruchinin * Establish NLM service thread.
493bbaa8b6Dan Kruchinin */
494bbaa8b6Dan Kruchininstatic int
495bbaa8b6Dan Kruchininnlmsvc(int fd, struct netbuf addrmask, struct netconfig *nconf)
496bbaa8b6Dan Kruchinin{
497bbaa8b6Dan Kruchinin	struct lm_svc_args lma;
498bbaa8b6Dan Kruchinin
499bbaa8b6Dan Kruchinin	lma = lmargs; /* init by struct copy */
500bbaa8b6Dan Kruchinin
501bbaa8b6Dan Kruchinin	/*
502bbaa8b6Dan Kruchinin	 * The kernel code needs to reconstruct a complete
503bbaa8b6Dan Kruchinin	 * knetconfig from n_fmly, n_proto.  We use these
504bbaa8b6Dan Kruchinin	 * two fields to convey the family and semantics.
505bbaa8b6Dan Kruchinin	 */
506bbaa8b6Dan Kruchinin	lma.fd = fd;
507bbaa8b6Dan Kruchinin	lma.n_fmly = ncfmly_to_lmfmly(nconf->nc_protofmly);
508bbaa8b6Dan Kruchinin	lma.n_proto = nctype_to_lmprot(nconf->nc_semantics);
509bbaa8b6Dan Kruchinin	lma.n_rdev = ncdev_to_rdev(nconf->nc_device);
510bbaa8b6Dan Kruchinin
511bbaa8b6Dan Kruchinin	return (_nfssys(LM_SVC, &lma));
512bbaa8b6Dan Kruchinin}
513bbaa8b6Dan Kruchinin
514bbaa8b6Dan Kruchininstatic void
515bbaa8b6Dan Kruchininusage(void)
516bbaa8b6Dan Kruchinin{
517bbaa8b6Dan Kruchinin	(void) fprintf(stderr, gettext(
518bbaa8b6Dan Kruchinin	    "usage: %s [options] [max_servers]\n"), MyName);
519bbaa8b6Dan Kruchinin	(void) fprintf(stderr, gettext(
520bbaa8b6Dan Kruchinin	    "options:  (see SMF property descriptions)\n"));
521bbaa8b6Dan Kruchinin	/* Note: don't translate these */
522bbaa8b6Dan Kruchinin	(void) fprintf(stderr, "\t-c max_connections\n");
523bbaa8b6Dan Kruchinin	(void) fprintf(stderr, "\t-d debug_level\n");
524bbaa8b6Dan Kruchinin	(void) fprintf(stderr, "\t-g grace_period\n");
525bbaa8b6Dan Kruchinin	(void) fprintf(stderr, "\t-l listen_backlog\n");
52642cdb25Marcel Telka	(void) fprintf(stderr, "\t-t retransmit_timeout\n");
527bbaa8b6Dan Kruchinin
528bbaa8b6Dan Kruchinin	exit(1);
529bbaa8b6Dan Kruchinin}