Commit bf31f256 authored by Elouan Martinet's avatar Elouan Martinet

shell: add procfs-maps script

parent 2e3d3a16
#!/usr/bin/env python3
# vim:ts=2 et
"""
Copyright (C) 2019 Elouan Martinet <exa@elou.world>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys
from os import readlink
from glob import glob
from argparse import ArgumentParser
def get_all_pids():
proc_dirs = glob('/proc/*/')
pids = []
for d in proc_dirs:
dirname = d.split('/')[2]
if dirname.isnumeric():
pids.append(dirname)
return pids
def get_pid_path(pid):
try:
return readlink('/proc/' + pid + '/exe')
except FileNotFoundError:
return None
except PermissionError:
return False
def get_mapped_paths(pid):
with open('/proc/' + pid + '/maps') as maps:
for l in maps:
s = l.split(' ')
if len(s) > 1 and s[1][0] == '/':
s = s[1].split(' (deleted)')
if len(s) > 1:
deleted = True
path = s[0]
else:
deleted = False
path = s[0][:-1] # remove LF
yield path, deleted
def get_program_args():
parser = ArgumentParser(description='Get mapped paths in processes')
parser.add_argument('-p', '--pid', action='append', type=int, default=[])
parser.add_argument('-d', '--delimiter', default=':')
parser.add_argument('-a', '--all', action='store_true')
parser.add_argument('-q', '--quiet', action='store_true')
return parser.parse_args()
args = get_program_args()
pids = list(map(str, args.pid)) if args.pid else get_all_pids()
sep = args.delimiter
for pid in pids:
exepath = get_pid_path(pid)
if exepath:
for path, deleted in get_mapped_paths(pid):
if args.all:
print(pid + sep + exepath + sep + path + sep + ('deleted' if deleted else ''))
elif deleted:
print(pid + sep + exepath + sep + path)
elif args.pid and not args.quiet:
if exepath == False:
print(pid + sep + 'denied', file=sys.stderr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment