* K&R function cleanup
[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 /*
11  * $FreeBSD: src/usr.sbin/bootparamd/bootparamd/main.c,v 1.9 1999/08/28 01:15:39 peter Exp $
12  * $DragonFly: src/usr.sbin/bootparamd/bootparamd/main.c,v 1.4 2003/11/15 23:33:35 eirikn 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 unsigned long 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         int i;
46         struct hostent *he;
47         struct stat buf;
48         char c;
49
50         while ((c = getopt(argc, argv,"dsr:f:")) != -1)
51           switch (c) {
52           case 'd':
53             debug = 1;
54             break;
55           case 'r':
56               if ( isdigit( *optarg)) {
57                 route_addr = inet_addr(optarg);
58                 break;
59               } else {
60                 he = gethostbyname(optarg);
61                 if (he) {
62                    bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr));
63                    break;
64                 } else {
65                    errx(1, "no such host %s", argv[i]);
66                 }
67               }
68           case 'f':
69             bootpfile = optarg;
70             break;
71           case 's':
72             dolog = 1;
73 #ifndef LOG_DAEMON
74             openlog("bootparamd", 0 , 0);
75 #else
76             openlog("bootparamd", 0 , LOG_DAEMON);
77             setlogmask(LOG_UPTO(LOG_NOTICE));
78 #endif
79             break;
80           default:
81             usage();
82           }
83
84         if ( stat(bootpfile, &buf ) )
85           err(1, "%s", bootpfile);
86
87         if (route_addr == -1) {
88           get_myaddress(&my_addr);
89           bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
90         }
91
92         if (!debug) {
93           if (daemon(0,0))
94             err(1, "fork");
95         }
96
97
98         (void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
99
100         transp = svcudp_create(RPC_ANYSOCK);
101         if (transp == NULL)
102                 errx(1, "cannot create udp service");
103         if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP))
104                 errx(1, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp)");
105
106         svc_run();
107         errx(1, "svc_run returned");
108 }
109
110 static void
111 usage(void)
112 {
113         fprintf(stderr,
114                 "usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
115         exit(1);
116 }