Home | History | Annotate | Download | only in umount
      1 /*
      2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * You should have received a copy of the GNU General Public License along
     13  * with this program; if not, write the Free Software Foundation, Inc.,
     14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     15  *
     16  *    AUTHOR		: Nirmala Devi Dhanasekar <nirmala.devi (at) wipro.com>
     17  *
     18  *    DESCRIPTION
     19  *	Verify that umount(2) returns -1 and sets errno to  EPERM if the user
     20  *	is not the super-user.
     21  *
     22  * RESTRICTIONS
     23  *	test must be run with the -D option
     24  *	test doesn't support -c option to run it in parallel, as mount
     25  *	syscall is not supposed to run in parallel.
     26  */
     27 
     28 #include <errno.h>
     29 #include <sys/mount.h>
     30 #include <sys/types.h>
     31 #include <sys/stat.h>
     32 #include <sys/fcntl.h>
     33 #include <sys/wait.h>
     34 #include <pwd.h>
     35 
     36 #include "test.h"
     37 #include "safe_macros.h"
     38 
     39 static void setup(void);
     40 static void cleanup(void);
     41 
     42 char *TCID = "umount03";
     43 
     44 #define FSTYPE_LEN	20
     45 #define DIR_MODE	S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
     46 #define MNTPOINT	"mntpoint"
     47 
     48 static const char *device;
     49 static int mount_flag;
     50 
     51 int TST_TOTAL = 1;
     52 
     53 int main(int ac, char **av)
     54 {
     55 	int lc;
     56 
     57 	tst_parse_opts(ac, av, NULL, NULL);
     58 
     59 	setup();
     60 
     61 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     62 		tst_count = 0;
     63 
     64 		TEST(umount(MNTPOINT));
     65 
     66 		if (TEST_RETURN != -1) {
     67 			tst_resm(TFAIL, "umout() didn't fail (ret=%ld)",
     68 				 TEST_RETURN);
     69 			continue;
     70 		}
     71 
     72 		if (TEST_ERRNO == EPERM) {
     73 			tst_resm(TPASS | TTERRNO, "umount() failed with EPERM");
     74 		} else {
     75 			tst_resm(TFAIL | TTERRNO,
     76 			         "umount() did not fail with EPERM(%d)", EPERM);
     77 		}
     78 	}
     79 
     80 	cleanup();
     81 	tst_exit();
     82 }
     83 
     84 static void setup(void)
     85 {
     86 	const char *fs_type;
     87 	struct passwd *ltpuser;
     88 
     89 	tst_sig(FORK, DEF_HANDLER, cleanup);
     90 
     91 	tst_require_root();
     92 
     93 	ltpuser = SAFE_GETPWNAM(NULL, "nobody");
     94 
     95 	tst_tmpdir();
     96 
     97 	fs_type = tst_dev_fs_type();
     98 	device = tst_acquire_device(cleanup);
     99 	if (!device)
    100 		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
    101 
    102 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
    103 
    104 	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
    105 
    106 	if (mount(device, MNTPOINT, fs_type, 0, NULL))
    107 		tst_brkm(TBROK | TERRNO, cleanup, "mount() failed");
    108 	mount_flag = 1;
    109 
    110 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
    111 	TEST_PAUSE;
    112 }
    113 
    114 static void cleanup(void)
    115 {
    116 	if (seteuid(0))
    117 		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
    118 
    119 	if (mount_flag && tst_umount(MNTPOINT))
    120 		tst_resm(TWARN | TERRNO, "umount() failed");
    121 
    122 	if (device)
    123 		tst_release_device(device);
    124 
    125 	tst_rmdir();
    126 }
    127