util/os_socket: Add socket related functions.
authorRafael Antognolli <rafael.antognolli@intel.com>
Wed, 11 Dec 2019 23:01:11 +0000 (15:01 -0800)
committerRafael Antognolli <rafael.antognolli@intel.com>
Fri, 13 Dec 2019 20:53:44 +0000 (20:53 +0000)
v3:
 - Add os_socket.c/h into Makefile.sources (Lionel)
 - Add empty non-linux implementation to public functions.

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
src/util/Makefile.sources
src/util/meson.build
src/util/os_socket.c [new file with mode: 0644]
src/util/os_socket.h [new file with mode: 0644]

index cce28db005effc15a317c639c97644459a24cd1f..71fe06a00962efa6dc89b46b5f7f50f2e6ea053f 100644 (file)
@@ -57,6 +57,8 @@ MESA_UTIL_FILES := \
        os_time.h \
        os_file.c \
        os_file.h \
        os_time.h \
        os_file.c \
        os_file.h \
+       os_socket.c \
+       os_socket.h \
        os_misc.c \
        os_misc.h \
        u_process.c \
        os_misc.c \
        os_misc.h \
        u_process.c \
index 540e4e9ce43b04f5d4da0ea8bd0428e7d054f441..88c6ab2d2a8f2d770705a63e981bdc9121a34cf1 100644 (file)
@@ -62,6 +62,8 @@ files_mesa_util = files(
   'os_file.c',
   'os_misc.c',
   'os_misc.h',
   'os_file.c',
   'os_misc.c',
   'os_misc.h',
+  'os_socket.c',
+  'os_socket.h',
   'u_process.c',
   'u_process.h',
   'sha1/sha1.c',
   'u_process.c',
   'u_process.h',
   'sha1/sha1.c',
diff --git a/src/util/os_socket.c b/src/util/os_socket.c
new file mode 100644 (file)
index 0000000..98ef013
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2019 Intel Corporation
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <errno.h>
+
+#include "os_socket.h"
+
+#if defined(__linux__)
+
+#include <fcntl.h>
+#include <poll.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int
+os_socket_listen_abstract(const char *path, int count)
+{
+   int s = socket(AF_UNIX, SOCK_STREAM, 0);
+   if (s < 0)
+      return -1;
+
+   struct sockaddr_un addr;
+   memset(&addr, 0, sizeof(addr));
+   addr.sun_family = AF_UNIX;
+   strncpy(addr.sun_path + 1, path, sizeof(addr.sun_path) - 2);
+
+   /* Create an abstract socket */
+   int ret = bind(s, (struct sockaddr*)&addr,
+                  offsetof(struct sockaddr_un, sun_path) +
+                  strlen(path) + 1);
+   if (ret < 0)
+      return -1;
+
+   listen(s, count);
+
+   return s;
+}
+
+int
+os_socket_accept(int s)
+{
+   return accept(s, NULL, NULL);
+}
+
+ssize_t
+os_socket_recv(int socket, void *buffer, size_t length, int flags)
+{
+   return recv(socket, buffer, length, flags);
+}
+
+ssize_t
+os_socket_send(int socket, const void *buffer, size_t length, int flags)
+{
+   return send(socket, buffer, length, flags);
+}
+
+void
+os_socket_block(int s, bool block)
+{
+   int old = fcntl(s, F_GETFL, 0);
+   if (old == -1)
+      return;
+
+   /* TODO obey block */
+   if (block)
+      fcntl(s, F_SETFL, old & ~O_NONBLOCK);
+   else
+      fcntl(s, F_SETFL, old | O_NONBLOCK);
+}
+
+void
+os_socket_close(int s)
+{
+   close(s);
+}
+
+#else
+
+int
+os_socket_listen_abstract(const char *path, int count)
+{
+   errno = -ENOSYS;
+   return -1;
+}
+
+int
+os_socket_accept(int s)
+{
+   errno = -ENOSYS;
+   return -1;
+}
+
+ssize_t
+os_socket_recv(int socket, void *buffer, size_t length, int flags)
+{
+   errno = -ENOSYS;
+   return -1;
+}
+
+ssize_t
+os_socket_send(int socket, const void *buffer, size_t length, int flags)
+{
+   errno = -ENOSYS;
+   return -1;
+}
+
+void
+os_socket_block(int s, bool block)
+{
+}
+
+void
+os_socket_close(int s)
+{
+}
+
+#endif
diff --git a/src/util/os_socket.h b/src/util/os_socket.h
new file mode 100644 (file)
index 0000000..0e413ef
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2019 Intel Corporation
+ * SPDX-License-Identifier: MIT
+ *
+ * Socket operations helpers
+ */
+
+#ifndef _OS_SOCKET_H_
+#define _OS_SOCKET_H_
+
+#include <stdio.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int os_socket_accept(int s);
+
+int os_socket_listen_abstract(const char *path, int count);
+
+ssize_t os_socket_recv(int socket, void *buffer, size_t length, int flags);
+ssize_t os_socket_send(int socket, const void *buffer, size_t length, int flags);
+
+void os_socket_block(int s, bool block);
+void os_socket_close(int s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _OS_SOCKET_H_ */