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 $
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.
48 #include <stdlib.h> /* getenv, exit */
49 #include <string.h> /* strcmp */
52 #include <rpc/pmap_clnt.h> /* for pmap_unset */
54 #include <sysent.h> /* getdtablesize, open */
55 #endif /* __cplusplus */
56 #include <sys/socket.h>
57 #include <netinet/in.h>
59 #include "yp_extern.h"
63 #define SIG_PF void(*)(int)
66 #define _RPCSVC_CLOSEDOWN 120
67 int _rpcpmstart
; /* Started by a port monitor ? */
68 static int _rpcfdtype
;
69 /* Whether Stream or Datagram ? */
70 /* States a server can be in wrt request */
76 extern void ypprog_1(struct svc_req
*, SVCXPRT
*);
77 extern void ypprog_2(struct svc_req
*, SVCXPRT
*);
78 extern int _rpc_dtablesize(void);
79 extern int _rpcsvcstate
; /* Set when a request is serviced */
80 char *progname
= "ypserv";
81 const char *yp_dir
= _PATH_YP
;
90 static struct socktype stlist
[] = {
91 { "tcp", SOCK_STREAM
},
92 { "udp", SOCK_DGRAM
},
101 syslog(LOG_ERR
, "%s", msg
);
105 syslog(LOG_ERR
, "%s", msg
);
117 #endif /* def FD_SETSIZE */
118 int fd_setsize
= _rpc_dtablesize();
119 struct timeval timeout
;
121 /* Establish the identity of the parent ypserv process. */
129 #endif /* def FD_SETSIZE */
131 FD_SET(resfd
, &readfds
);
133 timeout
.tv_sec
= RESOLVER_TIMEOUT
;
135 switch (select(fd_setsize
, &readfds
, NULL
, NULL
,
138 if (errno
== EINTR
) {
141 warn("svc_run: - select failed");
144 if (getpid() == yp_pid
)
148 if (getpid() == yp_pid
) {
149 if (FD_ISSET(resfd
, &readfds
)) {
151 FD_CLR(resfd
, &readfds
);
153 svc_getreqset(&readfds
);
156 if (yp_pid
!= getpid())
164 pmap_unset(YPPROG
, YPVERS
);
165 pmap_unset(YPPROG
, YPOLDVERS
);
185 if (sig
== SIGCHLD
) {
186 while (wait3(&status
, WNOHANG
, NULL
) > 0)
199 fprintf(stderr
, "usage: ypserv [-h] [-d] [-n] [-p path] [-P port]\n");
206 if (_rpcsvcstate
== _IDLE
) {
207 extern fd_set svc_fdset
;
211 if (_rpcfdtype
== SOCK_DGRAM
) {
216 size
= getdtablesize();
218 for (i
= 0, openfd
= 0; i
< size
&& openfd
< 2; i
++)
219 if (FD_ISSET(i
, &svc_fdset
))
226 if (_rpcsvcstate
== _SERVED
)
227 _rpcsvcstate
= _IDLE
;
229 signal(SIGALRM
, (SIG_PF
)closedown
);
230 alarm(_RPCSVC_CLOSEDOWN
/ 2);
234 main(int argc
, char *argv
[])
236 SVCXPRT
*transp
= NULL
;
239 struct sockaddr_in saddr
;
240 socklen_t asize
= sizeof (saddr
);
242 in_port_t yp_port
= 0;
246 while ((ch
= getopt(argc
, argv
, "hdnp:P:")) != -1) {
249 debug
= ypdb_debug
= 1;
258 yp_port
= (in_port_t
)strtonum(optarg
, 1, 65535,
259 (const char **)&errstr
);
260 if (yp_port
== 0 && errstr
!= NULL
) {
261 _msgout("invalid port number provided");
276 if (getsockname(0, (struct sockaddr
*)&saddr
, &asize
) == 0) {
277 int ssize
= sizeof (int);
279 if (saddr
.sin_family
!= AF_INET
)
281 if (getsockopt(0, SOL_SOCKET
, SO_TYPE
,
282 (char *)&_rpcfdtype
, &ssize
) == -1)
287 openlog("ypserv", LOG_PID
, LOG_DAEMON
);
291 err(1,"cannot fork");
293 openlog("ypserv", LOG_PID
, LOG_DAEMON
);
296 pmap_unset(YPPROG
, YPVERS
);
297 pmap_unset(YPPROG
, 1);
301 * Initialize TCP/UDP sockets.
303 memset((char *)&saddr
, 0, sizeof(saddr
));
304 saddr
.sin_family
= AF_INET
;
305 saddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
306 saddr
.sin_port
= htons(yp_port
);
307 for (st
= stlist
; st
->st_name
!= NULL
; st
++) {
308 /* Do not bind the socket if the user didn't specify a port */
312 sock
= socket(AF_INET
, st
->st_type
, 0);
314 if ((asprintf(&errstr
, "cannot create a %s socket",
316 err(1, "unexpected failure in asprintf()");
318 free((void *)errstr
);
321 if (bind(sock
, (struct sockaddr
*) &saddr
, sizeof(saddr
))
323 if ((asprintf(&errstr
, "cannot bind %s socket",
325 err(1, "unexpected failure in asprintf()");
327 free((void *)errstr
);
333 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_DGRAM
)) {
334 transp
= svcudp_create(sock
);
335 if (transp
== NULL
) {
336 _msgout("cannot create udp service");
341 if (!svc_register(transp
, YPPROG
, YPOLDVERS
, ypprog_1
, proto
)) {
342 _msgout("unable to register (YPPROG, YPOLDVERS, udp)");
345 if (!svc_register(transp
, YPPROG
, YPVERS
, ypprog_2
, proto
)) {
346 _msgout("unable to register (YPPROG, YPVERS, udp)");
351 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_STREAM
)) {
352 transp
= svctcp_create(sock
, 0, 0);
353 if (transp
== NULL
) {
354 _msgout("cannot create tcp service");
359 if (!svc_register(transp
, YPPROG
, YPOLDVERS
, ypprog_1
, proto
)) {
360 _msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
363 if (!svc_register(transp
, YPPROG
, YPVERS
, ypprog_2
, proto
)) {
364 _msgout("unable to register (YPPROG, YPVERS, tcp)");
369 if (transp
== NULL
) {
370 _msgout("could not create a handle");
374 signal(SIGALRM
, (SIG_PF
)closedown
);
375 alarm(_RPCSVC_CLOSEDOWN
/ 2);
378 * Make sure SIGPIPE doesn't blow us away while servicing TCP
381 signal(SIGPIPE
, SIG_IGN
);
382 signal(SIGCHLD
, (SIG_PF
) reaper
);
383 signal(SIGTERM
, (SIG_PF
) reaper
);
384 signal(SIGINT
, (SIG_PF
) reaper
);
385 signal(SIGHUP
, (SIG_PF
) reaper
);
387 _msgout("svc_run returned");