1/* 2 * Copyright (c) 1997-2014 Erez Zadok 3 * Copyright (c) 1990 Jan-Simon Pendry 4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5 * Copyright (c) 1990 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/restart.c 37 * 38 */ 39 40#ifdef HAVE_CONFIG_H 41# include <config.h> 42#endif /* HAVE_CONFIG_H */ 43#include <am_defs.h> 44#include <amd.h> 45 46 47static void 48restart_fake_mntfs(mntent_t *me, am_ops *fs_ops) 49{ 50 mntfs *mf; 51 am_opts mo; 52 char *cp; 53 54 /* 55 * Partially fake up an opts structure 56 */ 57 memset(&mo, 0, sizeof(mo)); 58 mo.opt_rhost = NULL; 59 mo.opt_rfs = NULL; 60 cp = strchr(me->mnt_fsname, ':'); 61 if (cp) { 62 *cp = '\0'; 63 mo.opt_rhost = xstrdup(me->mnt_fsname); 64 mo.opt_rfs = xstrdup(cp + 1); 65 *cp = ':'; 66 } else if (STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) { 67 /* 68 * Hacky workaround for mnttab NFS entries that only list the server 69 */ 70 plog(XLOG_WARNING, "NFS server entry assumed to be %s:/", me->mnt_fsname); 71 mo.opt_rhost = xstrdup(me->mnt_fsname); 72 mo.opt_rfs = xstrdup("/"); 73 me->mnt_fsname = str3cat(me->mnt_fsname, mo.opt_rhost, ":", "/"); 74 } 75 mo.opt_fs = me->mnt_dir; 76 mo.opt_opts = me->mnt_opts; 77 78 /* 79 * Make a new mounted filesystem 80 */ 81 mf = find_mntfs(fs_ops, &mo, me->mnt_dir, 82 me->mnt_fsname, "", me->mnt_opts, ""); 83 if (mf->mf_refc == 1) { 84 mf->mf_flags |= MFF_RESTART | MFF_MOUNTED; 85 mf->mf_error = 0; /* Already mounted correctly */ 86 /* 87 * Only timeout non-NFS entries 88 */ 89 if (!STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) 90 mf->mf_flags |= MFF_RSTKEEP; 91 if (fs_ops->fs_init) { 92 /* 93 * Don't care whether this worked since 94 * it is checked again when the fs is 95 * inherited. 96 */ 97 (void) (*fs_ops->fs_init) (mf); 98 } 99 plog(XLOG_INFO, "%s restarted fstype %s on %s, flags 0x%x", 100 me->mnt_fsname, fs_ops->fs_type, me->mnt_dir, mf->mf_flags); 101 } else { 102 /* Something strange happened - two mounts at the same place! */ 103 free_mntfs(mf); 104 } 105 /* 106 * Clean up mo 107 */ 108 XFREE(mo.opt_rhost); 109 XFREE(mo.opt_rfs); 110} 111 112 113/* 114 * Handle an amd restart. 115 * 116 * Scan through the mount list finding all "interesting" mount points. 117 * Next hack up partial data structures and add the mounted file 118 * system to the list of known filesystems. 119 * 120 * This module relies on internal details of other components. If 121 * you change something else make *sure* restart() still works. 122 */ 123void 124restart(void) 125{ 126 mntlist *ml, *mlp; 127 128 /* 129 * Read the existing mount table. For each entry, find nfs, ufs or auto 130 * mounts and create a partial am_node to represent it. 131 */ 132 for (mlp = ml = read_mtab("restart", mnttab_file_name); 133 mlp; 134 mlp = mlp->mnext) { 135 mntent_t *me = mlp->mnt; 136 am_ops *fs_ops = NULL; 137 138 if (STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) { 139 /* 140 * NFS entry, or possibly an Amd entry... 141 * The mnt_fsname for daemon mount points is 142 * host:(pidXXX) 143 * or (seen on Solaris) 144 * host:daemon(pidXXX) 145 */ 146 char *colon = strchr(me->mnt_fsname, ':'); 147 if (colon && strstr(colon, "(pid")) 148 continue; 149 } 150 151 /* Search for the correct filesystem ops */ 152 fs_ops = ops_search(me->mnt_type); 153 154 /* 155 * Catch everything else with symlinks to 156 * avoid recursive mounts. This is debatable... 157 */ 158 if (!fs_ops) 159 fs_ops = &amfs_link_ops; 160 161 restart_fake_mntfs(me, fs_ops); 162 } 163 164 /* 165 * Free the mount list 166 */ 167 free_mntlist(ml); 168} 169 170 171/* 172 * Handle an amd restart for amd's own mount points. 173 * 174 * Scan through the mount list finding all daemon mount points 175 * (determined by the presence of a pid inside the mount info). 176 * Next hack up partial data structures and add the mounted file 177 * system to the list of known filesystems. 178 * 179 * This module relies on internal details of other components. If 180 * you change something else make *sure* restart() still works. 181 */ 182void 183restart_automounter_nodes(void) 184{ 185 mntlist *ml, *mlp; 186 /* reasonably sized list of restarted nfs ports */ 187 u_short old_ports[256]; 188 189 memset((voidp) &old_ports, 0, sizeof(u_short) * 256); 190 191 /* 192 * Read the existing mount table. For each entry, find nfs, ufs or auto 193 * mounts and create a partial am_node to represent it. 194 */ 195 for (mlp = ml = read_mtab("restart", mnttab_file_name); 196 mlp; 197 mlp = mlp->mnext) { 198 mntent_t *me = mlp->mnt; 199 am_ops *fs_ops = NULL; 200 char *colon; 201 long pid; 202 u_short port; 203 int err; 204 205 if (!STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) 206 continue; /* to next mlp */ 207 /* 208 * NFS entry, or possibly an Amd entry... 209 * The mnt_fsname for daemon mount points is 210 * host:(pidXXX) 211 * or (seen on Solaris) 212 * host:daemon(pidXXX) 213 */ 214 colon = strchr(me->mnt_fsname, ':'); 215 if (!colon || !strstr(colon, "(pid")) 216 continue; 217 /* if got here, then we matched an existing Amd mount point */ 218 err = 1; 219 220 plog(XLOG_WARNING, "%s is an existing automount point", me->mnt_dir); 221 222 /* Is the old automounter still alive? */ 223 if (sscanf(colon, "%*[^(](pid%ld%*[,)]", &pid) != 1) { 224 plog(XLOG_WARNING, "Can't parse pid in %s", me->mnt_fsname); 225 goto give_up; 226 } 227 if (kill(pid, 0) != -1 || errno != ESRCH) { 228 plog(XLOG_WARNING, "Automounter (pid: %ld) still alive", pid); 229 goto give_up; 230 } 231 232 /* 233 * Do we have a map for this mount point? Who cares, we'll restart 234 * anyway -- getting ESTALE is way better than hanging. 235 */ 236 237 /* Can we restart it? Only if it tells us what port it was using... */ 238 if (sscanf(colon, "%*[^,],port%hu)", &port) != 1) { 239 plog(XLOG_WARNING, "No port specified for %s", me->mnt_fsname); 240 goto give_up; 241 } 242 243 /* Maybe we already own that port... */ 244 if (port != nfs_port) { 245 int i; 246 for (i = 0; i < 256; i++) { 247 if (old_ports[i] == port || 248 old_ports[i] == 0) 249 break; 250 } 251 if (i == 256) { 252 plog(XLOG_WARNING, "Too many open ports (256)"); 253 goto give_up; 254 } 255 256 if (old_ports[i] == 0) { 257 int soNFS; 258 SVCXPRT *nfsxprt; 259 if (create_nfs_service(&soNFS, &port, &nfsxprt, nfs_dispatcher, 260 get_nfs_dispatcher_version(nfs_dispatcher)) != 0) { 261 plog(XLOG_WARNING, "Can't bind to port %u", port); 262 goto give_up; 263 } 264 old_ports[i] = nfs_port = port; 265 } 266 } 267 err = 0; 268 269 give_up: 270 if (err) { 271 plog(XLOG_WARNING, "Can't restart %s, leaving it alone", me->mnt_dir); 272 fs_ops = &amfs_link_ops; 273 } else { 274 fs_ops = &amfs_toplvl_ops; 275 } 276 277 restart_fake_mntfs(me, fs_ops); 278 } /* end of "for (mlp" */ 279 280 /* free the mount list */ 281 free_mntlist(ml); 282} 283