Remove __P macros from src/usr.bin and src/usr.sbin.
[dragonfly.git] / usr.sbin / rpc.statd / statd.c
1 /*
2  * Copyright (c) 1995
3  *      A.R. Gordon (andrew.gordon@net-tel.co.uk).  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 for the FreeBSD project
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 ANDREW GORDON 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 THE AUTHOR 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.statd/statd.c,v 1.6 1999/10/05 14:40:38 marcel Exp $
33  * $DragonFly: src/usr.sbin/rpc.statd/statd.c,v 1.3 2003/11/03 19:31:42 eirikn Exp $
34  */
35
36 /* main() function for status monitor daemon.  Some of the code in this */
37 /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x  */
38 /* The actual program logic is in the file procs.c                      */
39
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <rpc/rpc.h>
44 #include <rpc/pmap_clnt.h>
45 #include <syslog.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <signal.h>
49 #include "statd.h"
50
51 int debug = 0;          /* Controls syslog() calls for debug messages   */
52
53 extern void sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
54 static void handle_sigchld();
55 static void usage(void);
56
57 int
58 main(int argc, char **argv)
59 {
60   SVCXPRT *transp;
61   struct sigaction sa;
62
63   if (argc > 1)
64   {
65     if (strcmp(argv[1], "-d"))
66         usage();
67     debug = 1;
68   }
69
70   (void)pmap_unset(SM_PROG, SM_VERS);
71
72   transp = svcudp_create(RPC_ANYSOCK);
73   if (transp == NULL)
74     errx(1, "cannot create udp service");
75   if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP))
76     errx(1, "unable to register (SM_PROG, SM_VERS, udp)");
77
78   transp = svctcp_create(RPC_ANYSOCK, 0, 0);
79   if (transp == NULL)
80     errx(1, "cannot create tcp service");
81   if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP))
82     errx(1, "unable to register (SM_PROG, SM_VERS, tcp)");
83   init_file("/var/db/statd.status");
84
85   /* Note that it is NOT sensible to run this program from inetd - the  */
86   /* protocol assumes that it will run immediately at boot time.        */
87   daemon(0, 0);
88   openlog("rpc.statd", 0, LOG_DAEMON);
89   if (debug) syslog(LOG_INFO, "Starting - debug enabled");
90   else syslog(LOG_INFO, "Starting");
91
92   /* Install signal handler to collect exit status of child processes   */
93   sa.sa_handler = handle_sigchld;
94   sigemptyset(&sa.sa_mask);
95   sigaddset(&sa.sa_mask, SIGCHLD);
96   sa.sa_flags = SA_RESTART;
97   sigaction(SIGCHLD, &sa, NULL);
98
99   /* Initialisation now complete - start operating                      */
100   notify_hosts();       /* Forks a process (if necessary) to do the     */
101                         /* SM_NOTIFY calls, which may be slow.          */
102
103   svc_run();    /* Should never return                                  */
104   exit(1);
105 }
106
107 static void
108 usage()
109 {
110       fprintf(stderr, "usage: rpc.statd [-d]\n");
111       exit(1);
112 }
113
114 /* handle_sigchld ---------------------------------------------------------- */
115 /*
116    Purpose:     Catch SIGCHLD and collect process status
117    Retruns:     Nothing.
118    Notes:       No special action required, other than to collect the
119                 process status and hence allow the child to die:
120                 we only use child processes for asynchronous transmission
121                 of SM_NOTIFY to other systems, so it is normal for the
122                 children to exit when they have done their work.
123 */
124
125 static void handle_sigchld(int sig, int code, struct sigcontext *scp)
126 {
127   int pid, status;
128   pid = wait4(-1, &status, WNOHANG, (struct rusage*)0);
129   if (!pid) syslog(LOG_ERR, "Phantom SIGCHLD??");
130   else if (status == 0)
131   {
132     if (debug) syslog(LOG_DEBUG, "Child %d exited OK", pid);
133   }
134   else syslog(LOG_ERR, "Child %d failed with status %d", pid,
135     WEXITSTATUS(status));
136 }
137