blob: b6de63075b58aea1a3dad42e491d6997d219cce6 [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
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., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
16 */
17/******************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
vapierd13d74b2006-02-11 07:24:00 +000019 * TEST IDENTIFIER : umount02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
vapierd13d74b2006-02-11 07:24:00 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
vapierd13d74b2006-02-11 07:24:00 +000023 * TEST TITLE : Test for checking basic error conditions for umount(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
vapierd13d74b2006-02-11 07:24:00 +000025 * TEST CASE TOTAL : 5
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
vapierd13d74b2006-02-11 07:24:00 +000027 * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
vapierd13d74b2006-02-11 07:24:00 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Check for basic errors returned by umount(2) system call.
35 *
36 * Verify that umount(2) returns -1 and sets errno to
37 *
38 * 1) EBUSY if it cannot be umounted, because dir is still busy.
39 * 2) EFAULT if specialfile or device file points to invalid address space.
40 * 3) ENOENT if pathname was empty or has a nonexistent component.
subrata_modak4bb656a2009-02-26 12:02:09 +000041 * 4) EINVAL if specialfile or device is invalid or not a mount point.
vapierd13d74b2006-02-11 07:24:00 +000042 * 5) ENAMETOOLONG if pathname was longer than MAXPATHLEN.
subrata_modakbdbaec52009-02-26 12:14:51 +000043 *
vapierd13d74b2006-02-11 07:24:00 +000044 * Setup:
45 * Setup signal handling.
46 * Create a mount point.
47 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000048 *
vapierd13d74b2006-02-11 07:24:00 +000049 * Test:
50 * Loop if the proper options are given.
51 * Do necessary setup for each test.
52 * Execute system call
53 * Check return code, if system call failed and errno == expected errno
54 * Issue sys call passed with expected return value and errno.
55 * Otherwise,
56 * Issue sys call failed to produce expected error.
57 * Do cleanup for each test.
subrata_modak4bb656a2009-02-26 12:02:09 +000058 *
vapierd13d74b2006-02-11 07:24:00 +000059 * Cleanup:
60 * Print errno log and/or timing stats if options given
61 * Delete the temporary directory(s)/file(s) created.
subrata_modak4bb656a2009-02-26 12:02:09 +000062 *
vapierd13d74b2006-02-11 07:24:00 +000063 * USAGE: <for command-line>
64 * umount02 [-T type] -D device [-e] [-i n] [-I x] [-p x] [-t]
65 * where, -T type : specifies the type of filesystem to
66 * be mounted. Default ext2.
67 * -D device : device to be mounted.
68 * -e : Turn on errno logging.
69 * -i n : Execute test n times.
70 * -I x : Execute test for x seconds.
71 * -p : Pause for SIGUSR1 before starting
72 * -P x : Pause for x seconds between iterations.
73 * -t : Turn on syscall timing.
74 *
75 * RESTRICTIONS
76 * test must be run with the -D option
77 * test doesn't support -c option to run it in parallel, as mount
78 * syscall is not supposed to run in parallel.
79 *****************************************************************************/
80
81#include <errno.h>
82#include <sys/mount.h>
83#include <sys/types.h>
84#include <sys/stat.h>
85#include <sys/fcntl.h>
86#include <pwd.h>
87#include "test.h"
88#include "usctest.h"
89
90static void help(void);
91static void setup(void);
92static void cleanup(void);
93
94static int setup_test(int, int);
95static int cleanup_test(int);
96
subrata_modak56207ce2009-03-23 13:35:39 +000097char *TCID = "umount02"; /* Test program identifier. */
98extern int Tst_count; /* TestCase counter for tst_* routine */
vapierd13d74b2006-02-11 07:24:00 +000099
100#define DEFAULT_FSTYPE "ext2"
101#define FSTYPE_LEN 20
102#define DIR_MODE S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
103#define FILE_MODE S_IRWXU | S_IRWXG | S_IRWXO
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105static char *Einval = "nonexixstent";
106static char Longpathname[PATH_MAX + 2];
107static char Path[PATH_MAX];
108static char *Type;
109static char *Fstype;
110static char *Device;
111static char *Mntpoint;
vapierd13d74b2006-02-11 07:24:00 +0000112static unsigned long Flag;
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114static int fd;
115static char mntpoint[PATH_MAX];
116static char *fstype;
117static char *device;
118static int Tflag = 0;
119static int Dflag = 0;
vapierd13d74b2006-02-11 07:24:00 +0000120
121static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000122 char *err_desc; /* error description */
123 int exp_errno; /* Expected error no */
124 char *exp_errval; /* Expected error value string */
vapierd13d74b2006-02-11 07:24:00 +0000125} testcases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000126 {
127 "Already mounted/busy", EBUSY, "EBUSY"}, {
128 "Invalid address space", EFAULT, "EFAULT"}, {
129 "Directory not found", ENOENT, "ENOENT"}, {
130 "Invalid device ", EINVAL, "EINVAL"}, {
131 "Pathname too long", ENAMETOOLONG, "ENAMETOOLONG"}
vapierd13d74b2006-02-11 07:24:00 +0000132};
133
134/* Total number of test cases. */
135int TST_TOTAL = sizeof(testcases) / sizeof(testcases[0]);
136
137static int exp_enos[] = { EBUSY, EINVAL, EFAULT, ENAMETOOLONG, ENOENT, 0 };
138
subrata_modak56207ce2009-03-23 13:35:39 +0000139static option_t options[] = { /* options supported by umount02 test */
140 {"T:", &Tflag, &fstype}, /* -T type of filesystem */
141 {"D:", &Dflag, &device}, /* -D device used for mounting */
142 {NULL, NULL, NULL}
vapierd13d74b2006-02-11 07:24:00 +0000143};
144
subrata_modak56207ce2009-03-23 13:35:39 +0000145int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +0000146{
subrata_modak56207ce2009-03-23 13:35:39 +0000147 int lc, i; /* loop counter */
148 char *msg; /* message returned from parse_opts */
vapierd13d74b2006-02-11 07:24:00 +0000149
150 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800151 if ((msg = parse_opts(ac, av, options, &help)) != NULL) {
vapierd13d74b2006-02-11 07:24:00 +0000152 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
153 tst_exit();
154 }
155
156 /* Check for mandatory option of the testcase */
157 if (Dflag == 0) {
158 tst_brkm(TBROK, NULL, "You must specifiy the device used for "
159 " mounting with -D option, Run '%s -h' for option "
160 " information.", TCID);
161 tst_exit();
162 }
163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 Type = (char *)malloc(FSTYPE_LEN);
vapierd13d74b2006-02-11 07:24:00 +0000165 if (!Type) {
166 tst_brkm(TBROK, NULL, "malloc - alloc of %d failed",
167 FSTYPE_LEN);
168 tst_exit();
169 }
170
171 if (Tflag == 1) {
172 strncpy(Type, fstype,
subrata_modak56207ce2009-03-23 13:35:39 +0000173 (FSTYPE_LEN <
subrata_modak344d5c32009-05-13 07:12:43 +0000174 (strlen(fstype)+1)) ? FSTYPE_LEN : (strlen(fstype)+1));
vapierd13d74b2006-02-11 07:24:00 +0000175 } else {
subrata_modak344d5c32009-05-13 07:12:43 +0000176 strncpy(Type, DEFAULT_FSTYPE, strlen(DEFAULT_FSTYPE)+1);
vapierd13d74b2006-02-11 07:24:00 +0000177 }
178
subrata_modak56207ce2009-03-23 13:35:39 +0000179 if (STD_COPIES != 1) {
vapierd13d74b2006-02-11 07:24:00 +0000180 tst_resm(TINFO, "-c option has no effect for this testcase - "
181 "%s doesn't allow running more than one instance "
182 "at a time", TCID);
183 STD_COPIES = 1;
184 }
185
186 /* perform global setup for test */
187 setup();
188
vapierd13d74b2006-02-11 07:24:00 +0000189 /* check looping state if -i option given */
190 for (lc = 0; TEST_LOOPING(lc); lc++) {
191
192 /* reset Tst_count in case we are looping. */
193 Tst_count = 0;
194
195 for (i = 0; i < TST_TOTAL; ++i) {
196
197 if (setup_test(i, lc)) {
198 tst_resm(TWARN, "Not able to test mount(2) for "
199 "error %s as setup failed",
200 testcases[i].exp_errval);
201 continue;
202 }
203
204 /* Call umount(2) to test different test conditions.
205 * verify that it fails with -1 return value and
206 * sets appropriate errno.*/
207
208 TEST(umount(Mntpoint));
209
210 /* check return code */
subrata_modak4bb656a2009-02-26 12:02:09 +0000211 if ((TEST_RETURN == -1) &&
vapierd13d74b2006-02-11 07:24:00 +0000212 (TEST_ERRNO == testcases[i].exp_errno)) {
213 tst_resm(TPASS, "umount(2) expected failure; "
subrata_modak56207ce2009-03-23 13:35:39 +0000214 "Got errno - %s : %s",
vapierd13d74b2006-02-11 07:24:00 +0000215 testcases[i].exp_errval,
216 testcases[i].err_desc);
217 } else {
218 tst_resm(TFAIL, "umount(2) failed to produce "
subrata_modak56207ce2009-03-23 13:35:39 +0000219 "expected error; %d, errno:%s got %d",
vapierd13d74b2006-02-11 07:24:00 +0000220 testcases[i].exp_errno,
221 testcases[i].exp_errval, TEST_ERRNO);
222 }
223
224 TEST_ERROR_LOG(TEST_ERRNO);
225
subrata_modak56207ce2009-03-23 13:35:39 +0000226 (void)cleanup_test(i);
vapierd13d74b2006-02-11 07:24:00 +0000227
subrata_modak56207ce2009-03-23 13:35:39 +0000228 } /* End of TEST CASE LOOPING. */
229 } /* End for TEST_LOOPING */
vapierd13d74b2006-02-11 07:24:00 +0000230
231 /* cleanup and exit */
232 cleanup();
233
subrata_modak56207ce2009-03-23 13:35:39 +0000234 /*NOTREACHED*/ return 0;
vapierd13d74b2006-02-11 07:24:00 +0000235
subrata_modak56207ce2009-03-23 13:35:39 +0000236} /* End main */
vapierd13d74b2006-02-11 07:24:00 +0000237
238/*
239 * int
240 * setup_test() - Setup function for test cases based on the error values
241 * to be returned.
242 */
subrata_modak56207ce2009-03-23 13:35:39 +0000243int setup_test(int i, int cnt)
vapierd13d74b2006-02-11 07:24:00 +0000244{
subrata_modak56207ce2009-03-23 13:35:39 +0000245 char temp[20];
vapierd13d74b2006-02-11 07:24:00 +0000246
247 Device = device;
248 Fstype = Type;
249 Mntpoint = mntpoint;
250 Flag = 0;
251
subrata_modak56207ce2009-03-23 13:35:39 +0000252 switch (i) {
vapierd13d74b2006-02-11 07:24:00 +0000253 case 0:
254 /* Setup for umount(2) returning errno EBUSY. */
subrata_modak344d5c32009-05-13 07:12:43 +0000255 if(access(Device,F_OK)) {
256 tst_brkm(TBROK, cleanup,
257 "Device %s does not exist", Device);
258 return 1;
259 }
vapierd13d74b2006-02-11 07:24:00 +0000260
261 TEST(mount(Device, Mntpoint, Fstype, Flag, NULL));
262
263 if (TEST_RETURN == -1) {
264 tst_brkm(TBROK, cleanup, "mount(2) failed to mount "
subrata_modak344d5c32009-05-13 07:12:43 +0000265 "device %s at mountpoint %s, Got errno - %d :"
vapierd13d74b2006-02-11 07:24:00 +0000266 " %s", Device, Mntpoint, TEST_ERRNO,
267 strerror(TEST_ERRNO));
268 return 1;
269 }
270
271 if (getcwd(Path, PATH_MAX) == NULL) {
272 tst_resm(TWARN, "getcwd() failed to get current working"
subrata_modak56207ce2009-03-23 13:35:39 +0000273 " directory errno = %d : %s", errno,
274 strerror(errno));
vapierd13d74b2006-02-11 07:24:00 +0000275 return 1;
276 }
subrata_modak56207ce2009-03-23 13:35:39 +0000277 sprintf(temp, "/%s/t_%d", Mntpoint, cnt);
vapierd13d74b2006-02-11 07:24:00 +0000278 strcat(Path, temp);
subrata_modak56207ce2009-03-23 13:35:39 +0000279 if ((fd = open(Path, O_CREAT | O_RDWR, S_IRWXU)) == -1) {
vapierd13d74b2006-02-11 07:24:00 +0000280 tst_resm(TWARN, "open() failed to create a file "
281 " %s errno = %d : %s", Path, errno,
subrata_modak56207ce2009-03-23 13:35:39 +0000282 strerror(errno));
vapierd13d74b2006-02-11 07:24:00 +0000283 return 1;
284 } else {
285 return 0;
286 }
287 case 1:
288
289 /* Setup for umount(2) returning errno EFAULT. */
290
291 Mntpoint = NULL;
292 break;
293 case 2:
294 /* Setup for umount(2) returning errno ENOENT. */
295
296 Mntpoint = Einval;
297 break;
298 case 3:
299 /* Setup for umount(2) returning errno EINVAL. */
300
301 if (getcwd(Path, PATH_MAX) == NULL) {
302 tst_resm(TWARN, "getcwd() failed to get current working"
subrata_modak56207ce2009-03-23 13:35:39 +0000303 " directory errno = %d : %s", errno,
304 strerror(errno));
vapierd13d74b2006-02-11 07:24:00 +0000305 return 1;
306 }
307 Mntpoint = Path;
308 break;
309 case 4:
310 /* Setup for umount(2) returning errno ENAMETOOLONG. */
311
subrata_modak56207ce2009-03-23 13:35:39 +0000312 memset(Longpathname, 'a', PATH_MAX + 2);
vapierd13d74b2006-02-11 07:24:00 +0000313 Mntpoint = Longpathname;
314 break;
315 }
316 return 0;
317}
318
vapierd13d74b2006-02-11 07:24:00 +0000319/*
320 * int
321 * cleanup_test() - Setup function for test cases based on the error values
322 * to be returned.
323 */
subrata_modak56207ce2009-03-23 13:35:39 +0000324int cleanup_test(int i)
vapierd13d74b2006-02-11 07:24:00 +0000325{
subrata_modak56207ce2009-03-23 13:35:39 +0000326 switch (i) {
vapierd13d74b2006-02-11 07:24:00 +0000327 case 0:
328 close(fd);
329 TEST(umount(mntpoint));
330 if (TEST_RETURN != 0) {
331 tst_resm(TWARN, "umount(2) Failed while unmounting"
332 " errno %d for testcase %s", TEST_ERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +0000333 testcases[i].exp_errval);
vapierd13d74b2006-02-11 07:24:00 +0000334 }
335 break;
336 }
337 return 0;
338}
vapierd13d74b2006-02-11 07:24:00 +0000339
340/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000341void setup()
vapierd13d74b2006-02-11 07:24:00 +0000342{
343 /* capture signals */
344 tst_sig(FORK, DEF_HANDLER, cleanup);
345
subrata_modak56207ce2009-03-23 13:35:39 +0000346 /* Check whether we are root */
vapierd13d74b2006-02-11 07:24:00 +0000347 if (geteuid() != 0) {
348 if (Type != NULL) {
349 free(Type);
350 }
351 tst_brkm(TBROK, tst_exit, "Test must be run as root");
352 }
353
subrata_modak4bb656a2009-02-26 12:02:09 +0000354 /* make a temp directory */
vapierd13d74b2006-02-11 07:24:00 +0000355 tst_tmpdir();
356
357 (void)sprintf(mntpoint, "mnt_%d", getpid());
358
359 if (mkdir(mntpoint, DIR_MODE)) {
360 tst_brkm(TBROK, cleanup, "mkdir(%s, %#o) failed; "
361 "errno = %d: %s", mntpoint, DIR_MODE, errno,
subrata_modak56207ce2009-03-23 13:35:39 +0000362 strerror(errno));
vapierd13d74b2006-02-11 07:24:00 +0000363 }
364
365 /* set up expected error numbers */
366 TEST_EXP_ENOS(exp_enos);
367
368 /* Pause if that option was specified */
369 TEST_PAUSE;
370
371 return;
subrata_modak56207ce2009-03-23 13:35:39 +0000372} /* End setup() */
vapierd13d74b2006-02-11 07:24:00 +0000373
subrata_modak4bb656a2009-02-26 12:02:09 +0000374/*
vapierd13d74b2006-02-11 07:24:00 +0000375 *cleanup() - performs all ONE TIME cleanup for this test at
376 * completion or premature exit.
377 */
subrata_modak56207ce2009-03-23 13:35:39 +0000378void cleanup()
vapierd13d74b2006-02-11 07:24:00 +0000379{
380 if (Type != NULL) {
381 free(Type);
382 }
383
384 /*
385 * print timing stats if that option was specified.
386 * print errno log if that option was specified.
387 */
388 TEST_CLEANUP;
389
390 /* Remove tmp dir and all files in it. */
391 tst_rmdir();
392
393 /* exit with return code appropriate for results */
394 tst_exit();
395
396 return;
subrata_modak56207ce2009-03-23 13:35:39 +0000397} /* End cleanup() */
vapierd13d74b2006-02-11 07:24:00 +0000398
399/*
400 * issue a help message
401 */
subrata_modak56207ce2009-03-23 13:35:39 +0000402void help()
vapierd13d74b2006-02-11 07:24:00 +0000403{
404 printf("-T type : specifies the type of filesystem to be mounted."
subrata_modak56207ce2009-03-23 13:35:39 +0000405 " Default ext2. \n");
vapierd13d74b2006-02-11 07:24:00 +0000406 printf("-D device : device used for mounting \n");
407}