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