147485Spendry /* 247485Spendry * Copyright (c) 1989 Jan-Simon Pendry 347485Spendry * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 447485Spendry * Copyright (c) 1989 The Regents of the University of California. 547485Spendry * All rights reserved. 647485Spendry * 747485Spendry * This code is derived from software contributed to Berkeley by 847485Spendry * Jan-Simon Pendry at Imperial College, London. 947485Spendry * 1047530Spendry * %sccs.include.redist.c% 1147485Spendry * 12*49686Spendry * @(#)fsinfo.c 5.3 (Berkeley) 05/12/91 13*49686Spendry * 14*49686Spendry * $Id: fsinfo.c,v 5.2.1.3 91/05/07 22:19:01 jsp Alpha $ 15*49686Spendry * 1647485Spendry */ 1747485Spendry 1847485Spendry /* 1947485Spendry * fsinfo 2047485Spendry */ 2147485Spendry 2247485Spendry #include "../fsinfo/fsinfo.h" 2347485Spendry #include "fsi_gram.h" 2447485Spendry #include <pwd.h> 2547485Spendry 2647485Spendry qelem *list_of_hosts; 2747485Spendry qelem *list_of_automounts; 2847485Spendry dict *dict_of_volnames; 2947485Spendry dict *dict_of_hosts; 3047485Spendry char *autodir = "/a"; 3147485Spendry char hostname[MAXHOSTNAMELEN+1]; 3247485Spendry char *username; 3347485Spendry int file_io_errors; 3447485Spendry int parse_errors; 3547485Spendry int errors; 3647485Spendry int verbose; 3747485Spendry char idvbuf[1024]; 3847485Spendry 3947485Spendry char **g_argv; 4047485Spendry char *progname; 4147485Spendry 4247485Spendry /* 4347485Spendry * Output file prefixes 4447485Spendry */ 4547485Spendry char *exportfs_pref; 4647485Spendry char *fstab_pref; 4747485Spendry char *dumpset_pref; 4847485Spendry char *mount_pref; 4947485Spendry char *bootparams_pref; 5047485Spendry 5147485Spendry /* 5247485Spendry * Argument cracking... 5347485Spendry */ 5447485Spendry static void get_args(c, v) 5547485Spendry int c; 5647485Spendry char *v[]; 5747485Spendry { 5847485Spendry extern char *optarg; 5947485Spendry extern int optind; 6047485Spendry int ch; 6147485Spendry int usage = 0; 6247485Spendry char *iptr = idvbuf; 6347485Spendry 6447485Spendry /* 6547485Spendry * Determine program name 6647485Spendry */ 6747485Spendry if (v[0]) { 6847485Spendry progname = strrchr(v[0], '/'); 6947485Spendry if (progname && progname[1]) 7047485Spendry progname++; 7147485Spendry else 7247485Spendry progname = v[0]; 7347485Spendry } 7447485Spendry if (!progname) 7547485Spendry progname = "fsinfo"; 7647485Spendry 7747485Spendry while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != EOF) 7847485Spendry switch (ch) { 7947485Spendry case 'a': 8047485Spendry autodir = optarg; 8147485Spendry break; 8247485Spendry case 'b': 8347485Spendry if (bootparams_pref) 8447485Spendry fatal("-b option specified twice"); 8547485Spendry bootparams_pref = optarg; 8647485Spendry break; 8747485Spendry case 'd': 8847485Spendry if (dumpset_pref) 8947485Spendry fatal("-d option specified twice"); 9047485Spendry dumpset_pref = optarg; 9147485Spendry break; 9247485Spendry case 'h': 9347485Spendry strncpy(hostname, optarg, sizeof(hostname)-1); 9447485Spendry break; 9547485Spendry case 'e': 9647485Spendry if (exportfs_pref) 9747485Spendry fatal("-e option specified twice"); 9847485Spendry exportfs_pref = optarg; 9947485Spendry break; 10047485Spendry case 'f': 10147485Spendry if (fstab_pref) 10247485Spendry fatal("-f option specified twice"); 10347485Spendry fstab_pref = optarg; 10447485Spendry break; 10547485Spendry case 'm': 10647485Spendry if (mount_pref) 10747485Spendry fatal("-m option specified twice"); 10847485Spendry mount_pref = optarg; 10947485Spendry break; 11047485Spendry case 'q': 11147485Spendry verbose = -1; 11247485Spendry break; 11347485Spendry case 'v': 11447485Spendry verbose = 1; 11547485Spendry break; 11647485Spendry case 'I': case 'D': case 'U': 11747485Spendry sprintf(iptr, "-%c%s ", ch, optarg); 11847485Spendry iptr += strlen(iptr); 11947485Spendry break; 12047485Spendry default: 12147485Spendry usage++; 12247485Spendry break; 12347485Spendry } 12447485Spendry 12547485Spendry if (c != optind) { 12647485Spendry g_argv = v + optind - 1; 12747485Spendry if (yywrap()) 12847485Spendry fatal("Cannot read any input files"); 12947485Spendry } else { 13047485Spendry usage++; 13147485Spendry } 13247485Spendry 13347485Spendry if (usage) { 13447485Spendry fprintf(stderr, 13547485Spendry "\ 13647485Spendry Usage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\ 13747485Spendry \t[-e exports] [-f fstabs] [-m automounts]\n\ 13847485Spendry \t[-I dir] [-D|-U string[=string]] config ...\n", progname); 13947485Spendry exit(1); 14047485Spendry } 14147485Spendry 14247485Spendry 14347485Spendry if (g_argv[0]) 14447485Spendry log("g_argv[0] = %s", g_argv[0]); 14547485Spendry else 14647485Spendry log("g_argv[0] = (nil)"); 14747485Spendry } 14847485Spendry 14947485Spendry /* 15047485Spendry * Determine username of caller 15147485Spendry */ 15247485Spendry static char *find_username() 15347485Spendry { 15447485Spendry extern char *getlogin(); 15547485Spendry extern char *getenv(); 15647485Spendry char *u = getlogin(); 15747485Spendry if (!u) { 15847485Spendry struct passwd *pw = getpwuid(getuid()); 15947485Spendry if (pw) 16047485Spendry u = pw->pw_name; 16147485Spendry } 16247485Spendry if (!u) 16347485Spendry u = getenv("USER"); 16447485Spendry if (!u) 16547485Spendry u = getenv("LOGNAME"); 16647485Spendry if (!u) 16747485Spendry u = "root"; 16847485Spendry 16947485Spendry return strdup(u); 17047485Spendry } 17147485Spendry 17247485Spendry /* 17347485Spendry * MAIN 17447485Spendry */ 17547485Spendry main(argc, argv) 17647485Spendry int argc; 17747485Spendry char *argv[]; 17847485Spendry { 17947485Spendry /* 18047485Spendry * Process arguments 18147485Spendry */ 18247485Spendry get_args(argc, argv); 18347485Spendry 18447485Spendry /* 18547485Spendry * If no hostname given then use the local name 18647485Spendry */ 18747485Spendry if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) { 18847485Spendry perror("gethostname"); 18947485Spendry exit(1); 19047485Spendry } 19147485Spendry 19247485Spendry /* 19347485Spendry * Get the username 19447485Spendry */ 19547485Spendry username = find_username(); 19647485Spendry 19747485Spendry /* 19847485Spendry * New hosts and automounts 19947485Spendry */ 20047485Spendry list_of_hosts = new_que(); 20147485Spendry list_of_automounts = new_que(); 20247485Spendry 20347485Spendry /* 20447485Spendry * New dictionaries 20547485Spendry */ 20647485Spendry dict_of_volnames = new_dict(); 20747485Spendry dict_of_hosts = new_dict(); 20847485Spendry 20947485Spendry /* 21047485Spendry * Parse input 21147485Spendry */ 21247485Spendry show_area_being_processed("read config", 11); 21347485Spendry if (yyparse()) 21447485Spendry errors = 1; 21547485Spendry errors += file_io_errors + parse_errors; 21647485Spendry 21747485Spendry if (errors == 0) { 21847485Spendry /* 21947485Spendry * Do semantic analysis of input 22047485Spendry */ 22147485Spendry analyze_hosts(list_of_hosts); 22247485Spendry analyze_automounts(list_of_automounts); 22347485Spendry } 22447485Spendry 22547485Spendry /* 22647485Spendry * Give up if errors 22747485Spendry */ 22847485Spendry if (errors == 0) { 22947485Spendry /* 23047485Spendry * Output data files 23147485Spendry */ 23247485Spendry 23347485Spendry write_atab(list_of_automounts); 23447485Spendry write_bootparams(list_of_hosts); 23547485Spendry write_dumpset(list_of_hosts); 23647485Spendry write_exportfs(list_of_hosts); 23747485Spendry write_fstab(list_of_hosts); 23847485Spendry } 23947485Spendry 24047485Spendry col_cleanup(1); 24147485Spendry 24247485Spendry exit(errors); 24347485Spendry } 244