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