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