dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fs.d / nfs / lockd / lockd.c
blob78621fcffa567c909eb5d496dadcd8fc28074aae
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
34 * All Rights Reserved
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
38 * contributors.
41 /* LINTLIBRARY */
42 /* PROTOLIB1 */
45 * NLM server
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>
53 #include <sys/stat.h>
54 #include <syslog.h>
55 #include <tiuser.h>
56 #include <rpc/rpc.h>
57 #include <errno.h>
58 #include <thread.h>
59 #include <sys/time.h>
60 #include <sys/file.h>
61 #include <nfs/nfs.h>
62 #include <nfs/nfssys.h>
63 #include <stdio.h>
64 #include <stdio_ext.h>
65 #include <stdlib.h>
66 #include <signal.h>
67 #include <netconfig.h>
68 #include <netdir.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include <stropts.h>
72 #include <sys/tihdr.h>
73 #include <poll.h>
74 #include <priv_utils.h>
75 #include <sys/tiuser.h>
76 #include <netinet/tcp.h>
77 #include <deflt.h>
78 #include <rpcsvc/daemon_utils.h>
79 #include <rpcsvc/nlm_prot.h>
80 #include <libintl.h>
81 #include <libscf.h>
82 #include <libshare.h>
83 #include "nfs_tbind.h"
84 #include "thrpool.h"
85 #include "smfcfg.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) */
91 .debug = 0,
92 .timout = 5 * 60,
93 .grace = 90,
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);
114 static char *MyName;
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)[] = {
122 "/dev/ticotsord",
123 "/dev/tcp",
124 "/dev/udp",
125 "/dev/tcp6",
126 "/dev/udp6",
127 NULL
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;
144 char *dir = "/";
145 char *provider = NULL;
146 struct protob *protobp;
147 NETSELPDECL(providerp);
148 sigset_t sgset;
149 int i, c, pid, ret, val;
150 int pipe_fd = -1;
151 struct sigaction act;
153 MyName = *av;
156 * Initializations that require more privileges than we need to run.
158 (void) _create_daemon_lock(LOCKD, DAEMON_UID, DAEMON_GID);
159 svcsetprio();
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]);
165 exit(1);
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);
179 if (ret == SA_OK) {
180 if (val <= 0)
181 fprintf(stderr, gettext(
182 "Invalid %s from SMF"), propname);
183 else
184 lmargs.grace = val;
185 } else {
186 syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
187 "value", propname);
190 propname = "lockd_listen_backlog"; /* also -l */
191 ret = nfs_smf_get_iprop(propname, &val,
192 DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
193 if (ret == SA_OK) {
194 if (val <= 0)
195 fprintf(stderr, gettext(
196 "Invalid %s from SMF"), propname);
197 else
198 listen_backlog = val;
199 } else {
200 syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
201 "value", propname);
204 propname = "lockd_servers"; /* also argv[1] */
205 ret = nfs_smf_get_iprop(propname, &val,
206 DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
207 if (ret == SA_OK) {
208 if (val <= 0)
209 fprintf(stderr, gettext(
210 "Invalid %s from SMF"), propname);
211 else
212 max_servers = val;
213 } else {
214 syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
215 "value", propname);
218 propname = "lockd_retransmit_timeout"; /* also -t */
219 ret = nfs_smf_get_iprop(propname, &val,
220 DEFAULT_INSTANCE, SCF_TYPE_INTEGER, LOCKD);
221 if (ret == SA_OK) {
222 if (val <= 0)
223 fprintf(stderr, gettext(
224 "Invalid %s from SMF"), propname);
225 else
226 lmargs.retransmittimeout = val;
227 } else {
228 syslog(LOG_ERR, "Reading of %s from SMF failed, using default "
229 "value", propname);
232 while ((c = getopt(ac, av, "c:d:g:l:t:")) != EOF)
233 switch (c) {
234 case 'c': /* max_connections */
235 if ((val = atoi(optarg)) <= 0)
236 goto badval;
237 max_conns_allowed = val;
238 break;
240 case 'd': /* debug */
241 lmargs.debug = atoi(optarg);
242 break;
244 case 'g': /* grace_period */
245 if ((val = atoi(optarg)) <= 0)
246 goto badval;
247 lmargs.grace = val;
248 break;
250 case 'l': /* listen_backlog */
251 if ((val = atoi(optarg)) <= 0)
252 goto badval;
253 listen_backlog = val;
254 break;
256 case 't': /* retrans_timeout */
257 if ((val = atoi(optarg)) <= 0)
258 goto badval;
259 lmargs.retransmittimeout = val;
260 break;
262 badval:
263 fprintf(stderr, gettext(
264 "Invalid -%c option value"), c);
265 /* FALLTHROUGH */
266 default:
267 usage();
268 /* NOTREACHED */
272 * If there is exactly one more argument, it is the number of
273 * servers.
275 if (optind < ac) {
276 val = atoi(av[optind]);
277 if (val <= 0) {
278 fprintf(stderr, gettext(
279 "Invalid max_servers argument"));
280 usage();
282 max_servers = val;
283 optind++;
286 * If there are two or more arguments, then this is a usage error.
288 if (optind != ac)
289 usage();
291 if (lmargs.debug) {
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);
307 perror(dir);
308 exit(1);
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);
323 switch (pid) {
324 case 0:
325 break;
326 case -1:
327 fprintf(stderr, "error locking for %s: %s", LOCKD,
328 strerror(errno));
329 exit(2);
330 default:
331 /* daemon was already running */
332 exit(0);
336 * Block all signals till we spawn other
337 * threads.
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",
352 strerror(errno));
353 exit(1);
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",
361 strerror(errno));
362 exit(1);
366 * Install atexit and sigterm handlers
368 act.sa_handler = sigterm_handler;
369 act.sa_flags = 0;
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);
395 free(protobp);
397 if (num_fds == 0) {
398 fprintf(stderr, "Could not start NLM service for any protocol."
399 " Exiting");
400 exit(1);
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.
420 poll_for_action();
423 * If we get here, something failed in poll_for_action().
425 return (1);
428 static int
429 nlmsvcpool(int maxservers)
431 struct svcpool_args npa;
433 npa.id = NLM_SVCPOOL_ID;
434 npa.maxthreads = maxservers;
435 npa.redline = 0;
436 npa.qsize = 0;
437 npa.timeout = 0;
438 npa.stksize = 0;
439 npa.max_same_xprt = 0;
440 return (_nfssys(SVCPOOL_CREATE, &npa));
443 static int
444 ncfmly_to_lmfmly(const char *ncfmly)
446 if (0 == strcmp(ncfmly, NC_INET))
447 return (LM_INET);
448 if (0 == strcmp(ncfmly, NC_INET6))
449 return (LM_INET6);
450 if (0 == strcmp(ncfmly, NC_LOOPBACK))
451 return (LM_LOOPBACK);
452 return (-1);
455 static int
456 nctype_to_lmprot(uint_t semantics)
458 switch (semantics) {
459 case NC_TPI_CLTS:
460 return (LM_UDP);
461 case NC_TPI_COTS_ORD:
462 return (LM_TCP);
464 return (-1);
467 static dev_t
468 ncdev_to_rdev(const char *ncdev)
470 struct stat st;
472 if (stat(ncdev, &st) < 0)
473 return (NODEV);
474 return (st.st_rdev);
477 static void
478 sigterm_handler(void)
480 /* to call atexit handler */
481 exit(0);
484 static void
485 shutdown_lockd(void)
487 (void) _nfssys(KILL_LOCKMGR, NULL);
492 * Establish NLM service thread.
494 static int
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.
506 lma.fd = fd;
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));
514 static void
515 usage(void)
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");
528 exit(1);