1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * The dlmgmtd daemon is started by the datalink-management SMF service. 29 * This daemon is used to manage <link name, linkid> mapping and the 30 * persistent datalink configuration. 31 * 32 * Today, the <link name, linkid> mapping and the persistent configuration 33 * of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps 34 * a copy of the datalinks in the memory (see dlmgmt_id_avl and 35 * dlmgmt_name_avl). The active <link name, linkid> mapping is kept in 36 * /etc/svc/volatile/dladm cache file, so that the mapping can be recovered 37 * when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally 38 * killed). 39 */ 40 41 #include <assert.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <priv_utils.h> 45 #include <signal.h> 46 #include <stdlib.h> 47 #include <stdio.h> 48 #include <strings.h> 49 #include <syslog.h> 50 #include <sys/dld.h> 51 #include <sys/param.h> 52 #include <sys/stat.h> 53 #include <unistd.h> 54 #include <libdladm_impl.h> 55 #include <libdlmgmt.h> 56 #include "dlmgmt_impl.h" 57 58 const char *progname; 59 boolean_t debug; 60 static int pfds[2]; 61 /* 62 * This file descriptor to DLMGMT_DOOR cannot be in the libdladm 63 * handle because the door isn't created when the handle is created. 64 */ 65 static int dlmgmt_door_fd = -1; 66 67 /* 68 * This libdladm handle is global so that dlmgmt_upcall_linkprop_init() 69 * can pass to libdladm. The handle is opened during dlmgmt_init_privileges() 70 * with "ALL" privileges. It is not able to open DLMGMT_DOOR at that time as 71 * it hasn't been created yet. This door in the handle is opened in the first 72 * call to dladm_door_fd(). 73 */ 74 dladm_handle_t dld_handle = NULL; 75 76 static void dlmgmtd_exit(int); 77 static int dlmgmt_init(); 78 static void dlmgmt_fini(); 79 static int dlmgmt_init_privileges(); 80 static void dlmgmt_fini_privileges(); 81 82 static int 83 dlmgmt_set_doorfd(boolean_t start) 84 { 85 dld_ioc_door_t did; 86 int err = 0; 87 88 assert(dld_handle != NULL); 89 90 did.did_start_door = start; 91 92 if (ioctl(dladm_dld_fd(dld_handle), DLDIOC_DOORSERVER, &did) == -1) 93 err = errno; 94 95 return (err); 96 } 97 98 static int 99 dlmgmt_door_init() 100 { 101 int fd; 102 int err; 103 104 /* 105 * Create the door file for dlmgmtd. 106 */ 107 if ((fd = open(DLMGMT_DOOR, O_CREAT|O_RDONLY, 0644)) == -1) { 108 err = errno; 109 dlmgmt_log(LOG_ERR, "open(%s) failed: %s", 110 DLMGMT_DOOR, strerror(err)); 111 return (err); 112 } 113 (void) close(fd); 114 115 if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL, 116 DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 117 err = errno; 118 dlmgmt_log(LOG_ERR, "door_create() failed: %s", 119 strerror(err)); 120 return (err); 121 } 122 /* 123 * fdetach first in case a previous daemon instance exited 124 * ungracefully. 125 */ 126 (void) fdetach(DLMGMT_DOOR); 127 if (fattach(dlmgmt_door_fd, DLMGMT_DOOR) != 0) { 128 err = errno; 129 dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s", 130 DLMGMT_DOOR, strerror(err)); 131 goto fail; 132 } 133 if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) { 134 dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s", 135 strerror(err)); 136 (void) fdetach(DLMGMT_DOOR); 137 goto fail; 138 } 139 140 return (0); 141 fail: 142 (void) door_revoke(dlmgmt_door_fd); 143 dlmgmt_door_fd = -1; 144 return (err); 145 } 146 147 static void 148 dlmgmt_door_fini() 149 { 150 if (dlmgmt_door_fd == -1) 151 return; 152 153 if (door_revoke(dlmgmt_door_fd) == -1) { 154 dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s", 155 DLMGMT_DOOR, strerror(errno)); 156 } 157 158 (void) fdetach(DLMGMT_DOOR); 159 (void) dlmgmt_set_doorfd(B_FALSE); 160 } 161 162 static int 163 dlmgmt_init() 164 { 165 int err; 166 167 if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR || 168 signal(SIGINT, dlmgmtd_exit) == SIG_ERR) { 169 err = errno; 170 dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s", 171 strerror(err)); 172 return (err); 173 } 174 175 if ((err = dlmgmt_linktable_init()) != 0) 176 return (err); 177 178 if ((err = dlmgmt_db_init()) != 0 || (err = dlmgmt_door_init()) != 0) 179 dlmgmt_linktable_fini(); 180 181 return (err); 182 } 183 184 static void 185 dlmgmt_fini() 186 { 187 dlmgmt_door_fini(); 188 dlmgmt_linktable_fini(); 189 } 190 191 /* 192 * This is called by the child process to inform the parent process to 193 * exit with the given return value. 194 */ 195 static void 196 dlmgmt_inform_parent_exit(int rv) 197 { 198 if (debug) 199 return; 200 201 if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) { 202 dlmgmt_log(LOG_WARNING, 203 "dlmgmt_inform_parent_exit() failed: %s", strerror(errno)); 204 (void) close(pfds[1]); 205 exit(EXIT_FAILURE); 206 } 207 (void) close(pfds[1]); 208 } 209 210 /*ARGSUSED*/ 211 static void 212 dlmgmtd_exit(int signo) 213 { 214 (void) close(pfds[1]); 215 dlmgmt_fini(); 216 dlmgmt_fini_privileges(); 217 exit(EXIT_FAILURE); 218 } 219 220 static void 221 usage(void) 222 { 223 (void) fprintf(stderr, "Usage: %s [-d]\n", progname); 224 exit(EXIT_FAILURE); 225 } 226 227 /* 228 * Set the uid of this daemon to the "dladm" user. Finish the following 229 * operations before setuid() because they need root privileges: 230 * 231 * - create the /etc/svc/volatile/dladm directory; 232 * - change its uid/gid to "dladm"/"sys"; 233 * - open the dld control node 234 */ 235 static int 236 dlmgmt_init_privileges() 237 { 238 struct stat statbuf; 239 240 /* 241 * Create the DLMGMT_TMPFS_DIR directory. 242 */ 243 if (stat(DLMGMT_TMPFS_DIR, &statbuf) < 0) { 244 if (mkdir(DLMGMT_TMPFS_DIR, (mode_t)0755) < 0) 245 return (errno); 246 } else { 247 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) 248 return (ENOTDIR); 249 } 250 251 if ((chmod(DLMGMT_TMPFS_DIR, 0755) < 0) || 252 (chown(DLMGMT_TMPFS_DIR, UID_DLADM, GID_SYS) < 0)) { 253 return (EPERM); 254 } 255 256 /* 257 * When dlmgmtd is started at boot, "ALL" privilege is required 258 * to open the dld control node. The door isn't created yet. 259 */ 260 if (dladm_open(&dld_handle) != DLADM_STATUS_OK) { 261 dlmgmt_log(LOG_ERR, "dladm_open() failed"); 262 return (EPERM); 263 } 264 265 /* 266 * We need PRIV_SYS_DL_CONFIG for the DLDIOC_DOORSERVER ioctl, 267 * and PRIV_SYS_CONFIG to post sysevents. 268 */ 269 if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_DLADM, 270 GID_SYS, PRIV_SYS_DL_CONFIG, PRIV_SYS_CONFIG, NULL) == -1) { 271 dladm_close(dld_handle); 272 dld_handle = NULL; 273 return (EPERM); 274 } 275 276 277 return (0); 278 } 279 280 static void 281 dlmgmt_fini_privileges() 282 { 283 if (dld_handle != NULL) { 284 dladm_close(dld_handle); 285 dld_handle = NULL; 286 } 287 } 288 289 /* 290 * Keep the pfds fd open, close other fds. 291 */ 292 /*ARGSUSED*/ 293 static int 294 closefunc(void *arg, int fd) 295 { 296 if (fd != pfds[1]) 297 (void) close(fd); 298 return (0); 299 } 300 301 static boolean_t 302 dlmgmt_daemonize(void) 303 { 304 pid_t pid; 305 int rv; 306 307 if (pipe(pfds) < 0) { 308 (void) fprintf(stderr, "%s: pipe() failed: %s\n", 309 progname, strerror(errno)); 310 exit(EXIT_FAILURE); 311 } 312 313 if ((pid = fork()) == -1) { 314 (void) fprintf(stderr, "%s: fork() failed: %s\n", 315 progname, strerror(errno)); 316 exit(EXIT_FAILURE); 317 } else if (pid > 0) { /* Parent */ 318 (void) close(pfds[1]); 319 320 /* 321 * Read the child process's return value from the pfds. 322 * If the child process exits unexpected, read() returns -1. 323 */ 324 if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) { 325 (void) kill(pid, SIGKILL); 326 rv = EXIT_FAILURE; 327 } 328 329 (void) close(pfds[0]); 330 exit(rv); 331 } 332 333 /* Child */ 334 (void) close(pfds[0]); 335 (void) setsid(); 336 337 /* 338 * Close all files except pfds[1]. 339 */ 340 (void) fdwalk(closefunc, NULL); 341 (void) chdir("/"); 342 openlog(progname, LOG_PID, LOG_DAEMON); 343 return (B_TRUE); 344 } 345 346 int 347 main(int argc, char *argv[]) 348 { 349 int opt; 350 351 progname = strrchr(argv[0], '/'); 352 if (progname != NULL) 353 progname++; 354 else 355 progname = argv[0]; 356 357 /* 358 * Process options. 359 */ 360 while ((opt = getopt(argc, argv, "d")) != EOF) { 361 switch (opt) { 362 case 'd': 363 debug = B_TRUE; 364 break; 365 default: 366 usage(); 367 } 368 } 369 370 if (!debug && !dlmgmt_daemonize()) 371 return (EXIT_FAILURE); 372 373 if ((errno = dlmgmt_init_privileges()) != 0) { 374 dlmgmt_log(LOG_ERR, "dlmgmt_init_privileges() failed: %s", 375 strerror(errno)); 376 goto child_out; 377 } 378 379 if (dlmgmt_init() != 0) { 380 dlmgmt_fini_privileges(); 381 goto child_out; 382 } 383 384 /* 385 * Inform the parent process that it can successfully exit. 386 */ 387 dlmgmt_inform_parent_exit(EXIT_SUCCESS); 388 389 for (;;) 390 (void) pause(); 391 392 child_out: 393 /* return from main() forcibly exits an MT process */ 394 dlmgmt_inform_parent_exit(EXIT_FAILURE); 395 return (EXIT_FAILURE); 396 } 397