xref: /freebsd/usr.sbin/rpc.statd/statd.c (revision 7573cb47cf4b1f33b6794f82a8a5e0bcbdcc1ada)
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  */
33 
34 
35 /* main() function for status monitor daemon.  Some of the code in this	*/
36 /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x	*/
37 /* The actual program logic is in the file procs.c			*/
38 
39 #include <stdio.h>
40 #include <rpc/rpc.h>
41 #include <syslog.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <signal.h>
45 #include "statd.h"
46 
47 #ifndef lint
48 static char rcsid[] = "$id: $";
49 #endif /* not lint */
50 
51 int debug = 0;		/* Controls syslog() calls for debug messages	*/
52 
53 static void sm_prog_1();
54 static void handle_sigchld();
55 
56 main(int argc, char **argv)
57 {
58   SVCXPRT *transp;
59   struct sigaction sa;
60 
61   if (argc > 1)
62   {
63     if (strcmp(argv[1], "-d"))
64     {
65       fprintf(stderr, "Usage: rpc.statd [-d]\n");
66       exit(1);
67     }
68     debug = 1;
69   }
70 
71   (void)pmap_unset(SM_PROG, SM_VERS);
72 
73   transp = svcudp_create(RPC_ANYSOCK);
74   if (transp == NULL)
75   {
76     fprintf(stderr, "cannot create udp service.\n");
77     exit(1);
78   }
79   if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP))
80   {
81     fprintf(stderr, "unable to register (SM_PROG, SM_VERS, udp).\n");
82     exit(1);
83   }
84 
85   transp = svctcp_create(RPC_ANYSOCK, 0, 0);
86   if (transp == NULL)
87   {
88     fprintf(stderr, "cannot create tcp service.\n");
89     exit(1);
90   }
91   if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) {
92     fprintf(stderr, "unable to register (SM_PROG, SM_VERS, tcp).\n");
93     exit(1);
94   }
95   init_file("/var/db/statd.status");
96 
97   /* Note that it is NOT sensible to run this program from inetd - the 	*/
98   /* protocol assumes that it will run immediately at boot time.	*/
99   daemon(0, 0);
100   openlog("rpc.statd", 0, LOG_DAEMON);
101   if (debug) syslog(LOG_INFO, "Starting - debug enabled");
102   else syslog(LOG_INFO, "Starting");
103 
104   /* Install signal handler to collect exit status of child processes	*/
105   sa.sa_handler = handle_sigchld;
106   sigemptyset(&sa.sa_mask);
107   sigaddset(&sa.sa_mask, SIGCHLD);
108   sa.sa_flags = SA_RESTART;
109   sigaction(SIGCHLD, &sa, NULL);
110 
111   /* Initialisation now complete - start operating			*/
112   notify_hosts();	/* Forks a process (if necessary) to do the	*/
113 			/* SM_NOTIFY calls, which may be slow.		*/
114 
115   svc_run();	/* Should never return					*/
116   exit(1);
117 }
118 
119 
120 /* handle_sigchld ---------------------------------------------------------- */
121 /*
122    Purpose:	Catch SIGCHLD and collect process status
123    Retruns:	Nothing.
124    Notes:	No special action required, other than to collect the
125 		process status and hence allow the child to die:
126 		we only use child processes for asynchronous transmission
127 		of SM_NOTIFY to other systems, so it is normal for the
128 		children to exit when they have done their work.
129 */
130 
131 static void handle_sigchld(int sig, int code, struct sigcontext *scp)
132 {
133   int pid, status;
134   pid = wait4(-1, &status, WNOHANG, (struct rusage*)0);
135   if (!pid) syslog(LOG_ERR, "Phantom SIGCHLD??");
136   else if (status == 0)
137   {
138     if (debug) syslog(LOG_DEBUG, "Child %d exited OK", pid);
139   }
140   else syslog(LOG_ERR, "Child %d failed with status %d", pid,
141     WEXITSTATUS(status));
142 }
143 
144 
145 /* sm_prog1 ---------------------------------------------------------------- */
146 /*
147    Purpose:	Handle one RPC request
148    Returns:	Nothing
149    Notes:	Called from RPC libraray on receipt of a request.
150 		Code for this function was auto-generated by rpcgen.
151 */
152 
153 static void
154 sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp)
155 {
156   union
157   {
158     struct sm_name sm_stat_1_arg;
159     struct mon sm_mon_1_arg;
160     struct mon_id sm_unmon_1_arg;
161     struct my_id sm_unmon_all_1_arg;
162     struct stat_chge sm_notify_1_arg;
163   } argument;
164   char *result;
165   bool_t (*xdr_argument)(), (*xdr_result)();
166   char *(*local)();
167 
168   switch (rqstp->rq_proc)
169   {
170     case NULLPROC:
171       (void)svc_sendreply(transp, xdr_void, (char *)NULL);
172       return;
173 
174     case SM_STAT:
175       xdr_argument = xdr_sm_name;
176       xdr_result = xdr_sm_stat_res;
177       local = (char *(*)()) sm_stat_1;
178       break;
179 
180     case SM_MON:
181       xdr_argument = xdr_mon;
182       xdr_result = xdr_sm_stat_res;
183       local = (char *(*)()) sm_mon_1;
184       break;
185 
186     case SM_UNMON:
187       xdr_argument = xdr_mon_id;
188       xdr_result = xdr_sm_stat;
189       local = (char *(*)()) sm_unmon_1;
190       break;
191 
192     case SM_UNMON_ALL:
193       xdr_argument = xdr_my_id;
194       xdr_result = xdr_sm_stat;
195       local = (char *(*)()) sm_unmon_all_1;
196       break;
197 
198     case SM_SIMU_CRASH:
199       xdr_argument = xdr_void;
200       xdr_result = xdr_void;
201       local = (char *(*)()) sm_simu_crash_1;
202       break;
203 
204     case SM_NOTIFY:
205       xdr_argument = xdr_stat_chge;
206       xdr_result = xdr_void;
207       local = (char *(*)()) sm_notify_1;
208       break;
209 
210     default:
211       svcerr_noproc(transp);
212       return;
213   }
214 
215   bzero((char *)&argument, sizeof(argument));
216   if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument))
217   {
218     svcerr_decode(transp);
219     return;
220   }
221   result = (*local)(&argument, rqstp);
222   if (result != NULL && !svc_sendreply(transp, xdr_result, result))
223   {
224     svcerr_systemerr(transp);
225   }
226   if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument))
227   {
228     syslog(LOG_ERR, "unable to free arguments");
229     exit(1);
230   }
231 }
232 
233