|
| 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