From 7c06963e32f8d7bde0057a348fef9ff5e73d0159 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 16 Oct 2021 01:42:57 +0900 Subject: [PATCH] lsfd: (test) add a case for displaying a character device Signed-off-by: Masatake YAMATO --- tests/expected/lsfd/mkfds-rw-character-device | 2 + tests/helpers/test_mkfds.c | 33 +++++++++++++ tests/ts/lsfd/mkfds-rw-character-device | 46 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 tests/expected/lsfd/mkfds-rw-character-device create mode 100755 tests/ts/lsfd/mkfds-rw-character-device diff --git a/tests/expected/lsfd/mkfds-rw-character-device b/tests/expected/lsfd/mkfds-rw-character-device new file mode 100644 index 0000000000..5d2e7b9480 --- /dev/null +++ b/tests/expected/lsfd/mkfds-rw-character-device @@ -0,0 +1,2 @@ + 3 rw- CHR /dev/zero mem:5 0 1:5 mem char 1:5 +ASSOC,MODE,TYPE,NAME,SOURCE,POS,MAJ:MIN,CHRDRV,DEVTYPE,RDEV: 0 diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index 3e0f3badc9..369b291c64 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -370,6 +370,22 @@ static void open_directory(const struct factory *factory, struct fdesc fdescs[], }; } +static void open_rw_chrdev(const struct factory *factory, struct fdesc fdescs[], pid_t * child _U_, + int argc, char ** argv) +{ + struct arg chrdev = decode_arg("chrdev", factory->params, argc, argv); + int fd = open(ARG_STRING(chrdev), O_RDWR); + if (fd < 0) + err(EXIT_FAILURE, "failed to open: %s", ARG_STRING(chrdev)); + free_arg(&chrdev); + + fdescs[0] = (struct fdesc){ + .fd = fd, + .close = close_fdesc, + .data = NULL + }; +} + #define PARAM_END { .name = NULL, } static const struct factory factories[] = { { @@ -435,6 +451,23 @@ static const struct factory factories[] = { PARAM_END }, }, + { + .name = "rw-character-device", + .desc = "character device with O_RDWR flag", + .priv = false, + .N = 1, + .fork = false, + .make = open_rw_chrdev, + .params = (struct parameter []) { + { + .name = "chrdev", + .type = PTYPE_STRING, + .desc = "character device node to be opened", + .defv.string = "/dev/zero", + }, + PARAM_END + }, + }, }; static int count_parameters(const struct factory *factory) diff --git a/tests/ts/lsfd/mkfds-rw-character-device b/tests/ts/lsfd/mkfds-rw-character-device new file mode 100755 index 0000000000..063f71e4cb --- /dev/null +++ b/tests/ts/lsfd/mkfds-rw-character-device @@ -0,0 +1,46 @@ +#!/bin/bash +# +# Copyright (C) 2021 Masatake YAMATO +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +TS_TOPDIR="${0%/*}/../.." +TS_DESC="character device with O_RDWR" + +. $TS_TOPDIR/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_CMD_LSFD" +ts_check_test_command "$TS_HELPER_MKFDS" + +ts_check_prog "stat" + +ts_cd "$TS_OUTDIR" + +PID= +FD=3 +EXPR= + +{ + coproc MKFDS { "$TS_HELPER_MKFDS" rw-character-device $FD chrdev=/dev/zero; } + if read -u ${MKFDS[0]} PID; then + EXPR='(PID == '"${PID}"') and (FD == '"$FD"')' + ${TS_CMD_LSFD} -n -o ASSOC,MODE,TYPE,NAME,SOURCE,POS,MAJ:MIN,CHRDRV,DEVTYPE,RDEV -Q "${EXPR}" + echo 'ASSOC,MODE,TYPE,NAME,SOURCE,POS,MAJ:MIN,CHRDRV,DEVTYPE,RDEV': $? + + kill -CONT ${PID} + wait ${MKFDS_PID} + fi +} > $TS_OUTPUT 2>&1 + +ts_finalize -- 2.47.3