daemon_utils.c revision b56bf881a9655cb27b53cba1468312f7c6dfb0a2
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
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 * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int open_daemon_lock(const char *, int);
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 * check if another process is holding lock on the lock file.
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 if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
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 * i.e., we want the last two path components in the name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* First, strip off ":<instance>", if present. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Next, find final '/' and replace it with a dash */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now find the start of what we want our name to be */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
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 (void) snprintf(line, sizeof (line), "%ld\n", pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((pid_t)0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte_create_daemon_lock(const char *name, uid_t uid, gid_t gid)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);