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