1 /* $NetBSD: amfs_program.c,v 1.1.1.2 2009/03/20 20:26:49 christos Exp $ */ 2 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. 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. 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. 28 * 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. 40 * 41 * 42 * File: am-utils/amd/amfs_program.c 43 * 44 */ 45 46 /* 47 * Program file system 48 */ 49 50 #ifdef HAVE_CONFIG_H 51 # include <config.h> 52 #endif /* HAVE_CONFIG_H */ 53 #include <am_defs.h> 54 #include <amd.h> 55 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); 61 62 /* 63 * Ops structure 64 */ 65 am_ops amfs_program_ops = 66 { 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 */ 84 }; 85 86 87 /* 88 * Execute needs a mount and unmount command. 89 */ 90 static char * 91 amfs_program_match(am_opts *fo) 92 { 93 char *prog; 94 95 if (fo->opt_unmount && fo->opt_umount) { 96 plog(XLOG_ERROR, "program: cannot specify both unmount and umount options"); 97 return 0; 98 } 99 if (!fo->opt_mount) { 100 plog(XLOG_ERROR, "program: must specify mount command"); 101 return 0; 102 } 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); 107 } 108 prog = strchr(fo->opt_mount, ' '); 109 110 return strdup(prog ? prog + 1 : fo->opt_mount); 111 } 112 113 114 static int 115 amfs_program_init(mntfs *mf) 116 { 117 /* check if already saved value */ 118 if (mf->mf_private != NULL) 119 return 0; 120 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; 127 128 return 0; 129 } 130 131 132 static int 133 amfs_program_exec(char *info) 134 { 135 char **xivec; 136 int error; 137 138 /* 139 * Split copy of command info string 140 */ 141 info = strdup(info); 142 if (info == 0) 143 return ENOBUFS; 144 xivec = strsplit(info, ' ', '\''); 145 146 /* 147 * Put stdout to stderr 148 */ 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)); 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 } 176 177 /* 178 * Save error number 179 */ 180 error = errno; 181 plog(XLOG_ERROR, "exec failed: %m"); 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 } 208