| /* |
| * NAME |
| * open03.c |
| * |
| * DESCRIPTION |
| * Check for the following errors: |
| * 1. EEXIST |
| * 2. EISDIR |
| * 3. ENOTDIR |
| * 4. ENAMETOOLONG |
| * 5. EACCES |
| * 6. EFAULT |
| * |
| * ALGORITHM |
| * 1. Open a file with O_CREAT and O_EXCL, when the file already |
| * exists. Check the errno for EEXIST |
| * |
| * 2. Pass a directory as the pathname and request a write access, |
| * check for errno for EISDIR |
| * |
| * 3. Specify O_DIRECTORY as a parameter to open and pass a file as the |
| * pathname, check errno for ENOTDIR |
| * |
| * 4. Attempt to open() a filename which is more than VFS_MAXNAMLEN, and |
| * check for errno to be ENAMETOOLONG. |
| * |
| * 5. Attempt to open a test executable in WRONLY mode, |
| * open(2) should fail with EACCES. |
| * |
| * 6. Attempt to pass an invalid pathname with an address pointing outside |
| * the address space of the process, as the argument to open(), and |
| * expect to get EFAULT. |
| * |
| * USAGE: <for command-line> |
| * open03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| * where, -c n : Run n copies concurrently. |
| * -e : Turn on errno logging. |
| * -i n : Execute test n times. |
| * -I x : Execute test for x seconds. |
| * -P x : Pause for x seconds between iterations. |
| * -t : Turn on syscall timing. |
| * |
| * HISTORY |
| * 07/2001 Ported by Wayne Boyer |
| * |
| * RESTRICTIONS |
| * None |
| */ |
| #include <stdio.h> |
| #include <errno.h> |
| #include <sys/types.h> |
| #include <asm/fcntl.h> /* for O_DIRECTORY definition */ |
| #include <signal.h> |
| #include <pwd.h> |
| #include "test.h" |
| #include "usctest.h" |
| |
| void setup(void); |
| void cleanup(void); |
| |
| char *TCID = "open03"; |
| int TST_TOTAL = 6; |
| extern int Tst_count; |
| |
| char nobody_uid[] = "nobody"; |
| struct passwd *ltpuser; |
| |
| int exp_enos[]={EEXIST, EISDIR, ENOTDIR, ENAMETOOLONG, EACCES, EFAULT, 0}; |
| |
| char filename[40] = ""; |
| char fname[] = "/usr/bin/test"; /* test executable to open */ |
| char bad_file[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
| |
| struct test_case_t { |
| char *fname; |
| int flags; |
| int error; |
| } TC[] = { |
| {filename, O_CREAT | O_EXCL, EEXIST}, |
| {"/tmp", O_RDWR, EISDIR}, |
| {filename, O_DIRECTORY, ENOTDIR}, |
| {bad_file, O_RDWR, ENAMETOOLONG}, |
| {fname, O_WRONLY, EACCES}, |
| {(char *)-1, O_CREAT, EFAULT} |
| }; |
| |
| main(int ac, char **av) |
| { |
| int lc; /* loop counter */ |
| char *msg; /* message returned from parse_opts */ |
| int i; |
| |
| /* parse standard options */ |
| if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| } |
| |
| setup(); |
| |
| /* set up the expected errnos */ |
| TEST_EXP_ENOS(exp_enos); |
| |
| /* The following loop checks looping state if -i option given */ |
| for (lc = 0; TEST_LOOPING(lc); lc++) { |
| |
| /* reset Tst_count in case we are looping */ |
| Tst_count = 0; |
| |
| /* loop through the test cases */ |
| for (i = 0; i < TST_TOTAL; i++) { |
| |
| TEST(open(TC[i].fname, TC[i].flags)); |
| |
| if (TEST_RETURN != -1) { |
| tst_resm(TFAIL, "call succeeded unexpectedly"); |
| continue; |
| } |
| |
| TEST_ERROR_LOG(TEST_ERRNO); |
| |
| if (TEST_ERRNO == TC[i].error) { |
| tst_resm(TPASS, "expected failure - " |
| "errno = %d : %s", TEST_ERRNO, |
| strerror(TEST_ERRNO)); |
| } else { |
| tst_resm(TFAIL, "unexpected error - %d : %s - " |
| "expected %d", TEST_ERRNO, |
| strerror(TEST_ERRNO), TC[i].error); |
| } |
| } |
| } |
| cleanup(); |
| |
| /*NOTREACHED*/ |
| } |
| |
| /* |
| * setup() - performs all ONE TIME setup for this test |
| */ |
| void |
| setup(void) |
| { |
| int fildes; |
| |
| /* capture signals */ |
| tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| |
| umask(0); |
| |
| /* Pause if that option was specified */ |
| TEST_PAUSE; |
| |
| /* Switch to nobody user for correct error code collection */ |
| if (geteuid() != 0) { |
| tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| } |
| ltpuser = getpwnam(nobody_uid); |
| if (setgid(ltpuser->pw_gid) == -1) { |
| tst_resm(TINFO, "setgid failed to " |
| "to set the effective uid to %d", |
| ltpuser->pw_gid); |
| perror("setgid"); |
| } |
| if (setuid(ltpuser->pw_uid) == -1) { |
| tst_resm(TINFO, "setuid failed to " |
| "to set the effective uid to %d", |
| ltpuser->pw_uid); |
| perror("setuid"); |
| } |
| |
| |
| /* make a temp directory and cd to it */ |
| tst_tmpdir(); |
| |
| sprintf(filename, "open3.%d", getpid()); |
| |
| if ((fildes = creat(filename, 0600)) == -1) { |
| tst_brkm(TBROK, cleanup, "Can't creat %s", filename); |
| } |
| close(fildes); |
| } |
| |
| /* |
| * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| * or premature exit. |
| */ |
| void |
| cleanup(void) |
| { |
| /* |
| * print timing status if that option was specified. |
| * print errno log if that option was specified |
| */ |
| TEST_CLEANUP; |
| |
| /* Remove tmp dir and all files in it */ |
| tst_rmdir(); |
| |
| /* exit with return code appropriate for results */ |
| tst_exit(); |
| } |