]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/amd/amd/amfs_program.c
MFC r308493, r308619: Update amd from am-utils 6.1.5 to 6.2.
[FreeBSD/stable/10.git] / contrib / amd / amd / amfs_program.c
1 /*
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.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  *
36  * File: am-utils/amd/amfs_program.c
37  *
38  */
39
40 /*
41  * Program file system
42  */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 #include <am_defs.h>
48 #include <amd.h>
49
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);
55
56 /*
57  * Ops structure
58  */
59 am_ops amfs_program_ops =
60 {
61   "program",
62   amfs_program_match,
63   amfs_program_init,
64   amfs_program_mount,
65   amfs_program_umount,
66   amfs_error_lookup_child,
67   amfs_error_mount_child,
68   amfs_error_readdir,
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 */
75 #ifdef HAVE_FS_AUTOFS
76   AUTOFS_PROGRAM_FS_FLAGS,
77 #endif /* HAVE_FS_AUTOFS */
78 };
79
80
81 /*
82  * Execute needs a mount and unmount command.
83  */
84 static char *
85 amfs_program_match(am_opts *fo)
86 {
87   char *prog;
88
89   if (fo->opt_unmount && fo->opt_umount) {
90     plog(XLOG_ERROR, "program: cannot specify both unmount and umount options");
91     return 0;
92   }
93   if (!fo->opt_mount) {
94     plog(XLOG_ERROR, "program: must specify mount command");
95     return 0;
96   }
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\"",
100          fo->opt_unmount);
101   }
102   prog = strchr(fo->opt_mount, ' ');
103
104   return xstrdup(prog ? prog + 1 : fo->opt_mount);
105 }
106
107
108 static int
109 amfs_program_init(mntfs *mf)
110 {
111   /* check if already saved value */
112   if (mf->mf_private != NULL)
113     return 0;
114
115   if (mf->mf_fo == NULL)
116     return 0;
117
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);
121   else
122     mf->mf_private = (opaque_t) xstrdup(mf->mf_fo->opt_umount);
123   mf->mf_prfree = (void (*)(opaque_t)) free;
124
125   return 0;
126 }
127
128
129 static int
130 amfs_program_exec(char *info)
131 {
132   char **xivec;
133   int error;
134
135   /*
136    * Split copy of command info string
137    */
138   info = xstrdup(info);
139   xivec = strsplit(info, ' ', '\'');
140
141   /*
142    * Put stdout to stderr
143    */
144   (void) fclose(stdout);
145   if (!logfp)
146     logfp = stderr;             /* initialize before possible first use */
147     if (dup(fileno(logfp)) == -1)
148       goto out;
149   if (fileno(logfp) != fileno(stderr)) {
150     (void) fclose(stderr);
151     if (dup(fileno(logfp)) == -1)
152       goto out;
153   }
154
155   /*
156    * Try the exec
157    */
158   if (amuDebug(D_FULL)) {
159     char **cp = xivec;
160     plog(XLOG_DEBUG, "executing (un)mount command...");
161     while (*cp) {
162       plog(XLOG_DEBUG, "arg[%ld] = '%s'", (long) (cp - xivec), *cp);
163       cp++;
164     }
165   }
166
167   if (xivec[0] == 0 || xivec[1] == 0) {
168     errno = EINVAL;
169     plog(XLOG_USER, "1st/2nd args missing to (un)mount program");
170   } else {
171     (void) execv(xivec[0], xivec + 1);
172     error = errno;
173     plog(XLOG_ERROR, "exec failed: %m");
174     errno = error;
175   }
176
177 out:
178   /*
179    * Save error number
180    */
181   error = errno;
182
183   /*
184    * Free allocate memory
185    */
186   XFREE(info);
187   XFREE(xivec);
188
189   /*
190    * Return error
191    */
192   return error;
193 }
194
195
196 static int
197 amfs_program_mount(am_node *am, mntfs *mf)
198 {
199   return amfs_program_exec(mf->mf_fo->opt_mount);
200 }
201
202
203 static int
204 amfs_program_umount(am_node *am, mntfs *mf)
205 {
206   return amfs_program_exec((char *) mf->mf_private);
207 }