Print this page
OS-3007 dlmgmtd needs to work with non-native zones
OS-1572
OS-375 i_dls_mgmt_upcall()/dlmgmt_zfop() deadlock in dlmgmtd
OS-375-1
OS-327
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/dlmgmtd/dlmgmt_main.c
+++ new/usr/src/cmd/dlmgmtd/dlmgmt_main.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.
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
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 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 + * Copyright 2014 Joyent, Inc. All rights reserved.
25 26 */
26 27
27 28 /*
28 29 * The dlmgmtd daemon is started by the datalink-management SMF service.
29 30 * This daemon is used to manage <link name, linkid> mapping and the
30 31 * persistent datalink configuration.
31 32 *
32 33 * Today, the <link name, linkid> mapping and the persistent configuration
33 34 * of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps
34 35 * a copy of the datalinks in the memory (see dlmgmt_id_avl and
35 36 * dlmgmt_name_avl). The active <link name, linkid> mapping is kept in
36 37 * /etc/svc/volatile/dladm cache file, so that the mapping can be recovered
37 38 * when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally
38 39 * killed).
39 40 */
40 41
41 42 #include <assert.h>
42 43 #include <errno.h>
43 44 #include <fcntl.h>
44 45 #include <priv.h>
45 46 #include <signal.h>
46 47 #include <stdlib.h>
47 48 #include <stdio.h>
48 49 #include <strings.h>
49 50 #include <syslog.h>
50 51 #include <zone.h>
51 52 #include <sys/dld.h>
52 53 #include <sys/dld_ioc.h>
53 54 #include <sys/param.h>
54 55 #include <sys/stat.h>
55 56 #include <unistd.h>
56 57 #include <libdladm_impl.h>
57 58 #include <libdlmgmt.h>
58 59 #include "dlmgmt_impl.h"
59 60
60 61 const char *progname;
61 62 boolean_t debug;
62 63 static int pfds[2];
63 64 /*
64 65 * This file descriptor to DLMGMT_DOOR cannot be in the libdladm
65 66 * handle because the door isn't created when the handle is created.
66 67 */
67 68 static int dlmgmt_door_fd = -1;
68 69
69 70 /*
70 71 * This libdladm handle is global so that dlmgmt_upcall_linkprop_init() can
71 72 * pass to libdladm. The handle is opened with "ALL" privileges, before
72 73 * privileges are dropped in dlmgmt_drop_privileges(). It is not able to open
73 74 * DLMGMT_DOOR at that time as it hasn't been created yet. This door in the
74 75 * handle is opened in the first call to dladm_door_fd().
75 76 */
76 77 dladm_handle_t dld_handle = NULL;
77 78
78 79 static void dlmgmtd_exit(int);
79 80 static int dlmgmt_init();
80 81 static void dlmgmt_fini();
81 82 static int dlmgmt_set_privileges();
82 83
83 84 static int
84 85 dlmgmt_set_doorfd(boolean_t start)
85 86 {
86 87 dld_ioc_door_t did;
87 88 int err = 0;
88 89
89 90 assert(dld_handle != NULL);
90 91
91 92 did.did_start_door = start;
92 93
93 94 if (ioctl(dladm_dld_fd(dld_handle), DLDIOC_DOORSERVER, &did) == -1)
94 95 err = errno;
95 96
96 97 return (err);
97 98 }
98 99
99 100 static int
100 101 dlmgmt_door_init(void)
101 102 {
102 103 int err = 0;
103 104
104 105 if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL,
105 106 DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
106 107 err = errno;
107 108 dlmgmt_log(LOG_ERR, "door_create() failed: %s",
108 109 strerror(err));
109 110 return (err);
110 111 }
111 112 return (err);
112 113 }
113 114
114 115 static void
115 116 dlmgmt_door_fini(void)
116 117 {
117 118 if (dlmgmt_door_fd == -1)
|
↓ open down ↓ |
83 lines elided |
↑ open up ↑ |
118 119 return;
119 120
120 121 if (door_revoke(dlmgmt_door_fd) == -1) {
121 122 dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s",
122 123 DLMGMT_DOOR, strerror(errno));
123 124 }
124 125 (void) dlmgmt_set_doorfd(B_FALSE);
125 126 dlmgmt_door_fd = -1;
126 127 }
127 128
128 -int
129 +static int
129 130 dlmgmt_door_attach(zoneid_t zoneid, char *rootdir)
130 131 {
131 132 int fd;
132 133 int err = 0;
133 134 char doorpath[MAXPATHLEN];
135 + struct stat statbuf;
134 136
135 - (void) snprintf(doorpath, sizeof (doorpath), "%s%s", rootdir,
136 - DLMGMT_DOOR);
137 + /* Handle running in a non-native branded zone (i.e. has /native) */
138 + (void) snprintf(doorpath, sizeof (doorpath), "%s/native%s",
139 + rootdir, DLMGMT_TMPFS_DIR);
140 + if (stat(doorpath, &statbuf) == 0) {
141 + (void) snprintf(doorpath, sizeof (doorpath), "%s/native%s",
142 + rootdir, DLMGMT_DOOR);
143 + } else {
144 + (void) snprintf(doorpath, sizeof (doorpath), "%s%s",
145 + rootdir, DLMGMT_DOOR);
146 + }
137 147
138 148 /*
139 149 * Create the door file for dlmgmtd.
140 150 */
141 151 if ((fd = open(doorpath, O_CREAT|O_RDONLY, 0644)) == -1) {
142 152 err = errno;
143 153 dlmgmt_log(LOG_ERR, "open(%s) failed: %s", doorpath,
144 154 strerror(err));
145 155 return (err);
146 156 }
147 157 (void) close(fd);
148 158 if (chown(doorpath, UID_DLADM, GID_NETADM) == -1)
149 159 return (errno);
150 160
151 161 /*
152 162 * fdetach first in case a previous daemon instance exited
153 163 * ungracefully.
154 164 */
155 165 (void) fdetach(doorpath);
156 166 if (fattach(dlmgmt_door_fd, doorpath) != 0) {
157 167 err = errno;
158 168 dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s", doorpath,
159 169 strerror(err));
160 170 } else if (zoneid == GLOBAL_ZONEID) {
161 171 if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) {
162 172 dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s",
163 173 strerror(err));
164 174 }
165 175 }
166 176
167 177 return (err);
168 178 }
169 179
170 180 /*
171 181 * Create the /etc/svc/volatile/dladm/ directory if it doesn't exist, load the
172 182 * datalink.conf data for this zone, and create/attach the door rendezvous
173 183 * file.
174 184 */
175 185 int
176 186 dlmgmt_zone_init(zoneid_t zoneid)
177 187 {
178 188 char rootdir[MAXPATHLEN], tmpfsdir[MAXPATHLEN];
179 189 int err;
180 190 struct stat statbuf;
181 191
182 192 if (zoneid == GLOBAL_ZONEID) {
183 193 rootdir[0] = '\0';
184 194 } else if (zone_getattr(zoneid, ZONE_ATTR_ROOT, rootdir,
|
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
185 195 sizeof (rootdir)) < 0) {
186 196 return (errno);
187 197 }
188 198
189 199 /*
190 200 * Create the DLMGMT_TMPFS_DIR directory.
191 201 */
192 202 (void) snprintf(tmpfsdir, sizeof (tmpfsdir), "%s%s", rootdir,
193 203 DLMGMT_TMPFS_DIR);
194 204 if (stat(tmpfsdir, &statbuf) < 0) {
195 - if (mkdir(tmpfsdir, (mode_t)0755) < 0)
196 - return (errno);
205 + if (mkdir(tmpfsdir, (mode_t)0755) < 0) {
206 + /*
207 + * Handle running in a non-native branded zone
208 + * (i.e. has /native)
209 + */
210 + (void) snprintf(tmpfsdir, sizeof (tmpfsdir),
211 + "%s/native%s", rootdir, DLMGMT_TMPFS_DIR);
212 + if (mkdir(tmpfsdir, (mode_t)0755) < 0)
213 + return (errno);
214 + }
197 215 } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
198 216 return (ENOTDIR);
199 217 }
200 218
201 219 if ((chmod(tmpfsdir, 0755) < 0) ||
202 220 (chown(tmpfsdir, UID_DLADM, GID_NETADM) < 0)) {
203 221 return (EPERM);
204 222 }
205 223
206 - if ((err = dlmgmt_db_init(zoneid)) != 0)
224 + if ((err = dlmgmt_db_init(zoneid, rootdir)) != 0)
207 225 return (err);
208 226 return (dlmgmt_door_attach(zoneid, rootdir));
209 227 }
210 228
211 229 /*
212 230 * Initialize each running zone.
213 231 */
214 232 static int
215 233 dlmgmt_allzones_init(void)
216 234 {
217 - int err, i;
235 + int i;
218 236 zoneid_t *zids = NULL;
219 237 uint_t nzids, nzids_saved;
220 238
221 239 if (zone_list(NULL, &nzids) != 0)
222 240 return (errno);
223 241 again:
224 242 nzids *= 2;
225 243 if ((zids = malloc(nzids * sizeof (zoneid_t))) == NULL)
226 244 return (errno);
227 245 nzids_saved = nzids;
228 246 if (zone_list(zids, &nzids) != 0) {
229 247 free(zids);
230 248 return (errno);
231 249 }
232 250 if (nzids > nzids_saved) {
233 251 free(zids);
234 252 goto again;
235 253 }
236 254
237 255 for (i = 0; i < nzids; i++) {
238 - if ((err = dlmgmt_zone_init(zids[i])) != 0)
239 - break;
256 + int res;
257 + zone_status_t status;
258 +
259 + /*
260 + * Skip over zones that have gone away or are going down
261 + * since we got the list. Process all zones in the list,
262 + * logging errors for any that failed.
263 + */
264 + if (zone_getattr(zids[i], ZONE_ATTR_STATUS, &status,
265 + sizeof (status)) < 0)
266 + continue;
267 + switch (status) {
268 + case ZONE_IS_SHUTTING_DOWN:
269 + case ZONE_IS_EMPTY:
270 + case ZONE_IS_DOWN:
271 + case ZONE_IS_DYING:
272 + case ZONE_IS_DEAD:
273 + /* FALLTHRU */
274 + continue;
275 + default:
276 + break;
277 + }
278 + if ((res = dlmgmt_zone_init(zids[i])) != 0) {
279 + (void) fprintf(stderr, "zone (%ld) init error %s",
280 + zids[i], strerror(res));
281 + dlmgmt_log(LOG_ERR, "zone (%d) init error %s",
282 + zids[i], strerror(res));
283 + }
240 284 }
241 285 free(zids);
242 - return (err);
286 + return (0);
243 287 }
244 288
245 289 static int
246 290 dlmgmt_init(void)
247 291 {
248 292 int err;
249 293 char *fmri, *c;
250 294 char filename[MAXPATHLEN];
251 295
252 296 if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
253 297 dlmgmt_log(LOG_ERR, "dladm_open() failed");
254 298 return (EPERM);
|
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
255 299 }
256 300
257 301 if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR ||
258 302 signal(SIGINT, dlmgmtd_exit) == SIG_ERR) {
259 303 err = errno;
260 304 dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
261 305 strerror(err));
262 306 return (err);
263 307 }
264 308
309 + (void) unlink(ZONE_LOCK);
310 +
265 311 /*
266 312 * First derive the name of the cache file from the FMRI name. This
267 313 * cache name is used to keep active datalink configuration.
268 314 */
269 315 if (debug) {
270 316 (void) snprintf(cachefile, MAXPATHLEN, "%s/%s%s",
271 317 DLMGMT_TMPFS_DIR, progname, ".debug.cache");
272 318 } else {
273 319 if ((fmri = getenv("SMF_FMRI")) == NULL) {
274 320 dlmgmt_log(LOG_ERR, "dlmgmtd is an smf(5) managed "
275 321 "service and should not be run from the command "
276 322 "line.");
277 323 return (EINVAL);
278 324 }
279 325
280 326 /*
281 327 * The FMRI name is in the form of
282 328 * svc:/service/service:instance. We need to remove the
283 329 * prefix "svc:/" and replace '/' with '-'. The cache file
284 330 * name is in the form of "service:instance.cache".
285 331 */
286 332 if ((c = strchr(fmri, '/')) != NULL)
287 333 c++;
288 334 else
289 335 c = fmri;
290 336 (void) snprintf(filename, MAXPATHLEN, "%s.cache", c);
291 337 c = filename;
292 338 while ((c = strchr(c, '/')) != NULL)
293 339 *c = '-';
294 340
295 341 (void) snprintf(cachefile, MAXPATHLEN, "%s/%s",
296 342 DLMGMT_TMPFS_DIR, filename);
297 343 }
298 344
299 345 dlmgmt_linktable_init();
300 346 if ((err = dlmgmt_door_init()) != 0)
301 347 goto done;
302 348
303 349 /*
304 350 * Load datalink configuration and create dlmgmtd door files for all
305 351 * currently running zones.
306 352 */
307 353 if ((err = dlmgmt_allzones_init()) != 0)
308 354 dlmgmt_door_fini();
309 355
310 356 done:
311 357 if (err != 0)
312 358 dlmgmt_linktable_fini();
313 359 return (err);
314 360 }
315 361
316 362 static void
317 363 dlmgmt_fini(void)
318 364 {
319 365 dlmgmt_door_fini();
320 366 dlmgmt_linktable_fini();
321 367 if (dld_handle != NULL) {
322 368 dladm_close(dld_handle);
323 369 dld_handle = NULL;
324 370 }
325 371 }
326 372
327 373 /*
328 374 * This is called by the child process to inform the parent process to
329 375 * exit with the given return value.
330 376 */
331 377 static void
332 378 dlmgmt_inform_parent_exit(int rv)
333 379 {
334 380 if (debug)
335 381 return;
336 382
337 383 if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
338 384 dlmgmt_log(LOG_WARNING,
339 385 "dlmgmt_inform_parent_exit() failed: %s", strerror(errno));
340 386 (void) close(pfds[1]);
341 387 exit(EXIT_FAILURE);
342 388 }
343 389 (void) close(pfds[1]);
344 390 }
345 391
346 392 /*ARGSUSED*/
347 393 static void
348 394 dlmgmtd_exit(int signo)
349 395 {
350 396 (void) close(pfds[1]);
351 397 dlmgmt_fini();
352 398 exit(EXIT_FAILURE);
353 399 }
354 400
355 401 static void
356 402 usage(void)
357 403 {
358 404 (void) fprintf(stderr, "Usage: %s [-d]\n", progname);
359 405 exit(EXIT_FAILURE);
360 406 }
361 407
362 408 /*
363 409 * Restrict privileges to only those needed.
364 410 */
365 411 int
366 412 dlmgmt_drop_privileges(void)
367 413 {
368 414 priv_set_t *pset;
369 415 priv_ptype_t ptype;
370 416 zoneid_t zoneid = getzoneid();
371 417 int err = 0;
372 418
373 419 if ((pset = priv_allocset()) == NULL)
374 420 return (errno);
375 421
376 422 /*
377 423 * The global zone needs PRIV_PROC_FORK so that it can fork() when it
378 424 * issues db ops in non-global zones, PRIV_SYS_CONFIG to post
379 425 * sysevents, and PRIV_SYS_DL_CONFIG to initialize link properties in
380 426 * dlmgmt_upcall_linkprop_init().
381 427 *
382 428 * We remove non-basic privileges from the permitted (and thus
383 429 * effective) set. When executing in a non-global zone, dlmgmtd
384 430 * only needs to read and write to files that it already owns.
385 431 */
386 432 priv_basicset(pset);
387 433 (void) priv_delset(pset, PRIV_PROC_EXEC);
388 434 (void) priv_delset(pset, PRIV_PROC_INFO);
389 435 (void) priv_delset(pset, PRIV_PROC_SESSION);
390 436 (void) priv_delset(pset, PRIV_FILE_LINK_ANY);
391 437 if (zoneid == GLOBAL_ZONEID) {
392 438 ptype = PRIV_EFFECTIVE;
393 439 if (priv_addset(pset, PRIV_SYS_CONFIG) == -1 ||
394 440 priv_addset(pset, PRIV_SYS_DL_CONFIG) == -1)
395 441 err = errno;
396 442 } else {
397 443 (void) priv_delset(pset, PRIV_PROC_FORK);
398 444 ptype = PRIV_PERMITTED;
399 445 }
400 446 if (err == 0 && setppriv(PRIV_SET, ptype, pset) == -1)
401 447 err = errno;
402 448 done:
403 449 priv_freeset(pset);
404 450 return (err);
405 451 }
406 452
407 453 int
408 454 dlmgmt_elevate_privileges(void)
409 455 {
410 456 priv_set_t *privset;
411 457 int err = 0;
412 458
413 459 if ((privset = priv_str_to_set("zone", ",", NULL)) == NULL)
414 460 return (errno);
415 461 if (setppriv(PRIV_SET, PRIV_EFFECTIVE, privset) == -1)
416 462 err = errno;
417 463 priv_freeset(privset);
418 464 return (err);
419 465 }
420 466
421 467 /*
422 468 * Set the uid of this daemon to the "dladm" user and drop privileges to only
423 469 * those needed.
424 470 */
425 471 static int
426 472 dlmgmt_set_privileges(void)
427 473 {
428 474 int err;
429 475
430 476 (void) setgroups(0, NULL);
431 477 if (setegid(GID_NETADM) == -1 || seteuid(UID_DLADM) == -1)
432 478 err = errno;
433 479 else
434 480 err = dlmgmt_drop_privileges();
435 481 done:
436 482 return (err);
437 483 }
438 484
439 485 /*
440 486 * Keep the pfds fd open, close other fds.
441 487 */
442 488 /*ARGSUSED*/
443 489 static int
444 490 closefunc(void *arg, int fd)
445 491 {
446 492 if (fd != pfds[1])
447 493 (void) close(fd);
448 494 return (0);
449 495 }
450 496
451 497 static boolean_t
452 498 dlmgmt_daemonize(void)
453 499 {
454 500 pid_t pid;
455 501 int rv;
456 502
457 503 if (pipe(pfds) < 0) {
458 504 (void) fprintf(stderr, "%s: pipe() failed: %s\n",
459 505 progname, strerror(errno));
460 506 exit(EXIT_FAILURE);
461 507 }
462 508
463 509 if ((pid = fork()) == -1) {
464 510 (void) fprintf(stderr, "%s: fork() failed: %s\n",
465 511 progname, strerror(errno));
466 512 exit(EXIT_FAILURE);
467 513 } else if (pid > 0) { /* Parent */
468 514 (void) close(pfds[1]);
469 515
470 516 /*
471 517 * Read the child process's return value from the pfds.
472 518 * If the child process exits unexpected, read() returns -1.
473 519 */
474 520 if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
475 521 (void) kill(pid, SIGKILL);
476 522 rv = EXIT_FAILURE;
477 523 }
478 524
479 525 (void) close(pfds[0]);
480 526 exit(rv);
481 527 }
482 528
483 529 /* Child */
484 530 (void) close(pfds[0]);
485 531 (void) setsid();
486 532
487 533 /*
488 534 * Close all files except pfds[1].
489 535 */
490 536 (void) fdwalk(closefunc, NULL);
491 537 (void) chdir("/");
492 538 openlog(progname, LOG_PID, LOG_DAEMON);
493 539 return (B_TRUE);
494 540 }
495 541
496 542 int
497 543 main(int argc, char *argv[])
498 544 {
499 545 int opt, err;
500 546
501 547 progname = strrchr(argv[0], '/');
502 548 if (progname != NULL)
503 549 progname++;
504 550 else
505 551 progname = argv[0];
506 552
507 553 /*
508 554 * Process options.
509 555 */
510 556 while ((opt = getopt(argc, argv, "d")) != EOF) {
511 557 switch (opt) {
512 558 case 'd':
513 559 debug = B_TRUE;
514 560 break;
515 561 default:
516 562 usage();
517 563 }
518 564 }
519 565
520 566 if (!debug && !dlmgmt_daemonize())
521 567 return (EXIT_FAILURE);
522 568
523 569 if ((err = dlmgmt_init()) != 0) {
524 570 dlmgmt_log(LOG_ERR, "unable to initialize daemon: %s",
525 571 strerror(err));
526 572 goto child_out;
527 573 } else if ((err = dlmgmt_set_privileges()) != 0) {
528 574 dlmgmt_log(LOG_ERR, "unable to set daemon privileges: %s",
529 575 strerror(err));
530 576 dlmgmt_fini();
531 577 goto child_out;
532 578 }
533 579
534 580 /*
535 581 * Inform the parent process that it can successfully exit.
536 582 */
537 583 dlmgmt_inform_parent_exit(EXIT_SUCCESS);
538 584
539 585 for (;;)
540 586 (void) pause();
541 587
542 588 child_out:
543 589 /* return from main() forcibly exits an MT process */
544 590 dlmgmt_inform_parent_exit(EXIT_FAILURE);
545 591 return (EXIT_FAILURE);
546 592 }
|
↓ open down ↓ |
272 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX