-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathposix_spawn.py
More file actions
32 lines (27 loc) · 1.07 KB
/
Copy pathposix_spawn.py
File metadata and controls
32 lines (27 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# This file is part of libdebug Python library (https://github.com/libdebug/libdebug).
# Copyright (c) 2024 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
import os
POSIX_SPAWN_CLOSE = 0
POSIX_SPAWN_DUP2 = 1
POSIX_SPAWN_OPEN = 2
def posix_spawn(file: str, argv: list, env: dict, file_actions: list, setpgroup: bool) -> int:
"""Spawn a new process, emulating the POSIX spawn function."""
child_pid = os.fork()
if child_pid == 0:
for element in file_actions:
if element[0] == POSIX_SPAWN_CLOSE:
os.close(element[1])
elif element[0] == POSIX_SPAWN_DUP2:
os.dup2(element[1], element[2])
elif element[0] == POSIX_SPAWN_OPEN:
fd, path, flags, mode = element[1:]
os.dup2(os.open(path, flags, mode), fd)
else:
raise ValueError("Invalid file action")
if setpgroup == 0:
os.setpgid(0, 0)
os.execve(file, argv, env)
return child_pid