build: Eliminate cutils/sockets dependencies from system/bt

Eliminate cutils/sockets dependencies by pulling sources files from
core/libcutils into osi/.
   - Pulled source files from cutils/ into osi/ and modified GN files and
     Android.mk
   - Updated includes to use the headers in osi/ and removed unused/duplicated
     headers.
   - Renamed the functions of osi/sockets and updated wherever they are called
     to avoid usign symbols from cutils/sockets, since other cutils
     dependencies have not been eliminated yet.

Bug: 21667795
Change-Id: I3dd4371d585f120d97ac0433ab1f35edb820dbb9
diff --git a/audio_a2dp_hw/audio_a2dp_hw.c b/audio_a2dp_hw/audio_a2dp_hw.c
index f15ec99..7f5cea4 100644
--- a/audio_a2dp_hw/audio_a2dp_hw.c
+++ b/audio_a2dp_hw/audio_a2dp_hw.c
@@ -25,29 +25,29 @@
  *****************************************************************************/
 
 #include <errno.h>
+#include <fcntl.h>
 #include <inttypes.h>
 #include <pthread.h>
 #include <stdint.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/poll.h>
 #include <sys/errno.h>
+#include <sys/poll.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/un.h>
 #include <unistd.h>
-#include <fcntl.h>
+
 #include <cutils/str_parms.h>
-#include <cutils/sockets.h>
-
-#include <system/audio.h>
 #include <hardware/audio.h>
-
 #include <hardware/hardware.h>
-#include "audio_a2dp_hw.h"
-#include "bt_utils.h"
+#include <system/audio.h>
 
 #define LOG_TAG "bt_a2dp_hw"
+
+#include "audio_a2dp_hw.h"
+#include "bt_utils.h"
 #include "osi/include/log.h"
+#include "osi/include/socket_utils/sockets.h"
 
 /*****************************************************************************
 **  Constants & Macros
@@ -205,7 +205,7 @@
 
     skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
 
-    if(socket_local_client_connect(skt_fd, path,
+    if(osi_socket_local_client_connect(skt_fd, path,
             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0)
     {
         ERROR("failed to connect (%s)", strerror(errno));
diff --git a/btif/co/bta_hl_co.c b/btif/co/bta_hl_co.c
index 9aa91f7..c06ad5c 100644
--- a/btif/co/bta_hl_co.c
+++ b/btif/co/bta_hl_co.c
@@ -23,26 +23,25 @@
  *
  ******************************************************************************/
 
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <string.h>
-#include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <sys/un.h>
 #include <time.h>
-#include <fcntl.h>
-#include <ctype.h>
-#include <cutils/sockets.h>
+
 #include "bta_api.h"
-#include "btm_api.h"
-#include "bta_sys.h"
 #include "bta_hl_api.h"
-#include "bta_hl_co.h"
 #include "bta_hl_ci.h"
+#include "bta_hl_co.h"
+#include "bta_sys.h"
 #include "btif_hl.h"
 #include "btif_util.h"
