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