138494Sobrien/*
2174294Sobrien * Copyright (c) 1997-2006 Erez Zadok
338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1990 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174294Sobrien * File: am-utils/amd/restart.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien#ifdef HAVE_CONFIG_H
4538494Sobrien# include <config.h>
4638494Sobrien#endif /* HAVE_CONFIG_H */
4738494Sobrien#include <am_defs.h>
4838494Sobrien#include <amd.h>
4938494Sobrien
5038494Sobrien
51174294Sobrienstatic void
52174294Sobrienrestart_fake_mntfs(mntent_t *me, am_ops *fs_ops)
53174294Sobrien{
54174294Sobrien  mntfs *mf;
55174294Sobrien  am_opts mo;
56174294Sobrien  char *cp;
57174294Sobrien
58174294Sobrien  /*
59174294Sobrien   * Partially fake up an opts structure
60174294Sobrien   */
61174294Sobrien  memset(&mo, 0, sizeof(mo));
62174294Sobrien  mo.opt_rhost = 0;
63174294Sobrien  mo.opt_rfs = 0;
64174294Sobrien  cp = strchr(me->mnt_fsname, ':');
65174294Sobrien  if (cp) {
66174294Sobrien    *cp = '\0';
67174294Sobrien    mo.opt_rhost = strdup(me->mnt_fsname);
68174294Sobrien    mo.opt_rfs = strdup(cp + 1);
69174294Sobrien    *cp = ':';
70174294Sobrien  } else if (STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) {
71174294Sobrien    /*
72174294Sobrien     * Hacky workaround for mnttab NFS entries that only list the server
73174294Sobrien     */
74174294Sobrien    plog(XLOG_WARNING, "NFS server entry assumed to be %s:/", me->mnt_fsname);
75174294Sobrien    mo.opt_rhost = strdup(me->mnt_fsname);
76174294Sobrien    mo.opt_rfs = strdup("/");
77174294Sobrien    me->mnt_fsname = str3cat(me->mnt_fsname, mo.opt_rhost, ":", "/");
78174294Sobrien  }
79174294Sobrien  mo.opt_fs = me->mnt_dir;
80174294Sobrien  mo.opt_opts = me->mnt_opts;
81174294Sobrien
82174294Sobrien  /*
83174294Sobrien   * Make a new mounted filesystem
84174294Sobrien   */
85174294Sobrien  mf = find_mntfs(fs_ops, &mo, me->mnt_dir,
86174294Sobrien		  me->mnt_fsname, "", me->mnt_opts, "");
87174294Sobrien  if (mf->mf_refc == 1) {
88174294Sobrien    mf->mf_flags |= MFF_RESTART | MFF_MOUNTED;
89174294Sobrien    mf->mf_error = 0;		     /* Already mounted correctly */
90174294Sobrien    mf->mf_fo = 0;
91174294Sobrien    /*
92174294Sobrien     * Only timeout non-NFS entries
93174294Sobrien     */
94174294Sobrien    if (!STREQ(me->mnt_type, MNTTAB_TYPE_NFS))
95174294Sobrien      mf->mf_flags |= MFF_RSTKEEP;
96174294Sobrien    if (fs_ops->fs_init) {
97174294Sobrien      /*
98174294Sobrien       * Don't care whether this worked since
99174294Sobrien       * it is checked again when the fs is
100174294Sobrien       * inherited.
101174294Sobrien       */
102174294Sobrien      (void) (*fs_ops->fs_init) (mf);
103174294Sobrien    }
104174294Sobrien    plog(XLOG_INFO, "%s restarted fstype %s on %s, flags 0x%x",
105174294Sobrien	 me->mnt_fsname, fs_ops->fs_type, me->mnt_dir, mf->mf_flags);
106174294Sobrien  } else {
107174294Sobrien    /* Something strange happened - two mounts at the same place! */
108174294Sobrien    free_mntfs(mf);
109174294Sobrien  }
110174294Sobrien  /*
111174294Sobrien   * Clean up mo
112174294Sobrien   */
113174294Sobrien  if (mo.opt_rhost)
114174294Sobrien    XFREE(mo.opt_rhost);
115174294Sobrien  if (mo.opt_rfs)
116174294Sobrien    XFREE(mo.opt_rfs);
117174294Sobrien}
118174294Sobrien
119174294Sobrien
12038494Sobrien/*
12138494Sobrien * Handle an amd restart.
12238494Sobrien *
12338494Sobrien * Scan through the mount list finding all "interesting" mount points.
12438494Sobrien * Next hack up partial data structures and add the mounted file
125174294Sobrien * system to the list of known filesystems.
12638494Sobrien *
12738494Sobrien * This module relies on internal details of other components.  If
12838494Sobrien * you change something else make *sure* restart() still works.
12938494Sobrien */
13038494Sobrienvoid
13138494Sobrienrestart(void)
13238494Sobrien{
13338494Sobrien  mntlist *ml, *mlp;
13438494Sobrien
13538494Sobrien  /*
136174294Sobrien   * Read the existing mount table.  For each entry, find nfs, ufs or auto
137174294Sobrien   * mounts and create a partial am_node to represent it.
13838494Sobrien   */
13938494Sobrien  for (mlp = ml = read_mtab("restart", mnttab_file_name);
14038494Sobrien       mlp;
14138494Sobrien       mlp = mlp->mnext) {
14238494Sobrien    mntent_t *me = mlp->mnt;
14338494Sobrien    am_ops *fs_ops = 0;
144174294Sobrien
145174294Sobrien    if (STREQ(me->mnt_type, MNTTAB_TYPE_NFS)) {
14638494Sobrien      /*
14738494Sobrien       * NFS entry, or possibly an Amd entry...
14838494Sobrien       * The mnt_fsname for daemon mount points is
14938494Sobrien       * 	host:(pidXXX)
15038494Sobrien       * or (seen on Solaris)
15138494Sobrien       *        host:daemon(pidXXX)
15238494Sobrien       */
15338494Sobrien      char *colon = strchr(me->mnt_fsname, ':');
154174294Sobrien      if (colon && strstr(colon, "(pid"))
155174294Sobrien	continue;
156174294Sobrien    }
15738494Sobrien
158174294Sobrien    /* Search for the correct filesystem ops */
159174294Sobrien    fs_ops = ops_search(me->mnt_type);
160174294Sobrien
161174294Sobrien    /*
162174294Sobrien     * Catch everything else with symlinks to
163174294Sobrien     * avoid recursive mounts.  This is debatable...
164174294Sobrien     */
165174294Sobrien    if (!fs_ops)
16638494Sobrien      fs_ops = &amfs_link_ops;
167174294Sobrien
168174294Sobrien    restart_fake_mntfs(me, fs_ops);
169174294Sobrien  }
170174294Sobrien
171174294Sobrien  /*
172174294Sobrien   * Free the mount list
173174294Sobrien   */
174174294Sobrien  free_mntlist(ml);
175174294Sobrien}
176174294Sobrien
177174294Sobrien
178174294Sobrien/*
179174294Sobrien * Handle an amd restart for amd's own mount points.
180174294Sobrien *
181174294Sobrien * Scan through the mount list finding all daemon mount points
182174294Sobrien * (determined by the presence of a pid inside the mount info).
183174294Sobrien * Next hack up partial data structures and add the mounted file
184174294Sobrien * system to the list of known filesystems.
185174294Sobrien *
186174294Sobrien * This module relies on internal details of other components.  If
187174294Sobrien * you change something else make *sure* restart() still works.
188174294Sobrien */
189174294Sobrienvoid
190174294Sobrienrestart_automounter_nodes(void)
191174294Sobrien{
192174294Sobrien  mntlist *ml, *mlp;
193174294Sobrien  /* reasonably sized list of restarted nfs ports */
194174294Sobrien  u_short old_ports[256];
195174294Sobrien
196174294Sobrien  memset((voidp) &old_ports, 0, sizeof(u_short) * 256);
197174294Sobrien
198174294Sobrien  /*
199174294Sobrien   * Read the existing mount table.  For each entry, find nfs, ufs or auto
200174294Sobrien   * mounts and create a partial am_node to represent it.
201174294Sobrien   */
202174294Sobrien  for (mlp = ml = read_mtab("restart", mnttab_file_name);
203174294Sobrien       mlp;
204174294Sobrien       mlp = mlp->mnext) {
205174294Sobrien    mntent_t *me = mlp->mnt;
206174294Sobrien    am_ops *fs_ops = 0;
207174294Sobrien    char *colon;
208174294Sobrien    long pid;
209174294Sobrien    u_short port;
210174294Sobrien    int err;
211174294Sobrien
212174294Sobrien    if (!STREQ(me->mnt_type, MNTTAB_TYPE_NFS))
213174294Sobrien      continue;			/* to next mlp */
214174294Sobrien    /*
215174294Sobrien     * NFS entry, or possibly an Amd entry...
216174294Sobrien     * The mnt_fsname for daemon mount points is
217174294Sobrien     *	   host:(pidXXX)
218174294Sobrien     * or (seen on Solaris)
219174294Sobrien     *     host:daemon(pidXXX)
220174294Sobrien     */
221174294Sobrien    colon = strchr(me->mnt_fsname, ':');
222174294Sobrien    if (!colon || !strstr(colon, "(pid"))
223174294Sobrien      continue;
224174294Sobrien    /* if got here, then we matched an existing Amd mount point */
225174294Sobrien    err = 1;
226174294Sobrien
227174294Sobrien    plog(XLOG_WARNING, "%s is an existing automount point", me->mnt_dir);
228174294Sobrien
229174294Sobrien    /* Is the old automounter still alive? */
230174294Sobrien    if (sscanf(colon, "%*[^(](pid%ld%*[,)]", &pid) != 1) {
231174294Sobrien      plog(XLOG_WARNING, "Can't parse pid in %s", me->mnt_fsname);
232174294Sobrien      goto give_up;
23338494Sobrien    }
234174294Sobrien    if (kill(pid, 0) != -1 || errno != ESRCH) {
235174294Sobrien      plog(XLOG_WARNING, "Automounter (pid: %ld) still alive", pid);
236174294Sobrien      goto give_up;
237174294Sobrien    }
23838494Sobrien
23938494Sobrien    /*
240174294Sobrien     * Do we have a map for this mount point?  Who cares, we'll restart
241174294Sobrien     * anyway -- getting ESTALE is way better than hanging.
24238494Sobrien     */
24338494Sobrien
244174294Sobrien    /* Can we restart it? Only if it tells us what port it was using... */
245174294Sobrien    if (sscanf(colon, "%*[^,],port%hu)", &port) != 1) {
246174294Sobrien      plog(XLOG_WARNING, "No port specified for %s", me->mnt_fsname);
247174294Sobrien      goto give_up;
248174294Sobrien    }
249174294Sobrien
250174294Sobrien    /* Maybe we already own that port... */
251174294Sobrien    if (port != nfs_port) {
252174294Sobrien      int i;
253174294Sobrien      for (i = 0; i < 256; i++) {
254174294Sobrien	if (old_ports[i] == port ||
255174294Sobrien	    old_ports[i] == 0)
256174294Sobrien	  break;
25738494Sobrien      }
258174294Sobrien      if (i == 256) {
259174294Sobrien	plog(XLOG_WARNING, "Too many open ports (256)");
260174294Sobrien	goto give_up;
261174294Sobrien      }
26238494Sobrien
263174294Sobrien      if (old_ports[i] == 0) {
264174294Sobrien	int soNFS;
265174294Sobrien	SVCXPRT *nfsxprt;
266174294Sobrien	if (create_nfs_service(&soNFS, &port, &nfsxprt, nfs_program_2) != 0) {
267174294Sobrien	  plog(XLOG_WARNING, "Can't bind to port %u", port);
268174294Sobrien	  goto give_up;
26938494Sobrien	}
270174294Sobrien	old_ports[i] = nfs_port = port;
27138494Sobrien      }
27238494Sobrien    }
273174294Sobrien    err = 0;
27438494Sobrien
275174294Sobrien  give_up:
276174294Sobrien    if (err) {
277174294Sobrien      plog(XLOG_WARNING, "Can't restart %s, leaving it alone", me->mnt_dir);
278174294Sobrien      fs_ops = &amfs_link_ops;
279174294Sobrien    } else {
280174294Sobrien      fs_ops = &amfs_toplvl_ops;
281174294Sobrien    }
282174294Sobrien
283174294Sobrien    restart_fake_mntfs(me, fs_ops);
284174294Sobrien  } /* end of "for (mlp" */
285174294Sobrien
286174294Sobrien  /* free the mount list */
28738494Sobrien  free_mntlist(ml);
28838494Sobrien}
289