Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / am-utils / dist / amd / amfs_program.c
blob0d3536d2232d1d72cdb9948385fe0be0f48261a2
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1989 Jan-Simon Pendry
6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1989 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/amfs_program.c
47 * Program 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 *amfs_program_match(am_opts *fo);
58 static int amfs_program_mount(am_node *am, mntfs *mf);
59 static int amfs_program_umount(am_node *am, mntfs *mf);
60 static int amfs_program_init(mntfs *mf);
63 * Ops structure
65 am_ops amfs_program_ops =
67 "program",
68 amfs_program_match,
69 amfs_program_init,
70 amfs_program_mount,
71 amfs_program_umount,
72 amfs_error_lookup_child,
73 amfs_error_mount_child,
74 amfs_error_readdir,
75 0, /* amfs_program_readlink */
76 0, /* amfs_program_mounted */
77 0, /* amfs_program_umounted */
78 amfs_generic_find_srvr,
79 0, /* amfs_program_get_wchan */
80 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
81 #ifdef HAVE_FS_AUTOFS
82 AUTOFS_PROGRAM_FS_FLAGS,
83 #endif /* HAVE_FS_AUTOFS */
88 * Execute needs a mount and unmount command.
90 static char *
91 amfs_program_match(am_opts *fo)
93 char *prog;
95 if (fo->opt_unmount && fo->opt_umount) {
96 plog(XLOG_ERROR, "program: cannot specify both unmount and umount options");
97 return 0;
99 if (!fo->opt_mount) {
100 plog(XLOG_ERROR, "program: must specify mount command");
101 return 0;
103 if (!fo->opt_unmount && !fo->opt_umount) {
104 fo->opt_unmount = str3cat(NULL, UNMOUNT_PROGRAM, " umount ", fo->opt_fs);
105 plog(XLOG_INFO, "program: un/umount not specified; using default \"%s\"",
106 fo->opt_unmount);
108 prog = strchr(fo->opt_mount, ' ');
110 return strdup(prog ? prog + 1 : fo->opt_mount);
114 static int
115 amfs_program_init(mntfs *mf)
117 /* check if already saved value */
118 if (mf->mf_private != NULL)
119 return 0;
121 /* save unmount (or umount) command */
122 if (mf->mf_fo->opt_unmount != NULL)
123 mf->mf_private = (opaque_t) strdup(mf->mf_fo->opt_unmount);
124 else
125 mf->mf_private = (opaque_t) strdup(mf->mf_fo->opt_umount);
126 mf->mf_prfree = (void (*)(opaque_t)) free;
128 return 0;
132 static int
133 amfs_program_exec(char *info)
135 char **xivec;
136 int error;
139 * Split copy of command info string
141 info = strdup(info);
142 if (info == 0)
143 return ENOBUFS;
144 xivec = strsplit(info, ' ', '\'');
147 * Put stdout to stderr
149 (void) fclose(stdout);
150 if (!logfp)
151 logfp = stderr; /* initialize before possible first use */
152 (void) dup(fileno(logfp));
153 if (fileno(logfp) != fileno(stderr)) {
154 (void) fclose(stderr);
155 (void) dup(fileno(logfp));
159 * Try the exec
161 if (amuDebug(D_FULL)) {
162 char **cp = xivec;
163 plog(XLOG_DEBUG, "executing (un)mount command...");
164 while (*cp) {
165 plog(XLOG_DEBUG, "arg[%ld] = '%s'", (long) (cp - xivec), *cp);
166 cp++;
170 if (xivec[0] == 0 || xivec[1] == 0) {
171 errno = EINVAL;
172 plog(XLOG_USER, "1st/2nd args missing to (un)mount program");
173 } else {
174 (void) execv(xivec[0], xivec + 1);
178 * Save error number
180 error = errno;
181 plog(XLOG_ERROR, "exec failed: %m");
184 * Free allocate memory
186 XFREE(info);
187 XFREE(xivec);
190 * Return error
192 return error;
196 static int
197 amfs_program_mount(am_node *am, mntfs *mf)
199 return amfs_program_exec(mf->mf_fo->opt_mount);
203 static int
204 amfs_program_umount(am_node *am, mntfs *mf)
206 return amfs_program_exec((char *) mf->mf_private);