main.c revision 24428
1253883Ssjg/*
2246149Ssjg
3246149SsjgThis code is not copyright, and is placed in the public domain. Feel free to
4246149Ssjguse and modify. Please send modifications and/or suggestions + bug fixes to
5246149Ssjg
6246149Ssjg        Klas Heggemann <klas@nada.kth.se>
7246149Ssjg
8246149Ssjg
9246149Ssjg	$Id: main.c,v 1.5 1997/02/22 16:04:24 peter Exp $
10246149Ssjg
11246149Ssjg*/
12246149Ssjg
13246149Ssjg#include <stdio.h>
14246149Ssjg#include <string.h>
15246149Ssjg#include <syslog.h>
16246149Ssjg#include <sys/ioctl.h>
17246149Ssjg#include <rpc/rpc.h>
18246149Ssjg#include "bootparam_prot.h"
19246149Ssjg#include <sys/types.h>
20246149Ssjg#include <sys/stat.h>
21246149Ssjg#include <sys/socket.h>
22246149Ssjg#include <netdb.h>
23246149Ssjg#include <ctype.h>
24246149Ssjg
25246149Ssjgint debug = 0;
26246149Ssjgint dolog = 0;
27246149Ssjgunsigned long route_addr = -1, inet_addr();
28246149Ssjgstruct sockaddr_in my_addr;
29246149Ssjgchar *progname;
30246149Ssjgchar *bootpfile = "/etc/bootparams";
31246149Ssjg
32246149Ssjgextern  void bootparamprog_1();
33246149Ssjg
34246149Ssjgextern char *optarg;
35246149Ssjgextern int optind;
36246149Ssjg
37246149Ssjgmain(argc, argv)
38246149Ssjgint argc;
39246149Ssjgchar **argv;
40246149Ssjg{
41246149Ssjg	SVCXPRT *transp;
42246149Ssjg	int i;
43246149Ssjg	struct hostent *he;
44246149Ssjg	struct stat buf;
45246149Ssjg	char *optstring;
46246149Ssjg	char c;
47246149Ssjg
48246149Ssjg	progname = rindex(argv[0],'/');
49246149Ssjg	if ( progname ) progname++;
50246149Ssjg	else progname = argv[0];
51246149Ssjg
52246149Ssjg	while ((c = getopt(argc, argv,"dsr:f:")) != -1)
53246149Ssjg	  switch (c) {
54246149Ssjg	  case 'd':
55246149Ssjg	    debug = 1;
56246149Ssjg	    break;
57246149Ssjg	  case 'r':
58246149Ssjg	      if ( isdigit( *optarg)) {
59246149Ssjg		route_addr = inet_addr(optarg);
60246149Ssjg		break;
61246149Ssjg	      } else {
62246149Ssjg		he = gethostbyname(optarg);
63246149Ssjg		if (he) {
64246149Ssjg		   bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
65246149Ssjg		   break;
66246149Ssjg		 } else {
67246149Ssjg		   fprintf(stderr,"%s: No such host %s\n", progname, argv[i]);
68246149Ssjg		   exit(1);
69246149Ssjg		 }
70246149Ssjg	      }
71246149Ssjg	  case 'f':
72246149Ssjg	    bootpfile = optarg;
73246149Ssjg	    break;
74246149Ssjg	  case 's':
75246149Ssjg	    dolog = 1;
76246149Ssjg#ifndef LOG_DAEMON
77246149Ssjg	    openlog(progname, 0 , 0);
78246149Ssjg#else
79246149Ssjg	    openlog(progname, 0 , LOG_DAEMON);
80246149Ssjg	    setlogmask(LOG_UPTO(LOG_NOTICE));
81246149Ssjg#endif
82246149Ssjg	    break;
83246149Ssjg	  default:
84246149Ssjg	    fprintf(stderr,
85246149Ssjg		    "Usage: %s [-d ] [ -s ] [ -r router ] [ -f bootparmsfile ]\n", progname);
86246149Ssjg	    exit(1);
87246149Ssjg	  }
88246149Ssjg
89246149Ssjg	if ( stat(bootpfile, &buf ) ) {
90246149Ssjg	  fprintf(stderr,"%s: ", progname);
91246149Ssjg	  perror(bootpfile);
92246149Ssjg	  exit(1);
93253883Ssjg	}
94246149Ssjg
95246149Ssjg
96246149Ssjg	if (route_addr == -1) {
97246149Ssjg	  get_myaddress(&my_addr);
98246149Ssjg	  bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
99246149Ssjg	}
100246149Ssjg
101246149Ssjg	if (!debug) {
102246149Ssjg	  if (daemon(0,0)) {
103246149Ssjg	    perror("bootparamd: fork");
104246149Ssjg	    exit(1);
105246149Ssjg	  }
106246149Ssjg	}
107246149Ssjg
108246149Ssjg
109246149Ssjg	(void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
110246149Ssjg
111246149Ssjg	transp = svcudp_create(RPC_ANYSOCK);
112246149Ssjg	if (transp == NULL) {
113246149Ssjg		(void)fprintf(stderr, "cannot create udp service.\n");
114246149Ssjg		exit(1);
115246149Ssjg	}
116246149Ssjg	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP)) {
117246149Ssjg		(void)fprintf(stderr, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp).\n");
118246149Ssjg		exit(1);
119246149Ssjg	}
120246149Ssjg
121246149Ssjg	svc_run();
122246149Ssjg	(void)fprintf(stderr, "svc_run returned\n");
123246149Ssjg	exit(1);
124246149Ssjg}
125246149Ssjg
126246149Ssjg
127246149Ssjg