1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/stat.h>
36*0Sstevel@tonic-gate #include <time.h>
37*0Sstevel@tonic-gate #include <wait.h>
38*0Sstevel@tonic-gate #include <fcntl.h>
39*0Sstevel@tonic-gate #include <thread.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <ucontext.h>
43*0Sstevel@tonic-gate #include <syslog.h>
44*0Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
45*0Sstevel@tonic-gate #include <libscf.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static int open_daemon_lock(const char *, int);
48*0Sstevel@tonic-gate static int is_auto_enabled(char *);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Check an array of services and enable any that don't have the
52*0Sstevel@tonic-gate  * "application/auto_enable" property set to "false", which is
53*0Sstevel@tonic-gate  * the interface to turn off this behaviour (see PSARC 2004/739).
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate void
56*0Sstevel@tonic-gate _check_services(char **svcs)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	char *s;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	for (; *svcs; svcs++) {
61*0Sstevel@tonic-gate 		if (is_auto_enabled(*svcs) == 0)
62*0Sstevel@tonic-gate 			continue;
63*0Sstevel@tonic-gate 		if ((s = smf_get_state(*svcs)) != NULL) {
64*0Sstevel@tonic-gate 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
65*0Sstevel@tonic-gate 				(void) smf_enable_instance(*svcs, 0);
66*0Sstevel@tonic-gate 			free(s);
67*0Sstevel@tonic-gate 		}
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is
73*0Sstevel@tonic-gate  * active in the system at any point in time. If the lock is held
74*0Sstevel@tonic-gate  * by another process, do not block but return the pid owner of
75*0Sstevel@tonic-gate  * the lock to the caller immediately. The lock is cleared if the
76*0Sstevel@tonic-gate  * holding daemon process exits for any reason even if the lock
77*0Sstevel@tonic-gate  * file remains, so the daemon can be restarted if necessary.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * check if another process is holding lock on the lock file.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * return: 0 if file is not locked, else,
84*0Sstevel@tonic-gate  *	   1 if file is locked by another process, else,
85*0Sstevel@tonic-gate  *	   -1 on any error.
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate int
88*0Sstevel@tonic-gate _check_daemon_lock(const char *name)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	int		fd, err;
91*0Sstevel@tonic-gate 	struct flock	lock;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
94*0Sstevel@tonic-gate 		if (errno == ENOENT)
95*0Sstevel@tonic-gate 			return (0);
96*0Sstevel@tonic-gate 		return (-1);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
100*0Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
101*0Sstevel@tonic-gate 	lock.l_start = (off_t)0;
102*0Sstevel@tonic-gate 	lock.l_len = (off_t)0;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	err = fcntl(fd, F_GETLK, &lock);
105*0Sstevel@tonic-gate 	(void) close(fd);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (err == -1)
108*0Sstevel@tonic-gate 		return (-1);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	return ((lock.l_type == F_UNLCK) ? 0 : 1);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static int
114*0Sstevel@tonic-gate open_daemon_lock(const char *name, int mode)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	char		lock_file[MAXPATHLEN], buf[MAXPATHLEN];
117*0Sstevel@tonic-gate 	int		fd;
118*0Sstevel@tonic-gate 	char		*p;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	/*
121*0Sstevel@tonic-gate 	 * Our args look like this:
122*0Sstevel@tonic-gate 	 *   svc:/network/nfs/status:default
123*0Sstevel@tonic-gate 	 * We want to create a lock file named like this:
124*0Sstevel@tonic-gate 	 *   /etc/svc/volatile/nfs-status.lock
125*0Sstevel@tonic-gate 	 * i.e., we want the last two path components in the name.
126*0Sstevel@tonic-gate 	 */
127*0Sstevel@tonic-gate 	strncpy(buf, name, MAXPATHLEN);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/* First, strip off ":<instance>", if present. */
130*0Sstevel@tonic-gate 	p = strrchr(buf, ':');
131*0Sstevel@tonic-gate 	if (p != NULL)
132*0Sstevel@tonic-gate 		*p = '\0';
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/* Next, find final '/' and replace it with a dash */
135*0Sstevel@tonic-gate 	p = strrchr(buf, '/');
136*0Sstevel@tonic-gate 	if (p == NULL)
137*0Sstevel@tonic-gate 		p = buf;
138*0Sstevel@tonic-gate 	else {
139*0Sstevel@tonic-gate 		*p = '-';
140*0Sstevel@tonic-gate 		/* Now find the start of what we want our name to be */
141*0Sstevel@tonic-gate 		p = strrchr(buf, '/');
142*0Sstevel@tonic-gate 		if (p == NULL)
143*0Sstevel@tonic-gate 			p = buf;
144*0Sstevel@tonic-gate 		else
145*0Sstevel@tonic-gate 			p++;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	(void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if ((fd = open(lock_file, mode, 0644)) == -1)
151*0Sstevel@tonic-gate 		return (-1);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (mode & O_CREAT)
154*0Sstevel@tonic-gate 		(void) fchmod(fd, 0644);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	return (fd);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * lock the file, write caller's pid to the lock file
160*0Sstevel@tonic-gate  * return: 0 if caller can establish lock, else,
161*0Sstevel@tonic-gate  *	   pid of the current lock holder, else,
162*0Sstevel@tonic-gate  *	   -1 on any printable error.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate pid_t
165*0Sstevel@tonic-gate _enter_daemon_lock(const char *name)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	int		fd;
168*0Sstevel@tonic-gate 	pid_t		pid;
169*0Sstevel@tonic-gate 	char		line[BUFSIZ];
170*0Sstevel@tonic-gate 	time_t		cur_time;
171*0Sstevel@tonic-gate 	struct flock	lock;
172*0Sstevel@tonic-gate 	struct stat	f_stat;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	pid = getpid();
175*0Sstevel@tonic-gate 	(void) snprintf(line, sizeof (line), "%ld\n", pid);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
178*0Sstevel@tonic-gate 		return ((pid_t)-1);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
181*0Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
182*0Sstevel@tonic-gate 	lock.l_start = (off_t)0;
183*0Sstevel@tonic-gate 	lock.l_len = (off_t)0;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (fcntl(fd, F_SETLK, &lock) == -1) {
186*0Sstevel@tonic-gate 		if (fcntl(fd, F_GETLK, &lock) == -1) {
187*0Sstevel@tonic-gate 			(void) close(fd);
188*0Sstevel@tonic-gate 			return ((pid_t)-1);
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 		(void) close(fd);
191*0Sstevel@tonic-gate 		return (lock.l_pid);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if (write(fd, line, strlen(line)) == -1) {
195*0Sstevel@tonic-gate 		(void) close(fd);
196*0Sstevel@tonic-gate 		return ((pid_t)-1);
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	return ((pid_t)0);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate int
203*0Sstevel@tonic-gate _create_daemon_lock(const char *name, uid_t uid, gid_t gid)
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	int fd = open_daemon_lock(name, O_CREAT);
206*0Sstevel@tonic-gate 	int ret;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (fd < 0)
209*0Sstevel@tonic-gate 		return (-1);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	ret = fchown(fd, uid, gid);
212*0Sstevel@tonic-gate 	(void) close(fd);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	return (ret);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * Check the "application/auto_enable" property for the passed FMRI.
219*0Sstevel@tonic-gate  * scf_simple_prop_get() should find the property on an instance
220*0Sstevel@tonic-gate  * or on the service FMRI.  The routine returns:
221*0Sstevel@tonic-gate  * -1: inconclusive (likely no such property or FMRI)
222*0Sstevel@tonic-gate  *  0: auto_enable is false
223*0Sstevel@tonic-gate  *  1: auto_enable is true
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate int
226*0Sstevel@tonic-gate is_auto_enabled(char *fmri)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	scf_simple_prop_t *prop;
229*0Sstevel@tonic-gate 	int retval = -1;
230*0Sstevel@tonic-gate 	uint8_t *ret;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	prop = scf_simple_prop_get(NULL, fmri, "application", "auto_enable");
233*0Sstevel@tonic-gate 	if (!prop)
234*0Sstevel@tonic-gate 		return (retval);
235*0Sstevel@tonic-gate 	ret = scf_simple_prop_next_boolean(prop);
236*0Sstevel@tonic-gate 	retval = (*ret != 0);
237*0Sstevel@tonic-gate 	scf_simple_prop_free(prop);
238*0Sstevel@tonic-gate 	return (retval);
239*0Sstevel@tonic-gate }
240