Add android_get_control_socket am: cdd2683cbf

Original change: https://android-review.googlesource.com/c/platform/system/librustutils/+/2306122

Change-Id: I00835268045dfb5519a9e67b1970571bbefb649b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/Android.bp b/Android.bp
index 179bff7..90f5f47 100644
--- a/Android.bp
+++ b/Android.bp
@@ -19,6 +19,7 @@
     defaults: ["librustutils_defaults"],
     shared_libs: [
         "libbase",
+        "libcutils",
     ],
     apex_available: [
         "//apex_available:platform",
@@ -71,6 +72,7 @@
     source_stem: "bindings",
     header_libs: ["libcutils_headers"],
     bindgen_flags: [
+        "--allowlist-function", "android_get_control_socket",
         "--allowlist-var", "AID_KEYSTORE",
         "--allowlist-var", "AID_ROOT",
         "--allowlist-var", "AID_SHELL",
diff --git a/bindgen/cutils.h b/bindgen/cutils.h
index 4ddf1e9..983f4a5 100644
--- a/bindgen/cutils.h
+++ b/bindgen/cutils.h
@@ -2,3 +2,4 @@
 
 #include <cutils/multiuser.h>
 #include <cutils/android_filesystem_config.h>
+#include <cutils/sockets.h>
diff --git a/lib.rs b/lib.rs
index 80f9fac..d15eb99 100644
--- a/lib.rs
+++ b/lib.rs
@@ -14,5 +14,6 @@
 
 //! Android rust utilities.
 
+pub mod sockets;
 pub mod system_properties;
 pub mod users;
diff --git a/sockets.rs b/sockets.rs
new file mode 100644
index 0000000..1453665
--- /dev/null
+++ b/sockets.rs
@@ -0,0 +1,44 @@
+// Copyright (C) 2022 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.
+
+//! Provides utilities for sockets.
+
+use std::ffi::CString;
+use std::os::unix::io::RawFd;
+use thiserror::Error;
+
+/// Errors this crate can generate
+#[derive(Error, Debug)]
+pub enum SocketError {
+    /// invalid name parameter
+    #[error("socket name {0} contains NUL byte")]
+    NulError(String),
+
+    /// android_get_control_socket failed to get a fd
+    #[error("android_get_control_socket({0}) failed")]
+    GetControlSocketFailed(String),
+}
+
+/// 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.
+pub fn android_get_control_socket(name: &str) -> Result<RawFd, SocketError> {
+    let cstr = CString::new(name).map_err(|_| SocketError::NulError(name.to_owned()))?;
+    // SAFETY: android_get_control_socket doesn't take ownership of name
+    let fd = unsafe { cutils_bindgen::android_get_control_socket(cstr.as_ptr()) };
+    if fd < 0 {
+        return Err(SocketError::GetControlSocketFailed(name.to_owned()));
+    }
+    Ok(fd)
+}