2296a43f57457dc3d28e2374ced503fb0800e748
[dragonfly.git] / usr.sbin / bootparamd / bootparamd / main.c
blob2296a43f57457dc3d28e2374ced503fb0800e748
1 /*
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
6 Klas Heggemann <klas@nada.kth.se>
8 */
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.5 2004/12/18 22:48:02 swildner Exp $
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"
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";
38 extern void bootparamprog_1();
39 static void usage(void);
41 int
42 main(int argc, char **argv)
44 SVCXPRT *transp;
45 int i;
46 struct hostent *he;
47 struct stat buf;
48 char c;
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]);
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();
84 if ( stat(bootpfile, &buf ) )
85 err(1, "%s", bootpfile);
87 if (route_addr == -1) {
88 get_myaddress(&my_addr);
89 bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr));
92 if (!debug) {
93 if (daemon(0,0))
94 err(1, "fork");
98 pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
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)");
106 svc_run();
107 errx(1, "svc_run returned");
110 static void
111 usage(void)
113 fprintf(stderr,
114 "usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n");
115 exit(1);