• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #![cfg(target_os = "android")]
6 
7 use std::ffi::CString;
8 
9 use anyhow::anyhow;
10 use anyhow::Result;
11 use libc;
12 
13 #[link(name = "processgroup")]
14 extern "C" {
android_set_process_profiles( uid: libc::uid_t, pid: libc::pid_t, num_profiles: libc::size_t, profiles: *const *const libc::c_char, ) -> bool15     fn android_set_process_profiles(
16         uid: libc::uid_t,
17         pid: libc::pid_t,
18         num_profiles: libc::size_t,
19         profiles: *const *const libc::c_char,
20     ) -> bool;
21 }
22 
23 // Apply the listed task profiles to all tasks (current and future) in this process.
set_process_profiles(profiles: &[String]) -> Result<()>24 pub fn set_process_profiles(profiles: &[String]) -> Result<()> {
25     if profiles.is_empty() {
26         return Ok(());
27     }
28     let owned: Vec<CString> = profiles
29         .iter()
30         .map(|s| CString::new(s.clone()).unwrap())
31         .collect();
32     let ptrs: Vec<*const libc::c_char> = owned.iter().map(|s| s.as_ptr()).collect();
33     // SAFETY: the ownership of the array of string is not passed. The function copies it
34     // internally.
35     unsafe {
36         if android_set_process_profiles(libc::getuid(), libc::getpid(), ptrs.len(), ptrs.as_ptr()) {
37             Ok(())
38         } else {
39             Err(anyhow!("failed to set task profiles"))
40         }
41     }
42 }
43