aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/ioctl/ioctl01.c
blob: 2989c0e9be1a0d63377d7149c69900b20f53fa8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines Corp., 2001
 * Copyright (c) 2020 Petr Vorel <petr.vorel@gmail.com>
 * 07/2001 Ported by Wayne Boyer
 * 04/2002 Fixes by wjhuie
 *
 *	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
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include "tst_test.h"
#include "lapi/ioctl.h"

#define	INVAL_IOCTL	9999999

static int fd, fd_file;
static int bfd = -1;

static struct termio termio;

static struct tcase {
	int *fd;
	int request;
	struct termio *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 */
	/* 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}
};

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");
}

static void setup(void)
{
	unsigned int i;

	if (!device)
		tst_brk(TBROK, "You must specify a tty device with -D option");

	fd = SAFE_OPEN(device, O_RDWR, 0777);
	fd_file = SAFE_OPEN("x", O_CREAT, 0777);
}

static void cleanup(void)
{
	if (fd > 0)
		SAFE_CLOSE(fd);

	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]"},
		{}
	}
};