4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
47 * Most of this copied from ../nfsd/nfsd.c
48 * and then s:NFS:NLM: applied, etc.
51 #include <sys/param.h>
52 #include <sys/types.h>
62 #include <nfs/nfssys.h>
64 #include <stdio_ext.h>
67 #include <netconfig.h>
72 #include <sys/tihdr.h>
74 #include <priv_utils.h>
75 #include <sys/tiuser.h>
76 #include <netinet/tcp.h>
78 #include <rpcsvc/daemon_utils.h>
79 #include <rpcsvc/nlm_prot.h>
83 #include "nfs_tbind.h"
87 /* Option defaults. See nfssys.h */
88 struct lm_svc_args lmargs
= {
89 .version
= LM_SVC_CUR_VERS
,
90 /* fd, n_fmly, n_proto, n_rdev (below) */
94 .retransmittimeout
= 5
96 int max_servers
= 256;
99 #define RET_OK 0 /* return code for no error */
100 #define RET_ERR 33 /* return code for error(s) */
102 static int nlmsvc(int fd
, struct netbuf addrmask
,
103 struct netconfig
*nconf
);
104 static int nlmsvcpool(int max_servers
);
105 static void usage(void);
107 extern int _nfssys(int, void *);
108 static void sigterm_handler(void);
109 static void shutdown_lockd(void);
111 extern int daemonize_init(void);
112 extern void daemonize_fini(int fd
);
117 * We want to bind to these TLI providers, and in this order,
118 * because the kernel NLM needs the loopback first for its
119 * initialization. (It uses it to talk to statd.)
121 static NETSELDECL(defaultproviders
)[] = {
131 * The following are all globals used by routines in nfs_tbind.c.
133 size_t end_listen_fds
; /* used by conn_close_oldest() */
134 size_t num_fds
= 0; /* used by multiple routines */
135 int listen_backlog
= 32; /* used by bind_to_{provider,proto}() */
136 int (*Mysvc
)(int, struct netbuf
, struct netconfig
*) = nlmsvc
;
137 /* used by cots_listen_event() */
138 int max_conns_allowed
= -1; /* used by cots_listen_event() */
141 main(int ac
, char *av
[])
143 char *propname
= NULL
;
145 char *provider
= NULL
;
146 struct protob
*protobp
;
147 NETSELPDECL(providerp
);
149 int i
, c
, pid
, ret
, val
;
151 struct sigaction act
;
156 * Initializations that require more privileges than we need to run.
158 (void) _create_daemon_lock(LOCKD
, DAEMON_UID
, DAEMON_GID
);
161 if (__init_daemon_priv(PU_RESETGROUPS
|PU_CLEARLIMITSET
,
162 DAEMON_UID
, DAEMON_GID
, PRIV_SYS_NFS
, NULL
) == -1) {
163 (void) fprintf(stderr
, "%s should be run with"
164 " sufficient privileges\n", av
[0]);
168 (void) enable_extended_FILE_stdio(-1, -1);
171 * Read in the values from SMF first before we check
172 * command line options so the options override SMF values.
175 /* How long to wait for clients to re-establish locks. */
176 propname
= "grace_period"; /* also -g */
177 ret
= nfs_smf_get_iprop(propname
, &val
,
178 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, LOCKD
);
181 fprintf(stderr
, gettext(
182 "Invalid %s from SMF"), propname
);
186 syslog(LOG_ERR
, "Reading of %s from SMF failed, using default "
190 propname
= "lockd_listen_backlog"; /* also -l */
191 ret
= nfs_smf_get_iprop(propname
, &val
,
192 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, LOCKD
);
195 fprintf(stderr
, gettext(
196 "Invalid %s from SMF"), propname
);
198 listen_backlog
= val
;
200 syslog(LOG_ERR
, "Reading of %s from SMF failed, using default "
204 propname
= "lockd_servers"; /* also argv[1] */
205 ret
= nfs_smf_get_iprop(propname
, &val
,
206 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, LOCKD
);
209 fprintf(stderr
, gettext(
210 "Invalid %s from SMF"), propname
);
214 syslog(LOG_ERR
, "Reading of %s from SMF failed, using default "
218 propname
= "lockd_retransmit_timeout"; /* also -t */
219 ret
= nfs_smf_get_iprop(propname
, &val
,
220 DEFAULT_INSTANCE
, SCF_TYPE_INTEGER
, LOCKD
);
223 fprintf(stderr
, gettext(
224 "Invalid %s from SMF"), propname
);
226 lmargs
.retransmittimeout
= val
;
228 syslog(LOG_ERR
, "Reading of %s from SMF failed, using default "
232 while ((c
= getopt(ac
, av
, "c:d:g:l:t:")) != EOF
)
234 case 'c': /* max_connections */
235 if ((val
= atoi(optarg
)) <= 0)
237 max_conns_allowed
= val
;
240 case 'd': /* debug */
241 lmargs
.debug
= atoi(optarg
);
244 case 'g': /* grace_period */
245 if ((val
= atoi(optarg
)) <= 0)
250 case 'l': /* listen_backlog */
251 if ((val
= atoi(optarg
)) <= 0)
253 listen_backlog
= val
;
256 case 't': /* retrans_timeout */
257 if ((val
= atoi(optarg
)) <= 0)
259 lmargs
.retransmittimeout
= val
;
263 fprintf(stderr
, gettext(
264 "Invalid -%c option value"), c
);
272 * If there is exactly one more argument, it is the number of
276 val
= atoi(av
[optind
]);
278 fprintf(stderr
, gettext(
279 "Invalid max_servers argument"));
286 * If there are two or more arguments, then this is a usage error.
292 printf("%s: debug= %d, conn_idle_timout= %d,"
293 " grace_period= %d, listen_backlog= %d,"
294 " max_connections= %d, max_servers= %d,"
295 " retrans_timeout= %d\n",
296 MyName
, lmargs
.debug
, lmargs
.timout
,
297 lmargs
.grace
, listen_backlog
,
298 max_conns_allowed
, max_servers
,
299 lmargs
.retransmittimeout
);
303 * Set current dir to server root
305 if (chdir(dir
) < 0) {
306 (void) fprintf(stderr
, "%s: ", MyName
);
311 /* Daemonize, if not debug. */
312 if (lmargs
.debug
== 0)
313 pipe_fd
= daemonize_init();
315 openlog(MyName
, LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
318 * establish our lock on the lock file and write our pid to it.
319 * exit if some other process holds the lock, or if there's any
320 * error in writing/locking the file.
322 pid
= _enter_daemon_lock(LOCKD
);
327 fprintf(stderr
, "error locking for %s: %s", LOCKD
,
331 /* daemon was already running */
336 * Block all signals till we spawn other
339 (void) sigfillset(&sgset
);
340 (void) thr_sigsetmask(SIG_BLOCK
, &sgset
, NULL
);
342 /* Unregister any previous versions. */
343 for (i
= NLM_VERS
; i
< NLM4_VERS
; i
++) {
344 svc_unreg(NLM_PROG
, i
);
348 * Set up kernel RPC thread pool for the NLM server.
350 if (nlmsvcpool(max_servers
)) {
351 fprintf(stderr
, "Can't set up kernel NLM service: %s. Exiting",
357 * Set up blocked thread to do LWP creation on behalf of the kernel.
359 if (svcwait(NLM_SVCPOOL_ID
)) {
360 fprintf(stderr
, "Can't set up NLM pool creator: %s. Exiting",
366 * Install atexit and sigterm handlers
368 act
.sa_handler
= sigterm_handler
;
371 (void) sigaction(SIGTERM
, &act
, NULL
);
372 (void) atexit(shutdown_lockd
);
375 * Now open up for signal delivery
377 (void) thr_sigsetmask(SIG_UNBLOCK
, &sgset
, NULL
);
380 * Build a protocol block list for registration.
382 protobp
= (struct protob
*)malloc(sizeof (struct protob
));
383 protobp
->serv
= "NLM";
384 protobp
->versmin
= NLM_VERS
;
385 protobp
->versmax
= NLM4_VERS
;
386 protobp
->program
= NLM_PROG
;
387 protobp
->next
= NULL
;
389 for (providerp
= defaultproviders
;
390 *providerp
!= NULL
; providerp
++) {
391 provider
= *providerp
;
392 do_one(provider
, NULL
, protobp
, nlmsvc
);
398 fprintf(stderr
, "Could not start NLM service for any protocol."
403 end_listen_fds
= num_fds
;
406 * lockd is up and running as far as we are concerned.
408 if (lmargs
.debug
== 0)
409 daemonize_fini(pipe_fd
);
412 * Get rid of unneeded privileges.
414 __fini_daemon_priv(PRIV_PROC_FORK
, PRIV_PROC_EXEC
, PRIV_PROC_SESSION
,
415 PRIV_FILE_LINK_ANY
, PRIV_PROC_INFO
, NULL
);
418 * Poll for non-data control events on the transport descriptors.
423 * If we get here, something failed in poll_for_action().
429 nlmsvcpool(int maxservers
)
431 struct svcpool_args npa
;
433 npa
.id
= NLM_SVCPOOL_ID
;
434 npa
.maxthreads
= maxservers
;
439 npa
.max_same_xprt
= 0;
440 return (_nfssys(SVCPOOL_CREATE
, &npa
));
444 ncfmly_to_lmfmly(const char *ncfmly
)
446 if (0 == strcmp(ncfmly
, NC_INET
))
448 if (0 == strcmp(ncfmly
, NC_INET6
))
450 if (0 == strcmp(ncfmly
, NC_LOOPBACK
))
451 return (LM_LOOPBACK
);
456 nctype_to_lmprot(uint_t semantics
)
461 case NC_TPI_COTS_ORD
:
468 ncdev_to_rdev(const char *ncdev
)
472 if (stat(ncdev
, &st
) < 0)
478 sigterm_handler(void)
480 /* to call atexit handler */
487 (void) _nfssys(KILL_LOCKMGR
, NULL
);
492 * Establish NLM service thread.
495 nlmsvc(int fd
, struct netbuf addrmask
, struct netconfig
*nconf
)
497 struct lm_svc_args lma
;
499 lma
= lmargs
; /* init by struct copy */
502 * The kernel code needs to reconstruct a complete
503 * knetconfig from n_fmly, n_proto. We use these
504 * two fields to convey the family and semantics.
507 lma
.n_fmly
= ncfmly_to_lmfmly(nconf
->nc_protofmly
);
508 lma
.n_proto
= nctype_to_lmprot(nconf
->nc_semantics
);
509 lma
.n_rdev
= ncdev_to_rdev(nconf
->nc_device
);
511 return (_nfssys(LM_SVC
, &lma
));
517 (void) fprintf(stderr
, gettext(
518 "usage: %s [options] [max_servers]\n"), MyName
);
519 (void) fprintf(stderr
, gettext(
520 "options: (see SMF property descriptions)\n"));
521 /* Note: don't translate these */
522 (void) fprintf(stderr
, "\t-c max_connections\n");
523 (void) fprintf(stderr
, "\t-d debug_level\n");
524 (void) fprintf(stderr
, "\t-g grace_period\n");
525 (void) fprintf(stderr
, "\t-l listen_backlog\n");
526 (void) fprintf(stderr
, "\t-t retransmit_timeout\n");