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