File: ioctl-v.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (57 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2015-2024 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>

int
main(void)
{
	uint8_t array[8] = { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef };
	char array_str[] = "\\xfe\\xed\\xfa\\xce\\xde\\xad\\xbe\\xef";

	/* Zero-length read */
	(void) ioctl(-1, _IOC(_IOC_READ, 0xde, 0x0, 0), (kernel_ulong_t) -1ULL);
	printf("ioctl(-1, _IOC(_IOC_READ, 0xde, 0, 0), %#lx)"
	       RVAL_EBADF, -1UL);

	/* Zero-length write */
	(void) ioctl(-1, _IOC(_IOC_WRITE, 0xde, 0, 0), (kernel_ulong_t) -1ULL);
	printf("ioctl(-1, _IOC(_IOC_WRITE, 0xde, 0, 0), %#lx)"
	       RVAL_EBADF, -1UL);

	/* Data read from ioctl */
	(void) ioctl(-1, _IOR(0xde, 0xad, array), array);
	printf("ioctl(-1, _IOC(_IOC_READ, 0xde, 0xad, 0x8), \"%s\")"
	       RVAL_EBADF, array_str);

	/* Data written to ioctl */
	(void) ioctl(-1, _IOW(0xde, 0xad, array), array);
	printf("ioctl(-1, _IOC(_IOC_WRITE, 0xde, 0xad, 0x8), \"%s\")"
	       RVAL_EBADF, array_str);

	/* Bi-directional data movement */
	(void) ioctl(-1, _IOWR(0xde, 0xad, array), array);
	printf("ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, 0xde, 0xad, 0x8)"
	       ", \"%s\" => \"%s\")"
	       RVAL_EBADF, array_str, array_str);

	/* Neither read nor write implied by ioctl, but valid arg provided */
	(void) ioctl(-1, _IOC(_IOC_NONE, 0xde, 0xad, sizeof(array)), array);
	printf("ioctl(-1, _IOC(_IOC_NONE, 0xde, 0xad, 0x8), \"%s\" => \"%s\")"
	       RVAL_EBADF, array_str, array_str);

	/* Arg expected for ioctl, but arg pointer is bad */
	(void) ioctl(-1, _IOC(_IOC_NONE, 0xde, 0, 8), (kernel_ulong_t) -1ULL);
	printf("ioctl(-1, _IOC(_IOC_NONE, 0xde, 0, 0x8), %#lx)"
	       RVAL_EBADF, -1UL);

	puts("+++ exited with 0 +++");
	return 0;
}