vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 16 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 17 | * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 19 | * DESCRIPTION |
| 20 | * Check for basic errors returned by umount(2) system call. |
| 21 | * |
| 22 | * Verify that umount(2) returns -1 and sets errno to |
| 23 | * |
| 24 | * 1) EBUSY if it cannot be umounted, because dir is still busy. |
| 25 | * 2) EFAULT if specialfile or device file points to invalid address space. |
| 26 | * 3) ENOENT if pathname was empty or has a nonexistent component. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * 4) EINVAL if specialfile or device is invalid or not a mount point. |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 28 | * 5) ENAMETOOLONG if pathname was longer than MAXPATHLEN. |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 29 | *****************************************************************************/ |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <sys/mount.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/fcntl.h> |
| 36 | #include <pwd.h> |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 37 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 38 | #include "test.h" |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 39 | #include "safe_macros.h" |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 40 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 41 | static void setup(void); |
| 42 | static void cleanup(void); |
| 43 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 44 | char *TCID = "umount02"; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 45 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 46 | #define DIR_MODE S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH |
| 47 | #define FILE_MODE S_IRWXU | S_IRWXG | S_IRWXO |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 48 | #define MNTPOINT "mntpoint" |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 50 | static char long_path[PATH_MAX + 2]; |
| 51 | static int mount_flag; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 52 | static int fd; |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 53 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 54 | static const char *device; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 55 | |
| 56 | static struct test_case_t { |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 57 | char *err_desc; |
| 58 | char *mntpoint; |
| 59 | int exp_errno; |
| 60 | char *exp_retval; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 61 | } testcases[] = { |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 62 | {"Already mounted/busy", MNTPOINT, EBUSY, "EBUSY"}, |
| 63 | {"Invalid address space", NULL, EFAULT, "EFAULT"}, |
| 64 | {"Directory not found", "nonexistent", ENOENT, "ENOENT"}, |
| 65 | {"Invalid device", "./", EINVAL, "EINVAL"}, |
| 66 | {"Pathname too long", long_path, ENAMETOOLONG, "ENAMETOOLONG"} |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 69 | int TST_TOTAL = ARRAY_SIZE(testcases); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int main(int ac, char **av) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 72 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 73 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 74 | const char *msg; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, NULL, NULL))) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 78 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 79 | setup(); |
| 80 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 82 | tst_count = 0; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 83 | |
| 84 | for (i = 0; i < TST_TOTAL; ++i) { |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 85 | TEST(umount(testcases[i].mntpoint)); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 86 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 87 | if ((TEST_RETURN == -1) && |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 88 | (TEST_ERRNO == testcases[i].exp_errno)) { |
| 89 | tst_resm(TPASS, "umount(2) expected failure; " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | "Got errno - %s : %s", |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 91 | testcases[i].exp_retval, |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 92 | testcases[i].err_desc); |
| 93 | } else { |
| 94 | tst_resm(TFAIL, "umount(2) failed to produce " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | "expected error; %d, errno:%s got %d", |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 96 | testcases[i].exp_errno, |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 97 | testcases[i].exp_retval, TEST_ERRNO); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 98 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 101 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 102 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 103 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 104 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 105 | |
Cyril Hrubis | 7224b78 | 2013-08-08 18:03:54 +0200 | [diff] [blame] | 106 | static void setup(void) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 107 | { |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 108 | const char *fs_type; |
| 109 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 110 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 111 | |
Cyril Hrubis | 7224b78 | 2013-08-08 18:03:54 +0200 | [diff] [blame] | 112 | tst_require_root(NULL); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 113 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 114 | tst_tmpdir(); |
| 115 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 116 | fs_type = tst_dev_fs_type(); |
| 117 | device = tst_acquire_device(cleanup); |
| 118 | |
| 119 | if (!device) |
| 120 | tst_brkm(TCONF, cleanup, "Failed to obtain block device"); |
| 121 | |
| 122 | tst_mkfs(cleanup, device, fs_type, NULL); |
| 123 | |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 124 | memset(long_path, 'a', PATH_MAX + 1); |
| 125 | |
| 126 | SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE); |
| 127 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 128 | if (mount(device, MNTPOINT, fs_type, 0, NULL)) |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 129 | tst_brkm(TBROK | TERRNO, cleanup, "mount() failed"); |
| 130 | mount_flag = 1; |
| 131 | |
| 132 | fd = SAFE_OPEN(cleanup, MNTPOINT "/file", O_CREAT | O_RDWR); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 133 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 134 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | 7224b78 | 2013-08-08 18:03:54 +0200 | [diff] [blame] | 137 | static void cleanup(void) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 138 | { |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 139 | if (fd > 0 && close(fd)) |
| 140 | tst_resm(TWARN | TERRNO, "Failed to close file"); |
| 141 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 142 | if (mount_flag && tst_umount(MNTPOINT)) |
Cyril Hrubis | c437b62 | 2014-08-14 11:01:33 +0200 | [diff] [blame] | 143 | tst_resm(TWARN | TERRNO, "umount() failed"); |
| 144 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 145 | if (device) |
| 146 | tst_release_device(NULL, device); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 147 | |
Cyril Hrubis | be070ad | 2014-08-14 11:06:05 +0200 | [diff] [blame] | 148 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 149 | } |