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