Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / bootparamd / bootparamd / main.c
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 #ifndef lint
11 static const char rcsid[] =
12   "$FreeBSD: src/usr.sbin/bootparamd/bootparamd/main.c,v 1.9 1999/08/28 01:15:39 peter Exp $";
13 #endif /* not lint */
14
15 #include <ctype.h>
16 #include <err.h>
17 #include <netdb.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <syslog.h>
22 #include <unistd.h>
23 #include <rpc/rpc.h>
24 #include <rpc/pmap_clnt.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include "bootparam_prot.h"
32
33 int debug = 0;
34 int dolog = 0;
35 unsigned long route_addr = -1;
36 struct sockaddr_in my_addr;
37 char *bootpfile = "/etc/bootparams";
38
39 extern  void bootparamprog_1();
40 static void usage __P((void));
41
42 int
43 main(argc, argv)
44 int argc;
45 char **argv;
46 {
47         SVCXPRT *transp;
48         int i;
49         struct hostent *he;
50         struct stat buf;
51         char c;
52
53         while ((c = getopt(argc, argv,"dsr:f:")) != -1)
54           switch (c) {
55           case 'd':
56             debug = 1;
57             break;
58           case 'r':
59               if ( isdigit( *optarg)) {
60                 route_addr = inet_addr(optarg);
61                 break;
62               } else {
63                 he = gethostbyname(optarg);
64                 if (he) {
65                    bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
66                    break;
67                 } else {
68                    errx(1, "no such host %s", argv[i]);
69                 }
70               }
71           case 'f':
72             bootpfile = optarg;
73             break;
74           case 's':
75             dolog = 1;
76 #ifndef LOG_DAEMON
77             openlog("bootparamd", 0 , 0);
78 #else
79             openlog("bootparamd", 0 , LOG_DAEMON);
80             setlogmask(LOG_UPTO(LOG_NOTICE));
81 #endif
82             break;
83           default:
84             usage();
85           }
86
87         if ( stat(bootpfile, &buf ) )
88           err(1, "%s", bootpfile);
89
90         if (route_addr == -1) {
91           get_myaddress(&my_addr);
92           bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
93         }
94
95         if (!debug) {
96           if (daemon(0,0))
97             err(1, "fork");
98         }
99
100
101         (void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
102
103         transp = svcudp_create(RPC_ANYSOCK);
104         if (transp == NULL)
105                 errx(1, "cannot create udp service");
106         if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP))
107                 errx(1, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp)");
108
109         svc_run();
110         errx(1, "svc_run returned");
111 }
112
113 static void
114 usage()
115 {
116         fprintf(stderr,
117                 "usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
118         exit(1);
119 }