xref: /netbsd-src/external/bsd/am-utils/dist/amd/amfs_auto.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: amfs_auto.c,v 1.1.1.2 2009/03/20 20:26:48 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2009 Erez Zadok
5  * Copyright (c) 1990 Jan-Simon Pendry
6  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7  * Copyright (c) 1990 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry at Imperial College, London.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgment:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *
42  * File: am-utils/amd/amfs_auto.c
43  *
44  */
45 
46 /*
47  * Automount file system
48  */
49 
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54 #include <amd.h>
55 
56 /****************************************************************************
57  *** MACROS                                                               ***
58  ****************************************************************************/
59 
60 
61 /****************************************************************************
62  *** STRUCTURES                                                           ***
63  ****************************************************************************/
64 
65 
66 /****************************************************************************
67  *** FORWARD DEFINITIONS                                                  ***
68  ****************************************************************************/
69 static int amfs_auto_mount(am_node *mp, mntfs *mf);
70 
71 
72 /****************************************************************************
73  *** OPS STRUCTURES                                                       ***
74  ****************************************************************************/
75 am_ops amfs_auto_ops =
76 {
77   "auto",
78   amfs_generic_match,
79   0,				/* amfs_auto_init */
80   amfs_auto_mount,
81   amfs_generic_umount,
82   amfs_generic_lookup_child,
83   amfs_generic_mount_child,
84   amfs_generic_readdir,
85   0,				/* amfs_auto_readlink */
86   amfs_generic_mounted,
87   0,				/* amfs_auto_umounted */
88   amfs_generic_find_srvr,
89   0,				/* amfs_auto_get_wchan */
90   FS_AMQINFO | FS_DIRECTORY,
91 #ifdef HAVE_FS_AUTOFS
92   AUTOFS_AUTO_FS_FLAGS,
93 #endif /* HAVE_FS_AUTOFS */
94 };
95 
96 
97 /****************************************************************************
98  *** FUNCTIONS                                                             ***
99  ****************************************************************************/
100 /*
101  * Mount a sub-mount
102  */
103 static int
104 amfs_auto_mount(am_node *mp, mntfs *mf)
105 {
106   /*
107    * Pseudo-directories are used to provide some structure
108    * to the automounted directories instead
109    * of putting them all in the top-level automount directory.
110    *
111    * Here, just increment the parent's link count.
112    */
113   mp->am_parent->am_fattr.na_nlink++;
114 
115   /*
116    * Info field of . means use parent's info field.
117    * Historical - not documented.
118    */
119   if (mf->mf_info[0] == '.' && mf->mf_info[1] == '\0')
120     mf->mf_info = strealloc(mf->mf_info, mp->am_parent->am_mnt->mf_info);
121 
122   /*
123    * Compute prefix:
124    *
125    * If there is an option prefix then use that else
126    * If the parent had a prefix then use that with name
127    *      of this node appended else
128    * Use the name of this node.
129    *
130    * That means if you want no prefix you must say so
131    * in the map.
132    */
133   if (mf->mf_fo->opt_pref) {
134     /* allow pref:=null to set a real null prefix */
135     if (STREQ(mf->mf_fo->opt_pref, "null")) {
136       mp->am_pref = strdup("");
137     } else {
138       /*
139        * the prefix specified as an option
140        */
141       mp->am_pref = strdup(mf->mf_fo->opt_pref);
142     }
143   } else {
144     /*
145      * else the parent's prefix
146      * followed by the name
147      * followed by /
148      */
149     char *ppref = mp->am_parent->am_pref;
150     if (ppref == 0)
151       ppref = "";
152     mp->am_pref = str3cat((char *) NULL, ppref, mp->am_name, "/");
153   }
154 
155 #ifdef HAVE_FS_AUTOFS
156   if (mf->mf_flags & MFF_IS_AUTOFS) {
157     char opts[SIZEOF_OPTS];
158     int error;
159 
160     autofs_get_opts(opts, sizeof(opts), mp->am_autofs_fh);
161 
162     /* now do the mount */
163     error = amfs_mount(mp, mf, opts);
164     if (error) {
165       errno = error;
166       plog(XLOG_FATAL, "amfs_auto_mount: amfs_mount failed: %m");
167       return error;
168     }
169   }
170 #endif /* HAVE_FS_AUTOFS */
171 
172   /*
173    * Attach a map cache
174    */
175   amfs_mkcacheref(mf);
176 
177   return 0;
178 }
179