3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/usr.sbin/ypserv/yp_main.c,v 1.29 2008/02/03 17:39:37 matteo Exp $
33 * $DragonFly: src/usr.sbin/ypserv/yp_main.c,v 1.5 2005/11/24 22:23:02 swildner Exp $
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.
49 #include <stdlib.h> /* getenv, exit */
50 #include <string.h> /* strcmp */
53 #include <rpc/pmap_clnt.h> /* for pmap_unset */
55 #include <sysent.h> /* getdtablesize, open */
56 #endif /* __cplusplus */
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include "yp_extern.h"
64 #define SIG_PF void(*)(int)
67 #define _RPCSVC_CLOSEDOWN 120
68 int _rpcpmstart; /* Started by a port monitor ? */
69 static int _rpcfdtype;
70 /* Whether Stream or Datagram ? */
71 /* States a server can be in wrt request */
77 extern void ypprog_1(struct svc_req *, SVCXPRT *);
78 extern void ypprog_2(struct svc_req *, SVCXPRT *);
79 extern int _rpc_dtablesize(void);
80 extern int _rpcsvcstate; /* Set when a request is serviced */
81 char *progname = "ypserv";
82 const char *yp_dir = _PATH_YP;
91 static struct socktype stlist[] = {
92 { "tcp", SOCK_STREAM },
93 { "udp", SOCK_DGRAM },
102 syslog(LOG_ERR, "%s", msg);
106 syslog(LOG_ERR, "%s", msg);
118 #endif /* def FD_SETSIZE */
119 int fd_setsize = _rpc_dtablesize();
120 struct timeval timeout;
122 /* Establish the identity of the parent ypserv process. */
130 #endif /* def FD_SETSIZE */
132 FD_SET(resfd, &readfds);
134 timeout.tv_sec = RESOLVER_TIMEOUT;
136 switch (select(fd_setsize, &readfds, NULL, NULL,
139 if (errno == EINTR) {
142 warn("svc_run: - select failed");
145 if (getpid() == yp_pid)
149 if (getpid() == yp_pid) {
150 if (FD_ISSET(resfd, &readfds)) {
152 FD_CLR(resfd, &readfds);
154 svc_getreqset(&readfds);
157 if (yp_pid != getpid())
165 pmap_unset(YPPROG, YPVERS);
166 pmap_unset(YPPROG, YPOLDVERS);
186 if (sig == SIGCHLD) {
187 while (wait3(&status, WNOHANG, NULL) > 0)
200 fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path] [-P port]\n");
207 if (_rpcsvcstate == _IDLE) {
208 extern fd_set svc_fdset;
212 if (_rpcfdtype == SOCK_DGRAM) {
217 size = getdtablesize();
219 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
220 if (FD_ISSET(i, &svc_fdset))
227 if (_rpcsvcstate == _SERVED)
228 _rpcsvcstate = _IDLE;
230 signal(SIGALRM, (SIG_PF)closedown);
231 alarm(_RPCSVC_CLOSEDOWN / 2);
235 main(int argc, char **argv)
237 SVCXPRT *transp = NULL;
240 struct sockaddr_in saddr;
241 socklen_t asize = sizeof (saddr);
243 in_port_t yp_port = 0;
247 while ((ch = getopt(argc, argv, "hdnp:P:")) != -1) {
250 debug = ypdb_debug = 1;
259 yp_port = (in_port_t)strtonum(optarg, 1, 65535,
260 (const char **)&errstr);
261 if (yp_port == 0 && errstr != NULL) {
262 _msgout("invalid port number provided");
277 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
278 int ssize = sizeof (int);
280 if (saddr.sin_family != AF_INET)
282 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
283 (char *)&_rpcfdtype, &ssize) == -1)
288 openlog("ypserv", LOG_PID, LOG_DAEMON);
292 err(1,"cannot fork");
294 openlog("ypserv", LOG_PID, LOG_DAEMON);
297 pmap_unset(YPPROG, YPVERS);
298 pmap_unset(YPPROG, 1);
302 * Initialize TCP/UDP sockets.
304 memset((char *)&saddr, 0, sizeof(saddr));
305 saddr.sin_family = AF_INET;
306 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
307 saddr.sin_port = htons(yp_port);
308 for (st = stlist; st->st_name != NULL; st++) {
309 /* Do not bind the socket if the user didn't specify a port */
313 sock = socket(AF_INET, st->st_type, 0);
315 if ((asprintf(&errstr, "cannot create a %s socket",
317 err(1, "unexpected failure in asprintf()");
319 free((void *)errstr);
322 if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr))
324 if ((asprintf(&errstr, "cannot bind %s socket",
326 err(1, "unexpected failure in asprintf()");
328 free((void *)errstr);
334 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
335 transp = svcudp_create(sock);
336 if (transp == NULL) {
337 _msgout("cannot create udp service");
342 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
343 _msgout("unable to register (YPPROG, YPOLDVERS, udp)");
346 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
347 _msgout("unable to register (YPPROG, YPVERS, udp)");
352 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
353 transp = svctcp_create(sock, 0, 0);
354 if (transp == NULL) {
355 _msgout("cannot create tcp service");
360 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
361 _msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
364 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
365 _msgout("unable to register (YPPROG, YPVERS, tcp)");
370 if (transp == NULL) {
371 _msgout("could not create a handle");
375 signal(SIGALRM, (SIG_PF)closedown);
376 alarm(_RPCSVC_CLOSEDOWN / 2);
379 * Make sure SIGPIPE doesn't blow us away while servicing TCP
382 signal(SIGPIPE, SIG_IGN);
383 signal(SIGCHLD, (SIG_PF) reaper);
384 signal(SIGTERM, (SIG_PF) reaper);
385 signal(SIGINT, (SIG_PF) reaper);
386 signal(SIGHUP, (SIG_PF) reaper);
388 _msgout("svc_run returned");