main.c revision 29102
162587Sitojun/*
278064Sume
362587SitojunThis code is not copyright, and is placed in the public domain. Feel free to
4139826Simpuse and modify. Please send modifications and/or suggestions + bug fixes to
553541Sshin
653541Sshin        Klas Heggemann <klas@nada.kth.se>
753541Sshin
853541Sshin*/
953541Sshin
1053541Sshin#ifndef lint
1153541Sshinstatic const char rcsid[] =
1253541Sshin	"$Id$";
1353541Sshin#endif /* not lint */
1453541Sshin
1553541Sshin#include <ctype.h>
1653541Sshin#include <err.h>
1753541Sshin#include <netdb.h>
1853541Sshin#include <stdio.h>
1953541Sshin#include <stdlib.h>
2053541Sshin#include <string.h>
2153541Sshin#include <syslog.h>
2253541Sshin#include <unistd.h>
2353541Sshin#include <rpc/rpc.h>
2453541Sshin#include <rpc/pmap_clnt.h>
2553541Sshin#include <sys/ioctl.h>
2653541Sshin#include <sys/socket.h>
2753541Sshin#include <sys/stat.h>
2853541Sshin#include <sys/types.h>
2953541Sshin#include "bootparam_prot.h"
3053541Sshin
3153541Sshinint debug = 0;
3253541Sshinint dolog = 0;
33139826Simpunsigned long route_addr = -1, inet_addr();
3453541Sshinstruct sockaddr_in my_addr;
3553541Sshinchar *bootpfile = "/etc/bootparams";
3653541Sshin
3753541Sshinextern  void bootparamprog_1();
3853541Sshinstatic void usage __P((void));
3953541Sshin
4053541Sshinint
4153541Sshinmain(argc, argv)
4253541Sshinint argc;
4353541Sshinchar **argv;
4453541Sshin{
4553541Sshin	SVCXPRT *transp;
4653541Sshin	int i;
4753541Sshin	struct hostent *he;
4853541Sshin	struct stat buf;
4953541Sshin	char c;
5053541Sshin
5153541Sshin	while ((c = getopt(argc, argv,"dsr:f:")) != -1)
5253541Sshin	  switch (c) {
5353541Sshin	  case 'd':
5453541Sshin	    debug = 1;
5553541Sshin	    break;
5653541Sshin	  case 'r':
5753541Sshin	      if ( isdigit( *optarg)) {
5853541Sshin		route_addr = inet_addr(optarg);
5953541Sshin		break;
6053541Sshin	      } else {
6153541Sshin		he = gethostbyname(optarg);
6253541Sshin		if (he) {
6353541Sshin		   bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
6453541Sshin		   break;
6553541Sshin		} else {
6653541Sshin		   errx(1, "no such host %s", argv[i]);
6753541Sshin		}
6853541Sshin	      }
6953541Sshin	  case 'f':
7053541Sshin	    bootpfile = optarg;
7153541Sshin	    break;
7262587Sitojun	  case 's':
7362587Sitojun	    dolog = 1;
7462587Sitojun#ifndef LOG_DAEMON
7562587Sitojun	    openlog("bootparamd", 0 , 0);
7662587Sitojun#else
7762587Sitojun	    openlog("bootparamd", 0 , LOG_DAEMON);
7862587Sitojun	    setlogmask(LOG_UPTO(LOG_NOTICE));
7962587Sitojun#endif
8062587Sitojun	    break;
8162587Sitojun	  default:
8262587Sitojun	    usage();
8362587Sitojun	  }
8462587Sitojun
8562587Sitojun	if ( stat(bootpfile, &buf ) )
8662587Sitojun	  err(1, "%s", bootpfile);
8762587Sitojun
88121345Sume	if (route_addr == -1) {
8953541Sshin	  get_myaddress(&my_addr);
9053541Sshin	  bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
9153541Sshin	}
9262587Sitojun
9362587Sitojun	if (!debug) {
9462587Sitojun	  if (daemon(0,0))
9562587Sitojun	    err(1, "fork");
9653541Sshin	}
9762587Sitojun
9862587Sitojun
9962587Sitojun	(void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
10062587Sitojun
10162587Sitojun	transp = svcudp_create(RPC_ANYSOCK);
10262587Sitojun	if (transp == NULL)
10362587Sitojun		errx(1, "cannot create udp service");
10453541Sshin	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP))
10553541Sshin		errx(1, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp)");
10653541Sshin
10753541Sshin	svc_run();
10853541Sshin	errx(1, "svc_run returned");
10953541Sshin}
11053541Sshin
11153541Sshinstatic void
11260938Sjakeusage()
11353541Sshin{
11453541Sshin	fprintf(stderr,
11553541Sshin		"usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
11653541Sshin	exit(1);
11753541Sshin}
11853541Sshin