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