138494Sobrien/*
2310490Scy * Copyright (c) 1997-2014 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.
19310490Scy * 3. Neither the name of the University nor the names of its contributors
2038494Sobrien *    may be used to endorse or promote products derived from this software
2138494Sobrien *    without specific prior written permission.
2238494Sobrien *
2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3338494Sobrien * SUCH DAMAGE.
3438494Sobrien *
3538494Sobrien *
36174294Sobrien * File: am-utils/conf/umount/umount_bsd44.c
3738494Sobrien *
3838494Sobrien */
3938494Sobrien
4038494Sobrien/*
4138494Sobrien * Unmounting filesystems under BSD 4.4.
4238494Sobrien */
4338494Sobrien
4438494Sobrien#ifdef HAVE_CONFIG_H
4538494Sobrien# include <config.h>
4638494Sobrien#endif /* HAVE_CONFIG_H */
4738494Sobrien#include <am_defs.h>
4838494Sobrien#include <amu.h>
4938494Sobrien
5038494Sobrien
5138494Sobrienint
52174294Sobrienumount_fs(char *mntdir, const char *mnttabname, u_int unmount_flags)
5338494Sobrien{
5438494Sobrien  int error;
5538494Sobrien
5638494Sobrieneintr:
57174294Sobrien  error = unmount(mntdir, 0);
5838494Sobrien  if (error < 0)
5938494Sobrien    error = errno;
6038494Sobrien
6138494Sobrien  switch (error) {
6238494Sobrien  case EINVAL:
6338494Sobrien  case ENOTBLK:
6438494Sobrien  case ENOENT:
65174294Sobrien    plog(XLOG_WARNING, "unmount: %s is not mounted", mntdir);
6638494Sobrien    error = 0;			/* Not really an error */
6738494Sobrien    break;
6838494Sobrien
6938494Sobrien  case EINTR:
7038494Sobrien    /* not sure why this happens, but it does.  ask kirk one day... */
71174294Sobrien    dlog("%s: unmount: %m", mntdir);
7238494Sobrien    goto eintr;
7338494Sobrien
74174294Sobrien#ifdef MNT2_GEN_OPT_FORCE
75174294Sobrien  case EBUSY:
76174294Sobrien  case EIO:
77174294Sobrien  case ESTALE:
78174294Sobrien    /* caller determines if forced unmounts should be used */
79174294Sobrien    if (unmount_flags & AMU_UMOUNT_FORCE) {
80174294Sobrien      error = umount2_fs(mntdir, unmount_flags);
81174294Sobrien      if (error < 0)
82174294Sobrien	error = errno;
83174294Sobrien      else
84174294Sobrien	return error;
85174294Sobrien    }
86174294Sobrien    /* fallthrough */
87174294Sobrien#endif /* MNT2_GEN_OPT_FORCE */
88174294Sobrien
8938494Sobrien  default:
90174294Sobrien    dlog("%s: unmount: %m", mntdir);
9138494Sobrien    break;
9238494Sobrien  }
9338494Sobrien
9438494Sobrien  return error;
9538494Sobrien}
96174294Sobrien
97174294Sobrien
98174294Sobrien#ifdef MNT2_GEN_OPT_FORCE
99174294Sobrien/* force unmount, no questions asked, without touching mnttab file */
100174294Sobrienint
101174294Sobrienumount2_fs(const char *mntdir, u_int unmount_flags)
102174294Sobrien{
103174294Sobrien  int error = 0;
104310490Scy
105174294Sobrien  if (unmount_flags & AMU_UMOUNT_FORCE) {
106174294Sobrien    plog(XLOG_INFO, "umount2_fs: trying unmount/forced on %s", mntdir);
107174294Sobrien    error = unmount(mntdir, MNT2_GEN_OPT_FORCE);
108174294Sobrien    if (error < 0 && (errno == EINVAL || errno == ENOENT))
109174294Sobrien      error = 0;		/* ignore EINVAL/ENOENT */
110174294Sobrien    if (error < 0)
111174294Sobrien      plog(XLOG_WARNING, "%s: unmount/force: %m", mntdir);
112174294Sobrien    else
113174294Sobrien      dlog("%s: unmount/force: OK", mntdir);
114174294Sobrien  }
115174294Sobrien  return error;
116174294Sobrien}
117174294Sobrien#endif /* MNT2_GEN_OPT_FORCE */
118