In the magic Linux world 🐧 there is one specific entity with the label ‘pseudo file system’. It sits tight and altough is a little bit jealous that it’s not part of the /etc/fstab it’s the kernel best sharing friend and exposes all of its secrets.
After this introduction it’s time to give a down to earth explanation. In Linux you have a directory with the path and name /proc, which for the most part exposes each process that is running. If you wake up one day really curious about the number of open files a process is currently handling this is the best place to go.
Let’s visit this land with our best below buddy.
import threadingimport timeimport os print("PID: {0}".format(os.getpid())) def write(filename, n): with open(filename, "w") as f: f.write("vip msg") f.flush() time.sleep(100) threads = [] for i in range(0, 10): filename = "file-{0}".format(str(i)) t = threading.Thread(target=write, args=(filename, i,)) threads.append(t)for t in threads: t.start()for t in threads: t.join()
Long story short we are opening 10 files at once, write very important message to each one of them and wait for some time each worker.
python3 write.pyPID: 16899
Now let’s go to our visit place and see how it’s doing on one of its addresses 16899.
cd /proc/16899//proc/16899$ lsarch_status exe mem personality statusattr fd mountinfo projid_map syscallautogroup fdinfo mounts root taskauxv gid_map mountstats sched timens_offsetscgroup io net schedstat timersclear_refs ksm_merging_pages ns sessionid timerslack_nscmdline ksm_stat numa_maps setgroups uid_mapcomm latency oom_adj smaps wchancoredump_filter limits oom_score smaps_rollupcpu_resctrl_groups loginuid oom_score_adj stackcwd map_files pagemap statenviron maps patch_state statm
cmdline
environ
fd
fdinfo
limits
mounts
status
task
io
stack
From userland activity there are a few valuable piece of information that we could grab, specificially about processes. Once we get the process ID we care about ( ps -ef ) there are many options, but as a starter we could get the command the process was intitially launched with and the environment variables dedicated to it.
🐧
cat /proc/[pid]/cmdline
cat /proc/[pid]/environ
🐧
And of course all the file descriptors dedicated to this process. In lean times there are only 3 of them :)
tree /proc/[pid]/fd/
/proc/[pid]/fd/
├── 0 -> /dev/pts/0
├── 1 -> /dev/pts/0
└── 2 -> /dev/pts/0



Leave a comment