| 1 | /* |
| 2 | * Copyright (c) 1989 Jan-Simon Pendry |
| 3 | * Copyright (c) 1989 Imperial College of Science, Technology & Medicine |
| 4 | * Copyright (c) 1989 The Regents of the University of California. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to Berkeley by |
| 8 | * Jan-Simon Pendry at Imperial College, London. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by the University of |
| 21 | * California, Berkeley and its contributors. |
| 22 | * 4. Neither the name of the University nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this software |
| 24 | * without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | * SUCH DAMAGE. |
| 37 | * |
| 38 | * @(#)fsinfo.c 5.3 (Berkeley) 5/12/91 |
| 39 | * |
| 40 | * $Id: fsinfo.c,v 5.2.1.3 91/05/07 22:19:01 jsp Alpha $ |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * fsinfo |
| 46 | */ |
| 47 | |
| 48 | #include "../fsinfo/fsinfo.h" |
| 49 | #include "fsi_gram.h" |
| 50 | #include <pwd.h> |
| 51 | |
| 52 | qelem *list_of_hosts; |
| 53 | qelem *list_of_automounts; |
| 54 | dict *dict_of_volnames; |
| 55 | dict *dict_of_hosts; |
| 56 | char *autodir = "/a"; |
| 57 | char hostname[MAXHOSTNAMELEN+1]; |
| 58 | char *username; |
| 59 | int file_io_errors; |
| 60 | int parse_errors; |
| 61 | int errors; |
| 62 | int verbose; |
| 63 | char idvbuf[1024]; |
| 64 | |
| 65 | char **g_argv; |
| 66 | char *progname; |
| 67 | |
| 68 | /* |
| 69 | * Output file prefixes |
| 70 | */ |
| 71 | char *exportfs_pref; |
| 72 | char *fstab_pref; |
| 73 | char *dumpset_pref; |
| 74 | char *mount_pref; |
| 75 | char *bootparams_pref; |
| 76 | |
| 77 | /* |
| 78 | * Argument cracking... |
| 79 | */ |
| 80 | static void get_args(c, v) |
| 81 | int c; |
| 82 | char *v[]; |
| 83 | { |
| 84 | extern char *optarg; |
| 85 | extern int optind; |
| 86 | int ch; |
| 87 | int usage = 0; |
| 88 | char *iptr = idvbuf; |
| 89 | |
| 90 | /* |
| 91 | * Determine program name |
| 92 | */ |
| 93 | if (v[0]) { |
| 94 | progname = strrchr(v[0], '/'); |
| 95 | if (progname && progname[1]) |
| 96 | progname++; |
| 97 | else |
| 98 | progname = v[0]; |
| 99 | } |
| 100 | if (!progname) |
| 101 | progname = "fsinfo"; |
| 102 | |
| 103 | while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != EOF) |
| 104 | switch (ch) { |
| 105 | case 'a': |
| 106 | autodir = optarg; |
| 107 | break; |
| 108 | case 'b': |
| 109 | if (bootparams_pref) |
| 110 | fatal("-b option specified twice"); |
| 111 | bootparams_pref = optarg; |
| 112 | break; |
| 113 | case 'd': |
| 114 | if (dumpset_pref) |
| 115 | fatal("-d option specified twice"); |
| 116 | dumpset_pref = optarg; |
| 117 | break; |
| 118 | case 'h': |
| 119 | strncpy(hostname, optarg, sizeof(hostname)-1); |
| 120 | break; |
| 121 | case 'e': |
| 122 | if (exportfs_pref) |
| 123 | fatal("-e option specified twice"); |
| 124 | exportfs_pref = optarg; |
| 125 | break; |
| 126 | case 'f': |
| 127 | if (fstab_pref) |
| 128 | fatal("-f option specified twice"); |
| 129 | fstab_pref = optarg; |
| 130 | break; |
| 131 | case 'm': |
| 132 | if (mount_pref) |
| 133 | fatal("-m option specified twice"); |
| 134 | mount_pref = optarg; |
| 135 | break; |
| 136 | case 'q': |
| 137 | verbose = -1; |
| 138 | break; |
| 139 | case 'v': |
| 140 | verbose = 1; |
| 141 | break; |
| 142 | case 'I': case 'D': case 'U': |
| 143 | sprintf(iptr, "-%c%s ", ch, optarg); |
| 144 | iptr += strlen(iptr); |
| 145 | break; |
| 146 | default: |
| 147 | usage++; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (c != optind) { |
| 152 | g_argv = v + optind - 1; |
| 153 | if (yywrap()) |
| 154 | fatal("Cannot read any input files"); |
| 155 | } else { |
| 156 | usage++; |
| 157 | } |
| 158 | |
| 159 | if (usage) { |
| 160 | fprintf(stderr, |
| 161 | "\ |
| 162 | Usage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\ |
| 163 | \t[-e exports] [-f fstabs] [-m automounts]\n\ |
| 164 | \t[-I dir] [-D|-U string[=string]] config ...\n", progname); |
| 165 | exit(1); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | if (g_argv[0]) |
| 170 | log("g_argv[0] = %s", g_argv[0]); |
| 171 | else |
| 172 | log("g_argv[0] = (nil)"); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Determine username of caller |
| 177 | */ |
| 178 | static char *find_username() |
| 179 | { |
| 180 | extern char *getlogin(); |
| 181 | extern char *getenv(); |
| 182 | char *u = getlogin(); |
| 183 | if (!u) { |
| 184 | struct passwd *pw = getpwuid(getuid()); |
| 185 | if (pw) |
| 186 | u = pw->pw_name; |
| 187 | } |
| 188 | if (!u) |
| 189 | u = getenv("USER"); |
| 190 | if (!u) |
| 191 | u = getenv("LOGNAME"); |
| 192 | if (!u) |
| 193 | u = "root"; |
| 194 | |
| 195 | return strdup(u); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * MAIN |
| 200 | */ |
| 201 | main(argc, argv) |
| 202 | int argc; |
| 203 | char *argv[]; |
| 204 | { |
| 205 | /* |
| 206 | * Process arguments |
| 207 | */ |
| 208 | get_args(argc, argv); |
| 209 | |
| 210 | /* |
| 211 | * If no hostname given then use the local name |
| 212 | */ |
| 213 | if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) { |
| 214 | perror("gethostname"); |
| 215 | exit(1); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Get the username |
| 220 | */ |
| 221 | username = find_username(); |
| 222 | |
| 223 | /* |
| 224 | * New hosts and automounts |
| 225 | */ |
| 226 | list_of_hosts = new_que(); |
| 227 | list_of_automounts = new_que(); |
| 228 | |
| 229 | /* |
| 230 | * New dictionaries |
| 231 | */ |
| 232 | dict_of_volnames = new_dict(); |
| 233 | dict_of_hosts = new_dict(); |
| 234 | |
| 235 | /* |
| 236 | * Parse input |
| 237 | */ |
| 238 | show_area_being_processed("read config", 11); |
| 239 | if (yyparse()) |
| 240 | errors = 1; |
| 241 | errors += file_io_errors + parse_errors; |
| 242 | |
| 243 | if (errors == 0) { |
| 244 | /* |
| 245 | * Do semantic analysis of input |
| 246 | */ |
| 247 | analyze_hosts(list_of_hosts); |
| 248 | analyze_automounts(list_of_automounts); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Give up if errors |
| 253 | */ |
| 254 | if (errors == 0) { |
| 255 | /* |
| 256 | * Output data files |
| 257 | */ |
| 258 | |
| 259 | write_atab(list_of_automounts); |
| 260 | write_bootparams(list_of_hosts); |
| 261 | write_dumpset(list_of_hosts); |
| 262 | write_exportfs(list_of_hosts); |
| 263 | write_fstab(list_of_hosts); |
| 264 | } |
| 265 | |
| 266 | col_cleanup(1); |
| 267 | |
| 268 | exit(errors); |
| 269 | } |