xref: /aosp_15_r20/external/crosvm/base/src/sys/linux/descriptor.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2020 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 use crate::descriptor::SafeDescriptor;
6 
7 const KCMP_FILE: u32 = 0;
8 
9 impl PartialEq for SafeDescriptor {
eq(&self, other: &Self) -> bool10     fn eq(&self, other: &Self) -> bool {
11         // If RawFd numbers match then we can return early without calling kcmp
12         if self.descriptor == other.descriptor {
13             return true;
14         }
15 
16         // SAFETY:
17         // safe because we only use the return value and libc says it's always successful
18         let pid = unsafe { libc::getpid() };
19         // SAFETY:
20         // safe because we are passing everything by value and checking the return value
21         let ret = unsafe {
22             libc::syscall(
23                 libc::SYS_kcmp,
24                 pid,
25                 pid,
26                 KCMP_FILE,
27                 self.descriptor,
28                 other.descriptor,
29             )
30         };
31 
32         ret == 0
33     }
34 }
35