]>
Commit | Line | Data |
---|---|---|
ac2f15b3 A |
1 | /* |
2 | * Copyright (c) 1995 | |
3 | * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed for the FreeBSD project | |
16 | * 4. Neither the name of the author nor the names of any co-contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | * | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static const char rcsid[] = | |
36 | "$FreeBSD$"; | |
37 | #endif /* not lint */ | |
38 | ||
39 | /* main() function for status monitor daemon. Some of the code in this */ | |
40 | /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x */ | |
41 | /* The actual program logic is in the file procs.c */ | |
42 | ||
43 | #include <err.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <rpc/rpc.h> | |
47 | #include <rpc/pmap_clnt.h> | |
48 | #include <string.h> | |
49 | #include <syslog.h> | |
50 | #include <sys/types.h> | |
51 | #include <sys/wait.h> | |
52 | #include <signal.h> | |
53 | #include <unistd.h> | |
54 | #include <fcntl.h> | |
55 | #include <sys/stat.h> | |
2b484d24 | 56 | #include <sys/sysctl.h> |
ac2f15b3 A |
57 | #include "statd.h" |
58 | ||
59 | int debug = 0; /* Controls syslog() calls for debug messages */ | |
60 | int notify_only = 0; /* only send SM_NOTIFY messages */ | |
61 | const char *pid_file = NULL; /* name of any pid file that has been claimed */ | |
62 | ||
63 | extern void sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp); | |
64 | static void handle_sigchld(int sig); | |
65 | static void handle_sig_cleanup(int sig); | |
66 | static void cleanup_pid_file(void); | |
67 | static void usage(void); | |
68 | ||
69 | int | |
70 | main(int argc, char **argv) | |
71 | { | |
72 | SVCXPRT *transp; | |
73 | struct sigaction sa; | |
74 | int c; | |
2b484d24 A |
75 | int mib[6]; |
76 | int oldstate; | |
77 | int oldsize; | |
78 | int newstate; | |
79 | ||
ac2f15b3 A |
80 | |
81 | while ((c = getopt(argc, argv, "dn")) != EOF) | |
82 | switch (c) { | |
83 | case 'd': | |
84 | debug = 1; | |
85 | break; | |
86 | case 'n': | |
87 | notify_only = 1; | |
88 | break; | |
89 | default: | |
90 | usage(); | |
91 | } | |
92 | ||
93 | /* Install signal handler to remove any pid file */ | |
94 | signal(SIGINT, handle_sig_cleanup); | |
95 | signal(SIGTERM, handle_sig_cleanup); | |
96 | signal(SIGHUP, handle_sig_cleanup); | |
97 | signal(SIGQUIT, handle_sig_cleanup); | |
98 | ||
99 | init_file("/var/db/statd.status"); | |
100 | ||
101 | openlog("rpc.statd", 0, LOG_DAEMON); | |
102 | ||
103 | if (notify_only) { | |
104 | notify_hosts(); | |
105 | exit(0); | |
106 | } | |
107 | ||
108 | /* Note that it is NOT sensible to run this program from inetd - the */ | |
109 | /* protocol assumes that it will run immediately at boot time. */ | |
110 | daemon(0, 0); | |
111 | ||
2b484d24 A |
112 | mib[0] = CTL_KERN; |
113 | mib[1] = KERN_PROCDELAYTERM; | |
114 | ||
115 | oldstate = 0; | |
116 | oldsize = 4; | |
117 | newstate = 1; | |
118 | ||
119 | if (sysctl(mib, 2, &oldstate, &oldsize, &newstate, 4) < 0) { | |
120 | syslog(LOG_INFO, "cannot mark pid for delayed termination"); | |
121 | } | |
122 | ||
123 | ||
ac2f15b3 A |
124 | if (claim_pid_file("/var/run/statd.pid", 0) < 0) |
125 | errx(1, "rpc.statd already running"); | |
126 | ||
ac2f15b3 A |
127 | (void)pmap_unset(SM_PROG, SM_VERS); |
128 | ||
129 | transp = svcudp_create(RPC_ANYSOCK); | |
130 | if (transp == NULL) | |
131 | errx(1, "cannot create udp service"); | |
132 | if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) | |
133 | errx(1, "unable to register (SM_PROG, SM_VERS, udp)"); | |
134 | ||
135 | transp = svctcp_create(RPC_ANYSOCK, 0, 0); | |
136 | if (transp == NULL) | |
137 | errx(1, "cannot create tcp service"); | |
138 | if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) | |
139 | errx(1, "unable to register (SM_PROG, SM_VERS, tcp)"); | |
140 | ||
141 | if (debug) syslog(LOG_INFO, "Starting - debug enabled"); | |
142 | else syslog(LOG_INFO, "Starting"); | |
143 | ||
144 | /* Install signal handler to collect exit status of child processes */ | |
145 | sa.sa_handler = handle_sigchld; | |
146 | sigemptyset(&sa.sa_mask); | |
147 | sigaddset(&sa.sa_mask, SIGCHLD); | |
148 | sa.sa_flags = SA_RESTART; | |
149 | sigaction(SIGCHLD, &sa, NULL); | |
150 | ||
151 | /* Initialisation now complete - start operating */ | |
152 | notify_hosts(); /* Forks a process (if necessary) to do the */ | |
153 | /* SM_NOTIFY calls, which may be slow. */ | |
154 | ||
155 | svc_run(); /* Should never return */ | |
156 | exit(1); | |
157 | } | |
158 | ||
159 | static void | |
160 | usage() | |
161 | { | |
162 | fprintf(stderr, "usage: rpc.statd [-dn]\n"); | |
163 | exit(1); | |
164 | } | |
165 | ||
166 | /* handle_sigchld ---------------------------------------------------------- */ | |
167 | /* | |
168 | Purpose: Catch SIGCHLD and collect process status | |
169 | Returns: Nothing. | |
170 | Notes: No special action required, other than to collect the | |
171 | process status and hence allow the child to die: | |
172 | we only use child processes for asynchronous transmission | |
173 | of SM_NOTIFY to other systems, so it is normal for the | |
174 | children to exit when they have done their work. | |
175 | */ | |
176 | ||
177 | static void handle_sigchld(int sig __unused) | |
178 | { | |
179 | int pid, status; | |
180 | pid = wait4(-1, &status, WNOHANG, (struct rusage*)0); | |
181 | if (!pid) syslog(LOG_ERR, "Phantom SIGCHLD??"); | |
182 | else if (status == 0) | |
183 | { | |
184 | if (debug) syslog(LOG_DEBUG, "Child %d exited OK", pid); | |
185 | } | |
186 | else syslog(LOG_ERR, "Child %d failed with status %d", pid, | |
187 | WEXITSTATUS(status)); | |
188 | } | |
189 | ||
190 | ||
191 | /* claim_pid_file ---------------------------------------------------------- */ | |
192 | /* | |
193 | Purpose: take ownership of and store pid in given pid_file | |
194 | Returns: 0 on success or -1 on failure | |
195 | Notes: force parameter requests that current owner (if any) of | |
196 | pid file be terminated. | |
197 | */ | |
198 | ||
199 | int | |
200 | claim_pid_file(const char *name, int force) | |
201 | { | |
202 | int pidfd, rv, retried = 0; | |
203 | FILE *pidfile; | |
204 | ||
205 | try_again: | |
206 | ||
207 | /* attempt exclusive open of pid file */ | |
208 | pidfd = open(name, O_EXCL|O_CREAT|O_WRONLY, | |
209 | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); | |
210 | if (pidfd < 0) { | |
211 | char buf[16]; | |
212 | pid_t pid; | |
213 | if (retried) | |
214 | return -1; | |
215 | bzero(buf, 16); | |
216 | retried = 1; | |
217 | /* pid file busy, check validity */ | |
218 | pidfd = open(name, O_RDONLY); | |
219 | if (pidfd < 0) | |
220 | goto try_again; | |
221 | rv = read(pidfd, buf, 15); | |
222 | close(pidfd); | |
223 | if (rv <= 0) | |
224 | goto try_again; | |
225 | pid = atoi(buf); | |
226 | if (pid <= 0) | |
227 | goto try_again; | |
228 | rv = kill(pid, force ? SIGKILL : 0); | |
229 | /* if can't signal, assume stale pid file */ | |
230 | if ((rv < 0) || force) | |
231 | unlink(name); | |
232 | goto try_again; | |
233 | } | |
234 | pid_file = name; | |
235 | atexit(cleanup_pid_file); | |
236 | ||
237 | pidfile = fdopen(pidfd, "w"); | |
238 | if (pidfile) { | |
239 | fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); | |
240 | fprintf(pidfile, "%d\n", getpid()); | |
241 | fclose(pidfile); | |
242 | } else | |
243 | perror("fdopen"); | |
244 | close(pidfd); | |
245 | return 0; | |
246 | } | |
247 | ||
248 | ||
249 | /* cleanup_pid_file -------------------------------------------------------- */ | |
250 | /* | |
251 | Purpose: delete any pid_file that has been claimed | |
252 | Returns: Nothing | |
253 | */ | |
254 | ||
255 | void | |
256 | cleanup_pid_file(void) | |
257 | { | |
258 | if (pid_file) { | |
259 | unlink(pid_file); | |
260 | pid_file = NULL; | |
261 | } | |
262 | } | |
263 | ||
264 | ||
265 | /* handle_sig_cleanup ------------------------------------------------------ */ | |
266 | /* | |
267 | Purpose: call pid file cleanup function on signal | |
268 | Returns: Nothing | |
269 | */ | |
270 | ||
271 | static void | |
272 | handle_sig_cleanup(int sig __unused) | |
273 | { | |
274 | cleanup_pid_file(); | |
275 | exit(1); | |
276 | } | |
277 |