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
5*12483SAntonello.Cruz@Sun.COM  * Common Development and Distribution License (the "License").
6*12483SAntonello.Cruz@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*12483SAntonello.Cruz@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
261219Sraf #include "mt.h"
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <time.h>
340Sstevel@tonic-gate #include <wait.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <thread.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <ucontext.h>
400Sstevel@tonic-gate #include <syslog.h>
410Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int open_daemon_lock(const char *, int);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is
470Sstevel@tonic-gate  * active in the system at any point in time. If the lock is held
480Sstevel@tonic-gate  * by another process, do not block but return the pid owner of
490Sstevel@tonic-gate  * the lock to the caller immediately. The lock is cleared if the
500Sstevel@tonic-gate  * holding daemon process exits for any reason even if the lock
510Sstevel@tonic-gate  * file remains, so the daemon can be restarted if necessary.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * check if another process is holding lock on the lock file.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * return: 0 if file is not locked, else,
580Sstevel@tonic-gate  *	   1 if file is locked by another process, else,
590Sstevel@tonic-gate  *	   -1 on any error.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate int
620Sstevel@tonic-gate _check_daemon_lock(const char *name)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	int		fd, err;
650Sstevel@tonic-gate 	struct flock	lock;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
680Sstevel@tonic-gate 		if (errno == ENOENT)
690Sstevel@tonic-gate 			return (0);
700Sstevel@tonic-gate 		return (-1);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
740Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
750Sstevel@tonic-gate 	lock.l_start = (off_t)0;
760Sstevel@tonic-gate 	lock.l_len = (off_t)0;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	err = fcntl(fd, F_GETLK, &lock);
790Sstevel@tonic-gate 	(void) close(fd);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (err == -1)
820Sstevel@tonic-gate 		return (-1);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	return ((lock.l_type == F_UNLCK) ? 0 : 1);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int
880Sstevel@tonic-gate open_daemon_lock(const char *name, int mode)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	char		lock_file[MAXPATHLEN], buf[MAXPATHLEN];
910Sstevel@tonic-gate 	int		fd;
920Sstevel@tonic-gate 	char		*p;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * Our args look like this:
960Sstevel@tonic-gate 	 *   svc:/network/nfs/status:default
970Sstevel@tonic-gate 	 * We want to create a lock file named like this:
980Sstevel@tonic-gate 	 *   /etc/svc/volatile/nfs-status.lock
990Sstevel@tonic-gate 	 * i.e., we want the last two path components in the name.
1000Sstevel@tonic-gate 	 */
101132Srobinson 	(void) strncpy(buf, name, MAXPATHLEN);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* First, strip off ":<instance>", if present. */
1040Sstevel@tonic-gate 	p = strrchr(buf, ':');
1050Sstevel@tonic-gate 	if (p != NULL)
1060Sstevel@tonic-gate 		*p = '\0';
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/* Next, find final '/' and replace it with a dash */
1090Sstevel@tonic-gate 	p = strrchr(buf, '/');
1100Sstevel@tonic-gate 	if (p == NULL)
1110Sstevel@tonic-gate 		p = buf;
1120Sstevel@tonic-gate 	else {
1130Sstevel@tonic-gate 		*p = '-';
1140Sstevel@tonic-gate 		/* Now find the start of what we want our name to be */
1150Sstevel@tonic-gate 		p = strrchr(buf, '/');
1160Sstevel@tonic-gate 		if (p == NULL)
1170Sstevel@tonic-gate 			p = buf;
1180Sstevel@tonic-gate 		else
1190Sstevel@tonic-gate 			p++;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((fd = open(lock_file, mode, 0644)) == -1)
1250Sstevel@tonic-gate 		return (-1);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (mode & O_CREAT)
1280Sstevel@tonic-gate 		(void) fchmod(fd, 0644);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	return (fd);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * lock the file, write caller's pid to the lock file
1340Sstevel@tonic-gate  * return: 0 if caller can establish lock, else,
1350Sstevel@tonic-gate  *	   pid of the current lock holder, else,
1360Sstevel@tonic-gate  *	   -1 on any printable error.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate pid_t
1390Sstevel@tonic-gate _enter_daemon_lock(const char *name)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	int		fd;
1420Sstevel@tonic-gate 	pid_t		pid;
1430Sstevel@tonic-gate 	char		line[BUFSIZ];
1440Sstevel@tonic-gate 	struct flock	lock;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	pid = getpid();
1470Sstevel@tonic-gate 	(void) snprintf(line, sizeof (line), "%ld\n", pid);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
1500Sstevel@tonic-gate 		return ((pid_t)-1);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
1530Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
1540Sstevel@tonic-gate 	lock.l_start = (off_t)0;
1550Sstevel@tonic-gate 	lock.l_len = (off_t)0;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (fcntl(fd, F_SETLK, &lock) == -1) {
1580Sstevel@tonic-gate 		if (fcntl(fd, F_GETLK, &lock) == -1) {
1590Sstevel@tonic-gate 			(void) close(fd);
1600Sstevel@tonic-gate 			return ((pid_t)-1);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 		(void) close(fd);
1630Sstevel@tonic-gate 		return (lock.l_pid);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (write(fd, line, strlen(line)) == -1) {
1670Sstevel@tonic-gate 		(void) close(fd);
1680Sstevel@tonic-gate 		return ((pid_t)-1);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return ((pid_t)0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate int
1750Sstevel@tonic-gate _create_daemon_lock(const char *name, uid_t uid, gid_t gid)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	int fd = open_daemon_lock(name, O_CREAT);
1780Sstevel@tonic-gate 	int ret;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (fd < 0)
1810Sstevel@tonic-gate 		return (-1);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	ret = fchown(fd, uid, gid);
1840Sstevel@tonic-gate 	(void) close(fd);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	return (ret);
1870Sstevel@tonic-gate }
188