xref: /original-bsd/usr.sbin/amd/amd/umount_fs.c (revision c10fb627)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)umount_fs.c	5.3 (Berkeley) 05/12/91
13  *
14  * $Id: umount_fs.c,v 5.2.1.3 91/05/07 22:18:39 jsp Alpha $
15  *
16  */
17 
18 #include "am.h"
19 
20 #ifdef NEED_UMOUNT_BSD
21 
22 #include <sys/mount.h>		/* For MNT_NOFORCE */
23 
24 int umount_fs P((char *fs_name));
25 int umount_fs(fs_name)
26 char *fs_name;
27 {
28 	int error;
29 
30 eintr:
31 	error = unmount(fs_name, MNT_NOFORCE);
32 	if (error < 0)
33 		error = errno;
34 
35 	switch (error) {
36 	case EINVAL:
37 	case ENOTBLK:
38 	case ENOENT:
39 		plog(XLOG_WARNING, "unmount: %s is not mounted", fs_name);
40 		error = 0;	/* Not really an error */
41 		break;
42 
43 	case EINTR:
44 #ifdef DEBUG
45 		/* not sure why this happens, but it does.  ask kirk one day... */
46 		dlog("%s: unmount: %m", fs_name);
47 #endif /* DEBUG */
48 		goto eintr;
49 
50 #ifdef DEBUG
51 	default:
52 		dlog("%s: unmount: %m", fs_name);
53 		break;
54 #endif /* DEBUG */
55 	}
56 
57 	return error;
58 }
59 
60 #endif /* NEED_UMOUNT_BSD */
61 
62 #ifdef NEED_UMOUNT_FS
63 
64 int umount_fs(fs_name)
65 char *fs_name;
66 {
67 	mntlist *mlist, *mp, *mp_save = 0;
68 	int error = 0;
69 
70 	mp = mlist = read_mtab(fs_name);
71 
72 	/*
73 	 * Search the mount table looking for
74 	 * the correct (ie last) matching entry
75 	 */
76 	while (mp) {
77 		if (strcmp(mp->mnt->mnt_fsname, fs_name) == 0 ||
78 				strcmp(mp->mnt->mnt_dir, fs_name) == 0)
79 			mp_save = mp;
80 		mp = mp->mnext;
81 	}
82 
83 	if (mp_save) {
84 #ifdef DEBUG
85 		dlog("Trying unmount(%s)", mp_save->mnt->mnt_dir);
86 #endif /* DEBUG */
87 		if (UNMOUNT_TRAP(mp_save->mnt) < 0) {
88 			switch (error = errno) {
89 			case EINVAL:
90 			case ENOTBLK:
91 				plog(XLOG_WARNING, "unmount: %s is not mounted", mp_save->mnt->mnt_dir);
92 				error = 0;	/* Not really an error */
93 				break;
94 
95 			case ENOENT:
96 				plog(XLOG_ERROR, "mount point %s: %m", mp_save->mnt->mnt_dir);
97 				break;
98 
99 			default:
100 #ifdef DEBUG
101 				dlog("%s: unmount: %m", mp_save->mnt->mnt_dir);
102 #endif /* DEBUG */
103 				break;
104 			}
105 		}
106 
107 #ifdef UPDATE_MTAB
108 		if (!error) {
109 			mnt_free(mp_save->mnt);
110 			mp_save->mnt = 0;
111 
112 			rewrite_mtab(mlist);
113 		}
114 #endif /* UPDATE_MTAB */
115 	} else {
116 		plog(XLOG_ERROR, "Couldn't find how to unmount %s", fs_name);
117 		/*
118 		 * Assume it is already unmounted
119 		 */
120 		error = 0;
121 	}
122 
123 	free_mntlist(mlist);
124 
125 	return error;
126 }
127 
128 #endif /* NEED_UMOUNT_FS */
129