Home | History | Annotate | Line # | Download | only in rpc.rstatd
rstatd.c revision 1.10
      1 /*	$NetBSD: rstatd.c,v 1.10 2000/06/02 23:20:19 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, John Brezak
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: rstatd.c,v 1.10 2000/06/02 23:20:19 fvdl Exp $");
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/socket.h>
     43 
     44 #include <stdio.h>
     45 #include <rpc/rpc.h>
     46 #include <signal.h>
     47 #include <syslog.h>
     48 #include <string.h>
     49 #include <stdlib.h>
     50 #include <rpcsvc/rstat.h>
     51 
     52 extern void rstat_service __P((struct svc_req *, SVCXPRT *));
     53 void cleanup __P((int));
     54 int main __P((int, char *[]));
     55 
     56 int from_inetd = 1;     /* started from inetd ? */
     57 int closedown = 20;	/* how long to wait before going dormant */
     58 
     59 void
     60 cleanup(dummy)
     61 	int dummy;
     62 {
     63         (void) pmap_unset(RSTATPROG, RSTATVERS_TIME);
     64         (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
     65         (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG);
     66         exit(0);
     67 }
     68 
     69 int
     70 main(argc, argv)
     71         int argc;
     72         char *argv[];
     73 {
     74 	SVCXPRT *transp;
     75 	struct sockaddr_in from;
     76 	int fromlen;
     77 
     78         if (argc == 2)
     79                 closedown = atoi(argv[1]);
     80         if (closedown <= 0)
     81                 closedown = 20;
     82 
     83         /*
     84          * See if inetd started us
     85          */
     86 	fromlen = sizeof(from);
     87         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
     88                 from_inetd = 0;
     89 
     90         if (!from_inetd) {
     91                 /* daemon(0, 0); */
     92 
     93                 (void)pmap_unset(RSTATPROG, RSTATVERS_TIME);
     94                 (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
     95                 (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG);
     96 
     97 		(void) signal(SIGINT, cleanup);
     98 		(void) signal(SIGTERM, cleanup);
     99 		(void) signal(SIGHUP, cleanup);
    100         }
    101 
    102         openlog("rpc.rstatd", LOG_PID, LOG_DAEMON);
    103 
    104 	if (from_inetd) {
    105 		transp = svc_dg_create(0, 0, 0);
    106 		if (transp == NULL) {
    107 			syslog(LOG_ERR, "cannot create udp service.");
    108 			exit(1);
    109 		}
    110 
    111 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstat_service,
    112 		    NULL)) {
    113 			syslog(LOG_ERR, "unable to register (RSTATPROG,"
    114 			    "RSTATVERS_TIME)");
    115 			exit(1);
    116 		}
    117 
    118 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service,
    119 		    NULL)) {
    120 			syslog(LOG_ERR, "unable to register (RSTATPROG,"
    121 			    "RSTATVERS_TIME)");
    122 			exit(1);
    123 		}
    124 
    125 		if (!svc_reg(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service,
    126 		    NULL)) {
    127 			syslog(LOG_ERR, "unable to register (RSTATPROG,"
    128 			    "RSTATVERS_ORIG)");
    129 			exit(1);
    130 		}
    131 	} else {
    132 		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_TIME,
    133 		    "udp")) {
    134 			syslog(LOG_ERR,
    135 			    "unable to create (RSTATPROG, RSTATVERS_TIME).");
    136 			exit(1);
    137 		}
    138 		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_SWTCH,
    139 		    "udp")) {
    140 			syslog(LOG_ERR,
    141 			    "unable to create (RSTATPROG, RSTATVERS_SWTCH).");
    142 			exit(1);
    143 		}
    144 		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_ORIG,
    145 		    "udp")) {
    146 			syslog(LOG_ERR,
    147 			    "unable to register (RSTATPROG, RSTATVERS_ORIG).");
    148 			exit(1);
    149 		}
    150 	}
    151 
    152         svc_run();
    153 	syslog(LOG_ERR, "svc_run returned");
    154 	exit(1);
    155 }
    156