2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/amfs_program.c
46 #endif /* HAVE_CONFIG_H */
50 /* forward definitions */
51 static char *amfs_program_match(am_opts *fo);
52 static int amfs_program_mount(am_node *am, mntfs *mf);
53 static int amfs_program_umount(am_node *am, mntfs *mf);
54 static int amfs_program_init(mntfs *mf);
59 am_ops amfs_program_ops =
66 amfs_error_lookup_child,
67 amfs_error_mount_child,
69 0, /* amfs_program_readlink */
70 0, /* amfs_program_mounted */
71 0, /* amfs_program_umounted */
72 amfs_generic_find_srvr,
73 0, /* amfs_program_get_wchan */
74 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
76 AUTOFS_PROGRAM_FS_FLAGS,
77 #endif /* HAVE_FS_AUTOFS */
82 * Execute needs a mount and unmount command.
85 amfs_program_match(am_opts *fo)
89 if (fo->opt_unmount && fo->opt_umount) {
90 plog(XLOG_ERROR, "program: cannot specify both unmount and umount options");
94 plog(XLOG_ERROR, "program: must specify mount command");
97 if (!fo->opt_unmount && !fo->opt_umount) {
98 fo->opt_unmount = str3cat(NULL, UNMOUNT_PROGRAM, " umount ", fo->opt_fs);
99 plog(XLOG_INFO, "program: un/umount not specified; using default \"%s\"",
102 prog = strchr(fo->opt_mount, ' ');
104 return xstrdup(prog ? prog + 1 : fo->opt_mount);
109 amfs_program_init(mntfs *mf)
111 /* check if already saved value */
112 if (mf->mf_private != NULL)
115 if (mf->mf_fo == NULL)
118 /* save unmount (or umount) command */
119 if (mf->mf_fo->opt_unmount != NULL)
120 mf->mf_private = (opaque_t) xstrdup(mf->mf_fo->opt_unmount);
122 mf->mf_private = (opaque_t) xstrdup(mf->mf_fo->opt_umount);
123 mf->mf_prfree = (void (*)(opaque_t)) free;
130 amfs_program_exec(char *info)
136 * Split copy of command info string
138 info = xstrdup(info);
139 xivec = strsplit(info, ' ', '\'');
142 * Put stdout to stderr
144 (void) fclose(stdout);
146 logfp = stderr; /* initialize before possible first use */
147 if (dup(fileno(logfp)) == -1)
149 if (fileno(logfp) != fileno(stderr)) {
150 (void) fclose(stderr);
151 if (dup(fileno(logfp)) == -1)
158 if (amuDebug(D_FULL)) {
160 plog(XLOG_DEBUG, "executing (un)mount command...");
162 plog(XLOG_DEBUG, "arg[%ld] = '%s'", (long) (cp - xivec), *cp);
167 if (xivec[0] == 0 || xivec[1] == 0) {
169 plog(XLOG_USER, "1st/2nd args missing to (un)mount program");
171 (void) execv(xivec[0], xivec + 1);
173 plog(XLOG_ERROR, "exec failed: %m");
184 * Free allocate memory
197 amfs_program_mount(am_node *am, mntfs *mf)
199 return amfs_program_exec(mf->mf_fo->opt_mount);
204 amfs_program_umount(am_node *am, mntfs *mf)
206 return amfs_program_exec((char *) mf->mf_private);