daemon_utils.c revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
5679c89fcd2facbb4334df8870d3d7a4d2b11673jv/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "mt.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdio.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/param.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/stat.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <time.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <wait.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <thread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
5679c89fcd2facbb4334df8870d3d7a4d2b11673jv#include <ucontext.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <syslog.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <rpcsvc/daemon_utils.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int open_daemon_lock(const char *, int);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use an advisory lock to ensure that only one daemon process is
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * active in the system at any point in time. If the lock is held
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * by another process, do not block but return the pid owner of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the lock to the caller immediately. The lock is cleared if the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * holding daemon process exits for any reason even if the lock
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file remains, so the daemon can be restarted if necessary.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * check if another process is holding lock on the lock file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return: 0 if file is not locked, else,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 1 if file is locked by another process, else,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -1 on any error.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_check_daemon_lock(const char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd, err;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct flock lock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (errno == ENOENT)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_type = F_WRLCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_whence = SEEK_SET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_start = (off_t)0;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto lock.l_len = (off_t)0;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte err = fcntl(fd, F_GETLK, &lock);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto (void) close(fd);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (err == -1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((lock.l_type == F_UNLCK) ? 0 : 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteopen_daemon_lock(const char *name, int mode)
034d83c4b3be1c9bbe03552a652ebb90d4d66885tim szeto{
034d83c4b3be1c9bbe03552a652ebb90d4d66885tim szeto char lock_file[MAXPATHLEN], buf[MAXPATHLEN];
034d83c4b3be1c9bbe03552a652ebb90d4d66885tim szeto int fd;
034d83c4b3be1c9bbe03552a652ebb90d4d66885tim szeto char *p;
034d83c4b3be1c9bbe03552a652ebb90d4d66885tim szeto
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Our args look like this:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * svc:/network/nfs/status:default
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We want to create a lock file named like this:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * /etc/svc/volatile/nfs-status.lock
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * i.e., we want the last two path components in the name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) strncpy(buf, name, MAXPATHLEN);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* First, strip off ":<instance>", if present. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = strrchr(buf, ':');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (p != NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = '\0';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Next, find final '/' and replace it with a dash */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = strrchr(buf, '/');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (p == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = '-';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now find the start of what we want our name to be */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = strrchr(buf, '/');
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (p == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p = buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte p++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open(lock_file, mode, 0644)) == -1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (mode & O_CREAT)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fchmod(fd, 0644);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * lock the file, write caller's pid to the lock file
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * return: 0 if caller can establish lock, else,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * pid of the current lock holder, else,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -1 on any printable error.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepid_t
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_enter_daemon_lock(const char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char line[BUFSIZ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct flock lock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid = getpid();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) snprintf(line, sizeof (line), "%ld\n", pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((pid_t)-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_type = F_WRLCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_whence = SEEK_SET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_start = (off_t)0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_len = (off_t)0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fcntl(fd, F_SETLK, &lock) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fcntl(fd, F_GETLK, &lock) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((pid_t)-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (lock.l_pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write(fd, line, strlen(line)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((pid_t)-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((pid_t)0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_create_daemon_lock(const char *name, uid_t uid, gid_t gid)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd = open_daemon_lock(name, O_CREAT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int ret;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fd < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ret = fchown(fd, uid, gid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ret);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte