yp_main.c revision 12892
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.12 1995/12/11 22:38:19 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.12 1995/12/11 22:38:19 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_2 __P((struct svc_req, register SVCXPRT)); 80extern int _rpc_dtablesize __P((void)); 81extern int _rpcsvcstate; /* Set when a request is serviced */ 82char *progname = "ypserv"; 83char *yp_dir = _PATH_YP; 84int debug = 0; 85int do_dns = 0; 86int sunos_4_kludge = 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 if (sunos_4_kludge) 142 (void) pmap_unset(YPPROG, 1); 143} 144 145static void reaper(sig) 146 int sig; 147{ 148 int status; 149 150 if (sig == SIGCHLD) { 151 while (wait3(&status, WNOHANG, NULL) > 0) 152 children--; 153 } else { 154 unregister(); 155 exit(0); 156 } 157} 158 159static void usage() 160{ 161 fprintf(stderr, "Usage: %s [-h] [-d] [-n] [-k] [-p path]\n", progname); 162 exit(1); 163} 164 165static void 166closedown(int sig) 167{ 168 if (_rpcsvcstate == _IDLE) { 169 extern fd_set svc_fdset; 170 static int size; 171 int i, openfd; 172 173 if (_rpcfdtype == SOCK_DGRAM) { 174 unregister(); 175 exit(0); 176 } 177 if (size == 0) { 178 size = getdtablesize(); 179 } 180 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 181 if (FD_ISSET(i, &svc_fdset)) 182 openfd++; 183 if (openfd <= 1) { 184 unregister(); 185 exit(0); 186 } 187 } 188 if (_rpcsvcstate == _SERVED) 189 _rpcsvcstate = _IDLE; 190 191 (void) signal(SIGALRM, (SIG_PF) closedown); 192 (void) alarm(_RPCSVC_CLOSEDOWN/2); 193} 194 195main(argc, argv) 196 int argc; 197 char *argv[]; 198{ 199 register SVCXPRT *transp; 200 int sock; 201 int proto; 202 struct sockaddr_in saddr; 203 int asize = sizeof (saddr); 204 int ch; 205 206 while ((ch = getopt(argc, argv, "hdnkp:")) != EOF) { 207 switch(ch) { 208 case 'd': 209 debug = 1; 210 break; 211 case 'n': 212 do_dns = 1; 213 break; 214 case 'k': 215 sunos_4_kludge = 1; 216 break; 217 case 'p': 218 yp_dir = optarg; 219 break; 220 case 'h': 221 default: 222 usage(); 223 } 224 } 225 226 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 227 int ssize = sizeof (int); 228 229 if (saddr.sin_family != AF_INET) 230 exit(1); 231 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 232 (char *)&_rpcfdtype, &ssize) == -1) 233 exit(1); 234 sock = 0; 235 _rpcpmstart = 1; 236 proto = 0; 237 openlog(progname, LOG_PID, LOG_DAEMON); 238 } else { 239 if (!debug) { 240 if (daemon(0,0)) { 241 perror("cannot fork"); 242 exit(1); 243 } 244 openlog(progname, LOG_PID, LOG_DAEMON); 245 } 246 sock = RPC_ANYSOCK; 247 (void) pmap_unset(YPPROG, YPVERS); 248 if (sunos_4_kludge) 249 (void) pmap_unset(YPPROG, 1); 250 } 251 252 if (sunos_4_kludge && ((_rpcfdtype == 0)||(_rpcfdtype == SOCK_DGRAM))) { 253 transp = svcudp_create(sock); 254 if (transp == NULL) { 255 _msgout("cannot create udp service."); 256 exit(1); 257 } 258 if (!_rpcpmstart) 259 proto = IPPROTO_UDP; 260 if (!svc_register(transp, YPPROG, 1, ypprog_2, proto)) { 261 _msgout("unable to register (YPPROG, OLDYPVERS, udp)."); 262 exit(1); 263 } 264 } 265 266 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 267 transp = svcudp_create(sock); 268 if (transp == NULL) { 269 _msgout("cannot create udp service."); 270 exit(1); 271 } 272 if (!_rpcpmstart) 273 proto = IPPROTO_UDP; 274 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) { 275 _msgout("unable to register (YPPROG, YPVERS, udp)."); 276 exit(1); 277 } 278 } 279 280 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 281 transp = svctcp_create(sock, 0, 0); 282 if (transp == NULL) { 283 _msgout("cannot create tcp service."); 284 exit(1); 285 } 286 if (!_rpcpmstart) 287 proto = IPPROTO_TCP; 288 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) { 289 _msgout("unable to register (YPPROG, YPVERS, tcp)."); 290 exit(1); 291 } 292 } 293 294 if (transp == (SVCXPRT *)NULL) { 295 _msgout("could not create a handle"); 296 exit(1); 297 } 298 if (_rpcpmstart) { 299 (void) signal(SIGALRM, (SIG_PF) closedown); 300 (void) alarm(_RPCSVC_CLOSEDOWN/2); 301 } 302/* 303 * Make sure SIGPIPE doesn't blow us away while servicing TCP 304 * connections. 305 */ 306 (void) signal(SIGPIPE, SIG_IGN); 307 (void) signal(SIGCHLD, (SIG_PF) reaper); 308 (void) signal(SIGTERM, (SIG_PF) reaper); 309 (void) signal(SIGINT, (SIG_PF) reaper); 310 (void) signal(SIGHUP, (SIG_PF) reaper); 311 yp_svc_run(); 312 _msgout("svc_run returned"); 313 exit(1); 314 /* NOTREACHED */ 315} 316