10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22132Srobinson 230Sstevel@tonic-gate /* 240Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <stdio.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <sys/stat.h> 360Sstevel@tonic-gate #include <time.h> 370Sstevel@tonic-gate #include <wait.h> 380Sstevel@tonic-gate #include <fcntl.h> 390Sstevel@tonic-gate #include <thread.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <errno.h> 420Sstevel@tonic-gate #include <ucontext.h> 430Sstevel@tonic-gate #include <syslog.h> 440Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h> 450Sstevel@tonic-gate #include <libscf.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate static int open_daemon_lock(const char *, int); 480Sstevel@tonic-gate static int is_auto_enabled(char *); 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* 510Sstevel@tonic-gate * Check an array of services and enable any that don't have the 520Sstevel@tonic-gate * "application/auto_enable" property set to "false", which is 530Sstevel@tonic-gate * the interface to turn off this behaviour (see PSARC 2004/739). 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate void 560Sstevel@tonic-gate _check_services(char **svcs) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate char *s; 590Sstevel@tonic-gate 600Sstevel@tonic-gate for (; *svcs; svcs++) { 610Sstevel@tonic-gate if (is_auto_enabled(*svcs) == 0) 620Sstevel@tonic-gate continue; 630Sstevel@tonic-gate if ((s = smf_get_state(*svcs)) != NULL) { 640Sstevel@tonic-gate if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0) 65*330Sthurlow (void) smf_enable_instance(*svcs, 66*330Sthurlow SMF_TEMPORARY); 670Sstevel@tonic-gate free(s); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate } 700Sstevel@tonic-gate } 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * Use an advisory lock to ensure that only one daemon process is 740Sstevel@tonic-gate * active in the system at any point in time. If the lock is held 750Sstevel@tonic-gate * by another process, do not block but return the pid owner of 760Sstevel@tonic-gate * the lock to the caller immediately. The lock is cleared if the 770Sstevel@tonic-gate * holding daemon process exits for any reason even if the lock 780Sstevel@tonic-gate * file remains, so the daemon can be restarted if necessary. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* 820Sstevel@tonic-gate * check if another process is holding lock on the lock file. 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * return: 0 if file is not locked, else, 850Sstevel@tonic-gate * 1 if file is locked by another process, else, 860Sstevel@tonic-gate * -1 on any error. 870Sstevel@tonic-gate */ 880Sstevel@tonic-gate int 890Sstevel@tonic-gate _check_daemon_lock(const char *name) 900Sstevel@tonic-gate { 910Sstevel@tonic-gate int fd, err; 920Sstevel@tonic-gate struct flock lock; 930Sstevel@tonic-gate 940Sstevel@tonic-gate if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) { 950Sstevel@tonic-gate if (errno == ENOENT) 960Sstevel@tonic-gate return (0); 970Sstevel@tonic-gate return (-1); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 1000Sstevel@tonic-gate lock.l_type = F_WRLCK; 1010Sstevel@tonic-gate lock.l_whence = SEEK_SET; 1020Sstevel@tonic-gate lock.l_start = (off_t)0; 1030Sstevel@tonic-gate lock.l_len = (off_t)0; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate err = fcntl(fd, F_GETLK, &lock); 1060Sstevel@tonic-gate (void) close(fd); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (err == -1) 1090Sstevel@tonic-gate return (-1); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate return ((lock.l_type == F_UNLCK) ? 0 : 1); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate static int 1150Sstevel@tonic-gate open_daemon_lock(const char *name, int mode) 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate char lock_file[MAXPATHLEN], buf[MAXPATHLEN]; 1180Sstevel@tonic-gate int fd; 1190Sstevel@tonic-gate char *p; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * Our args look like this: 1230Sstevel@tonic-gate * svc:/network/nfs/status:default 1240Sstevel@tonic-gate * We want to create a lock file named like this: 1250Sstevel@tonic-gate * /etc/svc/volatile/nfs-status.lock 1260Sstevel@tonic-gate * i.e., we want the last two path components in the name. 1270Sstevel@tonic-gate */ 128132Srobinson (void) strncpy(buf, name, MAXPATHLEN); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* First, strip off ":<instance>", if present. */ 1310Sstevel@tonic-gate p = strrchr(buf, ':'); 1320Sstevel@tonic-gate if (p != NULL) 1330Sstevel@tonic-gate *p = '\0'; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* Next, find final '/' and replace it with a dash */ 1360Sstevel@tonic-gate p = strrchr(buf, '/'); 1370Sstevel@tonic-gate if (p == NULL) 1380Sstevel@tonic-gate p = buf; 1390Sstevel@tonic-gate else { 1400Sstevel@tonic-gate *p = '-'; 1410Sstevel@tonic-gate /* Now find the start of what we want our name to be */ 1420Sstevel@tonic-gate p = strrchr(buf, '/'); 1430Sstevel@tonic-gate if (p == NULL) 1440Sstevel@tonic-gate p = buf; 1450Sstevel@tonic-gate else 1460Sstevel@tonic-gate p++; 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate (void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate if ((fd = open(lock_file, mode, 0644)) == -1) 1520Sstevel@tonic-gate return (-1); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate if (mode & O_CREAT) 1550Sstevel@tonic-gate (void) fchmod(fd, 0644); 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate return (fd); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * lock the file, write caller's pid to the lock file 1610Sstevel@tonic-gate * return: 0 if caller can establish lock, else, 1620Sstevel@tonic-gate * pid of the current lock holder, else, 1630Sstevel@tonic-gate * -1 on any printable error. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate pid_t 1660Sstevel@tonic-gate _enter_daemon_lock(const char *name) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate int fd; 1690Sstevel@tonic-gate pid_t pid; 1700Sstevel@tonic-gate char line[BUFSIZ]; 1710Sstevel@tonic-gate struct flock lock; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate pid = getpid(); 1740Sstevel@tonic-gate (void) snprintf(line, sizeof (line), "%ld\n", pid); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1) 1770Sstevel@tonic-gate return ((pid_t)-1); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate lock.l_type = F_WRLCK; 1800Sstevel@tonic-gate lock.l_whence = SEEK_SET; 1810Sstevel@tonic-gate lock.l_start = (off_t)0; 1820Sstevel@tonic-gate lock.l_len = (off_t)0; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate if (fcntl(fd, F_SETLK, &lock) == -1) { 1850Sstevel@tonic-gate if (fcntl(fd, F_GETLK, &lock) == -1) { 1860Sstevel@tonic-gate (void) close(fd); 1870Sstevel@tonic-gate return ((pid_t)-1); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate (void) close(fd); 1900Sstevel@tonic-gate return (lock.l_pid); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (write(fd, line, strlen(line)) == -1) { 1940Sstevel@tonic-gate (void) close(fd); 1950Sstevel@tonic-gate return ((pid_t)-1); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate return ((pid_t)0); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate int 2020Sstevel@tonic-gate _create_daemon_lock(const char *name, uid_t uid, gid_t gid) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate int fd = open_daemon_lock(name, O_CREAT); 2050Sstevel@tonic-gate int ret; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate if (fd < 0) 2080Sstevel@tonic-gate return (-1); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate ret = fchown(fd, uid, gid); 2110Sstevel@tonic-gate (void) close(fd); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate return (ret); 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Check the "application/auto_enable" property for the passed FMRI. 2180Sstevel@tonic-gate * scf_simple_prop_get() should find the property on an instance 2190Sstevel@tonic-gate * or on the service FMRI. The routine returns: 2200Sstevel@tonic-gate * -1: inconclusive (likely no such property or FMRI) 2210Sstevel@tonic-gate * 0: auto_enable is false 2220Sstevel@tonic-gate * 1: auto_enable is true 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate int 2250Sstevel@tonic-gate is_auto_enabled(char *fmri) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate scf_simple_prop_t *prop; 2280Sstevel@tonic-gate int retval = -1; 2290Sstevel@tonic-gate uint8_t *ret; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate prop = scf_simple_prop_get(NULL, fmri, "application", "auto_enable"); 2320Sstevel@tonic-gate if (!prop) 2330Sstevel@tonic-gate return (retval); 2340Sstevel@tonic-gate ret = scf_simple_prop_next_boolean(prop); 2350Sstevel@tonic-gate retval = (*ret != 0); 2360Sstevel@tonic-gate scf_simple_prop_free(prop); 2370Sstevel@tonic-gate return (retval); 2380Sstevel@tonic-gate } 239