Skip to content

Commit ac334ae

Browse files
xuyang0410metan-ucw
authored andcommitted
syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
It is designed to test LOOP_CHANGE_FD can not succeed(get EINAL error) when loop_dev is not read only. Signed-off-by: Yang Xu <[email protected]> Signed-off-by: Cyril Hrubis <[email protected]>
1 parent 5dfa37c commit ac334ae

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ ioctl08 ioctl08
531531

532532
ioctl_loop01 ioctl_loop01
533533
ioctl_loop02 ioctl_loop02
534+
ioctl_loop03 ioctl_loop03
534535

535536
ioctl_ns01 ioctl_ns01
536537
ioctl_ns02 ioctl_ns02

testcases/kernel/syscalls/ioctl/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/ioctl08
99
/ioctl_loop01
1010
/ioctl_loop02
11+
/ioctl_loop03
1112
/ioctl_ns01
1213
/ioctl_ns02
1314
/ioctl_ns03
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]>
5+
*
6+
* This is a basic ioctl test about loopdevice.
7+
*
8+
* It is designed to test LOOP_CHANGE_FD can not succeed (get EINVAL error)
9+
* when loop_dev is not read only.
10+
*/
11+
12+
#include <stdio.h>
13+
#include <unistd.h>
14+
#include <string.h>
15+
#include "lapi/loop.h"
16+
#include "tst_test.h"
17+
18+
static char dev_path[1024];
19+
static int dev_num, dev_fd, file_fd, attach_flag;
20+
21+
static void verify_ioctl_loop(void)
22+
{
23+
TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd));
24+
if (TST_RET == 0) {
25+
tst_res(TFAIL, "LOOP_CHANGE_FD succeeded unexpectedly");
26+
return;
27+
}
28+
if (TST_ERR == EINVAL)
29+
tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
30+
else
31+
tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
32+
}
33+
34+
static void setup(void)
35+
{
36+
struct loop_info loopinfoget;
37+
38+
memset(&loopinfoget, 0, sizeof(loopinfoget));
39+
dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
40+
if (dev_num < 0)
41+
tst_brk(TBROK, "Failed to find free loop device");
42+
43+
tst_fill_file("test.img", 0, 1024, 10);
44+
tst_attach_device(dev_path, "test.img");
45+
attach_flag = 1;
46+
47+
dev_fd = SAFE_OPEN(dev_path, O_RDWR);
48+
file_fd = SAFE_OPEN("test.img", O_RDWR);
49+
SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
50+
51+
if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
52+
tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
53+
}
54+
55+
static void cleanup(void)
56+
{
57+
if (dev_fd > 0)
58+
SAFE_CLOSE(dev_fd);
59+
if (file_fd > 0)
60+
SAFE_CLOSE(file_fd);
61+
if (attach_flag)
62+
tst_detach_device(dev_path);
63+
}
64+
65+
static struct tst_test test = {
66+
.setup = setup,
67+
.cleanup = cleanup,
68+
.test_all = verify_ioctl_loop,
69+
.needs_root = 1,
70+
.needs_tmpdir = 1,
71+
.needs_drivers = (const char *const []) {
72+
"loop",
73+
NULL
74+
}
75+
};

0 commit comments

Comments
 (0)