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]
23 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
30 #include <sys/param.h>
31 #include <sys/types.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,
62 _check_daemon_lock(const char *name
)
67 if ((fd
= open_daemon_lock(name
, O_RDONLY
)) == -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
);
84 return ((lock
.l_type
== F_UNLCK
) ? 0 : 1);
88 open_daemon_lock(const char *name
, int mode
)
90 char lock_file
[MAXPATHLEN
], buf
[MAXPATHLEN
];
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
, ':');
108 /* Next, find final '/' and replace it with a dash */
109 p
= strrchr(buf
, '/');
114 /* Now find the start of what we want our name to be */
115 p
= strrchr(buf
, '/');
122 (void) snprintf(lock_file
, MAXPATHLEN
, "/etc/svc/volatile/%s.lock", p
);
124 if ((fd
= open(lock_file
, mode
, 0644)) == -1)
128 (void) fchmod(fd
, 0644);
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.
139 _enter_daemon_lock(const char *name
)
147 (void) snprintf(line
, sizeof (line
), "%ld\n", pid
);
149 if ((fd
= open_daemon_lock(name
, O_RDWR
|O_CREAT
)) == -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) {
166 if (write(fd
, line
, strlen(line
)) == -1) {
175 _create_daemon_lock(const char *name
, uid_t uid
, gid_t gid
)
177 int fd
= open_daemon_lock(name
, O_CREAT
);
183 ret
= fchown(fd
, uid
, gid
);