blob: b408be60a7049e8f9d79208088e6e4eea630b153 [file] [log] [blame]
Paul Crowley9da969e2022-09-16 23:42:24 +00001// Copyright (C) 2022 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::ffi::CString;
16use std::os::unix::{net::UnixListener, prelude::FromRawFd};
17
18use anyhow::{ensure, Result};
19
20pub fn android_get_control_socket(name: &str) -> Result<UnixListener> {
21 let name = CString::new(name)?;
Andrew Walbranc7687332023-07-21 17:26:09 +010022 // SAFETY: name is a valid C string, and android_get_control_socket doesn't retain it after it
23 // returns.
Paul Crowley9da969e2022-09-16 23:42:24 +000024 let fd = unsafe { cutils_socket_bindgen::android_get_control_socket(name.as_ptr()) };
25 ensure!(fd >= 0, "android_get_control_socket failed");
Andrew Walbranc7687332023-07-21 17:26:09 +010026 // SAFETY: android_get_control_socket either returns a valid and open FD or -1, and we checked
27 // that it's not -1.
Paul Crowley9da969e2022-09-16 23:42:24 +000028 Ok(unsafe { UnixListener::from_raw_fd(fd) })
29}