From: Masatake YAMATO Date: Sat, 24 Sep 2022 08:54:38 +0000 (+0900) Subject: tests: (lsfd) add a case testing UNIX+DGRAM socket X-Git-Tag: v2.39-rc1~505^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2229a4b4600dee38b0bb4c88d380256904174b14;p=thirdparty%2Futil-linux.git tests: (lsfd) add a case testing UNIX+DGRAM socket Signed-off-by: Masatake YAMATO --- diff --git a/tests/expected/lsfd/mkfds-unix-dgram b/tests/expected/lsfd/mkfds-unix-dgram new file mode 100644 index 0000000000..915b34396e --- /dev/null +++ b/tests/expected/lsfd/mkfds-unix-dgram @@ -0,0 +1,6 @@ + 3 SOCK state=connected path=test_mkfds-unix-dgram type=dgram connected dgram test_mkfds-unix-dgram + 4 SOCK state=connected type=dgram connected dgram +ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH: 0 + 3 SOCK state=connected path=@test_mkfds-unix-dgram type=dgram connected dgram @test_mkfds-unix-dgram + 4 SOCK state=connected type=dgram connected dgram +ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH: 0 diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index 077aa49a45..fdd3cff1e8 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -935,6 +935,101 @@ static void *make_unix_stream(const struct factory *factory, struct fdesc fdescs return NULL; } +static void *make_unix_dgram(const struct factory *factory, struct fdesc fdescs[], + int argc, char ** argv) +{ + struct arg path = decode_arg("path", factory->params, argc, argv); + const char *spath = ARG_STRING(path); + + struct arg abstract = decode_arg("abstract", factory->params, argc, argv); + bool babstract = ARG_BOOLEAN(abstract); + + int ssd, csd; /* server and client socket descriptors */ + + struct sockaddr_un un; + size_t un_len = sizeof(un); + + memset(&un, 0, sizeof(un)); + un.sun_family = AF_UNIX; + if (babstract) { + strncpy(un.sun_path + 1, spath, sizeof(un.sun_path) - 1 - 1); + size_t pathlen = strlen(spath); + if (sizeof(un.sun_path) - 1 > pathlen) + un_len = sizeof(un) - sizeof(un.sun_path) + 1 + pathlen; + } else + strncpy(un.sun_path, spath, sizeof(un.sun_path) - 1 ); + + free_arg(&abstract); + free_arg(&path); + + ssd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (ssd < 0) + err(EXIT_FAILURE, + "failed to make a socket with AF_UNIX + SOCK_DGRAM (server side)"); + if (ssd != fdescs[0].fd) { + if (dup2(ssd, fdescs[0].fd) < 0) { + int e = errno; + close(ssd); + errno = e; + err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd); + } + close(ssd); + ssd = fdescs[0].fd; + } + + fdescs[0] = (struct fdesc){ + .fd = fdescs[0].fd, + .close = close_unix_socket, + .data = NULL, + }; + + if (!babstract) + unlink(un.sun_path); + if (bind(ssd, (const struct sockaddr *)&un, un_len) < 0) { + int e = errno; + close(ssd); + errno = e; + err(EXIT_FAILURE, "failed to bind a socket for server"); + } + + if (!babstract) + fdescs[0].data = xstrdup(un.sun_path); + csd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (csd < 0) + err(EXIT_FAILURE, + "failed to make a socket with AF_UNIX + SOCK_DGRAM (client side)"); + if (csd != fdescs[1].fd) { + if (dup2(csd, fdescs[1].fd) < 0) { + int e = errno; + close(csd); + close_unix_socket(ssd, fdescs[0].data); + errno = e; + err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd); + } + close(csd); + csd = fdescs[1].fd; + } + + fdescs[1] = (struct fdesc){ + .fd = fdescs[1].fd, + .close = close_fdesc, + .data = NULL, + }; + + if (connect(csd, (const struct sockaddr *)&un, un_len) < 0) { + int e = errno; + close_fdesc(csd, NULL); + close_unix_socket(ssd, fdescs[0].data); + errno = e; + err(EXIT_FAILURE, "failed to connect a socket to the server socket"); + } + + if (!babstract) + unlink(un.sun_path); + + return NULL; +} + #define PARAM_END { .name = NULL, } static const struct factory factories[] = { { @@ -1172,6 +1267,29 @@ static const struct factory factories[] = { PARAM_END }, }, + { + .name = "unix-dgram", + .desc = "AF_UNIX+SOCK_DGRAM sockets", + .priv = false, + .N = 2, + .EX_N = 0, + .make = make_unix_dgram, + .params = (struct parameter []) { + { + .name = "path", + .type = PTYPE_STRING, + .desc = "path for unix non-stream bound to", + .defv.string = "/tmp/test_mkfds-unix-dgram", + }, + { + .name = "abstract", + .type = PTYPE_BOOLEAN, + .desc = "use PATH as an abstract socket address", + .defv.boolean = false, + }, + PARAM_END + }, + }, }; static int count_parameters(const struct factory *factory) diff --git a/tests/ts/lsfd/mkfds-unix-dgram b/tests/ts/lsfd/mkfds-unix-dgram new file mode 100755 index 0000000000..82710e912c --- /dev/null +++ b/tests/ts/lsfd/mkfds-unix-dgram @@ -0,0 +1,60 @@ +#!/bin/bash +# +# Copyright (C) 2022 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="UNIX dgram sockets" + +. "$TS_TOPDIR"/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_CMD_LSFD" +ts_check_test_command "$TS_HELPER_MKFDS" + +ts_cd "$TS_OUTDIR" + +PID= +FDS=3 +FDC=4 +EXPR='(TYPE == "UNIX") and ((FD == 3) or (FD == 4))' + +{ + coproc MKFDS { "$TS_HELPER_MKFDS" unix-dgram $FDS $FDC \ + path=test_mkfds-unix-dgram ; } + if read -r -u "${MKFDS[0]}" PID; then + ${TS_CMD_LSFD} -n \ + -o ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH \ + -p "${PID}" -Q "${EXPR}" + echo 'ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH': $? + + kill -CONT "${PID}" + wait "${MKFDS_PID}" + fi + + coproc MKFDS { "$TS_HELPER_MKFDS" unix-dgram $FDS $FDC \ + path=test_mkfds-unix-dgram \ + abstract=true ; } + if read -r -u "${MKFDS[0]}" PID; then + ${TS_CMD_LSFD} -n \ + -o ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH \ + -p "${PID}" -Q "${EXPR}" + echo 'ASSOC,STTYPE,NAME,SOCKSTATE,SOCKTYPE,UNIX.PATH': $? + + kill -CONT "${PID}" + wait "${MKFDS_PID}" + fi +} > "$TS_OUTPUT" 2>&1 + +ts_finalize