1 /* $OpenBSD: rstatd.c,v 1.7 2001/11/18 23:45:39 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, John Brezak 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char rcsid[] = "$OpenBSD: rstatd.c,v 1.7 2001/11/18 23:45:39 deraadt Exp $"; 38 #endif /* not lint */ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include <signal.h> 46 #include <syslog.h> 47 #include <errno.h> 48 #include <stdlib.h> 49 #include <rpc/rpc.h> 50 #include <rpcsvc/rstat.h> 51 52 extern int __svc_fdsetsize; 53 extern fd_set *__svc_fdset; 54 extern void svc_getreqset2 __P((fd_set *, int)); 55 extern void rstat_service(struct svc_req *, SVCXPRT *); 56 57 void my_svc_run(void); 58 59 int from_inetd = 1; /* started from inetd ? */ 60 int closedown = 20; /* how long to wait before going dormant */ 61 62 void 63 cleanup() 64 { 65 (void) pmap_unset(RSTATPROG, RSTATVERS_TIME); /* XXX signal races */ 66 (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 67 (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG); 68 _exit(0); 69 } 70 71 int 72 main(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 int sock = 0, proto = 0, fromlen; 77 struct sockaddr_in from; 78 SVCXPRT *transp; 79 80 if (argc == 2) 81 closedown = atoi(argv[1]); 82 if (closedown <= 0) 83 closedown = 20; 84 85 /* 86 * See if inetd started us 87 */ 88 fromlen = sizeof(from); 89 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 90 from_inetd = 0; 91 sock = RPC_ANYSOCK; 92 proto = IPPROTO_UDP; 93 } 94 95 if (!from_inetd) { 96 daemon(0, 0); 97 98 (void)pmap_unset(RSTATPROG, RSTATVERS_TIME); 99 (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 100 (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG); 101 102 (void) signal(SIGINT, cleanup); 103 (void) signal(SIGTERM, cleanup); 104 (void) signal(SIGHUP, cleanup); 105 } 106 107 openlog("rpc.rstatd", LOG_CONS|LOG_PID, LOG_DAEMON); 108 109 transp = svcudp_create(sock); 110 if (transp == NULL) { 111 syslog(LOG_ERR, "cannot create udp service."); 112 exit(1); 113 } 114 if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) { 115 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp)."); 116 exit(1); 117 } 118 if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) { 119 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp)."); 120 exit(1); 121 } 122 if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) { 123 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp)."); 124 exit(1); 125 } 126 127 my_svc_run(); 128 syslog(LOG_ERR, "svc_run returned"); 129 exit(1); 130 } 131 132 void 133 my_svc_run() 134 { 135 extern volatile sig_atomic_t wantupdatestat; 136 extern void updatestat(void); 137 fd_set *fds; 138 139 for (;;) { 140 if (wantupdatestat) { 141 updatestat(); 142 wantupdatestat = 0; 143 } 144 145 if (__svc_fdset) { 146 int bytes = howmany(__svc_fdsetsize, NFDBITS) * 147 sizeof(fd_mask); 148 fds = (fd_set *)malloc(bytes); /* XXX */ 149 memcpy(fds, __svc_fdset, bytes); 150 } else 151 fds = NULL; 152 switch (select(svc_maxfd+1, fds, 0, 0, (struct timeval *)0)) { 153 case -1: 154 if (errno == EINTR) { 155 if (fds) 156 free(fds); 157 continue; 158 } 159 perror("svc_run: - select failed"); 160 if (fds) 161 free(fds); 162 return; 163 case 0: 164 if (fds) 165 free(fds); 166 continue; 167 default: 168 svc_getreqset2(fds, svc_maxfd+1); 169 free(fds); 170 } 171 } 172 } 173