2 * Copyright (c) 1997-2006 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.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
40 * File: am-utils/fsinfo/wr_atab.c
46 #endif /* HAVE_CONFIG_H */
53 * Write a sequence of automount mount map entries
56 write_amount_info(FILE *af, automount *ap, u_int sk)
63 * This can also be a top-level directory, in which
64 * case the type:=auto is not wanted...
66 * type:=auto;fs:=${map};pref:=whatever/
69 if (strlen(ap->a_name) > sk) {
70 fprintf(af, "%s type:=auto;fs:=${map};pref:=%s/\n",
71 ap->a_name + sk, ap->a_name + sk);
73 ITER(ap2, automount, ap->a_mount)
74 errors += write_amount_info(af, ap2, sk);
75 } else if (ap->a_hardwiredfs) {
78 * A hardwired filesystem "hostname:path"
79 * rhost:=hostname;rfs:=path
81 char *key = ap->a_name + sk;
82 char *hostname = ap->a_hardwiredfs;
83 char *path = strrchr(hostname, (int) ':');
86 fprintf(stderr, "%s: %s not an NFS filesystem\n", ap->a_name, ap->a_hardwiredfs);
96 fprintf(af, " rhost:=%s", hostname);
97 fprintf(af, ";rfs:=%s", path);
98 if (ap->a_opts && !STREQ(ap->a_opts, "")) {
99 fprintf(af, ";%s", ap->a_opts);
105 } else if (ap->a_mounted) {
108 * A mounted partition
109 * type:=link [ link entries ] type:=nfs [ nfs entries ]
112 dict_ent *de = ap->a_mounted;
113 int done_type_link = 0;
114 char *key = ap->a_name + sk;
122 * First output any Link locations that would not
123 * otherwise be correctly mounted. These refer
124 * to filesystem which are not mounted in the same
125 * place which the automounter would use.
127 ITER(dd, dict_data, &de->de_q) {
128 fsi_mount *mp = (fsi_mount *) dd->dd_data;
130 * If the mount point and the exported volname are the
131 * same then this filesystem will be recognized by
132 * the restart code - so we don't need to put out a
133 * special rule for it.
135 if (mp->m_dk->d_host->h_lochost) {
137 compute_automount_point(amountpt, sizeof(amountpt),
138 mp->m_dk->d_host, mp->m_exported->m_volname);
139 if (!STREQ(mp->m_dk->d_mountpt, amountpt)) {
141 * ap->a_volname is the name of the aliased volume
142 * mp->m_name is the mount point of the filesystem
143 * mp->m_volname is the volume name of the filesystems
147 * Find length of key and volume names
149 int avlen = strlen(ap->a_volname);
150 int mnlen = strlen(mp->m_volname);
153 * Make sure a -type:=link is output once
155 if (!done_type_link) {
157 fputs(" -type:=link", af);
161 * Output a selector for the hostname,
162 * the device from which to mount and
163 * where to mount. This will correspond
164 * to the values output for the fstab.
166 if (mp->m_dk->d_host->h_lochost)
167 fprintf(af, " host==%s", mp->m_dk->d_host->h_lochost);
169 fprintf(af, " hostd==%s", mp->m_dk->d_host->h_hostname);
170 fprintf(af, ";fs:=%s", mp->m_name);
173 * ... and a sublink if needed
176 char *sublink = ap->a_volname + mnlen + 1;
177 fprintf(af, "/%s", sublink);
185 * Next do the NFS locations
190 ITER(dd, dict_data, &de->de_q) {
191 fsi_mount *mp = (fsi_mount *) dd->dd_data;
192 int namelen = mp->m_name_len;
193 int exp_namelen = mp->m_exported->m_name_len;
194 int volnlen = strlen(ap->a_volname);
195 int mvolnlen = strlen(mp->m_volname);
200 * Output any selectors
203 fprintf(af, "%s;", mp->m_sel);
206 * Print host and volname of exported filesystem
208 fprintf(af, "rhost:=%s",
209 mp->m_dk->d_host->h_lochost ?
210 mp->m_dk->d_host->h_lochost :
211 mp->m_dk->d_host->h_hostname);
212 fprintf(af, ";rfs:=%s", mp->m_exported->m_volname);
213 if (ap->a_opts && !STREQ(ap->a_opts, "")) {
214 fprintf(af, ";%s", ap->a_opts);
218 * Now determine whether a sublink is required.
220 if (exp_namelen < namelen || mvolnlen < volnlen) {
223 if (exp_namelen < namelen) {
224 xstrlcat(sublink, mp->m_name + exp_namelen + 1, sizeof(sublink));
225 if (mvolnlen < volnlen)
226 xstrlcat(sublink, "/", sizeof(sublink));
228 if (mvolnlen < volnlen)
229 xstrlcat(sublink, ap->a_volname + mvolnlen + 1, sizeof(sublink));
231 fprintf(af, ";sublink:=%s", sublink);
235 } else if (ap->a_symlink) {
240 * type:=link;fs:=whatever
242 fprintf(af, "%s type:=link;fs:=%s\n", ap->a_name + sk, ap->a_symlink);
250 * Write a single automount configuration file
253 write_amount( qelem *q, char *def)
260 * Output all indirect maps
262 ITER(ap, automount, q) {
267 * If there is no a_mount node then this is really
268 * a direct mount, so just keep a count and continue.
269 * Direct mounts are output into a special file during
270 * the second pass below.
277 p = strrchr(ap->a_name, '/');
283 af = pref_open(mount_pref, p, gen_hdr, ap->a_name);
285 show_new(ap->a_name);
286 fputs("/defaults ", af);
288 fprintf(af, "%s;", def);
289 fputs("type:=nfs\n", af);
290 errors += write_amount_info(af, ap, strlen(ap->a_name) + 1);
291 errors += pref_close(af);
296 * Output any direct map entries which were found during the
297 * previous pass over the data.
300 FILE *af = pref_open(mount_pref, "direct.map", info_hdr, "direct mount");
303 show_new("direct mounts");
304 fputs("/defaults ", af);
306 fprintf(af, "%s;", def);
307 fputs("type:=nfs\n", af);
308 ITER(ap, automount, q)
310 errors += write_amount_info(af, ap, 1);
311 errors += pref_close(af);
319 * Write all the needed automount configuration files
328 show_area_being_processed("write automount", 5);
329 ITER(tp, auto_tree, q)
330 errors += write_amount(tp->t_mount, tp->t_defaults);