mirror of
https://github.com/lutris/lutris
synced 2024-11-02 13:31:16 +00:00
Safe access to process stat
This commit is contained in:
parent
637f8b7c3c
commit
d30a80a9e5
1 changed files with 15 additions and 5 deletions
|
@ -21,7 +21,10 @@ class Process(object):
|
|||
return "Process {}".format(self.pid)
|
||||
|
||||
def get_stat(self, parsed=True):
|
||||
with open("/proc/{}/stat".format(self.pid)) as stat_file:
|
||||
stat_filename = "/proc/{}/stat".format(self.pid)
|
||||
if not os.path.exists(stat_filename):
|
||||
return
|
||||
with open(stat_filename) as stat_file:
|
||||
_stat = stat_file.readline()
|
||||
if parsed:
|
||||
return _stat[_stat.rfind(")")+1:].split()
|
||||
|
@ -54,7 +57,8 @@ class Process(object):
|
|||
def name(self):
|
||||
"""Filename of the executable"""
|
||||
_stat = self.get_stat(parsed=False)
|
||||
return _stat[_stat.find("(")+1:_stat.rfind(")")]
|
||||
if _stat:
|
||||
return _stat[_stat.find("(")+1:_stat.rfind(")")]
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
|
@ -63,17 +67,23 @@ class Process(object):
|
|||
sleep, Z is zombie, T is traced or stopped (on a signal), and W is
|
||||
paging.
|
||||
"""
|
||||
return self.get_stat()[0]
|
||||
_stat = self.get_stat()
|
||||
if _stat:
|
||||
return _stat[0]
|
||||
|
||||
@property
|
||||
def ppid(self):
|
||||
"""PID of the parent"""
|
||||
return self.get_stat()[1]
|
||||
_stat = self.get_stat()
|
||||
if _stat:
|
||||
return _stat[1]
|
||||
|
||||
@property
|
||||
def pgrp(self):
|
||||
"""Process group ID of the process"""
|
||||
return self.get_stat()[2]
|
||||
_stat = self.get_stat()
|
||||
if _stat:
|
||||
return _stat[2]
|
||||
|
||||
@property
|
||||
def cmdline(self):
|
||||
|
|
Loading…
Reference in a new issue