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 /*
24*1219Sraf  * Copyright 2006 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 
30*1219Sraf #include "mt.h"
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <time.h>
380Sstevel@tonic-gate #include <wait.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <thread.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <ucontext.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
460Sstevel@tonic-gate #include <libscf.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int open_daemon_lock(const char *, int);
490Sstevel@tonic-gate static int is_auto_enabled(char *);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Check an array of services and enable any that don't have the
530Sstevel@tonic-gate  * "application/auto_enable" property set to "false", which is
540Sstevel@tonic-gate  * the interface to turn off this behaviour (see PSARC 2004/739).
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate void
570Sstevel@tonic-gate _check_services(char **svcs)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	char *s;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	for (; *svcs; svcs++) {
620Sstevel@tonic-gate 		if (is_auto_enabled(*svcs) == 0)
630Sstevel@tonic-gate 			continue;
640Sstevel@tonic-gate 		if ((s = smf_get_state(*svcs)) != NULL) {
650Sstevel@tonic-gate 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
66330Sthurlow 				(void) smf_enable_instance(*svcs,
67330Sthurlow 				    SMF_TEMPORARY);
680Sstevel@tonic-gate 			free(s);
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is
750Sstevel@tonic-gate  * active in the system at any point in time. If the lock is held
760Sstevel@tonic-gate  * by another process, do not block but return the pid owner of
770Sstevel@tonic-gate  * the lock to the caller immediately. The lock is cleared if the
780Sstevel@tonic-gate  * holding daemon process exits for any reason even if the lock
790Sstevel@tonic-gate  * file remains, so the daemon can be restarted if necessary.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * check if another process is holding lock on the lock file.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * return: 0 if file is not locked, else,
860Sstevel@tonic-gate  *	   1 if file is locked by another process, else,
870Sstevel@tonic-gate  *	   -1 on any error.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate int
900Sstevel@tonic-gate _check_daemon_lock(const char *name)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	int		fd, err;
930Sstevel@tonic-gate 	struct flock	lock;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
960Sstevel@tonic-gate 		if (errno == ENOENT)
970Sstevel@tonic-gate 			return (0);
980Sstevel@tonic-gate 		return (-1);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
1020Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
1030Sstevel@tonic-gate 	lock.l_start = (off_t)0;
1040Sstevel@tonic-gate 	lock.l_len = (off_t)0;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	err = fcntl(fd, F_GETLK, &lock);
1070Sstevel@tonic-gate 	(void) close(fd);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (err == -1)
1100Sstevel@tonic-gate 		return (-1);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	return ((lock.l_type == F_UNLCK) ? 0 : 1);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static int
1160Sstevel@tonic-gate open_daemon_lock(const char *name, int mode)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	char		lock_file[MAXPATHLEN], buf[MAXPATHLEN];
1190Sstevel@tonic-gate 	int		fd;
1200Sstevel@tonic-gate 	char		*p;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	/*
1230Sstevel@tonic-gate 	 * Our args look like this:
1240Sstevel@tonic-gate 	 *   svc:/network/nfs/status:default
1250Sstevel@tonic-gate 	 * We want to create a lock file named like this:
1260Sstevel@tonic-gate 	 *   /etc/svc/volatile/nfs-status.lock
1270Sstevel@tonic-gate 	 * i.e., we want the last two path components in the name.
1280Sstevel@tonic-gate 	 */
129132Srobinson 	(void) strncpy(buf, name, MAXPATHLEN);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/* First, strip off ":<instance>", if present. */
1320Sstevel@tonic-gate 	p = strrchr(buf, ':');
1330Sstevel@tonic-gate 	if (p != NULL)
1340Sstevel@tonic-gate 		*p = '\0';
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/* Next, find final '/' and replace it with a dash */
1370Sstevel@tonic-gate 	p = strrchr(buf, '/');
1380Sstevel@tonic-gate 	if (p == NULL)
1390Sstevel@tonic-gate 		p = buf;
1400Sstevel@tonic-gate 	else {
1410Sstevel@tonic-gate 		*p = '-';
1420Sstevel@tonic-gate 		/* Now find the start of what we want our name to be */
1430Sstevel@tonic-gate 		p = strrchr(buf, '/');
1440Sstevel@tonic-gate 		if (p == NULL)
1450Sstevel@tonic-gate 			p = buf;
1460Sstevel@tonic-gate 		else
1470Sstevel@tonic-gate 			p++;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	(void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if ((fd = open(lock_file, mode, 0644)) == -1)
1530Sstevel@tonic-gate 		return (-1);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (mode & O_CREAT)
1560Sstevel@tonic-gate 		(void) fchmod(fd, 0644);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	return (fd);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * lock the file, write caller's pid to the lock file
1620Sstevel@tonic-gate  * return: 0 if caller can establish lock, else,
1630Sstevel@tonic-gate  *	   pid of the current lock holder, else,
1640Sstevel@tonic-gate  *	   -1 on any printable error.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate pid_t
1670Sstevel@tonic-gate _enter_daemon_lock(const char *name)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	int		fd;
1700Sstevel@tonic-gate 	pid_t		pid;
1710Sstevel@tonic-gate 	char		line[BUFSIZ];
1720Sstevel@tonic-gate 	struct flock	lock;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	pid = getpid();
1750Sstevel@tonic-gate 	(void) snprintf(line, sizeof (line), "%ld\n", pid);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
1780Sstevel@tonic-gate 		return ((pid_t)-1);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
1810Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
1820Sstevel@tonic-gate 	lock.l_start = (off_t)0;
1830Sstevel@tonic-gate 	lock.l_len = (off_t)0;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (fcntl(fd, F_SETLK, &lock) == -1) {
1860Sstevel@tonic-gate 		if (fcntl(fd, F_GETLK, &lock) == -1) {
1870Sstevel@tonic-gate 			(void) close(fd);
1880Sstevel@tonic-gate 			return ((pid_t)-1);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		(void) close(fd);
1910Sstevel@tonic-gate 		return (lock.l_pid);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (write(fd, line, strlen(line)) == -1) {
1950Sstevel@tonic-gate 		(void) close(fd);
1960Sstevel@tonic-gate 		return ((pid_t)-1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	return ((pid_t)0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate int
2030Sstevel@tonic-gate _create_daemon_lock(const char *name, uid_t uid, gid_t gid)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	int fd = open_daemon_lock(name, O_CREAT);
2060Sstevel@tonic-gate 	int ret;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (fd < 0)
2090Sstevel@tonic-gate 		return (-1);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	ret = fchown(fd, uid, gid);
2120Sstevel@tonic-gate 	(void) close(fd);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	return (ret);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Check the "application/auto_enable" property for the passed FMRI.
2190Sstevel@tonic-gate  * scf_simple_prop_get() should find the property on an instance
2200Sstevel@tonic-gate  * or on the service FMRI.  The routine returns:
2210Sstevel@tonic-gate  * -1: inconclusive (likely no such property or FMRI)
2220Sstevel@tonic-gate  *  0: auto_enable is false
2230Sstevel@tonic-gate  *  1: auto_enable is true
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate int
2260Sstevel@tonic-gate is_auto_enabled(char *fmri)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	scf_simple_prop_t *prop;
2290Sstevel@tonic-gate 	int retval = -1;
2300Sstevel@tonic-gate 	uint8_t *ret;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	prop = scf_simple_prop_get(NULL, fmri, "application", "auto_enable");
2330Sstevel@tonic-gate 	if (!prop)
2340Sstevel@tonic-gate 		return (retval);
2350Sstevel@tonic-gate 	ret = scf_simple_prop_next_boolean(prop);
2360Sstevel@tonic-gate 	retval = (*ret != 0);
2370Sstevel@tonic-gate 	scf_simple_prop_free(prop);
2380Sstevel@tonic-gate 	return (retval);
2390Sstevel@tonic-gate }
240