-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.py
49 lines (35 loc) · 1.12 KB
/
proc.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import shlex
import errno
class Process(object):
def __init__(self, cmd, autorun = True):
self.cmd = cmd
self.pid = 0
self.stdout, self.stdoutIN = os.pipe()
self.stderr, self.stderrIN = os.pipe()
self.splerr, self.splerrIN = os.pipe()
if autorun: self.run()
def run(self):
self.pid = os.fork()
if not self.pid:
os.close(self.splerr)
stdin = os.open('/dev/zero', os.O_RDONLY)
os.dup2(stdin, 0)
os.close(stdin)
os.dup2(self.stdoutIN, 1)
os.dup2(self.stderrIN, 2)
os.dup2(self.splerrIN, 255)
os.closerange(3, 255)
try:
_cmd = shlex.split(self.cmd)
except ValueError, E:
os.write(255, E.args[0])
os._exit(255)
try:
os.execvp(_cmd[0], _cmd)
except OSError, E:
os.write(255, E.args[1])
os._exit(E.errno)
os.close(self.stdoutIN)
os.close(self.stderrIN)
os.close(self.splerrIN)