main.c revision 50479
16751Swpaul/* 26751Swpaul 38857SrgrimesThis code is not copyright, and is placed in the public domain. Feel free to 46751Swpauluse and modify. Please send modifications and/or suggestions + bug fixes to 56751Swpaul 66751Swpaul Klas Heggemann <klas@nada.kth.se> 76751Swpaul 829102Scharnier*/ 96751Swpaul 1029102Scharnier#ifndef lint 1129102Scharnierstatic const char rcsid[] = 1250479Speter "$FreeBSD: head/usr.sbin/bootparamd/bootparamd/main.c 50479 1999-08-28 01:35:59Z peter $"; 1329102Scharnier#endif /* not lint */ 146751Swpaul 1529102Scharnier#include <ctype.h> 1629102Scharnier#include <err.h> 1729102Scharnier#include <netdb.h> 189718Swpaul#include <stdio.h> 1929102Scharnier#include <stdlib.h> 209718Swpaul#include <string.h> 216751Swpaul#include <syslog.h> 2229102Scharnier#include <unistd.h> 2329102Scharnier#include <rpc/rpc.h> 2429102Scharnier#include <rpc/pmap_clnt.h> 256751Swpaul#include <sys/ioctl.h> 2629102Scharnier#include <sys/socket.h> 2729102Scharnier#include <sys/stat.h> 2829102Scharnier#include <sys/types.h> 2936917Speter#include <netinet/in.h> 3036917Speter#include <arpa/inet.h> 316751Swpaul#include "bootparam_prot.h" 326751Swpaul 336751Swpaulint debug = 0; 346751Swpaulint dolog = 0; 3536917Speterunsigned long route_addr = -1; 366751Swpaulstruct sockaddr_in my_addr; 376751Swpaulchar *bootpfile = "/etc/bootparams"; 386751Swpaul 396751Swpaulextern void bootparamprog_1(); 4029102Scharnierstatic void usage __P((void)); 416751Swpaul 4229102Scharnierint 436751Swpaulmain(argc, argv) 446751Swpaulint argc; 456751Swpaulchar **argv; 466751Swpaul{ 476751Swpaul SVCXPRT *transp; 489718Swpaul int i; 496751Swpaul struct hostent *he; 506751Swpaul struct stat buf; 516751Swpaul char c; 526751Swpaul 5324428Simp while ((c = getopt(argc, argv,"dsr:f:")) != -1) 546751Swpaul switch (c) { 556751Swpaul case 'd': 566751Swpaul debug = 1; 576751Swpaul break; 586751Swpaul case 'r': 596751Swpaul if ( isdigit( *optarg)) { 606751Swpaul route_addr = inet_addr(optarg); 616751Swpaul break; 626751Swpaul } else { 636751Swpaul he = gethostbyname(optarg); 646751Swpaul if (he) { 656751Swpaul bcopy(he->h_addr, (char *)&route_addr, sizeof(route_addr)); 666751Swpaul break; 6729102Scharnier } else { 6829102Scharnier errx(1, "no such host %s", argv[i]); 6929102Scharnier } 706751Swpaul } 716751Swpaul case 'f': 726751Swpaul bootpfile = optarg; 736751Swpaul break; 746751Swpaul case 's': 756751Swpaul dolog = 1; 768857Srgrimes#ifndef LOG_DAEMON 7729102Scharnier openlog("bootparamd", 0 , 0); 786751Swpaul#else 7929102Scharnier openlog("bootparamd", 0 , LOG_DAEMON); 806751Swpaul setlogmask(LOG_UPTO(LOG_NOTICE)); 816751Swpaul#endif 826751Swpaul break; 836751Swpaul default: 8429102Scharnier usage(); 856751Swpaul } 866751Swpaul 8729102Scharnier if ( stat(bootpfile, &buf ) ) 8829102Scharnier err(1, "%s", bootpfile); 896751Swpaul 906751Swpaul if (route_addr == -1) { 916751Swpaul get_myaddress(&my_addr); 926751Swpaul bcopy(&my_addr.sin_addr.s_addr, &route_addr, sizeof (route_addr)); 936751Swpaul } 946751Swpaul 956751Swpaul if (!debug) { 9629102Scharnier if (daemon(0,0)) 9729102Scharnier err(1, "fork"); 986751Swpaul } 996751Swpaul 1008857Srgrimes 1016751Swpaul (void)pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS); 1026751Swpaul 1036751Swpaul transp = svcudp_create(RPC_ANYSOCK); 10429102Scharnier if (transp == NULL) 10529102Scharnier errx(1, "cannot create udp service"); 10629102Scharnier if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, IPPROTO_UDP)) 10729102Scharnier errx(1, "unable to register (BOOTPARAMPROG, BOOTPARAMVERS, udp)"); 1086751Swpaul 1096751Swpaul svc_run(); 11029102Scharnier errx(1, "svc_run returned"); 11129102Scharnier} 11229102Scharnier 11329102Scharnierstatic void 11429102Scharnierusage() 11529102Scharnier{ 11629102Scharnier fprintf(stderr, 11729102Scharnier "usage: bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n"); 1186751Swpaul exit(1); 1196751Swpaul} 120