-
+#include "btm_api.h"
 
 /*****************************************************************************
 **  Constants and Data Types
diff --git a/btif/src/btif_hl.c b/btif/src/btif_hl.c
index 2f370a5..43d4a81 100644
--- a/btif/src/btif_hl.c
+++ b/btif/src/btif_hl.c
@@ -28,38 +28,25 @@
 
 #include <assert.h>
 #include <ctype.h>
-#include <cutils/sockets.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <hardware/bluetooth.h>
-#include <hardware/bt_hl.h>
 #include <pthread.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/poll.h>
+#include <sys/prctl.h>
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
 #include <time.h>
 #include <unistd.h>
-#include <pthread.h>
-#include <signal.h>
-#include <ctype.h>
-#include <sys/select.h>
-#include <sys/poll.h>
-#include <sys/prctl.h>
-#include <cutils/sockets.h>
-#include <cutils/log.h>
 
 #include <hardware/bluetooth.h>
 #include <hardware/bt_hl.h>
 
-#include "btif_common.h"
-#include "btif_util.h"
-#include "gki.h"
 #include "bta_api.h"
 #include "bta_hl_api.h"
 #include "btif_common.h"
@@ -68,8 +55,8 @@
 #include "btif_util.h"
 #include "btu.h"
 #include "gki.h"
-#include "osi/include/list.h"
 #include "mca_api.h"
+#include "osi/include/list.h"
 #include "osi/include/log.h"
 
 #define MAX_DATATYPE_SUPPORTED 8
diff --git a/btif/src/btif_sock_thread.c b/btif/src/btif_sock_thread.c
index 459aeba..8b39186 100644
--- a/btif/src/btif_sock_thread.c
+++ b/btif/src/btif_sock_thread.c
@@ -28,36 +28,33 @@
 #include <hardware/bluetooth.h>
 #include <hardware/bt_sock.h>
 
-//bta_jv_co_rfc_data
+#include <alloca.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <features.h>
+#include <pthread.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <features.h>
 #include <string.h>
-#include <sys/types.h>
+#include <sys/poll.h>
+#include <sys/select.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <sys/un.h>
 #include <time.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <signal.h>
-#include <pthread.h>
-#include <ctype.h>
-
-#include <sys/select.h>
-#include <sys/poll.h>
-#include <cutils/sockets.h>
-#include <alloca.h>
 
 #define LOG_TAG "bt_btif_sock"
-#include "btif_common.h"
-#include "btif_util.h"
-
 
 #include "bta_api.h"
+#include "btif_common.h"
 #include "btif_sock.h"
 #include "btif_sock_thread.h"
 #include "btif_sock_util.h"
+#include "btif_util.h"
+#include "osi/include/socket_utils/sockets.h"
 
 #define asrt(s) if(!(s)) APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
 #define print_events(events) do { \
@@ -129,7 +126,7 @@
     if(s < 0)
         return -1;
     APPL_TRACE_DEBUG("covert name to android abstract name:%s", name);
-    if(socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) >= 0)
+    if(osi_socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) >= 0)
     {
         if(listen(s, 5) == 0)
         {
@@ -148,7 +145,7 @@
     if(s < 0)
         return -1;
     set_socket_blocking(s, TRUE);
-    if(socket_local_client_connect(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) >= 0)
+    if(osi_socket_local_client_connect(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) >= 0)
     {
         APPL_TRACE_DEBUG("connected to local socket:%s, fd:%d", name, s);
         return s;
diff --git a/btif/src/btif_sock_util.c b/btif/src/btif_sock_util.c
index 4ce5270..e4c10c4 100644
--- a/btif/src/btif_sock_util.c
+++ b/btif/src/btif_sock_util.c
@@ -25,45 +25,40 @@
  *
  *
  ***********************************************************************************/
+#include <arpa/inet.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+
 #include <hardware/bluetooth.h>
 #include <hardware/bt_sock.h>
-#include <errno.h>
-#include <sys/ioctl.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-
-#include <cutils/sockets.h>
-#include <netinet/tcp.h>
-
 
 #define LOG_TAG "bt_btif_sock"
-#include "btif_common.h"
-#include "btif_util.h"
-
-#include "bta_api.h"
-#include "btif_sock_thread.h"
-#include "btif_sock_sdp.h"
 
 #include "bt_target.h"
-#include "gki.h"
-#include "hcimsgs.h"
-#include "sdp_api.h"
-#include "btu.h"
-#include "btm_api.h"
-#include "btm_int.h"
+#include "bta_api.h"
 #include "bta_jv_api.h"
 #include "bta_jv_co.h"
-#include "port_api.h"
+#include "btif_common.h"
+#include "btif_sock_sdp.h"
+#include "btif_sock_thread.h"
+#include "btif_util.h"
+#include "btm_api.h"
+#include "btm_int.h"
+#include "btu.h"
+#include "gki.h"
+#include "hcimsgs.h"
 #include "osi/include/log.h"
+#include "port_api.h"
+#include "sdp_api.h"
 
 #define asrt(s) if(!(s)) BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
 
diff --git a/osi/Android.mk b/osi/Android.mk
index 36a8659..ad22e20 100644
--- a/osi/Android.mk
+++ b/osi/Android.mk
@@ -27,6 +27,8 @@
     $(LOCAL_PATH)/include \
     $(LOCAL_PATH)/..
 
+# TODO(mcchou): Remove socket_utils sources after platform specific
+# dependencies are abstracted.
 LOCAL_SRC_FILES := \
     ./src/alarm.c \
     ./src/allocation_tracker.c \
@@ -46,6 +48,8 @@
     ./src/ringbuffer.c \
     ./src/semaphore.c \
     ./src/socket.c \
