]> git.saurik.com Git - apple/network_cmds.git/blame - rpc_statd.tproj/statd.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / rpc_statd.tproj / statd.c
CommitLineData
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
35static 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>
56#include "statd.h"
57
58int debug = 0; /* Controls syslog() calls for debug messages */
59int notify_only = 0; /* only send SM_NOTIFY messages */
60const char *pid_file = NULL; /* name of any pid file that has been claimed */
61
62extern void sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
63static void handle_sigchld(int sig);
64static void handle_sig_cleanup(int sig);
65static void cleanup_pid_file(void);
66static void usage(void);
67
68int
69main(int argc, char **argv)
70{
71 SVCXPRT *transp;
72 struct sigaction sa;
73 int c;
74 struct timespec ts;
75
76 while ((c = getopt(argc, argv, "dn")) != EOF)
77 switch (c) {
78 case 'd':
79 debug = 1;
80 break;
81 case 'n':
82 notify_only = 1;
83 break;
84 default:
85 usage();
86 }
87
88 /* Install signal handler to remove any pid file */
89 signal(SIGINT, handle_sig_cleanup);
90 signal(SIGTERM, handle_sig_cleanup);
91 signal(SIGHUP, handle_sig_cleanup);
92 signal(SIGQUIT, handle_sig_cleanup);
93
94 init_file("/var/db/statd.status");
95
96 openlog("rpc.statd", 0, LOG_DAEMON);
97
98 if (notify_only) {
99 notify_hosts();
100 exit(0);
101 }
102
103 /* Note that it is NOT sensible to run this program from inetd - the */
104 /* protocol assumes that it will run immediately at boot time. */
105 daemon(0, 0);
106
107 if (claim_pid_file("/var/run/statd.pid", 0) < 0)
108 errx(1, "rpc.statd already running");
109
110 /* start portmapper, in case it hasn't been started yet */
111 system("portmap");
112 /* sleep a little to give portmap a chance to start */
113 /* (better to sleep 50ms now than to timeout on portmap calls) */
114 ts.tv_sec = 0;
115 ts.tv_nsec = 50*1000*1000;
116 nanosleep(&ts, NULL);
117
118 (void)pmap_unset(SM_PROG, SM_VERS);
119
120 transp = svcudp_create(RPC_ANYSOCK);
121 if (transp == NULL)
122 errx(1, "cannot create udp service");
123 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP))
124 errx(1, "unable to register (SM_PROG, SM_VERS, udp)");
125
126 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
127 if (transp == NULL)
128 errx(1, "cannot create tcp service");
129 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP))
130 errx(1, "unable to register (SM_PROG, SM_VERS, tcp)");
131
132 if (debug) syslog(LOG_INFO, "Starting - debug enabled");
133 else syslog(LOG_INFO, "Starting");
134
135 /* Install signal handler to collect exit status of child processes */
136 sa.sa_handler = handle_sigchld;
137 sigemptyset(&sa.sa_mask);
138 sigaddset(&sa.sa_mask, SIGCHLD);
139 sa.sa_flags = SA_RESTART;
140 sigaction(SIGCHLD, &sa, NULL);
141
142 /* Initialisation now complete - start operating */
143 notify_hosts(); /* Forks a process (if necessary) to do the */
144 /* SM_NOTIFY calls, which may be slow. */
145
146 svc_run(); /* Should never return */
147 exit(1);
148}
149
150static void
151usage()
152{
153 fprintf(stderr, "usage: rpc.statd [-dn]\n");
154 exit(1);
155}
156
157/* handle_sigchld ---------------------------------------------------------- */
158/*
159 Purpose: Catch SIGCHLD and collect process status
160 Returns: Nothing.
161 Notes: No special action required, other than to collect the
162 process status and hence allow the child to die:
163 we only use child processes for asynchronous transmission
164 of SM_NOTIFY to other systems, so it is normal for the
165 children to exit when they have done their work.
166*/
167
168static void handle_sigchld(int sig __unused)
169{
170 int pid, status;
171 pid = wait4(-1, &status, WNOHANG, (struct rusage*)0);
172 if (!pid) syslog(LOG_ERR, "Phantom SIGCHLD??");
173 else if (status == 0)
174 {
175 if (debug) syslog(LOG_DEBUG, "Child %d exited OK", pid);
176 }
177 else syslog(LOG_ERR, "Child %d failed with status %d", pid,
178 WEXITSTATUS(status));
179}
180
181
182/* claim_pid_file ---------------------------------------------------------- */
183/*
184 Purpose: take ownership of and store pid in given pid_file
185 Returns: 0 on success or -1 on failure
186 Notes: force parameter requests that current owner (if any) of
187 pid file be terminated.
188*/
189
190int
191claim_pid_file(const char *name, int force)
192{
193 int pidfd, rv, retried = 0;
194 FILE *pidfile;
195
196try_again:
197
198 /* attempt exclusive open of pid file */
199 pidfd = open(name, O_EXCL|O_CREAT|O_WRONLY,
200 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
201 if (pidfd < 0) {
202 char buf[16];
203 pid_t pid;
204 if (retried)
205 return -1;
206 bzero(buf, 16);
207 retried = 1;
208 /* pid file busy, check validity */
209 pidfd = open(name, O_RDONLY);
210 if (pidfd < 0)
211 goto try_again;
212 rv = read(pidfd, buf, 15);
213 close(pidfd);
214 if (rv <= 0)
215 goto try_again;
216 pid = atoi(buf);
217 if (pid <= 0)
218 goto try_again;
219 rv = kill(pid, force ? SIGKILL : 0);
220 /* if can't signal, assume stale pid file */
221 if ((rv < 0) || force)
222 unlink(name);
223 goto try_again;
224 }
225 pid_file = name;
226 atexit(cleanup_pid_file);
227
228 pidfile = fdopen(pidfd, "w");
229 if (pidfile) {
230 fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
231 fprintf(pidfile, "%d\n", getpid());
232 fclose(pidfile);
233 } else
234 perror("fdopen");
235 close(pidfd);
236 return 0;
237}
238
239
240/* cleanup_pid_file -------------------------------------------------------- */
241/*
242 Purpose: delete any pid_file that has been claimed
243 Returns: Nothing
244*/
245
246void
247cleanup_pid_file(void)
248{
249 if (pid_file) {
250 unlink(pid_file);
251 pid_file = NULL;
252 }
253}
254
255
256/* handle_sig_cleanup ------------------------------------------------------ */
257/*
258 Purpose: call pid file cleanup function on signal
259 Returns: Nothing
260*/
261
262static void
263handle_sig_cleanup(int sig __unused)
264{
265 cleanup_pid_file();
266 exit(1);
267}
268