Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / am-utils / dist / amd / ops_udf.c
blob3ab66df9e7ed32cf3d0dfe5dfbf646f5dc4be838
1 /* $NetBSD$ */
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.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
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.
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.
42 * File: am-utils/amd/ops_udf.c
47 * UDF file system
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54 #include <amd.h>
56 /* forward definitions */
57 static char *udf_match(am_opts *fo);
58 static int udf_mount(am_node *am, mntfs *mf);
59 static int udf_umount(am_node *am, mntfs *mf);
62 * Ops structure
64 am_ops udf_ops =
66 "udf",
67 udf_match,
68 0, /* udf_init */
69 udf_mount,
70 udf_umount,
71 amfs_error_lookup_child,
72 amfs_error_mount_child,
73 amfs_error_readdir,
74 0, /* udf_readlink */
75 0, /* udf_mounted */
76 0, /* udf_umounted */
77 amfs_generic_find_srvr,
78 0, /* udf_get_wchan */
79 FS_MKMNT | FS_UBACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
80 #ifdef HAVE_FS_AUTOFS
81 AUTOFS_UDF_FS_FLAGS,
82 #endif /* HAVE_FS_AUTOFS */
85 #if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID)
86 static int
87 a_num(const char *s, const char *id_type)
89 int id;
90 char *ep;
92 id = strtol(s, &ep, 0);
93 if (*ep || s == ep || id < 0) {
94 plog(XLOG_ERROR, "mount_udf: unknown %s: %s", id_type, s);
95 return 0;
97 return id;
99 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
101 #if defined(HAVE_UDF_ARGS_T_NOBODY_GID)
102 static gid_t
103 a_gid(const char *s, const char *id_type)
105 struct group *gr;
107 if ((gr = getgrnam(s)) != NULL)
108 return gr->gr_gid;
109 return a_num(s, id_type);
111 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) */
113 #if defined(HAVE_UDF_ARGS_T_NOBODY_UID)
114 static uid_t
115 a_uid(const char *s, const char *id_type)
117 struct passwd *pw;
119 if ((pw = getpwnam(s)) != NULL)
120 return pw->pw_uid;
121 return a_num(s, id_type);
123 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) */
126 * UDF needs remote filesystem.
128 static char *
129 udf_match(am_opts *fo)
132 if (!fo->opt_dev) {
133 plog(XLOG_USER, "udf: no source device specified");
134 return 0;
136 dlog("UDF: mounting device \"%s\" on \"%s\"", fo->opt_dev, fo->opt_fs);
139 * Determine magic cookie to put in mtab
141 return strdup(fo->opt_dev);
144 static int
145 mount_udf(char *mntdir, char *fs_name, char *opts, int on_autofs)
147 udf_args_t udf_args;
148 mntent_t mnt;
149 int flags;
150 char *str;
151 #if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
152 uid_t uid_nobody;
153 gid_t gid_nobody;
154 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
156 * Figure out the name of the file system type.
158 MTYPE_TYPE type = MOUNT_TYPE_UDF;
160 #if defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID)
161 uid_nobody = a_uid("nobody", "user");
162 if (uid_nobody == 0) {
163 plog(XLOG_ERROR, "mount_udf: invalid uid for nobody");
164 return EPERM;
166 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_UID) || defined(HAVE_UDF_ARGS_T_ANON_UID) */
168 #if defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID)
169 gid_nobody = a_gid("nobody", "group");
170 if (gid_nobody == 0) {
171 plog(XLOG_ERROR, "mount_udf: invalid gid for nobody");
172 return EPERM;
174 #endif /* defined(HAVE_UDF_ARGS_T_NOBODY_GID) || defined(HAVE_UDF_ARGS_T_ANON_GID) */
176 str = NULL;
177 memset((voidp) &udf_args, 0, sizeof(udf_args)); /* Paranoid */
180 * Fill in the mount structure
182 memset((voidp)&mnt, 0, sizeof(mnt));
183 mnt.mnt_dir = mntdir;
184 mnt.mnt_fsname = fs_name;
185 mnt.mnt_type = MNTTAB_TYPE_UDF;
186 mnt.mnt_opts = opts;
188 flags = compute_mount_flags(&mnt);
190 #ifdef HAVE_UDF_ARGS_T_UDFMFLAGS
191 # if defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION)
192 if (amu_hasmntopt(&mnt, MNTTAB_OPT_CLOSESESSION))
193 udf_args.udfmflags |= MNT2_UDF_OPT_CLOSESESSION;
194 # endif /* defined(MNT2_UDF_OPT_CLOSESESSION) && defined(MNTTAB_OPT_CLOSESESSION) */
195 #endif /* HAVE_UDF_ARGS_T_UDFMFLAGS */
197 #ifdef HAVE_UDF_ARGS_T_NOBODY_UID
198 udf_args.nobody_uid = uid_nobody;
199 #endif /* HAVE_UDF_ARGS_T_NOBODY_UID */
201 #ifdef HAVE_UDF_ARGS_T_NOBODY_GID
202 udf_args.nobody_gid = gid_nobody;
203 #endif /* HAVE_UDF_ARGS_T_NOBODY_GID */
205 #ifdef HAVE_UDF_ARGS_T_ANON_UID
206 udf_args.anon_uid = uid_nobody; /* default to nobody */
207 if ((str = hasmntstr(&mnt, MNTTAB_OPT_USER)) != NULL) {
208 udf_args.anon_uid = a_uid(str, MNTTAB_OPT_USER);
209 XFREE(str);
211 #endif /* HAVE_UDF_ARGS_T_ANON_UID */
213 #ifdef HAVE_UDF_ARGS_T_ANON_GID
214 udf_args.anon_gid = gid_nobody; /* default to nobody */
215 if ((str = hasmntstr(&mnt, MNTTAB_OPT_GROUP)) != NULL) {
216 udf_args.anon_gid = a_gid(str, MNTTAB_OPT_GROUP);
217 XFREE(str);
219 #endif /* HAVE_UDF_ARGS_T_ANON_GID */
221 #ifdef HAVE_UDF_ARGS_T_GMTOFF
222 udf_args.gmtoff = 0;
223 if ((str = hasmntstr(&mnt, MNTTAB_OPT_GMTOFF)) != NULL) {
224 udf_args.gmtoff = a_num(str, MNTTAB_OPT_GMTOFF);
225 XFREE(str);
227 #endif /* HAVE_UDF_ARGS_T_GMTOFF */
229 #ifdef HAVE_UDF_ARGS_T_SESSIONNR
230 udf_args.sessionnr = 0;
231 if ((str = hasmntstr(&mnt, MNTTAB_OPT_SESSIONNR)) != NULL) {
232 udf_args.sessionnr = a_num(str, MNTTAB_OPT_SESSIONNR);
233 XFREE(str);
235 #endif /* HAVE_UDF_ARGS_T_SESSIONNR */
237 #ifdef HAVE_UDF_ARGS_T_VERSION
238 # ifdef UDFMNT_VERSION
239 udf_args.version = UDFMNT_VERSION;
240 # endif /* UDFMNT_VERSION */
241 #endif /* HAVE_UDF_ARGS_T_VERSION */
243 #ifdef HAVE_UFS_ARGS_T_FSPEC
244 udf_args.fspec = fs_name;
245 #endif /* HAVE_UFS_ARGS_T_FSPEC */
248 * Call generic mount routine
250 return mount_fs(&mnt, flags, (caddr_t)&udf_args, 0, type, 0, NULL,
251 mnttab_file_name, on_autofs);
254 static int
255 udf_mount(am_node *am, mntfs *mf)
257 int on_autofs;
258 int error;
260 on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
261 error = mount_udf(mf->mf_mount, mf->mf_info, mf->mf_mopts, on_autofs);
262 if (error) {
263 errno = error;
264 plog(XLOG_ERROR, "mount_udf: %m");
265 return error;
267 return 0;
271 static int
272 udf_umount(am_node *am, mntfs *mf)
274 int unmount_flags;
276 unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
277 return UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);