+    ./src/socket_utils/socket_local_client.c \
+    ./src/socket_utils/socket_local_server.c \
     ./src/thread.c
 
 LOCAL_CFLAGS := -std=c99 -Wall -Werror -fvisibility=hidden
diff --git a/osi/BUILD.gn b/osi/BUILD.gn
index 3c12636..3284aaf 100644
--- a/osi/BUILD.gn
+++ b/osi/BUILD.gn
@@ -34,6 +34,12 @@
     "src/reactor.c",
     "src/semaphore.c",
     "src/socket.c",
+
+    # TODO(mcchou): Remove these sources after platform specific
+    # dependencies are abstracted.
+    "src/socket_utils/socket_local_client.c",
+    "src/socket_utils/socket_local_server.c",
+
     "src/thread.c",
   ]
 
diff --git a/osi/include/socket_utils/socket_local.h b/osi/include/socket_utils/socket_local.h
new file mode 100644
index 0000000..1471b00
--- /dev/null
+++ b/osi/include/socket_utils/socket_local.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2006 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#define FILESYSTEM_SOCKET_PREFIX "/tmp/"
+#define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/"
+
+/*
+ * Set up a given sockaddr_un, to have it refer to the given
+ * name in the given namespace. The namespace must be one
+ * of <code>ANDROID_SOCKET_NAMESPACE_ABSTRACT</code>,
+ * <code>ANDROID_SOCKET_NAMESPACE_RESERVED</code>, or
+ * <code>ANDROID_SOCKET_NAMESPACE_FILESYSTEM</code>. Upon success,
+ * the pointed at sockaddr_un is filled in and the pointed at
+ * socklen_t is set to indicate the final length. This function
+ * will fail if the namespace is invalid (not one of the indicated
+ * constants) or if the name is too long.
+ *
+ * @return 0 on success or -1 on failure
+ */
+int osi_socket_make_sockaddr_un(const char *name, int namespaceId,
+                                struct sockaddr_un *p_addr, socklen_t *alen);
diff --git a/osi/include/socket_utils/sockets.h b/osi/include/socket_utils/sockets.h
new file mode 100644
index 0000000..5f0ac3a
--- /dev/null
+++ b/osi/include/socket_utils/sockets.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2006 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+
+#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_"
+#define ANDROID_SOCKET_DIR "/dev/socket"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * osi_android_get_control_socket - simple helper function to get the file
+ * descriptor of our init-managed Unix domain socket. `name' is the name of the
+ * socket, as given in init.rc. Returns -1 on error.
+ *
+ * This is inline and not in libcutils proper because we want to use this in
+ * third-party daemons with minimal modification.
+ */
+static inline int osi_android_get_control_socket(const char *name) {
+  char key[64];
+  snprintf(key, sizeof(key), ANDROID_SOCKET_ENV_PREFIX "%s", name);
+
+  const char *val = getenv(key);
+  if (!val) {
+    return -1;
+  }
+
+  errno = 0;
+  int fd = strtol(val, NULL, 10);
+  if (errno) {
+    return -1;
+  }
+
+  return fd;
+}
+
+/*
+ * See also android.os.LocalSocketAddress.Namespace
+ */
+// Linux "abstract" (non-filesystem) namespace
+#define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0
+// Android "reserved" (/dev/socket) namespace
+#define ANDROID_SOCKET_NAMESPACE_RESERVED 1
+// Normal filesystem namespace
+#define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2
+
+extern int osi_socket_local_server(const char *name, int namespaceId, int type);
+extern int osi_socket_local_server_bind(int s, const char *name,
+                                        int namespaceId);
+extern int osi_socket_local_client_connect(int fd, const char *name,
+                                           int namespaceId, int type);
+extern int osi_socket_local_client(const char *name, int namespaceId, int type);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/osi/src/socket_utils/README b/osi/src/socket_utils/README
new file mode 100644
index 0000000..8616447
--- /dev/null
+++ b/osi/src/socket_utils/README
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+The sources in this folder re-implement some of the sources in libcutils/sockets
+to provide a short-term solution eliminating libcutils dependency from
+system/bt. Once a long-term platform-independent abstraction is presented, these
+sources and the corresponding headers should be removed.
+
+Note that only a part of the source files are pulled from libcutils/sockets, and
+"osi_" prefix is added to all functions. The developers who want to pull sockets
+sources other than the existing ones must put the sources in this folder and
+refactor the functions as well.
+
+The current sources include:
+
+[Headers]
+ - osi/include/socket_utils/sockets.h
+ - osi/include/socket_utils/socket_local.h
+[Source files]
+ - osi/src/socket_utils/socket_local_client.c
+ - osi/src/socket_utils/socket_local_server.c
+
+Please update the above list if adding more sources.
diff --git a/osi/src/socket_utils/socket_local_client.c b/osi/src/socket_utils/socket_local_client.c
new file mode 100644
index 0000000..ccfe743
--- /dev/null
+++ b/osi/src/socket_utils/socket_local_client.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2006 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include "osi/include/osi.h"
+#include "osi/include/socket_utils/sockets.h"
+#include "osi/include/socket_utils/socket_local.h"
+
+#define LISTEN_BACKLOG 4
+
+/* Documented in header file. */
+int osi_socket_make_sockaddr_un(const char *name, int namespaceId,
+                                struct sockaddr_un *p_addr, socklen_t *alen) {
+  memset(p_addr, 0, sizeof(*p_addr));
+  size_t namelen;
+
+  switch (namespaceId) {
+    case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
+#if defined(__linux__)
+      namelen = strlen(name);
+
+      // Test with length +1 for the *initial* '\0'.
+      if ((namelen + 1) > sizeof(p_addr->sun_path)) {
+        goto error;
+      }
+
+      /*
+       * Note: The path in this case is *not* supposed to be
+       * '\0'-terminated. ("man 7 unix" for the gory details.)
+       */
+
+      p_addr->sun_path[0] = 0;
+      memcpy(p_addr->sun_path + 1, name, namelen);
+#else
+      /* this OS doesn't have the Linux abstract namespace */
+
+      namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
+      /* unix_path_max appears to be missing on linux */
+      if (namelen >
+          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+        goto error;
+      }
+
+      strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
+      strcat(p_addr->sun_path, name);
+#endif
+      break;
+
+    case ANDROID_SOCKET_NAMESPACE_RESERVED:
+      namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
+      /* unix_path_max appears to be missing on linux */
+      if (namelen >
+          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+        goto error;
+      }
+
+      strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
+      strcat(p_addr->sun_path, name);
+      break;
+
+    case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
+      namelen = strlen(name);
+      /* unix_path_max appears to be missing on linux */
+      if (namelen >
+          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+        goto error;
+      }
+
+      strcpy(p_addr->sun_path, name);
+      break;
+    default:
+      // invalid namespace id
+      return -1;
+  }
+
+  p_addr->sun_family = AF_LOCAL;
+  *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
+  return 0;
+error:
+  return -1;
+}
+
+/**
+ * connect to peer named "name" on fd
+ * returns same fd or -1 on error.
+ * fd is not closed on error. that's your job.
+ *
+ * Used by AndroidSocketImpl
+ */
+int osi_socket_local_client_connect(int fd, const char *name, int namespaceId,
+                                    int type UNUSED_ATTR) {
+  struct sockaddr_un addr;
+  socklen_t alen;
+  int err;
+
+  err = osi_socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
+
+  if (err < 0) {
+    goto error;
+  }
+
+  if (connect(fd, (struct sockaddr *)&addr, alen) < 0) {
+    goto error;
+  }
+
+  return fd;
+
+error:
+  return -1;
+}
+
+/**
+ * connect to peer named "name"
+ * returns fd or -1 on error
+ */
+int osi_socket_local_client(const char *name, int namespaceId, int type) {
+  int s;
+
+  s = socket(AF_LOCAL, type, 0);
+  if (s < 0) return -1;
+
+  if (0 > osi_socket_local_client_connect(s, name, namespaceId, type)) {
+    close(s);
+    return -1;
+  }
+
+  return s;
+}
diff --git a/osi/src/socket_utils/socket_local_server.c b/osi/src/socket_utils/socket_local_server.c
new file mode 100644
index 0000000..466cc1c
--- /dev/null
+++ b/osi/src/socket_utils/socket_local_server.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2006, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <netinet/in.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/select.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "osi/include/socket_utils/sockets.h"
+#include "osi/include/socket_utils/socket_local.h"
+
+#define LISTEN_BACKLOG 4
+
+/* Only the bottom bits are really the socket type; there are flags too. */
+#define SOCK_TYPE_MASK 0xf
+
+/**
+ * Binds a pre-created socket(AF_LOCAL) 's' to 'name'
+ * returns 's' on success, -1 on fail
+ *
+ * Does not call listen()
+ */
+int osi_socket_local_server_bind(int s, const char *name, int namespaceId) {
+  struct sockaddr_un addr;
+  socklen_t alen;
+  int n;
+  int err;
+
+  err = osi_socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
+
+  if (err < 0) {
+    return -1;
+  }
+
+/* basically: if this is a filesystem path, unlink first */
+#if !defined(__linux__)
+  if (1) {
+#else
+  if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED ||
+      namespaceId == ANDROID_SOCKET_NAMESPACE_FILESYSTEM) {
+#endif
+    /*ignore ENOENT*/
+    unlink(addr.sun_path);
+  }
+
+  n = 1;
+  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
+
+  if (bind(s, (struct sockaddr *)&addr, alen) < 0) {
+    return -1;
+  }
+
+  return s;
+}
+
+/** Open a server-side UNIX domain datagram socket in the Linux non-filesystem
+ *  namespace
+ *
+ *  Returns fd on success, -1 on fail
+ */
+int osi_socket_local_server(const char *name, int namespace, int type) {
+  int err;
+  int s;
+
+  s = socket(AF_LOCAL, type, 0);
+  if (s < 0) return -1;
+
+  err = osi_socket_local_server_bind(s, name, namespace);
+
+  if (err < 0) {
+    close(s);
+    return -1;
+  }
+
+  if ((type & SOCK_TYPE_MASK) == SOCK_STREAM) {
+    int ret;
+
+    ret = listen(s, LISTEN_BACKLOG);
+
+    if (ret < 0) {
+      close(s);
+      return -1;
+    }
+  }
+
+  return s;
+}
diff --git a/service/main.cpp b/service/main.cpp
index a818f74..c1668be 100644
--- a/service/main.cpp
+++ b/service/main.cpp
@@ -18,16 +18,14 @@
 #include <stdlib.h>
 
 #define LOG_TAG "BtHost"
-#include "osi/include/log.h"
 // For system properties
 // TODO(icoolidge): abstraction or non-cutils stub.
 #include <cutils/properties.h>
-// For init socket environment variable decode
-// TODO(icoolidge): abstraction or remove.
-#include <cutils/sockets.h>
 
 #include "core_stack.h"
 #include "host.h"
+#include "osi/include/log.h"
+#include "osi/include/socket_utils/sockets.h"
 
 namespace {
 
@@ -44,7 +42,7 @@
     return EXIT_SUCCESS;
   }
 
-  int server_socket = android_get_control_socket(kSocketFromInit);
+  int server_socket = osi_android_get_control_socket(kSocketFromInit);
   if (server_socket == -1) {
     LOG_ERROR("failed to get socket from init");
     return EXIT_FAILURE;
diff --git a/udrv/BUILD.gn b/udrv/BUILD.gn
index 6b94bfe..9562e32 100644
--- a/udrv/BUILD.gn
+++ b/udrv/BUILD.gn
@@ -22,6 +22,7 @@
   include_dirs = [
     "include",
     "uipc",
+    "//",
     "//audio_a2dp_hw",
     "//include",
     "//gki/common",
diff --git a/udrv/ulinux/uipc.c b/udrv/ulinux/uipc.c
index c3f8527..8fbffa7 100644
--- a/udrv/ulinux/uipc.c
+++ b/udrv/ulinux/uipc.c
@@ -24,33 +24,28 @@
  *
  *****************************************************************************/
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <pthread.h>
-#include <sys/select.h>
-#include <sys/poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/mman.h>
-#include <sys/stat.h>
+#include <sys/poll.h>
 #include <sys/prctl.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <unistd.h>
 
-
-#include "gki.h"
-#include "bt_types.h"
-#include "uipc.h"
-
-#include <cutils/sockets.h>
 #include "audio_a2dp_hw.h"
+#include "bt_types.h"
 #include "bt_utils.h"
+#include "gki.h"
+#include "osi/include/socket_utils/sockets.h"
+#include "uipc.h"
 
 /*****************************************************************************
 **  Constants & Macros
@@ -153,7 +148,7 @@
 
     BTIF_TRACE_EVENT("create_server_socket %s", name);
 
-    if(socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0)
+    if(osi_socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0)
     {
         BTIF_TRACE_EVENT("socket failed to create (%s)", strerror(errno));
         close(s);