[Yum-devel] [PATCH 2/3] add two functions to get list of processes and return the files the processes have open

Mike Bonnet mikeb at redhat.com
Tue Dec 8 23:08:33 UTC 2009


On 12/08/2009 04:45 PM, Seth Vidal wrote:
> ---
>  yum/misc.py |   30 ++++++++++++++++++++++++++++++
>  1 files changed, 30 insertions(+), 0 deletions(-)
> 
> diff --git a/yum/misc.py b/yum/misc.py
> index 642f9a2..bfebc38 100644
> --- a/yum/misc.py
> +++ b/yum/misc.py
> @@ -873,3 +873,33 @@ def get_my_lang_code():
>      
>      return mylang
>      
> +def return_running_pids():
> +    """return list of running processids, excluding this one"""
> +    mypid = os.getpid()
> +    pids = []
> +    for fn in glob.glob('/proc/[0123456789]*'):
> +        if mypid == os.path.basename(fn):
> +            continue
> +        pids.append(os.path.basename(fn))
> +    return pids
> +
> +def get_open_files(pid):
> +    """returns files open from this pid"""
> +    files = []
> +    smaps = '/proc/%s/smaps' % pid

/proc/%s/maps would work too, has fewer irrelevant lines to skip over,
and isn't dependent on CONFIG_MMU.

> +    try:
> +        maps = open(smaps, 'r')
> +    except (IOError, OSError), e:
> +        return files
> +
> +    for line in maps.readlines():
> +        if line.find('fd:') == -1:
> +            continue
> +        line = line.replace('\n', '')
> +        slash = line.find('/')
> +        filename = line[slash:]
> +        filename = filename.replace('(deleted)', '') #only mildly retarded
> +        filename = filename.strip()
> +        if filename not in files:
> +            files.append(filename)
> +    return files



More information about the Yum-devel mailing list