94084c
commit 5575daae5099e779bb860b566b4d608418a5b832
94084c
Author: Florian Weimer <fweimer@redhat.com>
94084c
Date:   Mon Jan 17 10:21:34 2022 +0100
94084c
94084c
    socket: Add the __sockaddr_un_set function
94084c
    
94084c
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
94084c
    (cherry picked from commit e368b12f6c16b6888dda99ba641e999b9c9643c8)
94084c
94084c
diff --git a/include/sys/un.h b/include/sys/un.h
94084c
index bdbee999806930f4..152afd9fc7426d8b 100644
94084c
--- a/include/sys/un.h
94084c
+++ b/include/sys/un.h
94084c
@@ -1 +1,13 @@
94084c
 #include <socket/sys/un.h>
94084c
+
94084c
+#ifndef _ISOMAC
94084c
+
94084c
+/* Set ADDR->sun_family to AF_UNIX and ADDR->sun_path to PATHNAME.
94084c
+   Return 0 on success or -1 on failure (due to overlong PATHNAME).
94084c
+   The caller should always use sizeof (struct sockaddr_un) as the
94084c
+   socket address length, disregaring the length of PATHNAME.
94084c
+   Only concrete (non-abstract) pathnames are supported.  */
94084c
+int __sockaddr_un_set (struct sockaddr_un *addr, const char *pathname)
94084c
+  attribute_hidden;
94084c
+
94084c
+#endif /* _ISOMAC */
94084c
diff --git a/socket/Makefile b/socket/Makefile
94084c
index 375957601024c12e..c2de11d73ca1e324 100644
94084c
--- a/socket/Makefile
94084c
+++ b/socket/Makefile
94084c
@@ -29,13 +29,17 @@ headers	:= sys/socket.h sys/un.h bits/sockaddr.h bits/socket.h \
94084c
 routines := accept bind connect getpeername getsockname getsockopt	\
94084c
 	    listen recv recvfrom recvmsg send sendmsg sendto		\
94084c
 	    setsockopt shutdown socket socketpair isfdtype opensock	\
94084c
-	    sockatmark accept4 recvmmsg sendmmsg
94084c
+	    sockatmark accept4 recvmmsg sendmmsg sockaddr_un_set
94084c
 
94084c
 tests := \
94084c
   tst-accept4 \
94084c
   tst-sockopt \
94084c
   # tests
94084c
 
94084c
+tests-internal := \
94084c
+  tst-sockaddr_un_set \
94084c
+  # tests-internal
94084c
+
94084c
 tests-time64 := \
94084c
   tst-sockopt-time64 \
94084c
   # tests
94084c
diff --git a/socket/sockaddr_un_set.c b/socket/sockaddr_un_set.c
94084c
new file mode 100644
94084c
index 0000000000000000..0bd40dc34e3d7efc
94084c
--- /dev/null
94084c
+++ b/socket/sockaddr_un_set.c
94084c
@@ -0,0 +1,41 @@
94084c
+/* Set the sun_path member of struct sockaddr_un.
94084c
+   Copyright (C) 2022 Free Software Foundation, Inc.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+#include <errno.h>
94084c
+#include <string.h>
94084c
+#include <sys/socket.h>
94084c
+#include <sys/un.h>
94084c
+
94084c
+int
94084c
+__sockaddr_un_set (struct sockaddr_un *addr, const char *pathname)
94084c
+{
94084c
+  size_t name_length = strlen (pathname);
94084c
+
94084c
+  /* The kernel supports names of exactly sizeof (addr->sun_path)
94084c
+     bytes, without a null terminator, but userspace does not; see the
94084c
+     SUN_LEN macro.  */
94084c
+  if (name_length >= sizeof (addr->sun_path))
94084c
+    {
94084c
+      __set_errno (EINVAL);     /* Error code used by the kernel.  */
94084c
+      return -1;
94084c
+    }
94084c
+
94084c
+  addr->sun_family = AF_UNIX;
94084c
+  memcpy (addr->sun_path, pathname, name_length + 1);
94084c
+  return 0;
94084c
+}
94084c
diff --git a/socket/tst-sockaddr_un_set.c b/socket/tst-sockaddr_un_set.c
94084c
new file mode 100644
94084c
index 0000000000000000..29c2a81afda81b5e
94084c
--- /dev/null
94084c
+++ b/socket/tst-sockaddr_un_set.c
94084c
@@ -0,0 +1,62 @@
94084c
+/* Test the __sockaddr_un_set function.
94084c
+   Copyright (C) 2022 Free Software Foundation, Inc.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+/* Re-compile the function because the version in libc is not
94084c
+   exported.  */
94084c
+#include "sockaddr_un_set.c"
94084c
+
94084c
+#include <support/check.h>
94084c
+
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  struct sockaddr_un sun;
94084c
+
94084c
+  memset (&sun, 0xcc, sizeof (sun));
94084c
+  __sockaddr_un_set (&sun, "");
94084c
+  TEST_COMPARE (sun.sun_family, AF_UNIX);
94084c
+  TEST_COMPARE (__sockaddr_un_set (&sun, ""), 0);
94084c
+
94084c
+  memset (&sun, 0xcc, sizeof (sun));
94084c
+  TEST_COMPARE (__sockaddr_un_set (&sun, "/example"), 0);
94084c
+  TEST_COMPARE_STRING (sun.sun_path, "/example");
94084c
+
94084c
+  {
94084c
+    char pathname[108];         /* Length of sun_path (ABI constant).  */
94084c
+    memset (pathname, 'x', sizeof (pathname));
94084c
+    pathname[sizeof (pathname) - 1] = '\0';
94084c
+    memset (&sun, 0xcc, sizeof (sun));
94084c
+    TEST_COMPARE (__sockaddr_un_set (&sun, pathname), 0);
94084c
+    TEST_COMPARE (sun.sun_family, AF_UNIX);
94084c
+    TEST_COMPARE_STRING (sun.sun_path, pathname);
94084c
+  }
94084c
+
94084c
+  {
94084c
+    char pathname[109];
94084c
+    memset (pathname, 'x', sizeof (pathname));
94084c
+    pathname[sizeof (pathname) - 1] = '\0';
94084c
+    memset (&sun, 0xcc, sizeof (sun));
94084c
+    errno = 0;
94084c
+    TEST_COMPARE (__sockaddr_un_set (&sun, pathname), -1);
94084c
+    TEST_COMPARE (errno, EINVAL);
94084c
+  }
94084c
+
94084c
+  return 0;
94084c
+}
94084c
+
94084c
+#include <support/test-driver.c>