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