main.c revision 24428
1/*
2
3This code is not copyright, and is placed in the public domain. Feel free to
4use and modify. Please send modifications and/or suggestions + bug fixes to
5
6        Klas Heggemann <klas@nada.kth.se>
7
8
9	$Id: main.c,v 1.5 1997/02/22 16:04:24 peter Exp $
10
11*/
12
13#include <stdio.h>
14#include <string.h>
15#include <syslog.h>
16#include <sys/ioctl.h>
17#include <rpc/rpc.h>
18#include "bootparam_prot.h"
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <sys/socket.h>
22#include <netdb.h>
23#include <ctype.h>
24
25int debug = 0;
26int dolog = 0;
27unsigned long route_addr = -1, inet_addr();
28struct sockaddr_in my_addr;
29char *progname;
30char *bootpfile = "/etc/bootparams";
31
32extern  void bootparamprog_1();
33
34extern char *optarg;
35extern int optind;
36
37main(argc, argv)
38int argc;
39char **argv;
40{
41	SVCXPRT *transp;
42	int i;
43	struct hostent *he;
44	struct stat buf;
45	char *optstring;
46	char c;
47
48	progname = rindex(argv[0],'/');
49	if ( progname ) progname++;
50	else progname = argv[0];
51
52	while ((c = getopt(argc, argv,"dsr:f:")) != -1)
53	  switch (c) {
54	  case 'd':
55	    debug = 1;
56	    break;
57	  case 'r':
58	      if ( isdigit( *optarg)) {
59		route_addr = inet_addr(optarg);
60		break;
61	      } else {
62		he = gethostbyname(optarg);
63		if (he) {
64		   bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
65		   break;
66		 } else {
67		   fprintf(stderr,"%s: No such host %s\n", progname, argv[i]);
68		   exit(1);
69		 }
70	      }
71	  case 'f':
72	    bootpfile = optarg;
73	    break;
74	  case 's':
75	    dolog = 1;
76#ifndef LOG_DAEMON
77	    openlog(progname, 0 , 0);
78#else
79	    openlog(progname, 0 , LOG_DAEMON);
80	    setlogmask(LOG_UPTO(LOG_NOTICE));
81#endif
82	    break;
83	  default:
84	    fprintf(stderr,
85		    "Usage: %s [-d ] [ -s ] [ -r router ] [ -f bootparmsfile ]\n", progname);
86	    exit(1);
87	  }
88
89	if ( stat(bootpfile, &buf ) ) {
90	  fprintf(stderr,"%s: ", progname);
91	  perror(bootpfile);
92	  exit(1);
93	}
94
95
96	if (route_addr == -1) {
97	  get_myaddress(&my_addr);
98	  bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
99	}
100
101	if (!debug) {
102	  if (daemon(0,0)) {
103	    perror("bootparamd: fork");
104	    exit(1);
105	  }
106	}
107
108
109	(void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
110
111	transp = svcudp_create(RPC_ANYSOCK);
112	if (transp == NULL) {
113		(void)fprintf(stderr, "cannot create udp service.\n");
114		exit(1);
115	}
116	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP)) {
117		(void)fprintf(stderr, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp).\n");
118		exit(1);
119	}
120
121	svc_run();
122	(void)fprintf(stderr, "svc_run returned\n");
123	exit(1);
124}
125
126
127