• 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::CStr;
8 use std::ffi::CString;
9 
10 use anyhow::anyhow;
11 use anyhow::Result;
12 use libc;
13 
14 #[link(name = "processgroup")]
15 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, ) -> bool16     fn android_set_process_profiles(
17         uid: libc::uid_t,
18         pid: libc::pid_t,
19         num_profiles: libc::size_t,
20         profiles: *const *const libc::c_char,
21     ) -> bool;
22 }
23 
24 // Apply the listed task profiles to all tasks (current and future) in this process.
set_process_profiles(profiles: &Vec<String>) -> Result<()>25 pub fn set_process_profiles(profiles: &Vec<String>) -> Result<()> {
26     if (profiles.is_empty()) {
27         return Ok(());
28     }
29     let owned: Vec<CString> = profiles
30         .iter()
31         .map(|s| CString::new(s.clone()).unwrap())
32         .collect();
33     let ptrs: Vec<*const libc::c_char> = owned.iter().map(|s| s.as_ptr()).collect();
34     // SAFETY: the ownership of the array of string is not passed. The function copies it
35     // internally.
36     unsafe {
37         if (android_set_process_profiles(libc::getuid(), libc::getpid(), ptrs.len(), ptrs.as_ptr()))
38         {
39             Ok(())
40         } else {
41             Err(anyhow!("failed to set task profiles"))
42         }
43     }
44 }
45