3 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 /* main() function for status monitor daemon. Some of the code in this */
35 /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x */
36 /* The actual program logic is in the file procs.c */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
46 #include <rpc/rpc_com.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
59 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
61 int debug = 0; /* Controls syslog() calls for debug messages */
63 char **hosts, *svcport_str = NULL;
66 static int mallocd_svcport = 0;
68 static int sock_fdcnt;
69 static int sock_fdpos;
71 static int create_service(struct netconfig *nconf);
72 static void complete_service(struct netconfig *nconf, char *port_str);
73 static void clearout_service(void);
74 static void handle_sigchld(int sig);
75 void out_of_mem(void);
77 static void usage(void);
80 main(int argc, char **argv)
83 struct netconfig *nconf;
87 char *endptr, **hosts_bak;
89 int maxrec = RPC_MAXDATASIZE;
90 int attempt_cnt, port_len, port_pos, ret;
93 while ((ch = getopt(argc, argv, "dh:p:")) != -1)
101 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
102 if (hosts_bak == NULL) {
104 for (i = 0; i < nhosts; i++)
111 hosts[nhosts - 1] = strdup(optarg);
112 if (hosts[nhosts - 1] == NULL) {
113 for (i = 0; i < (nhosts - 1); i++)
121 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
122 if (endptr == NULL || *endptr != '\0' || svcport == 0 ||
123 svcport >= IPPORT_MAX)
126 svcport_str = strdup(optarg);
134 (void)rpcb_unset(SM_PROG, SM_VERS, NULL);
137 * Check if IPv6 support is present.
139 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
145 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
148 * If no hosts were specified, add a wildcard entry to bind to
149 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
153 hosts = malloc(sizeof(char**));
162 hosts_bak = realloc(hosts, (nhosts + 2) *
164 if (hosts_bak == NULL) {
165 for (i = 0; i < nhosts; i++)
173 hosts[nhosts - 2] = "::1";
175 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
176 if (hosts_bak == NULL) {
177 for (i = 0; i < nhosts; i++)
187 hosts[nhosts - 1] = "127.0.0.1";
195 nc_handle = setnetconfig();
196 while ((nconf = getnetconfig(nc_handle))) {
197 /* We want to listen only on udp6, tcp6, udp, tcp transports */
198 if (nconf->nc_flag & NC_VISIBLE) {
199 /* Skip if there's no IPv6 support */
200 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
203 ret = create_service(nconf);
205 /* Ignore this call */
209 * Failed to bind port, so close off
210 * all sockets created and try again
211 * if the port# was dynamically
212 * assigned via bind(2).
215 if (mallocd_svcport != 0 &&
216 attempt_cnt < GETPORT_MAXTRY) {
223 "bindresvport_sa: %m");
227 /* Start over at the first service. */
231 nc_handle = setnetconfig();
233 } else if (mallocd_svcport != 0 &&
234 attempt_cnt == GETPORT_MAXTRY) {
236 * For the last attempt, allow
237 * different port #s for each nconf
238 * by saving the svcport_str and
239 * setting it back to NULL.
241 port_list = realloc(port_list,
242 (port_len + 1) * sizeof(char *));
243 if (port_list == NULL)
245 port_list[port_len++] = svcport_str;
254 * Successfully bound the ports, so call complete_service() to
255 * do the rest of the setup on the service(s).
259 nc_handle = setnetconfig();
260 while ((nconf = getnetconfig(nc_handle))) {
261 /* We want to listen only on udp6, tcp6, udp, tcp transports */
262 if (nconf->nc_flag & NC_VISIBLE) {
263 /* Skip if there's no IPv6 support */
264 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
266 } else if (port_list != NULL) {
267 if (port_pos >= port_len) {
268 syslog(LOG_ERR, "too many port#s");
271 complete_service(nconf, port_list[port_pos++]);
273 complete_service(nconf, svcport_str);
276 endnetconfig(nc_handle);
278 if (port_list != NULL) {
279 for (port_pos = 0; port_pos < port_len; port_pos++)
280 free(port_list[port_pos]);
284 init_file("/var/db/statd.status");
286 /* Note that it is NOT sensible to run this program from inetd - the */
287 /* protocol assumes that it will run immediately at boot time. */
289 openlog("rpc.statd", 0, LOG_DAEMON);
290 if (debug) syslog(LOG_INFO, "Starting - debug enabled");
291 else syslog(LOG_INFO, "Starting");
293 /* Install signal handler to collect exit status of child processes */
294 sa.sa_handler = handle_sigchld;
295 sigemptyset(&sa.sa_mask);
296 sigaddset(&sa.sa_mask, SIGCHLD);
297 sa.sa_flags = SA_RESTART;
298 sigaction(SIGCHLD, &sa, NULL);
300 /* Initialisation now complete - start operating */
301 notify_hosts(); /* Forks a process (if necessary) to do the */
302 /* SM_NOTIFY calls, which may be slow. */
304 svc_run(); /* Should never return */
309 * This routine creates and binds sockets on the appropriate
310 * addresses. It gets called one time for each transport.
311 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
312 * bind failed with EADDRINUSE.
313 * Any file descriptors that have been created are stored in sock_fd and
314 * the total count of them is maintained in sock_fdcnt.
317 create_service(struct netconfig *nconf)
319 struct addrinfo hints, *res = NULL;
320 struct sockaddr_in *sin;
321 struct sockaddr_in6 *sin6;
322 struct __rpc_sockinfo si;
327 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
330 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
331 (nconf->nc_semantics != NC_TPI_COTS) &&
332 (nconf->nc_semantics != NC_TPI_COTS_ORD))
333 return (1); /* not my type */
336 * XXX - using RPC library internal functions.
338 if (!__rpc_nconf2sockinfo(nconf, &si)) {
339 syslog(LOG_ERR, "cannot get information for %s",
344 /* Get rpc.statd's address on this transport */
345 memset(&hints, 0, sizeof hints);
346 hints.ai_family = si.si_af;
347 hints.ai_socktype = si.si_socktype;
348 hints.ai_protocol = si.si_proto;
351 * Bind to specific IPs if asked to
354 while (nhostsbak > 0) {
356 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
359 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
361 hints.ai_flags = AI_PASSIVE;
364 * XXX - using RPC library internal functions.
366 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
367 syslog(LOG_ERR, "cannot create socket for %s",
371 switch (hints.ai_family) {
373 if (inet_pton(AF_INET, hosts[nhostsbak],
375 hints.ai_flags |= AI_NUMERICHOST;
378 * Skip if we have an AF_INET6 address.
380 if (inet_pton(AF_INET6, hosts[nhostsbak],
388 if (inet_pton(AF_INET6, hosts[nhostsbak],
390 hints.ai_flags |= AI_NUMERICHOST;
393 * Skip if we have an AF_INET address.
395 if (inet_pton(AF_INET, hosts[nhostsbak],
407 * If no hosts were specified, just bind to INADDR_ANY
409 if (strcmp("*", hosts[nhostsbak]) == 0) {
410 if (svcport_str == NULL) {
411 res = malloc(sizeof(struct addrinfo));
415 res->ai_flags = hints.ai_flags;
416 res->ai_family = hints.ai_family;
417 res->ai_protocol = hints.ai_protocol;
418 switch (res->ai_family) {
420 sin = malloc(sizeof(struct sockaddr_in));
423 sin->sin_family = AF_INET;
424 sin->sin_port = htons(0);
425 sin->sin_addr.s_addr = htonl(INADDR_ANY);
426 res->ai_addr = (struct sockaddr*) sin;
427 res->ai_addrlen = (socklen_t)
428 sizeof(struct sockaddr_in);
431 sin6 = malloc(sizeof(struct sockaddr_in6));
434 sin6->sin6_family = AF_INET6;
435 sin6->sin6_port = htons(0);
436 sin6->sin6_addr = in6addr_any;
437 res->ai_addr = (struct sockaddr*) sin6;
438 res->ai_addrlen = (socklen_t)
439 sizeof(struct sockaddr_in6);
442 syslog(LOG_ERR, "bad addr fam %d",
447 if ((aicode = getaddrinfo(NULL, svcport_str,
448 &hints, &res)) != 0) {
450 "cannot get local address for %s: %s",
452 gai_strerror(aicode));
458 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
459 &hints, &res)) != 0) {
461 "cannot get local address for %s: %s",
462 nconf->nc_netid, gai_strerror(aicode));
469 sock_fd[sock_fdcnt - 1] = fd;
471 /* Now, attempt the bind. */
472 r = bindresvport_sa(fd, res->ai_addr);
474 if (errno == EADDRINUSE && mallocd_svcport != 0) {
475 if (mallocd_res != 0) {
482 syslog(LOG_ERR, "bindresvport_sa: %m");
486 if (svcport_str == NULL) {
487 svcport_str = malloc(NI_MAXSERV * sizeof(char));
488 if (svcport_str == NULL)
492 if (getnameinfo(res->ai_addr,
493 res->ai_addr->sa_len, NULL, NI_MAXHOST,
494 svcport_str, NI_MAXSERV * sizeof(char),
495 NI_NUMERICHOST | NI_NUMERICSERV))
496 errx(1, "Cannot get port number");
498 if (mallocd_res != 0) {
509 * Called after all the create_service() calls have succeeded, to complete
510 * the setup and registration.
513 complete_service(struct netconfig *nconf, char *port_str)
515 struct addrinfo hints, *res = NULL;
516 struct __rpc_sockinfo si;
517 struct netbuf servaddr;
518 SVCXPRT *transp = NULL;
519 int aicode, fd, nhostsbak;
522 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
523 (nconf->nc_semantics != NC_TPI_COTS) &&
524 (nconf->nc_semantics != NC_TPI_COTS_ORD))
525 return; /* not my type */
528 * XXX - using RPC library internal functions.
530 if (!__rpc_nconf2sockinfo(nconf, &si)) {
531 syslog(LOG_ERR, "cannot get information for %s",
537 while (nhostsbak > 0) {
539 if (sock_fdpos >= sock_fdcnt) {
540 /* Should never happen. */
541 syslog(LOG_ERR, "Ran out of socket fd's");
544 fd = sock_fd[sock_fdpos++];
548 if (nconf->nc_semantics != NC_TPI_CLTS)
549 listen(fd, SOMAXCONN);
551 transp = svc_tli_create(fd, nconf, NULL,
552 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
554 if (transp != (SVCXPRT *) NULL) {
555 if (!svc_register(transp, SM_PROG, SM_VERS,
557 syslog(LOG_ERR, "can't register on %s",
560 if (!svc_reg(transp, SM_PROG, SM_VERS,
563 "can't register %s SM_PROG service",
567 syslog(LOG_WARNING, "can't create %s services",
570 if (registered == 0) {
572 memset(&hints, 0, sizeof hints);
573 hints.ai_flags = AI_PASSIVE;
574 hints.ai_family = si.si_af;
575 hints.ai_socktype = si.si_socktype;
576 hints.ai_protocol = si.si_proto;
579 if ((aicode = getaddrinfo(NULL, port_str, &hints,
581 syslog(LOG_ERR, "cannot get local address: %s",
582 gai_strerror(aicode));
586 servaddr.buf = malloc(res->ai_addrlen);
587 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
588 servaddr.len = res->ai_addrlen;
590 rpcb_set(SM_PROG, SM_VERS, nconf, &servaddr);
599 * Clear out sockets after a failure to bind one of them, so that the
600 * cycle of socket creation/binding can start anew.
603 clearout_service(void)
607 for (i = 0; i < sock_fdcnt; i++) {
608 if (sock_fd[i] >= 0) {
609 shutdown(sock_fd[i], SHUT_RDWR);
618 fprintf(stderr, "usage: rpc.statd [-d] [-h <bindip>] [-p <port>]\n");
622 /* handle_sigchld ---------------------------------------------------------- */
624 Purpose: Catch SIGCHLD and collect process status
626 Notes: No special action required, other than to collect the
627 process status and hence allow the child to die:
628 we only use child processes for asynchronous transmission
629 of SM_NOTIFY to other systems, so it is normal for the
630 children to exit when they have done their work.
633 static void handle_sigchld(int sig __unused)
636 pid = wait4(-1, &status, WNOHANG, (struct rusage*)0);
637 if (!pid) syslog(LOG_ERR, "Phantom SIGCHLD??");
638 else if (status == 0)
640 if (debug) syslog(LOG_DEBUG, "Child %d exited OK", pid);
642 else syslog(LOG_ERR, "Child %d failed with status %d", pid,
643 WEXITSTATUS(status));
647 * Out of memory, fatal
653 syslog(LOG_ERR, "out of memory");