import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / daemon_utils.c
blob99e1fc6a385bfc8c027924cb52bc934d247f6952
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include "mt.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <time.h>
34 #include <wait.h>
35 #include <fcntl.h>
36 #include <thread.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <ucontext.h>
40 #include <syslog.h>
41 #include <rpcsvc/daemon_utils.h>
43 static int open_daemon_lock(const char *, int);
46 * Use an advisory lock to ensure that only one daemon process is
47 * active in the system at any point in time. If the lock is held
48 * by another process, do not block but return the pid owner of
49 * the lock to the caller immediately. The lock is cleared if the
50 * holding daemon process exits for any reason even if the lock
51 * file remains, so the daemon can be restarted if necessary.
55 * check if another process is holding lock on the lock file.
57 * return: 0 if file is not locked, else,
58 * 1 if file is locked by another process, else,
59 * -1 on any error.
61 int
62 _check_daemon_lock(const char *name)
64 int fd, err;
65 struct flock lock;
67 if ((fd = open_daemon_lock(name, O_RDONLY)) == -1) {
68 if (errno == ENOENT)
69 return (0);
70 return (-1);
73 lock.l_type = F_WRLCK;
74 lock.l_whence = SEEK_SET;
75 lock.l_start = (off_t)0;
76 lock.l_len = (off_t)0;
78 err = fcntl(fd, F_GETLK, &lock);
79 (void) close(fd);
81 if (err == -1)
82 return (-1);
84 return ((lock.l_type == F_UNLCK) ? 0 : 1);
87 static int
88 open_daemon_lock(const char *name, int mode)
90 char lock_file[MAXPATHLEN], buf[MAXPATHLEN];
91 int fd;
92 char *p;
95 * Our args look like this:
96 * svc:/network/nfs/status:default
97 * We want to create a lock file named like this:
98 * /etc/svc/volatile/nfs-status.lock
99 * i.e., we want the last two path components in the name.
101 (void) strncpy(buf, name, MAXPATHLEN);
103 /* First, strip off ":<instance>", if present. */
104 p = strrchr(buf, ':');
105 if (p != NULL)
106 *p = '\0';
108 /* Next, find final '/' and replace it with a dash */
109 p = strrchr(buf, '/');
110 if (p == NULL)
111 p = buf;
112 else {
113 *p = '-';
114 /* Now find the start of what we want our name to be */
115 p = strrchr(buf, '/');
116 if (p == NULL)
117 p = buf;
118 else
119 p++;
122 (void) snprintf(lock_file, MAXPATHLEN, "/etc/svc/volatile/%s.lock", p);
124 if ((fd = open(lock_file, mode, 0644)) == -1)
125 return (-1);
127 if (mode & O_CREAT)
128 (void) fchmod(fd, 0644);
130 return (fd);
133 * lock the file, write caller's pid to the lock file
134 * return: 0 if caller can establish lock, else,
135 * pid of the current lock holder, else,
136 * -1 on any printable error.
138 pid_t
139 _enter_daemon_lock(const char *name)
141 int fd;
142 pid_t pid;
143 char line[BUFSIZ];
144 struct flock lock;
146 pid = getpid();
147 (void) snprintf(line, sizeof (line), "%ld\n", pid);
149 if ((fd = open_daemon_lock(name, O_RDWR|O_CREAT)) == -1)
150 return ((pid_t)-1);
152 lock.l_type = F_WRLCK;
153 lock.l_whence = SEEK_SET;
154 lock.l_start = (off_t)0;
155 lock.l_len = (off_t)0;
157 if (fcntl(fd, F_SETLK, &lock) == -1) {
158 if (fcntl(fd, F_GETLK, &lock) == -1) {
159 (void) close(fd);
160 return ((pid_t)-1);
162 (void) close(fd);
163 return (lock.l_pid);
166 if (write(fd, line, strlen(line)) == -1) {
167 (void) close(fd);
168 return ((pid_t)-1);
171 return ((pid_t)0);
175 _create_daemon_lock(const char *name, uid_t uid, gid_t gid)
177 int fd = open_daemon_lock(name, O_CREAT);
178 int ret;
180 if (fd < 0)
181 return (-1);
183 ret = fchown(fd, uid, gid);
184 (void) close(fd);
186 return (ret);