Clean (void) casts from usr.sbin
[dragonfly.git] / usr.sbin / rpc.ypupdated / ypupdated_main.c
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  * $FreeBSD: src/usr.sbin/rpc.ypupdated/ypupdated_main.c,v 1.4.2.3 2002/02/15 00:46:58 des Exp $
33  * $DragonFly: src/usr.sbin/rpc.ypupdated/ypupdated_main.c,v 1.4 2004/12/18 22:48:14 swildner Exp $
34  */
35
36 #include "ypupdate_prot.h"
37 #include <stdio.h>
38 #include <stdlib.h> /* getenv, exit */
39 #include <rpc/pmap_clnt.h> /* for pmap_unset */
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>
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 int _rpcpmstart;                /* Started by a port monitor ? */
67 static int _rpcfdtype;
68                  /* Whether Stream or Datagram ? */
69         /* States a server can be in wrt request */
70
71 #define _IDLE 0
72 #define _SERVED 1
73 #define _SERVING 2
74
75 extern int _rpcsvcstate;         /* Set when a request is serviced */
76
77 char *progname = "rpc.ypupdated";
78 char *yp_dir = "/var/yp/";
79
80 static
81 void _msgout(char* msg)
82 {
83 #ifdef RPC_SVC_FG
84         if (_rpcpmstart)
85                 syslog(LOG_ERR, "%s", msg);
86         else
87                 warnx("%s", msg);
88 #else
89         syslog(LOG_ERR, "%s", msg);
90 #endif
91 }
92
93 static void
94 closedown(int sig)
95 {
96         if (_rpcsvcstate == _IDLE) {
97                 extern fd_set svc_fdset;
98                 static int size;
99                 int i, openfd;
100
101                 if (_rpcfdtype == SOCK_DGRAM)
102                         exit(0);
103                 if (size == 0) {
104                         size = getdtablesize();
105                 }
106                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
107                         if (FD_ISSET(i, &svc_fdset))
108                                 openfd++;
109                 if (openfd <= 1)
110                         exit(0);
111         }
112         if (_rpcsvcstate == _SERVED)
113                 _rpcsvcstate = _IDLE;
114
115         signal(SIGALRM, (SIG_PF) closedown);
116         alarm(_RPCSVC_CLOSEDOWN/2);
117 }
118
119 static void
120 ypupdated_svc_run()
121 {
122 #ifdef FD_SETSIZE
123         fd_set readfds;
124 #else
125         int readfds;
126 #endif /* def FD_SETSIZE */
127         extern int forked;
128         int pid;
129         int fd_setsize = _rpc_dtablesize();
130
131         /* Establish the identity of the parent ypupdated process. */
132         pid = getpid();
133
134         for (;;) {
135 #ifdef FD_SETSIZE
136                 readfds = svc_fdset;
137 #else
138                 readfds = svc_fds;
139 #endif /* def FD_SETSIZE */
140                 switch (select(fd_setsize, &readfds, NULL, NULL,
141                                (struct timeval *)0)) {
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
158 static void reaper(sig)
159         int sig;
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 {
174                 pmap_unset(YPU_PROG, YPU_VERS);
175                 exit(0);
176         }
177 }
178
179 void usage()
180 {
181         fprintf(stderr, "rpc.ypupdatedd [-p path]\n");
182         exit(0);
183 }
184
185 int
186 main(argc, argv)
187         int argc;
188         char *argv[];
189 {
190         SVCXPRT *transp = NULL;
191         int sock;
192         int proto = 0;
193         struct sockaddr_in saddr;
194         int asize = sizeof (saddr);
195         int ch;
196
197         while ((ch = getopt(argc, argv, "p:h")) != -1) {
198                 switch (ch) {
199                 case 'p':
200                         yp_dir = optarg;
201                         break;
202                 default:
203                         usage();
204                         break;
205                 }
206         }
207 #ifdef foo
208         load_securenets();
209 #endif
210
211         if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) {
212                 yp_error("failed to register AUTH_DES flavor");
213                 exit(1);
214         }
215
216         if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
217                 int ssize = sizeof (int);
218
219                 if (saddr.sin_family != AF_INET)
220                         exit(1);
221                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
222                                 (char *)&_rpcfdtype, &ssize) == -1)
223                         exit(1);
224                 sock = 0;
225                 _rpcpmstart = 1;
226                 proto = 0;
227                 openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON);
228         } else {
229 #ifndef RPC_SVC_FG
230                 if (daemon(0,0)) {
231                         err(1, "cannot fork");
232                 }
233                 openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON);
234 #endif
235                 sock = RPC_ANYSOCK;
236                 pmap_unset(YPU_PROG, YPU_VERS);
237         }
238
239         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
240                 transp = svcudp_create(sock);
241                 if (transp == NULL) {
242                         _msgout("cannot create udp service.");
243                         exit(1);
244                 }
245                 if (!_rpcpmstart)
246                         proto = IPPROTO_UDP;
247                 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
248                         _msgout("unable to register (YPU_PROG, YPU_VERS, udp).");
249                         exit(1);
250                 }
251         }
252
253         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
254                 transp = svctcp_create(sock, 0, 0);
255                 if (transp == NULL) {
256                         _msgout("cannot create tcp service.");
257                         exit(1);
258                 }
259                 if (!_rpcpmstart)
260                         proto = IPPROTO_TCP;
261                 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
262                         _msgout("unable to register (YPU_PROG, YPU_VERS, tcp).");
263                         exit(1);
264                 }
265         }
266
267         if (transp == (SVCXPRT *)NULL) {
268                 _msgout("could not create a handle");
269                 exit(1);
270         }
271         if (_rpcpmstart) {
272                 signal(SIGALRM, (SIG_PF) closedown);
273                 alarm(_RPCSVC_CLOSEDOWN/2);
274         }
275
276         signal(SIGPIPE, SIG_IGN);
277         signal(SIGCHLD, (SIG_PF) reaper);
278         signal(SIGTERM, (SIG_PF) reaper);
279         signal(SIGINT, (SIG_PF) reaper);
280         signal(SIGHUP, (SIG_PF) reaper);
281
282         ypupdated_svc_run();
283         _msgout("svc_run returned");
284         exit(1);
285         /* NOTREACHED */
286 }