1 /* 2 * Copyright (c) 1995, 1996 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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 by Bill Paul. 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 Bill Paul 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 Bill Paul 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 * $Id: ypxfrd_main.c,v 1.3 1997/02/22 16:13:02 peter Exp $ 33 */ 34 35 #include "ypxfrd.h" 36 #include <stdio.h> 37 #include <stdlib.h> /* getenv, exit */ 38 #include <unistd.h> 39 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 40 #include <string.h> /* strcmp */ 41 #include <signal.h> 42 #include <sys/ttycom.h> /* TIOCNOTTY */ 43 #ifdef __cplusplus 44 #include <sysent.h> /* getdtablesize, open */ 45 #endif /* __cplusplus */ 46 #include <memory.h> 47 #include <sys/socket.h> 48 #include <netinet/in.h> 49 #include <syslog.h> 50 #include "ypxfrd_extern.h" 51 #include <sys/wait.h> 52 #include <errno.h> 53 54 #ifndef SIG_PF 55 #define SIG_PF void(*)(int) 56 #endif 57 58 #ifdef DEBUG 59 #define RPC_SVC_FG 60 #endif 61 62 #define _RPCSVC_CLOSEDOWN 120 63 #ifndef lint 64 static const char rcsid[] = "$Id: ypxfrd_main.c,v 1.3 1997/02/22 16:13:02 peter Exp $"; 65 #endif /* not lint */ 66 int _rpcpmstart; /* Started by a port monitor ? */ 67 static int _rpcfdtype; 68 /* Whether Stream or Datagram ? */ 69 /* States a server can be in wrt request */ 70 71 #define _IDLE 0 72 #define _SERVED 1 73 #define _SERVING 2 74 75 extern int _rpcsvcstate; /* Set when a request is serviced */ 76 77 char *progname = "rpc.ypxfrd"; 78 char *yp_dir = "/var/yp/"; 79 80 static 81 void _msgout(char* msg) 82 { 83 #ifdef RPC_SVC_FG 84 if (_rpcpmstart) 85 syslog(LOG_ERR, msg); 86 else 87 (void) fprintf(stderr, "%s\n", msg); 88 #else 89 syslog(LOG_ERR, msg); 90 #endif 91 } 92 93 static void 94 closedown(int sig) 95 { 96 if (_rpcsvcstate == _IDLE) { 97 extern fd_set svc_fdset; 98 static int size; 99 int i, openfd; 100 101 if (_rpcfdtype == SOCK_DGRAM) 102 exit(0); 103 if (size == 0) { 104 size = getdtablesize(); 105 } 106 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 107 if (FD_ISSET(i, &svc_fdset)) 108 openfd++; 109 if (openfd <= 1) 110 exit(0); 111 } 112 if (_rpcsvcstate == _SERVED) 113 _rpcsvcstate = _IDLE; 114 115 (void) signal(SIGALRM, (SIG_PF) closedown); 116 (void) alarm(_RPCSVC_CLOSEDOWN/2); 117 } 118 119 120 static void 121 ypxfrd_svc_run() 122 { 123 #ifdef FD_SETSIZE 124 fd_set readfds; 125 #else 126 int readfds; 127 #endif /* def FD_SETSIZE */ 128 extern int forked; 129 int pid; 130 int fd_setsize = _rpc_dtablesize(); 131 132 /* Establish the identity of the parent ypserv process. */ 133 pid = getpid(); 134 135 for (;;) { 136 #ifdef FD_SETSIZE 137 readfds = svc_fdset; 138 #else 139 readfds = svc_fds; 140 #endif /* def FD_SETSIZE */ 141 switch (select(fd_setsize, &readfds, NULL, NULL, 142 (struct timeval *)0)) { 143 case -1: 144 if (errno == EINTR) { 145 continue; 146 } 147 perror("svc_run: - select failed"); 148 return; 149 case 0: 150 continue; 151 default: 152 svc_getreqset(&readfds); 153 if (forked && pid != getpid()) 154 exit(0); 155 } 156 } 157 } 158 159 static void reaper(sig) 160 int sig; 161 { 162 int status; 163 164 if (sig == SIGHUP) { 165 load_securenets(); 166 return; 167 } 168 169 if (sig == SIGCHLD) { 170 while (wait3(&status, WNOHANG, NULL) > 0) 171 children--; 172 } else { 173 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 174 exit(0); 175 } 176 } 177 178 void usage() 179 { 180 fprintf(stderr, "%s [-p path]\n", progname); 181 exit(0); 182 } 183 184 main(argc, argv) 185 int argc; 186 char *argv[]; 187 { 188 register SVCXPRT *transp; 189 int sock; 190 int proto; 191 struct sockaddr_in saddr; 192 int asize = sizeof (saddr); 193 int ch; 194 195 while ((ch = getopt(argc, argv, "p:h")) != EOF) { 196 switch(ch) { 197 case 'p': 198 yp_dir = optarg; 199 break; 200 default: 201 usage(); 202 break; 203 } 204 } 205 206 load_securenets(); 207 208 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 209 int ssize = sizeof (int); 210 211 if (saddr.sin_family != AF_INET) 212 exit(1); 213 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 214 (char *)&_rpcfdtype, &ssize) == -1) 215 exit(1); 216 sock = 0; 217 _rpcpmstart = 1; 218 proto = 0; 219 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 220 } else { 221 #ifndef RPC_SVC_FG 222 int size; 223 int pid, i; 224 225 pid = fork(); 226 if (pid < 0) { 227 perror("cannot fork"); 228 exit(1); 229 } 230 if (pid) 231 exit(0); 232 size = getdtablesize(); 233 for (i = 0; i < size; i++) 234 (void) close(i); 235 i = open("/dev/console", 2); 236 (void) dup2(i, 1); 237 (void) dup2(i, 2); 238 i = open("/dev/tty", 2); 239 if (i >= 0) { 240 (void) ioctl(i, TIOCNOTTY, (char *)NULL); 241 (void) close(i); 242 } 243 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 244 #endif 245 sock = RPC_ANYSOCK; 246 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 247 } 248 249 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 250 transp = svcudp_create(sock); 251 if (transp == NULL) { 252 _msgout("cannot create udp service."); 253 exit(1); 254 } 255 if (!_rpcpmstart) 256 proto = IPPROTO_UDP; 257 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 258 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp)."); 259 exit(1); 260 } 261 } 262 263 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 264 transp = svctcp_create(sock, 0, 0); 265 if (transp == NULL) { 266 _msgout("cannot create tcp service."); 267 exit(1); 268 } 269 if (!_rpcpmstart) 270 proto = IPPROTO_TCP; 271 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 272 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)."); 273 exit(1); 274 } 275 } 276 277 if (transp == (SVCXPRT *)NULL) { 278 _msgout("could not create a handle"); 279 exit(1); 280 } 281 if (_rpcpmstart) { 282 (void) signal(SIGALRM, (SIG_PF) closedown); 283 (void) alarm(_RPCSVC_CLOSEDOWN/2); 284 } 285 286 (void) signal(SIGPIPE, SIG_IGN); 287 (void) signal(SIGCHLD, (SIG_PF) reaper); 288 (void) signal(SIGTERM, (SIG_PF) reaper); 289 (void) signal(SIGINT, (SIG_PF) reaper); 290 (void) signal(SIGHUP, (SIG_PF) reaper); 291 292 ypxfrd_svc_run(); 293 _msgout("svc_run returned"); 294 exit(1); 295 /* NOTREACHED */ 296 } 297