1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 Petr Vorel <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2002-2023
6*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
7*49cdfc7eSAndroid Build Coastguard Worker * 04/2002 Fixes by wjhuie
8*49cdfc7eSAndroid Build Coastguard Worker */
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker /*\
11*49cdfc7eSAndroid Build Coastguard Worker * [Description]
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check the errnos set by the ioctl(2) system call.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * - EBADF: Pass an invalid fd to ioctl(fd, ...) and expect EBADF
16*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT: Pass an invalid address of arg in ioctl(fd, ..., arg)
17*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL: Pass invalid cmd in ioctl(fd, cmd, arg)
18*49cdfc7eSAndroid Build Coastguard Worker * - ENOTTY: Pass an non-streams fd in ioctl(fd, cmd, arg)
19*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT: Pass a NULL address for termio
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <pty.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ioctl.h"
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_IOCTL 9999999
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker static int amaster, aslave;
33*49cdfc7eSAndroid Build Coastguard Worker static int fd, fd_file;
34*49cdfc7eSAndroid Build Coastguard Worker static int bfd = -1;
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker static struct termio termio;
37*49cdfc7eSAndroid Build Coastguard Worker static struct termios termios;
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
40*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
41*49cdfc7eSAndroid Build Coastguard Worker int *fd;
42*49cdfc7eSAndroid Build Coastguard Worker int request;
43*49cdfc7eSAndroid Build Coastguard Worker void *s_tio;
44*49cdfc7eSAndroid Build Coastguard Worker int error;
45*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
46*49cdfc7eSAndroid Build Coastguard Worker {"File descriptor is invalid (termio)", &bfd, TCGETA, &termio, EBADF},
47*49cdfc7eSAndroid Build Coastguard Worker {"File descriptor is invalid (termios)", &bfd, TCGETS, &termios, EBADF},
48*49cdfc7eSAndroid Build Coastguard Worker {"Termio address is invalid", &fd, TCGETA, (struct termio *)-1, EFAULT},
49*49cdfc7eSAndroid Build Coastguard Worker {"Termios address is invalid", &fd, TCGETS, (struct termios *)-1, EFAULT},
50*49cdfc7eSAndroid Build Coastguard Worker /* This errno value was changed from EINVAL to ENOTTY
51*49cdfc7eSAndroid Build Coastguard Worker * by kernel commit 07d106d0 and bbb63c51
52*49cdfc7eSAndroid Build Coastguard Worker */
53*49cdfc7eSAndroid Build Coastguard Worker {"Command is invalid", &fd, INVAL_IOCTL, &termio, ENOTTY},
54*49cdfc7eSAndroid Build Coastguard Worker {"File descriptor is for a regular file (termio)", &fd_file, TCGETA, &termio, ENOTTY},
55*49cdfc7eSAndroid Build Coastguard Worker {"File descriptor is for a regular file (termios)", &fd_file, TCGETS, &termios, ENOTTY},
56*49cdfc7eSAndroid Build Coastguard Worker {"Termio is NULL", &fd, TCGETA, NULL, EFAULT},
57*49cdfc7eSAndroid Build Coastguard Worker {"Termios is NULL", &fd, TCGETS, NULL, EFAULT}
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker
verify_ioctl(unsigned int i)60*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioctl(unsigned int i)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(ioctl(*(tcases[i].fd), tcases[i].request, tcases[i].s_tio),
63*49cdfc7eSAndroid Build Coastguard Worker tcases[i].error, "%s", tcases[i].desc);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
setup(void)66*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker if (openpty(&amaster, &aslave, NULL, NULL, NULL) < 0)
69*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "unable to open pty");
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker fd = amaster;
72*49cdfc7eSAndroid Build Coastguard Worker fd_file = SAFE_OPEN("x", O_CREAT, 0777);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)75*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker if (amaster > 0)
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(amaster);
79*49cdfc7eSAndroid Build Coastguard Worker if (aslave > 0)
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(aslave);
81*49cdfc7eSAndroid Build Coastguard Worker if (fd_file > 0)
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_file);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
86*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
87*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
88*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
89*49cdfc7eSAndroid Build Coastguard Worker .test = verify_ioctl,
90*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases)
91*49cdfc7eSAndroid Build Coastguard Worker };
92