17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*b56bf881SAntonello Cruz * Common Development and Distribution License (the "License"). 6*b56bf881SAntonello Cruz * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2161961e0fSrobinson 227c478bd9Sstevel@tonic-gate /* 23*b56bf881SAntonello Cruz * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 26e8031f0aSraf #include "mt.h" 277c478bd9Sstevel@tonic-gate #include <stdio.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <string.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/stat.h> 337c478bd9Sstevel@tonic-gate #include <time.h> 347c478bd9Sstevel@tonic-gate #include <wait.h> 357c478bd9Sstevel@tonic-gate #include <fcntl.h> 367c478bd9Sstevel@tonic-gate #include <thread.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <ucontext.h> 407c478bd9Sstevel@tonic-gate #include <syslog.h> 417c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static int open_daemon_lock(const char *, int); 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * Use an advisory lock to ensure that only one daemon process is 477c478bd9Sstevel@tonic-gate * active in the system at any point in time. If the lock is held 487c478bd9Sstevel@tonic-gate * by another process, do not block but return the pid owner of 497c478bd9Sstevel@tonic-gate * the lock to the caller immediately. The lock is cleared if the 507c478bd9Sstevel@tonic-gate * holding daemon process exits for any reason even if the lock 517c478bd9Sstevel@tonic-gate * file remains, so the daemon can be restarted if necessary. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * check if another process is holding lock on the lock file. 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * return: 0 if file is not locked, else, 587c478bd9Sstevel@tonic-gate * 1 if file is locked by another process, else, 597c478bd9Sstevel@tonic-gate * -1 on any error. 607c478bd9Sstevel@tonic-gate */ 617c478bd9Sstevel@tonic-gate int 627c478bd9Sstevel@tonic-gate _check_daemon_lock(const char *name) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate int fd, err; 657c478bd9Sstevel@tonic-gate struct flock lock; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) { 687c478bd9Sstevel@tonic-gate if (errno == ENOENT) 697c478bd9Sstevel@tonic-gate return (0); 707c478bd9Sstevel@tonic-gate return (-1); 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 747c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 757c478bd9Sstevel@tonic-gate lock.l_start = (off_t)0; 767c478bd9Sstevel@tonic-gate lock.l_len = (off_t)0; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate err = fcntl(fd, F_GETLK, &lock); 797c478bd9Sstevel@tonic-gate (void) close(fd); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (err == -1) 827c478bd9Sstevel@tonic-gate return (-1); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate return ((lock.l_type == F_UNLCK) ? 0 : 1); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static int 887c478bd9Sstevel@tonic-gate open_daemon_lock(const char *name, int mode) 897c478bd9Sstevel@tonic-gate { 907c478bd9Sstevel@tonic-gate char lock_file[MAXPATHLEN], buf[MAXPATHLEN]; 917c478bd9Sstevel@tonic-gate int fd; 927c478bd9Sstevel@tonic-gate char *p; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * Our args look like this: 967c478bd9Sstevel@tonic-gate * svc:/network/nfs/status:default 977c478bd9Sstevel@tonic-gate * We want to create a lock file named like this: 987c478bd9Sstevel@tonic-gate * /etc/svc/volatile/nfs-status.lock 997c478bd9Sstevel@tonic-gate * i.e., we want the last two path components in the name. 1007c478bd9Sstevel@tonic-gate */ 10161961e0fSrobinson (void) strncpy(buf, name, MAXPATHLEN); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* First, strip off ":<instance>", if present. */ 1047c478bd9Sstevel@tonic-gate p = strrchr(buf, ':'); 1057c478bd9Sstevel@tonic-gate if (p != NULL) 1067c478bd9Sstevel@tonic-gate *p = '\0'; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* Next, find final '/' and replace it with a dash */ 1097c478bd9Sstevel@tonic-gate p = strrchr(buf, '/'); 1107c478bd9Sstevel@tonic-gate if (p == NULL) 1117c478bd9Sstevel@tonic-gate p = buf; 1127c478bd9Sstevel@tonic-gate else { 1137c478bd9Sstevel@tonic-gate *p = '-'; 1147c478bd9Sstevel@tonic-gate /* Now find the start of what we want our name to be */ 1157c478bd9Sstevel@tonic-gate p = strrchr(buf, '/'); 1167c478bd9Sstevel@tonic-gate if (p == NULL) 1177c478bd9Sstevel@tonic-gate p = buf; 1187c478bd9Sstevel@tonic-gate else 1197c478bd9Sstevel@tonic-gate p++; 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate (void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if ((fd = open(lock_file, mode, 0644)) == -1) 1257c478bd9Sstevel@tonic-gate return (-1); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (mode & O_CREAT) 1287c478bd9Sstevel@tonic-gate (void) fchmod(fd, 0644); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate return (fd); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate /* 1337c478bd9Sstevel@tonic-gate * lock the file, write caller's pid to the lock file 1347c478bd9Sstevel@tonic-gate * return: 0 if caller can establish lock, else, 1357c478bd9Sstevel@tonic-gate * pid of the current lock holder, else, 1367c478bd9Sstevel@tonic-gate * -1 on any printable error. 1377c478bd9Sstevel@tonic-gate */ 1387c478bd9Sstevel@tonic-gate pid_t 1397c478bd9Sstevel@tonic-gate _enter_daemon_lock(const char *name) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate int fd; 1427c478bd9Sstevel@tonic-gate pid_t pid; 1437c478bd9Sstevel@tonic-gate char line[BUFSIZ]; 1447c478bd9Sstevel@tonic-gate struct flock lock; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate pid = getpid(); 1477c478bd9Sstevel@tonic-gate (void) snprintf(line, sizeof (line), "%ld\n", pid); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1) 1507c478bd9Sstevel@tonic-gate return ((pid_t)-1); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 1537c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 1547c478bd9Sstevel@tonic-gate lock.l_start = (off_t)0; 1557c478bd9Sstevel@tonic-gate lock.l_len = (off_t)0; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (fcntl(fd, F_SETLK, &lock) == -1) { 1587c478bd9Sstevel@tonic-gate if (fcntl(fd, F_GETLK, &lock) == -1) { 1597c478bd9Sstevel@tonic-gate (void) close(fd); 1607c478bd9Sstevel@tonic-gate return ((pid_t)-1); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate (void) close(fd); 1637c478bd9Sstevel@tonic-gate return (lock.l_pid); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (write(fd, line, strlen(line)) == -1) { 1677c478bd9Sstevel@tonic-gate (void) close(fd); 1687c478bd9Sstevel@tonic-gate return ((pid_t)-1); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate return ((pid_t)0); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate int 1757c478bd9Sstevel@tonic-gate _create_daemon_lock(const char *name, uid_t uid, gid_t gid) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate int fd = open_daemon_lock(name, O_CREAT); 1787c478bd9Sstevel@tonic-gate int ret; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (fd < 0) 1817c478bd9Sstevel@tonic-gate return (-1); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate ret = fchown(fd, uid, gid); 1847c478bd9Sstevel@tonic-gate (void) close(fd); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate return (ret); 1877c478bd9Sstevel@tonic-gate } 188