1*12016SGirish.Moodalbail@Sun.COM /* 2*12016SGirish.Moodalbail@Sun.COM * CDDL HEADER START 3*12016SGirish.Moodalbail@Sun.COM * 4*12016SGirish.Moodalbail@Sun.COM * The contents of this file are subject to the terms of the 5*12016SGirish.Moodalbail@Sun.COM * Common Development and Distribution License (the "License"). 6*12016SGirish.Moodalbail@Sun.COM * You may not use this file except in compliance with the License. 7*12016SGirish.Moodalbail@Sun.COM * 8*12016SGirish.Moodalbail@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*12016SGirish.Moodalbail@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*12016SGirish.Moodalbail@Sun.COM * See the License for the specific language governing permissions 11*12016SGirish.Moodalbail@Sun.COM * and limitations under the License. 12*12016SGirish.Moodalbail@Sun.COM * 13*12016SGirish.Moodalbail@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*12016SGirish.Moodalbail@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*12016SGirish.Moodalbail@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*12016SGirish.Moodalbail@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*12016SGirish.Moodalbail@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*12016SGirish.Moodalbail@Sun.COM * 19*12016SGirish.Moodalbail@Sun.COM * CDDL HEADER END 20*12016SGirish.Moodalbail@Sun.COM */ 21*12016SGirish.Moodalbail@Sun.COM 22*12016SGirish.Moodalbail@Sun.COM /* 23*12016SGirish.Moodalbail@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24*12016SGirish.Moodalbail@Sun.COM * Use is subject to license terms. 25*12016SGirish.Moodalbail@Sun.COM */ 26*12016SGirish.Moodalbail@Sun.COM 27*12016SGirish.Moodalbail@Sun.COM /* 28*12016SGirish.Moodalbail@Sun.COM * The ipmgmtd daemon is started by ip-interface-management SMF service. This 29*12016SGirish.Moodalbail@Sun.COM * daemon is used to manage, mapping of 'address object' to 'interface name' and 30*12016SGirish.Moodalbail@Sun.COM * 'logical interface number', on which the address is created. It also provides 31*12016SGirish.Moodalbail@Sun.COM * a means to update the ipadm persistent data-store. 32*12016SGirish.Moodalbail@Sun.COM * 33*12016SGirish.Moodalbail@Sun.COM * The daemon tracks the <addrobj, lifname> mapping in-memory using a linked 34*12016SGirish.Moodalbail@Sun.COM * list `aobjmap'. Access to this list is synchronized using a readers-writers 35*12016SGirish.Moodalbail@Sun.COM * lock. The active <addrobj, lifname> mapping is kept in 36*12016SGirish.Moodalbail@Sun.COM * /etc/svc/volatile/ipadm/aobjmap.conf cache file, so that the mapping can be 37*12016SGirish.Moodalbail@Sun.COM * recovered when ipmgmtd exits for some reason (e.g., when ipmgmtd is restarted 38*12016SGirish.Moodalbail@Sun.COM * using svcadm or accidentally killed). 39*12016SGirish.Moodalbail@Sun.COM * 40*12016SGirish.Moodalbail@Sun.COM * Today, the persistent configuration of interfaces, addresses and protocol 41*12016SGirish.Moodalbail@Sun.COM * properties is kept in /etc/ipadm/ipadm.conf. The access to the persistent 42*12016SGirish.Moodalbail@Sun.COM * data store is synchronized using reader-writers lock `ipmgmt_dbconf_lock'. 43*12016SGirish.Moodalbail@Sun.COM * 44*12016SGirish.Moodalbail@Sun.COM * The communication between the library, libipadm.so and the daemon, is through 45*12016SGirish.Moodalbail@Sun.COM * doors RPC. The library interacts with the daemon using the commands defined 46*12016SGirish.Moodalbail@Sun.COM * by `ipmgmt_door_cmd_type_t'. Further any 'write' operation would require 47*12016SGirish.Moodalbail@Sun.COM * the `NETWORK_INTERFACE_CONFIG_AUTH' authorization. 48*12016SGirish.Moodalbail@Sun.COM * 49*12016SGirish.Moodalbail@Sun.COM * On reboot, the aforementioned SMF service starts the daemon before any other 50*12016SGirish.Moodalbail@Sun.COM * networking service that configures network IP interfaces is started. 51*12016SGirish.Moodalbail@Sun.COM * Afterwards, the network/physical SMF script instantiates the persisted 52*12016SGirish.Moodalbail@Sun.COM * network interfaces, interface properties and addresses. 53*12016SGirish.Moodalbail@Sun.COM */ 54*12016SGirish.Moodalbail@Sun.COM 55*12016SGirish.Moodalbail@Sun.COM #include <errno.h> 56*12016SGirish.Moodalbail@Sun.COM #include <fcntl.h> 57*12016SGirish.Moodalbail@Sun.COM #include <priv_utils.h> 58*12016SGirish.Moodalbail@Sun.COM #include <signal.h> 59*12016SGirish.Moodalbail@Sun.COM #include <stdlib.h> 60*12016SGirish.Moodalbail@Sun.COM #include <stdio.h> 61*12016SGirish.Moodalbail@Sun.COM #include <strings.h> 62*12016SGirish.Moodalbail@Sun.COM #include <sys/param.h> 63*12016SGirish.Moodalbail@Sun.COM #include <sys/stat.h> 64*12016SGirish.Moodalbail@Sun.COM #include <unistd.h> 65*12016SGirish.Moodalbail@Sun.COM #include "ipmgmt_impl.h" 66*12016SGirish.Moodalbail@Sun.COM 67*12016SGirish.Moodalbail@Sun.COM const char *progname; 68*12016SGirish.Moodalbail@Sun.COM 69*12016SGirish.Moodalbail@Sun.COM /* readers-writers lock for reading/writing daemon data store */ 70*12016SGirish.Moodalbail@Sun.COM pthread_rwlock_t ipmgmt_dbconf_lock; 71*12016SGirish.Moodalbail@Sun.COM 72*12016SGirish.Moodalbail@Sun.COM /* tracks address object to {ifname|logical number|interface id} mapping */ 73*12016SGirish.Moodalbail@Sun.COM ipmgmt_aobjmap_list_t aobjmap; 74*12016SGirish.Moodalbail@Sun.COM 75*12016SGirish.Moodalbail@Sun.COM /* used to communicate failure to parent process, which spawned the daemon */ 76*12016SGirish.Moodalbail@Sun.COM static int pfds[2]; 77*12016SGirish.Moodalbail@Sun.COM 78*12016SGirish.Moodalbail@Sun.COM /* file descriptor to IPMGMT_DOOR */ 79*12016SGirish.Moodalbail@Sun.COM static int ipmgmt_door_fd = -1; 80*12016SGirish.Moodalbail@Sun.COM 81*12016SGirish.Moodalbail@Sun.COM static void ipmgmt_exit(int); 82*12016SGirish.Moodalbail@Sun.COM static int ipmgmt_init(); 83*12016SGirish.Moodalbail@Sun.COM static int ipmgmt_init_privileges(); 84*12016SGirish.Moodalbail@Sun.COM 85*12016SGirish.Moodalbail@Sun.COM static int 86*12016SGirish.Moodalbail@Sun.COM ipmgmt_db_init() 87*12016SGirish.Moodalbail@Sun.COM { 88*12016SGirish.Moodalbail@Sun.COM int fd, err; 89*12016SGirish.Moodalbail@Sun.COM 90*12016SGirish.Moodalbail@Sun.COM /* creates the address object data store, if it doesn't exist */ 91*12016SGirish.Moodalbail@Sun.COM if ((fd = open(ADDROBJ_MAPPING_DB_FILE, O_CREAT|O_RDONLY, 92*12016SGirish.Moodalbail@Sun.COM IPADM_FILE_MODE)) == -1) { 93*12016SGirish.Moodalbail@Sun.COM err = errno; 94*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "could not open %s: %s", 95*12016SGirish.Moodalbail@Sun.COM ADDROBJ_MAPPING_DB_FILE, strerror(err)); 96*12016SGirish.Moodalbail@Sun.COM return (err); 97*12016SGirish.Moodalbail@Sun.COM } 98*12016SGirish.Moodalbail@Sun.COM (void) close(fd); 99*12016SGirish.Moodalbail@Sun.COM 100*12016SGirish.Moodalbail@Sun.COM aobjmap.aobjmap_head = NULL; 101*12016SGirish.Moodalbail@Sun.COM (void) pthread_rwlock_init(&aobjmap.aobjmap_rwlock, NULL); 102*12016SGirish.Moodalbail@Sun.COM 103*12016SGirish.Moodalbail@Sun.COM /* 104*12016SGirish.Moodalbail@Sun.COM * If the daemon is recovering from a crash or restart, read the 105*12016SGirish.Moodalbail@Sun.COM * address object to logical interface mapping and build an in-memory 106*12016SGirish.Moodalbail@Sun.COM * representation of the mapping. That is, build `aobjmap' structure 107*12016SGirish.Moodalbail@Sun.COM * from address object data store. 108*12016SGirish.Moodalbail@Sun.COM */ 109*12016SGirish.Moodalbail@Sun.COM if ((err = ipadm_rw_db(ipmgmt_aobjmap_init, NULL, 110*12016SGirish.Moodalbail@Sun.COM ADDROBJ_MAPPING_DB_FILE, 0, IPADM_DB_READ)) != 0) { 111*12016SGirish.Moodalbail@Sun.COM /* if there was nothing to initialize, it's fine */ 112*12016SGirish.Moodalbail@Sun.COM if (err != ENOENT) 113*12016SGirish.Moodalbail@Sun.COM return (err); 114*12016SGirish.Moodalbail@Sun.COM err = 0; 115*12016SGirish.Moodalbail@Sun.COM } 116*12016SGirish.Moodalbail@Sun.COM 117*12016SGirish.Moodalbail@Sun.COM (void) pthread_rwlock_init(&ipmgmt_dbconf_lock, NULL); 118*12016SGirish.Moodalbail@Sun.COM return (err); 119*12016SGirish.Moodalbail@Sun.COM } 120*12016SGirish.Moodalbail@Sun.COM 121*12016SGirish.Moodalbail@Sun.COM static int 122*12016SGirish.Moodalbail@Sun.COM ipmgmt_door_init() 123*12016SGirish.Moodalbail@Sun.COM { 124*12016SGirish.Moodalbail@Sun.COM int fd; 125*12016SGirish.Moodalbail@Sun.COM int err; 126*12016SGirish.Moodalbail@Sun.COM 127*12016SGirish.Moodalbail@Sun.COM /* create the door file for ipmgmtd */ 128*12016SGirish.Moodalbail@Sun.COM if ((fd = open(IPMGMT_DOOR, O_CREAT|O_RDONLY, IPADM_FILE_MODE)) == -1) { 129*12016SGirish.Moodalbail@Sun.COM err = errno; 130*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "could not open %s: %s", 131*12016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(err)); 132*12016SGirish.Moodalbail@Sun.COM return (err); 133*12016SGirish.Moodalbail@Sun.COM } 134*12016SGirish.Moodalbail@Sun.COM (void) close(fd); 135*12016SGirish.Moodalbail@Sun.COM 136*12016SGirish.Moodalbail@Sun.COM if ((ipmgmt_door_fd = door_create(ipmgmt_handler, NULL, 137*12016SGirish.Moodalbail@Sun.COM DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 138*12016SGirish.Moodalbail@Sun.COM err = errno; 139*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to create door: %s", strerror(err)); 140*12016SGirish.Moodalbail@Sun.COM return (err); 141*12016SGirish.Moodalbail@Sun.COM } 142*12016SGirish.Moodalbail@Sun.COM /* 143*12016SGirish.Moodalbail@Sun.COM * fdetach first in case a previous daemon instance exited 144*12016SGirish.Moodalbail@Sun.COM * ungracefully. 145*12016SGirish.Moodalbail@Sun.COM */ 146*12016SGirish.Moodalbail@Sun.COM (void) fdetach(IPMGMT_DOOR); 147*12016SGirish.Moodalbail@Sun.COM if (fattach(ipmgmt_door_fd, IPMGMT_DOOR) != 0) { 148*12016SGirish.Moodalbail@Sun.COM err = errno; 149*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to attach door to %s: %s", 150*12016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(err)); 151*12016SGirish.Moodalbail@Sun.COM goto fail; 152*12016SGirish.Moodalbail@Sun.COM } 153*12016SGirish.Moodalbail@Sun.COM return (0); 154*12016SGirish.Moodalbail@Sun.COM fail: 155*12016SGirish.Moodalbail@Sun.COM (void) door_revoke(ipmgmt_door_fd); 156*12016SGirish.Moodalbail@Sun.COM ipmgmt_door_fd = -1; 157*12016SGirish.Moodalbail@Sun.COM return (err); 158*12016SGirish.Moodalbail@Sun.COM } 159*12016SGirish.Moodalbail@Sun.COM 160*12016SGirish.Moodalbail@Sun.COM static void 161*12016SGirish.Moodalbail@Sun.COM ipmgmt_door_fini() 162*12016SGirish.Moodalbail@Sun.COM { 163*12016SGirish.Moodalbail@Sun.COM if (ipmgmt_door_fd == -1) 164*12016SGirish.Moodalbail@Sun.COM return; 165*12016SGirish.Moodalbail@Sun.COM 166*12016SGirish.Moodalbail@Sun.COM (void) fdetach(IPMGMT_DOOR); 167*12016SGirish.Moodalbail@Sun.COM if (door_revoke(ipmgmt_door_fd) == -1) { 168*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "failed to revoke access to door %s: %s", 169*12016SGirish.Moodalbail@Sun.COM IPMGMT_DOOR, strerror(errno)); 170*12016SGirish.Moodalbail@Sun.COM } 171*12016SGirish.Moodalbail@Sun.COM } 172*12016SGirish.Moodalbail@Sun.COM 173*12016SGirish.Moodalbail@Sun.COM static int 174*12016SGirish.Moodalbail@Sun.COM ipmgmt_init() 175*12016SGirish.Moodalbail@Sun.COM { 176*12016SGirish.Moodalbail@Sun.COM int err; 177*12016SGirish.Moodalbail@Sun.COM 178*12016SGirish.Moodalbail@Sun.COM if (signal(SIGTERM, ipmgmt_exit) == SIG_ERR || 179*12016SGirish.Moodalbail@Sun.COM signal(SIGINT, ipmgmt_exit) == SIG_ERR) { 180*12016SGirish.Moodalbail@Sun.COM err = errno; 181*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s", 182*12016SGirish.Moodalbail@Sun.COM strerror(err)); 183*12016SGirish.Moodalbail@Sun.COM return (err); 184*12016SGirish.Moodalbail@Sun.COM } 185*12016SGirish.Moodalbail@Sun.COM if ((err = ipmgmt_db_init()) != 0 || (err = ipmgmt_door_init()) != 0) 186*12016SGirish.Moodalbail@Sun.COM return (err); 187*12016SGirish.Moodalbail@Sun.COM return (0); 188*12016SGirish.Moodalbail@Sun.COM } 189*12016SGirish.Moodalbail@Sun.COM 190*12016SGirish.Moodalbail@Sun.COM /* 191*12016SGirish.Moodalbail@Sun.COM * This is called by the child process to inform the parent process to 192*12016SGirish.Moodalbail@Sun.COM * exit with the given return value. 193*12016SGirish.Moodalbail@Sun.COM */ 194*12016SGirish.Moodalbail@Sun.COM static void 195*12016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(int rv) 196*12016SGirish.Moodalbail@Sun.COM { 197*12016SGirish.Moodalbail@Sun.COM if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) { 198*12016SGirish.Moodalbail@Sun.COM ipmgmt_log(LOG_WARNING, 199*12016SGirish.Moodalbail@Sun.COM "failed to inform parent process of status: %s", 200*12016SGirish.Moodalbail@Sun.COM strerror(errno)); 201*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]); 202*12016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE); 203*12016SGirish.Moodalbail@Sun.COM } 204*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]); 205*12016SGirish.Moodalbail@Sun.COM } 206*12016SGirish.Moodalbail@Sun.COM 207*12016SGirish.Moodalbail@Sun.COM /*ARGSUSED*/ 208*12016SGirish.Moodalbail@Sun.COM static void 209*12016SGirish.Moodalbail@Sun.COM ipmgmt_exit(int signo) 210*12016SGirish.Moodalbail@Sun.COM { 211*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]); 212*12016SGirish.Moodalbail@Sun.COM ipmgmt_door_fini(); 213*12016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE); 214*12016SGirish.Moodalbail@Sun.COM } 215*12016SGirish.Moodalbail@Sun.COM 216*12016SGirish.Moodalbail@Sun.COM /* 217*12016SGirish.Moodalbail@Sun.COM * Set the uid of this daemon to the "ipadm" user. Finish the following 218*12016SGirish.Moodalbail@Sun.COM * operations before setuid() because they need root privileges: 219*12016SGirish.Moodalbail@Sun.COM * 220*12016SGirish.Moodalbail@Sun.COM * - create the /etc/svc/volatile/ipadm directory; 221*12016SGirish.Moodalbail@Sun.COM * - change its uid/gid to "ipadm"/"sys"; 222*12016SGirish.Moodalbail@Sun.COM */ 223*12016SGirish.Moodalbail@Sun.COM static int 224*12016SGirish.Moodalbail@Sun.COM ipmgmt_init_privileges() 225*12016SGirish.Moodalbail@Sun.COM { 226*12016SGirish.Moodalbail@Sun.COM struct stat statbuf; 227*12016SGirish.Moodalbail@Sun.COM int err; 228*12016SGirish.Moodalbail@Sun.COM 229*12016SGirish.Moodalbail@Sun.COM /* create the IPADM_TMPFS_DIR directory */ 230*12016SGirish.Moodalbail@Sun.COM if (stat(IPADM_TMPFS_DIR, &statbuf) < 0) { 231*12016SGirish.Moodalbail@Sun.COM if (mkdir(IPADM_TMPFS_DIR, (mode_t)0755) < 0) { 232*12016SGirish.Moodalbail@Sun.COM err = errno; 233*12016SGirish.Moodalbail@Sun.COM goto fail; 234*12016SGirish.Moodalbail@Sun.COM } 235*12016SGirish.Moodalbail@Sun.COM } else { 236*12016SGirish.Moodalbail@Sun.COM if ((statbuf.st_mode & S_IFMT) != S_IFDIR) { 237*12016SGirish.Moodalbail@Sun.COM err = ENOTDIR; 238*12016SGirish.Moodalbail@Sun.COM goto fail; 239*12016SGirish.Moodalbail@Sun.COM } 240*12016SGirish.Moodalbail@Sun.COM } 241*12016SGirish.Moodalbail@Sun.COM 242*12016SGirish.Moodalbail@Sun.COM if ((chmod(IPADM_TMPFS_DIR, 0755) < 0) || 243*12016SGirish.Moodalbail@Sun.COM (chown(IPADM_TMPFS_DIR, UID_NETADM, GID_NETADM) < 0)) { 244*12016SGirish.Moodalbail@Sun.COM err = errno; 245*12016SGirish.Moodalbail@Sun.COM goto fail; 246*12016SGirish.Moodalbail@Sun.COM } 247*12016SGirish.Moodalbail@Sun.COM 248*12016SGirish.Moodalbail@Sun.COM /* 249*12016SGirish.Moodalbail@Sun.COM * limit the privileges of this daemon and set the uid of this 250*12016SGirish.Moodalbail@Sun.COM * daemon to UID_NETADM 251*12016SGirish.Moodalbail@Sun.COM */ 252*12016SGirish.Moodalbail@Sun.COM if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, UID_NETADM, 253*12016SGirish.Moodalbail@Sun.COM GID_NETADM, NULL) == -1) { 254*12016SGirish.Moodalbail@Sun.COM err = EPERM; 255*12016SGirish.Moodalbail@Sun.COM goto fail; 256*12016SGirish.Moodalbail@Sun.COM } 257*12016SGirish.Moodalbail@Sun.COM 258*12016SGirish.Moodalbail@Sun.COM return (0); 259*12016SGirish.Moodalbail@Sun.COM fail: 260*12016SGirish.Moodalbail@Sun.COM (void) ipmgmt_log(LOG_ERR, "failed to initialize the daemon: %s", 261*12016SGirish.Moodalbail@Sun.COM strerror(err)); 262*12016SGirish.Moodalbail@Sun.COM return (err); 263*12016SGirish.Moodalbail@Sun.COM } 264*12016SGirish.Moodalbail@Sun.COM 265*12016SGirish.Moodalbail@Sun.COM /* 266*12016SGirish.Moodalbail@Sun.COM * Keep the pfds fd open, close other fds. 267*12016SGirish.Moodalbail@Sun.COM */ 268*12016SGirish.Moodalbail@Sun.COM /*ARGSUSED*/ 269*12016SGirish.Moodalbail@Sun.COM static int 270*12016SGirish.Moodalbail@Sun.COM closefunc(void *arg, int fd) 271*12016SGirish.Moodalbail@Sun.COM { 272*12016SGirish.Moodalbail@Sun.COM if (fd != pfds[1]) 273*12016SGirish.Moodalbail@Sun.COM (void) close(fd); 274*12016SGirish.Moodalbail@Sun.COM return (0); 275*12016SGirish.Moodalbail@Sun.COM } 276*12016SGirish.Moodalbail@Sun.COM 277*12016SGirish.Moodalbail@Sun.COM /* 278*12016SGirish.Moodalbail@Sun.COM * We cannot use libc's daemon() because the door we create is associated with 279*12016SGirish.Moodalbail@Sun.COM * the process ID. If we create the door before the call to daemon(), it will 280*12016SGirish.Moodalbail@Sun.COM * be associated with the parent and it's incorrect. On the other hand if we 281*12016SGirish.Moodalbail@Sun.COM * create the door later, after the call to daemon(), parent process exits 282*12016SGirish.Moodalbail@Sun.COM * early and gives a false notion to SMF that 'ipmgmtd' is up and running, 283*12016SGirish.Moodalbail@Sun.COM * which is incorrect. So, we have our own daemon() equivalent. 284*12016SGirish.Moodalbail@Sun.COM */ 285*12016SGirish.Moodalbail@Sun.COM static boolean_t 286*12016SGirish.Moodalbail@Sun.COM ipmgmt_daemonize(void) 287*12016SGirish.Moodalbail@Sun.COM { 288*12016SGirish.Moodalbail@Sun.COM pid_t pid; 289*12016SGirish.Moodalbail@Sun.COM int rv; 290*12016SGirish.Moodalbail@Sun.COM 291*12016SGirish.Moodalbail@Sun.COM if (pipe(pfds) < 0) { 292*12016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "%s: pipe() failed: %s\n", 293*12016SGirish.Moodalbail@Sun.COM progname, strerror(errno)); 294*12016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE); 295*12016SGirish.Moodalbail@Sun.COM } 296*12016SGirish.Moodalbail@Sun.COM 297*12016SGirish.Moodalbail@Sun.COM if ((pid = fork()) == -1) { 298*12016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "%s: fork() failed: %s\n", 299*12016SGirish.Moodalbail@Sun.COM progname, strerror(errno)); 300*12016SGirish.Moodalbail@Sun.COM exit(EXIT_FAILURE); 301*12016SGirish.Moodalbail@Sun.COM } else if (pid > 0) { /* Parent */ 302*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[1]); 303*12016SGirish.Moodalbail@Sun.COM 304*12016SGirish.Moodalbail@Sun.COM /* 305*12016SGirish.Moodalbail@Sun.COM * Parent should not exit early, it should wait for the child 306*12016SGirish.Moodalbail@Sun.COM * to return Success/Failure. If the parent exits early, then 307*12016SGirish.Moodalbail@Sun.COM * SMF will think 'ipmgmtd' is up and would start all the 308*12016SGirish.Moodalbail@Sun.COM * depended services. 309*12016SGirish.Moodalbail@Sun.COM * 310*12016SGirish.Moodalbail@Sun.COM * If the child process exits unexpectedly, read() returns -1. 311*12016SGirish.Moodalbail@Sun.COM */ 312*12016SGirish.Moodalbail@Sun.COM if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) { 313*12016SGirish.Moodalbail@Sun.COM (void) kill(pid, SIGKILL); 314*12016SGirish.Moodalbail@Sun.COM rv = EXIT_FAILURE; 315*12016SGirish.Moodalbail@Sun.COM } 316*12016SGirish.Moodalbail@Sun.COM 317*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[0]); 318*12016SGirish.Moodalbail@Sun.COM exit(rv); 319*12016SGirish.Moodalbail@Sun.COM } 320*12016SGirish.Moodalbail@Sun.COM 321*12016SGirish.Moodalbail@Sun.COM /* Child */ 322*12016SGirish.Moodalbail@Sun.COM (void) close(pfds[0]); 323*12016SGirish.Moodalbail@Sun.COM (void) setsid(); 324*12016SGirish.Moodalbail@Sun.COM 325*12016SGirish.Moodalbail@Sun.COM /* close all files except pfds[1] */ 326*12016SGirish.Moodalbail@Sun.COM (void) fdwalk(closefunc, NULL); 327*12016SGirish.Moodalbail@Sun.COM (void) chdir("/"); 328*12016SGirish.Moodalbail@Sun.COM openlog(progname, LOG_PID, LOG_DAEMON); 329*12016SGirish.Moodalbail@Sun.COM return (B_TRUE); 330*12016SGirish.Moodalbail@Sun.COM } 331*12016SGirish.Moodalbail@Sun.COM 332*12016SGirish.Moodalbail@Sun.COM int 333*12016SGirish.Moodalbail@Sun.COM main(int argc, char *argv[]) 334*12016SGirish.Moodalbail@Sun.COM { 335*12016SGirish.Moodalbail@Sun.COM int opt; 336*12016SGirish.Moodalbail@Sun.COM boolean_t fg = B_FALSE; 337*12016SGirish.Moodalbail@Sun.COM 338*12016SGirish.Moodalbail@Sun.COM progname = strrchr(argv[0], '/'); 339*12016SGirish.Moodalbail@Sun.COM if (progname != NULL) 340*12016SGirish.Moodalbail@Sun.COM progname++; 341*12016SGirish.Moodalbail@Sun.COM else 342*12016SGirish.Moodalbail@Sun.COM progname = argv[0]; 343*12016SGirish.Moodalbail@Sun.COM 344*12016SGirish.Moodalbail@Sun.COM /* Process options */ 345*12016SGirish.Moodalbail@Sun.COM while ((opt = getopt(argc, argv, "f")) != EOF) { 346*12016SGirish.Moodalbail@Sun.COM switch (opt) { 347*12016SGirish.Moodalbail@Sun.COM case 'f': 348*12016SGirish.Moodalbail@Sun.COM fg = B_TRUE; 349*12016SGirish.Moodalbail@Sun.COM break; 350*12016SGirish.Moodalbail@Sun.COM default: 351*12016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, "Usage: %s [-f]\n", progname); 352*12016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE); 353*12016SGirish.Moodalbail@Sun.COM } 354*12016SGirish.Moodalbail@Sun.COM } 355*12016SGirish.Moodalbail@Sun.COM 356*12016SGirish.Moodalbail@Sun.COM if (!fg && getenv("SMF_FMRI") == NULL) { 357*12016SGirish.Moodalbail@Sun.COM (void) fprintf(stderr, 358*12016SGirish.Moodalbail@Sun.COM "ipmgmtd is a smf(5) managed service and cannot be run " 359*12016SGirish.Moodalbail@Sun.COM "from the command line.\n"); 360*12016SGirish.Moodalbail@Sun.COM return (EINVAL); 361*12016SGirish.Moodalbail@Sun.COM } 362*12016SGirish.Moodalbail@Sun.COM 363*12016SGirish.Moodalbail@Sun.COM if (!fg && !ipmgmt_daemonize()) 364*12016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE); 365*12016SGirish.Moodalbail@Sun.COM 366*12016SGirish.Moodalbail@Sun.COM if (ipmgmt_init_privileges() != 0) 367*12016SGirish.Moodalbail@Sun.COM goto child_out; 368*12016SGirish.Moodalbail@Sun.COM 369*12016SGirish.Moodalbail@Sun.COM if (ipmgmt_init() != 0) 370*12016SGirish.Moodalbail@Sun.COM goto child_out; 371*12016SGirish.Moodalbail@Sun.COM 372*12016SGirish.Moodalbail@Sun.COM /* Inform the parent process that it can successfully exit */ 373*12016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(EXIT_SUCCESS); 374*12016SGirish.Moodalbail@Sun.COM 375*12016SGirish.Moodalbail@Sun.COM for (;;) 376*12016SGirish.Moodalbail@Sun.COM (void) pause(); 377*12016SGirish.Moodalbail@Sun.COM 378*12016SGirish.Moodalbail@Sun.COM child_out: 379*12016SGirish.Moodalbail@Sun.COM /* return from main() forcibly exits an MT process */ 380*12016SGirish.Moodalbail@Sun.COM ipmgmt_inform_parent_exit(EXIT_FAILURE); 381*12016SGirish.Moodalbail@Sun.COM return (EXIT_FAILURE); 382*12016SGirish.Moodalbail@Sun.COM } 383