team-ljyzipelo

공개
135줄4.0 KB
1use crate::protocol::ErrorCode;
2type Result<T> = std::result::Result<T, ErrorCode>;
3
4#[cfg(target_os = "linux")]
5pub(crate) struct Parent(i32);
6#[cfg(target_os = "linux")]
7unsafe extern "C" {
8 fn getppid() -> i32;
9}
10#[cfg(target_os = "linux")]
11impl Parent {
12 pub fn capture() -> Result<Self> {
13 let id = unsafe { getppid() };
14 if id <= 1 {
15 return Err(ErrorCode::Io);
16 }
17 Ok(Self(id))
18 }
19 pub fn check(&self) -> Result<()> {
20 if unsafe { getppid() } == self.0 {
21 Ok(())
22 } else {
23 Err(ErrorCode::Io)
24 }
25 }
26}
27
28#[cfg(windows)]
29mod windows {
30 use super::*;
31 use std::ffi::c_void;
32 type Handle = *mut c_void;
33 #[repr(C)]
34 struct Entry {
35 size: u32,
36 usage: u32,
37 pid: u32,
38 heap: usize,
39 module: u32,
40 threads: u32,
41 parent: u32,
42 priority: i32,
43 flags: u32,
44 executable: [u16; 260],
45 }
46 #[repr(C)]
47 #[derive(Default)]
48 struct Time {
49 low: u32,
50 high: u32,
51 }
52 unsafe extern "system" {
53 fn CreateToolhelp32Snapshot(flags: u32, pid: u32) -> Handle;
54 fn Process32FirstW(snapshot: Handle, entry: *mut Entry) -> i32;
55 fn Process32NextW(snapshot: Handle, entry: *mut Entry) -> i32;
56 fn GetCurrentProcessId() -> u32;
57 fn GetCurrentProcess() -> Handle;
58 fn OpenProcess(access: u32, inherit: i32, pid: u32) -> Handle;
59 fn GetProcessTimes(
60 handle: Handle,
61 creation: *mut Time,
62 exit: *mut Time,
63 kernel: *mut Time,
64 user: *mut Time,
65 ) -> i32;
66 fn WaitForSingleObject(handle: Handle, milliseconds: u32) -> u32;
67 fn CloseHandle(handle: Handle) -> i32;
68 }
69 fn created(handle: Handle) -> Result<u64> {
70 let (mut c, mut e, mut k, mut u) = (
71 Time::default(),
72 Time::default(),
73 Time::default(),
74 Time::default(),
75 );
76 if unsafe { GetProcessTimes(handle, &mut c, &mut e, &mut k, &mut u) } == 0 {
77 return Err(ErrorCode::Io);
78 }
79 Ok((c.high as u64) << 32 | c.low as u64)
80 }
81 pub(crate) struct Parent(Handle);
82 impl Parent {
83 pub fn capture() -> Result<Self> {
84 let snapshot = unsafe { CreateToolhelp32Snapshot(2, 0) };
85 if snapshot as isize == -1 {
86 return Err(ErrorCode::Io);
87 }
88 let mut entry: Entry = unsafe { std::mem::zeroed() };
89 entry.size = size_of::<Entry>() as u32;
90 let pid = unsafe { GetCurrentProcessId() };
91 let mut valid = unsafe { Process32FirstW(snapshot, &mut entry) };
92 let mut parent = 0;
93 while valid != 0 {
94 if entry.pid == pid {
95 parent = entry.parent;
96 break;
97 }
98 valid = unsafe { Process32NextW(snapshot, &mut entry) };
99 }
100 unsafe {
101 CloseHandle(snapshot);
102 }
103 if parent == 0 {
104 return Err(ErrorCode::Io);
105 }
106 let handle = unsafe { OpenProcess(0x100000 | 0x1000, 0, parent) };
107 if handle.is_null() {
108 return Err(ErrorCode::Io);
109 }
110 let guard = Self(handle);
111 // 부모 PID가 재사용된 경우 새 프로세스에 연결하지 않음.
112 if created(handle)? > created(unsafe { GetCurrentProcess() })? {
113 return Err(ErrorCode::Io);
114 }
115 guard.check()?;
116 Ok(guard)
117 }
118 pub fn check(&self) -> Result<()> {
119 if unsafe { WaitForSingleObject(self.0, 0) } == 258 {
120 Ok(())
121 } else {
122 Err(ErrorCode::Io)
123 }
124 }
125 }
126 impl Drop for Parent {
127 fn drop(&mut self) {
128 unsafe {
129 CloseHandle(self.0);
130 }
131 }
132 }
133}
134#[cfg(windows)]
135pub(crate) use windows::Parent;