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