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