diff options
Diffstat (limited to 'testcases/kernel/syscalls/ioctl/ioctl01.c')
-rw-r--r-- | testcases/kernel/syscalls/ioctl/ioctl01.c | 84 |
1 files changed, 35 insertions, 49 deletions
diff --git a/testcases/kernel/syscalls/ioctl/ioctl01.c b/testcases/kernel/syscalls/ioctl/ioctl01.c index 2989c0e9b..c84a72b9a 100644 --- a/testcases/kernel/syscalls/ioctl/ioctl01.c +++ b/testcases/kernel/syscalls/ioctl/ioctl01.c @@ -2,104 +2,90 @@ /* * Copyright (c) International Business Machines Corp., 2001 * Copyright (c) 2020 Petr Vorel <petr.vorel@gmail.com> + * Copyright (c) Linux Test Project, 2002-2023 * 07/2001 Ported by Wayne Boyer * 04/2002 Fixes by wjhuie + */ + +/*\ + * [Description] * - * Testcase to check the errnos set by the ioctl(2) system call. + * Testcase to check the errnos set by the ioctl(2) system call. * - * ALGORITHM - * 1. EBADF: Pass an invalid fd to ioctl(fd, ..) and expect EBADF. - * 2. EFAULT: Pass an invalid address of arg in ioctl(fd, .., arg) - * 3. EINVAL: Pass invalid cmd in ioctl(fd, cmd, arg) - * 4. ENOTTY: Pass an non-streams fd in ioctl(fd, cmd, arg) - * 5. EFAULT: Pass a NULL address for termio + * - EBADF: Pass an invalid fd to ioctl(fd, ...) and expect EBADF + * - EFAULT: Pass an invalid address of arg in ioctl(fd, ..., arg) + * - EINVAL: Pass invalid cmd in ioctl(fd, cmd, arg) + * - ENOTTY: Pass an non-streams fd in ioctl(fd, cmd, arg) + * - EFAULT: Pass a NULL address for termio */ #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <termios.h> +#include <pty.h> #include "tst_test.h" #include "lapi/ioctl.h" #define INVAL_IOCTL 9999999 +static int amaster, aslave; static int fd, fd_file; static int bfd = -1; static struct termio termio; +static struct termios termios; static struct tcase { + const char *desc; int *fd; int request; - struct termio *s_tio; + void *s_tio; int error; } tcases[] = { - /* file descriptor is invalid */ - {&bfd, TCGETA, &termio, EBADF}, - /* termio address is invalid */ - {&fd, TCGETA, (struct termio *)-1, EFAULT}, - /* command is invalid */ + {"File descriptor is invalid (termio)", &bfd, TCGETA, &termio, EBADF}, + {"File descriptor is invalid (termios)", &bfd, TCGETS, &termios, EBADF}, + {"Termio address is invalid", &fd, TCGETA, (struct termio *)-1, EFAULT}, + {"Termios address is invalid", &fd, TCGETS, (struct termios *)-1, EFAULT}, /* This errno value was changed from EINVAL to ENOTTY * by kernel commit 07d106d0 and bbb63c51 */ - {&fd, INVAL_IOCTL, &termio, ENOTTY}, - /* file descriptor is for a regular file */ - {&fd_file, TCGETA, &termio, ENOTTY}, - /* termio is NULL */ - {&fd, TCGETA, NULL, EFAULT} + {"Command is invalid", &fd, INVAL_IOCTL, &termio, ENOTTY}, + {"File descriptor is for a regular file (termio)", &fd_file, TCGETA, &termio, ENOTTY}, + {"File descriptor is for a regular file (termios)", &fd_file, TCGETS, &termios, ENOTTY}, + {"Termio is NULL", &fd, TCGETA, NULL, EFAULT}, + {"Termios is NULL", &fd, TCGETS, NULL, EFAULT} }; -static char *device; - static void verify_ioctl(unsigned int i) { - TEST(ioctl(*(tcases[i].fd), tcases[i].request, tcases[i].s_tio)); - - if (TST_RET != -1) { - tst_res(TFAIL, "call succeeded unexpectedly"); - return; - } - - if (TST_ERR != tcases[i].error) { - tst_res(TFAIL | TTERRNO, - "failed unexpectedly; expected %s", - tst_strerrno(tcases[i].error)); - return; - } - - tst_res(TPASS | TTERRNO, "failed as expected"); + TST_EXP_FAIL(ioctl(*(tcases[i].fd), tcases[i].request, tcases[i].s_tio), + tcases[i].error, "%s", tcases[i].desc); } static void setup(void) { - unsigned int i; + if (openpty(&amaster, &aslave, NULL, NULL, NULL) < 0) + tst_brk(TBROK | TERRNO, "unable to open pty"); - if (!device) - tst_brk(TBROK, "You must specify a tty device with -D option"); - - fd = SAFE_OPEN(device, O_RDWR, 0777); + fd = amaster; fd_file = SAFE_OPEN("x", O_CREAT, 0777); } static void cleanup(void) { - if (fd > 0) - SAFE_CLOSE(fd); - + if (amaster > 0) + SAFE_CLOSE(amaster); + if (aslave > 0) + SAFE_CLOSE(aslave); if (fd_file > 0) SAFE_CLOSE(fd_file); } static struct tst_test test = { - .needs_root = 1, .needs_tmpdir = 1, .setup = setup, .cleanup = cleanup, .test = verify_ioctl, - .tcnt = ARRAY_SIZE(tcases), - .options = (struct tst_option[]) { - {"D:", &device, "Tty device. For example, /dev/tty[0-9]"}, - {} - } + .tcnt = ARRAY_SIZE(tcases) }; |