1 /* $NetBSD: amfs_program.c,v 1.2 2018/02/04 09:15:44 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997-2014 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.
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *
38 * File: am-utils/amd/amfs_program.c
39 *
40 */
41
42 /*
43 * Program file system
44 */
45
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <amd.h>
51
52 /* forward definitions */
53 static char *amfs_program_match(am_opts *fo);
54 static int amfs_program_mount(am_node *am, mntfs *mf);
55 static int amfs_program_umount(am_node *am, mntfs *mf);
56 static int amfs_program_init(mntfs *mf);
57
58 /*
59 * Ops structure
60 */
61 am_ops amfs_program_ops =
62 {
63 "program",
64 amfs_program_match,
65 amfs_program_init,
66 amfs_program_mount,
67 amfs_program_umount,
68 amfs_error_lookup_child,
69 amfs_error_mount_child,
70 amfs_error_readdir,
71 0, /* amfs_program_readlink */
72 0, /* amfs_program_mounted */
73 0, /* amfs_program_umounted */
74 amfs_generic_find_srvr,
75 0, /* amfs_program_get_wchan */
76 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
77 #ifdef HAVE_FS_AUTOFS
78 AUTOFS_PROGRAM_FS_FLAGS,
79 #endif /* HAVE_FS_AUTOFS */
80 };
81
82
83 /*
84 * Execute needs a mount and unmount command.
85 */
86 static char *
amfs_program_match(am_opts * fo)87 amfs_program_match(am_opts *fo)
88 {
89 char *prog;
90
91 if (fo->opt_unmount && fo->opt_umount) {
92 plog(XLOG_ERROR, "program: cannot specify both unmount and umount options");
93 return 0;
94 }
95 if (!fo->opt_mount) {
96 plog(XLOG_ERROR, "program: must specify mount command");
97 return 0;
98 }
99 if (!fo->opt_unmount && !fo->opt_umount) {
100 fo->opt_unmount = str3cat(NULL, UNMOUNT_PROGRAM, " umount ", fo->opt_fs);
101 plog(XLOG_INFO, "program: un/umount not specified; using default \"%s\"",
102 fo->opt_unmount);
103 }
104 prog = strchr(fo->opt_mount, ' ');
105
106 return xstrdup(prog ? prog + 1 : fo->opt_mount);
107 }
108
109
110 static int
amfs_program_init(mntfs * mf)111 amfs_program_init(mntfs *mf)
112 {
113 /* check if already saved value */
114 if (mf->mf_private != NULL)
115 return 0;
116
117 if (mf->mf_fo == NULL)
118 return 0;
119
120 /* save unmount (or umount) command */
121 if (mf->mf_fo->opt_unmount != NULL)
122 mf->mf_private = (opaque_t) xstrdup(mf->mf_fo->opt_unmount);
123 else
124 mf->mf_private = (opaque_t) xstrdup(mf->mf_fo->opt_umount);
125 mf->mf_prfree = (void (*)(opaque_t)) free;
126
127 return 0;
128 }
129
130
131 static int
amfs_program_exec(char * info)132 amfs_program_exec(char *info)
133 {
134 char **xivec;
135 int error;
136
137 /*
138 * Split copy of command info string
139 */
140 info = xstrdup(info);
141 xivec = strsplit(info, ' ', '\'');
142
143 /*
144 * Put stdout to stderr
145 */
146 (void) fclose(stdout);
147 if (!logfp) {
148 logfp = stderr; /* initialize before possible first use */
149 if (dup(fileno(logfp)) == -1)
150 goto out;
151 }
152 if (fileno(logfp) != fileno(stderr)) {
153 (void) fclose(stderr);
154 if (dup(fileno(logfp)) == -1)
155 goto out;
156 }
157
158 /*
159 * Try the exec
160 */
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++;
167 }
168 }
169
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);
175 error = errno;
176 plog(XLOG_ERROR, "exec failed: %m");
177 errno = error;
178 }
179
180 out:
181 /*
182 * Save error number
183 */
184 error = errno;
185
186 /*
187 * Free allocate memory
188 */
189 XFREE(info);
190 XFREE(xivec);
191
192 /*
193 * Return error
194 */
195 return error;
196 }
197
198
199 static int
amfs_program_mount(am_node * am,mntfs * mf)200 amfs_program_mount(am_node *am, mntfs *mf)
201 {
202 return amfs_program_exec(mf->mf_fo->opt_mount);
203 }
204
205
206 static int
amfs_program_umount(am_node * am,mntfs * mf)207 amfs_program_umount(am_node *am, mntfs *mf)
208 {
209 return amfs_program_exec((char *) mf->mf_private);
210 }
211