]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (lsfd) add a case testing UNIX+DGRAM socket
authorMasatake YAMATO <yamato@redhat.com>
Sat, 24 Sep 2022 08:54:38 +0000 (17:54 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Sat, 24 Sep 2022 17:24:23 +0000 (02:24 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
tests/expected/lsfd/mkfds-unix-dgram [new file with mode: 0644]
tests/helpers/test_mkfds.c
tests/ts/lsfd/mkfds-unix-dgram [new file with mode: 0755]

diff --git a/tests/expected/lsfd/mkfds-unix-dgram b/tests/expected/lsfd/mkfds-unix-dgram
new file mode 100644 (file)
index 0000000..915b343
--- /dev/null
@@ -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
index 077aa49a45e799287c2cf5e83f0ae964d3c78a52..fdd3cff1e83f9c5118bbffa844f79467264bc07c 100644 (file)
@@ -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 (executable)
index 0000000..82710e9
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/bash
+#
+# Copyright (C) 2022 Masatake YAMATO <yamato@redhat.com>
+#
+# 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