138494Sobrien/*
2174313Sobrien * Copyright (c) 1997-2006 Erez Zadok
338494Sobrien * Copyright (c) 1989 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1989 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042633Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174313Sobrien * File: am-utils/fsinfo/fsinfo.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien/*
4538494Sobrien * fsinfo
4638494Sobrien */
4738494Sobrien
4838494Sobrien#ifdef HAVE_CONFIG_H
4938494Sobrien# include <config.h>
5038494Sobrien#endif /* HAVE_CONFIG_H */
5138494Sobrien#include <am_defs.h>
5238494Sobrien#include <fsi_data.h>
5338494Sobrien#include <fsinfo.h>
5438494Sobrien#include <fsi_gram.h>
5538494Sobrien
5638494Sobrien/* globals */
5738494Sobrienchar **g_argv;
5838494Sobrienchar *autodir = "/a";
5938494Sobrienchar *progname;
6042633Sobrienchar hostname[MAXHOSTNAMELEN + 1];
6138494Sobrienchar *username;
6238494Sobrienchar idvbuf[1024];
6338494Sobriendict *dict_of_hosts;
6438494Sobriendict *dict_of_volnames;
6538494Sobrienint errors;
6638494Sobrienint file_io_errors;
6738494Sobrienint parse_errors;
6838494Sobrienint verbose;
6938494Sobrienqelem *list_of_automounts;
7038494Sobrienqelem *list_of_hosts;
7138494Sobrien
7238494Sobrien/*
7338494Sobrien * Output file prefixes
7438494Sobrien */
7538494Sobrienchar *bootparams_pref;
7638494Sobrienchar *dumpset_pref;
7738494Sobrienchar *exportfs_pref;
7838494Sobrienchar *fstab_pref;
7938494Sobrienchar *mount_pref;
8038494Sobrien
8138494Sobrien
8238494Sobrien/*
8338494Sobrien * Argument cracking...
8438494Sobrien */
8538494Sobrienstatic void
8638494Sobrienfsi_get_args(int c, char *v[])
8738494Sobrien{
8838494Sobrien  int ch;
8938494Sobrien  int usage = 0;
9038494Sobrien  char *iptr = idvbuf;
9138494Sobrien
9238494Sobrien  /*
9338494Sobrien   * Determine program name
9438494Sobrien   */
9538494Sobrien  if (v[0]) {
9638494Sobrien    progname = strrchr(v[0], '/');
9738494Sobrien    if (progname && progname[1])
9838494Sobrien      progname++;
9938494Sobrien    else
10038494Sobrien      progname = v[0];
10138494Sobrien  }
10238494Sobrien
10338494Sobrien  if (!progname)
10438494Sobrien    progname = "fsinfo";
10538494Sobrien
10638500Sobrien  while ((ch = getopt(c, v, "a:b:d:e:f:h:m:D:U:I:qv")) != -1)
10738494Sobrien
10838494Sobrien    switch (ch) {
10938494Sobrien
11038494Sobrien    case 'a':
11138494Sobrien      autodir = optarg;
11238494Sobrien      break;
11338494Sobrien
11438494Sobrien    case 'b':
11538494Sobrien      if (bootparams_pref)
11638494Sobrien	fatal("-b option specified twice");
11738494Sobrien      bootparams_pref = optarg;
11838494Sobrien      break;
11938494Sobrien
12038494Sobrien    case 'd':
12138494Sobrien      if (dumpset_pref)
12238494Sobrien	fatal("-d option specified twice");
12338494Sobrien      dumpset_pref = optarg;
12438494Sobrien      break;
12538494Sobrien
12638494Sobrien    case 'h':
127174313Sobrien      xstrlcpy(hostname, optarg, sizeof(hostname));
12838494Sobrien      break;
12938494Sobrien
13038494Sobrien    case 'e':
13138494Sobrien      if (exportfs_pref)
13238494Sobrien	fatal("-e option specified twice");
13338494Sobrien      exportfs_pref = optarg;
13438494Sobrien      break;
13538494Sobrien
13638494Sobrien    case 'f':
13738494Sobrien      if (fstab_pref)
13838494Sobrien	fatal("-f option specified twice");
13938494Sobrien      fstab_pref = optarg;
14038494Sobrien      break;
14138494Sobrien
14238494Sobrien    case 'm':
14338494Sobrien      if (mount_pref)
14438494Sobrien	fatal("-m option specified twice");
14538494Sobrien      mount_pref = optarg;
14638494Sobrien      break;
14738494Sobrien
14838494Sobrien    case 'q':
14938494Sobrien      verbose = -1;
15038494Sobrien      break;
15138494Sobrien
15238494Sobrien    case 'v':
15338494Sobrien      verbose = 1;
15438494Sobrien      break;
15538494Sobrien
15638494Sobrien    case 'I':
15738494Sobrien    case 'D':
15838494Sobrien    case 'U':
159174313Sobrien      /* sizeof(iptr) is actually that of idvbuf.  See declaration above */
160174313Sobrien      xsnprintf(iptr, sizeof(idvbuf), "-%c%s ", ch, optarg);
16138494Sobrien      iptr += strlen(iptr);
16238494Sobrien      break;
16338494Sobrien
16438494Sobrien    default:
16538494Sobrien      usage++;
16638494Sobrien      break;
16738494Sobrien    }
16838494Sobrien
16938494Sobrien  if (c != optind) {
17038494Sobrien    g_argv = v + optind - 1;
171131706Smbr#ifdef yywrap
17238494Sobrien    if (yywrap())
173131706Smbr#endif /* yywrap */
17438494Sobrien      fatal("Cannot read any input files");
17538494Sobrien  } else {
17638494Sobrien    usage++;
17738494Sobrien  }
17838494Sobrien
17938494Sobrien  if (usage) {
18038494Sobrien    fprintf(stderr,
18138494Sobrien	    "\
18238494SobrienUsage: %s [-v] [-a autodir] [-h hostname] [-b bootparams] [-d dumpsets]\n\
18338494Sobrien\t[-e exports] [-f fstabs] [-m automounts]\n\
18438494Sobrien\t[-I dir] [-D|-U string[=string]] config ...\n", progname);
18538494Sobrien    exit(1);
18638494Sobrien  }
18738494Sobrien
18838494Sobrien  if (g_argv[0])
189119682Smbr    fsi_log("g_argv[0] = %s", g_argv[0]);
19038494Sobrien  else
191119682Smbr    fsi_log("g_argv[0] = (nil)");
19238494Sobrien}
19338494Sobrien
19438494Sobrien
19538494Sobrien/*
19638494Sobrien * Determine username of caller
19738494Sobrien */
19838494Sobrienstatic char *
19938494Sobrienfind_username(void)
20038494Sobrien{
201174313Sobrien  const char *u = getlogin();
20238494Sobrien
20338494Sobrien  if (!u) {
20438494Sobrien    struct passwd *pw = getpwuid(getuid());
20538494Sobrien    if (pw)
20638494Sobrien      u = pw->pw_name;
20738494Sobrien  }
20838494Sobrien
20938494Sobrien  if (!u)
21038494Sobrien    u = getenv("USER");
21138494Sobrien  if (!u)
21238494Sobrien    u = getenv("LOGNAME");
21338494Sobrien  if (!u)
21438494Sobrien    u = "root";
21538494Sobrien
21638494Sobrien  return strdup(u);
21738494Sobrien}
21838494Sobrien
21938494Sobrien
22038494Sobrien/*
22138494Sobrien * MAIN
22238494Sobrien */
22338494Sobrienint
22438494Sobrienmain(int argc, char *argv[])
22538494Sobrien{
22638494Sobrien  /*
22738494Sobrien   * Process arguments
22838494Sobrien   */
22938494Sobrien  fsi_get_args(argc, argv);
23038494Sobrien
23138494Sobrien  /*
23238494Sobrien   * If no hostname given then use the local name
23338494Sobrien   */
23438494Sobrien  if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) {
23538494Sobrien    perror("gethostname");
23638494Sobrien    exit(1);
23738494Sobrien  }
238174313Sobrien  hostname[sizeof(hostname) - 1] = '\0';
23938494Sobrien
24038494Sobrien  /*
24138494Sobrien   * Get the username
24238494Sobrien   */
24338494Sobrien  username = find_username();
24438494Sobrien
24538494Sobrien  /*
24638494Sobrien   * New hosts and automounts
24738494Sobrien   */
24838494Sobrien  list_of_hosts = new_que();
24938494Sobrien  list_of_automounts = new_que();
25038494Sobrien
25138494Sobrien  /*
25238494Sobrien   * New dictionaries
25338494Sobrien   */
25438494Sobrien  dict_of_volnames = new_dict();
25538494Sobrien  dict_of_hosts = new_dict();
25638494Sobrien
25738494Sobrien  /*
25838494Sobrien   * Parse input
25938494Sobrien   */
26038494Sobrien  show_area_being_processed("read config", 11);
26138494Sobrien  if (yyparse())
26238494Sobrien    errors = 1;
26338494Sobrien  errors += file_io_errors + parse_errors;
26438494Sobrien
26538494Sobrien  if (errors == 0) {
26638494Sobrien    /*
26738494Sobrien     * Do semantic analysis of input
26838494Sobrien     */
26938494Sobrien    analyze_hosts(list_of_hosts);
27038494Sobrien    analyze_automounts(list_of_automounts);
27138494Sobrien  }
27238494Sobrien
27338494Sobrien  /*
27438494Sobrien   * Give up if errors
27538494Sobrien   */
27638494Sobrien  if (errors == 0) {
27738494Sobrien    /*
27838494Sobrien     * Output data files
27938494Sobrien     */
28038494Sobrien
28138494Sobrien    write_atab(list_of_automounts);
28238494Sobrien    write_bootparams(list_of_hosts);
28338494Sobrien    write_dumpset(list_of_hosts);
28438494Sobrien    write_exportfs(list_of_hosts);
28538494Sobrien    write_fstab(list_of_hosts);
28638494Sobrien  }
28738494Sobrien  col_cleanup(1);
28838494Sobrien
28938494Sobrien  exit(errors);
29038494Sobrien  return errors; /* should never reach here */
29138494Sobrien}
292