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