From 736d8e4d3f2d32f18621a272326953ecd890779a Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 19 Jul 2016 11:18:25 +0000 Subject: [PATCH] tests: add socketcall function to libtests * tests/tests.h (socketcall): New prototype. * tests/libsocketcall.c: New file. * tests/Makefile.am (libtests_a_SOURCES): Add it. --- tests/Makefile.am | 1 + tests/libsocketcall.c | 69 +++++++++++++++++++++++++++++++++++++++++++ tests/tests.h | 4 +++ 3 files changed, 74 insertions(+) create mode 100644 tests/libsocketcall.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 13982533..c537a9ea 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -47,6 +47,7 @@ libtests_a_SOURCES = \ hexdump_strdup.c \ hexquote_strndup.c \ inode_of_sockfd.c \ + libsocketcall.c \ overflowuid.c \ print_quoted_string.c \ printflags.c \ diff --git a/tests/libsocketcall.c b/tests/libsocketcall.c new file mode 100644 index 00000000..718b50e1 --- /dev/null +++ b/tests/libsocketcall.c @@ -0,0 +1,69 @@ +/* + * Invoke a socket syscall, either directly or via __NR_socketcall. + * + * Copyright (c) 2016 Dmitry V. Levin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests.h" +#include +#include +#include + +/* + * Invoke a socket syscall, either directly or via __NR_socketcall. + * if nr == -1, no direct syscall invocation will be made. + */ +int +socketcall(const int nr, const int call, + long a1, long a2, long a3, long a4, long a5) +{ + int rc = -1; + errno = ENOSYS; + +# ifdef __NR_socketcall + static int have_socketcall = -1; + + if (have_socketcall < 0) { + if (syscall(__NR_socketcall, 0L, 0L, 0L, 0L, 0L) < 0 + && EINVAL == errno) { + have_socketcall = 1; + } else { + have_socketcall = 0; + } + } + + if (have_socketcall) { + const long args[] = { a1, a2, a3, a4, a5 }; + rc = syscall(__NR_socketcall, call, args); + } else +# endif + { + if (nr != -1) + rc = syscall(nr, a1, a2, a3, a4, a5); + } + + return rc; +} diff --git a/tests/tests.h b/tests/tests.h index e2928f09..c61d0051 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -107,6 +107,10 @@ int printflags(const struct xlat *, const unsigned long long, const char *); /* Print constant in symbolic form according to xlat table. */ int printxval(const struct xlat *, const unsigned long long, const char *); +/* Invoke a socket syscall, either directly or via __NR_socketcall. */ +int socketcall(const int nr, const int call, + long a1, long a2, long a3, long a4, long a5); + # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0]))) # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1) -- 2.40.0