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