πŸ— Wiki

proc

proc

/proc directory in Linux is a special directory, a virtual file system that is created when Linux system boots. You can find useful information about process in running, network information including port inodes, and so on.

Because of this, many program that have to be deal with Linux internal rely on the /proc file system.

1. What files are there?

1.1. uptime

You can see how much time has elapsed since you boot the system. First column is actual running time in seconds, and the second is idle time in seconds.

$ cat /proc/uptime
2494.87 39615.72

Wow, I’ve keep turning on the system about 11 hours!

2. Tips

2.1. Querying opened file (file descriptor)

You need pid of target process.

$ ps -e | grep bash
   363 pts/0    00:00:00 bash

There is fd directory under /proc/<pid> directory. In this case, because the pid of bash is 363, there should be /proc/363/fd directory.

$ ls -l /proc/363/fd
total 0
lrwx------ 1 ch1keen ch1keen 64 Jun 20 21:55 0 -> /dev/pts/0
lrwx------ 1 ch1keen ch1keen 64 Jun 20 21:55 1 -> /dev/pts/0
lrwx------ 1 ch1keen ch1keen 64 Jun 20 21:55 2 -> /dev/pts/0
lrwx------ 1 ch1keen ch1keen 64 Jun 20 21:55 255 -> /dev/pts/0

You might want to use the readlink command to see which binary is linked to the symbolic link.

$ readlink /proc/363/fd/255
/dev/pts/0

This might be useful when you use strace tool. When you use it, sometimes you can see the process are writing or reading to file descriptors, and these are written in number. You can query up what these number, the file descriptors, is actually linked.

2.2. Querying shared objects of the process

There are maps file under /proc/<pid>/ directory.

To do this, you need pid of target process, and the process should be running.

$ cat /proc/363/maps
55aed4e93000-55aed4ec2000 r--p 00000000 08:20 1234316                    /usr/bin/bash
55aed4ec2000-55aed4fa1000 r-xp 0002f000 08:20 1234316                    /usr/bin/bash
55aed4fa1000-55aed4fdb000 r--p 0010e000 08:20 1234316                    /usr/bin/bash
55aed4fdc000-55aed4fe0000 r--p 00148000 08:20 1234316                    /usr/bin/bash
55aed4fe0000-55aed4fe9000 rw-p 0014c000 08:20 1234316                    /usr/bin/bash
55aed4fe9000-55aed4ff4000 rw-p 00000000 00:00 0
55aed6e89000-55aed7011000 rw-p 00000000 00:00 0                          [heap]
7f970ac44000-7f970ac9b000 r--p 00000000 08:20 1221925                    /usr/lib/locale/C.utf8/LC_CTYPE
7f970ac9b000-7f970ac9c000 r--p 00000000 08:20 1221944                    /usr/lib/locale/C.utf8/LC_NUMERIC
7f970ac9c000-7f970ac9d000 r--p 00000000 08:20 1221973                    /usr/lib/locale/C.utf8/LC_TIME
7f970ac9d000-7f970ac9e000 r--p 00000000 08:20 1221801                    /usr/lib/locale/C.utf8/LC_COLLATE
7f970ac9e000-7f970ac9f000 r--p 00000000 08:20 1221939                    /usr/lib/locale/C.utf8/LC_MONETARY
7f970ac9f000-7f970aca0000 r--p 00000000 08:20 1221938                    /usr/lib/locale/C.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f970aca0000-7f970aca1000 r--p 00000000 08:20 1221955                    /usr/lib/locale/C.utf8/LC_PAPER
7f970aca1000-7f970aca4000 rw-p 00000000 00:00 0
7f970aca4000-7f970accc000 r--p 00000000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970accc000-7f970ae61000 r-xp 00028000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970ae61000-7f970aeb9000 r--p 001bd000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970aeb9000-7f970aeba000 ---p 00215000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970aeba000-7f970aebe000 r--p 00215000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970aebe000-7f970aec0000 rw-p 00219000 08:20 1221993                    /usr/lib/x86_64-linux-gnu/libc.so.6
7f970aec0000-7f970aecd000 rw-p 00000000 00:00 0
7f970aecd000-7f970aedb000 r--p 00000000 08:20 1226925                    /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
7f970aedb000-7f970aeec000 r-xp 0000e000 08:20 1226925                    /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
7f970aeec000-7f970aefa000 r--p 0001f000 08:20 1226925                    /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
7f970aefa000-7f970aefe000 r--p 0002c000 08:20 1226925                    /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
7f970aefe000-7f970aeff000 rw-p 00030000 08:20 1226925                    /usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
7f970aeff000-7f970af00000 r--p 00000000 08:20 1221942                    /usr/lib/locale/C.utf8/LC_NAME
7f970af00000-7f970af01000 r--p 00000000 08:20 1221798                    /usr/lib/locale/C.utf8/LC_ADDRESS
7f970af01000-7f970af02000 r--p 00000000 08:20 1221957                    /usr/lib/locale/C.utf8/LC_TELEPHONE
7f970af02000-7f970af03000 r--p 00000000 08:20 1221933                    /usr/lib/locale/C.utf8/LC_MEASUREMENT
7f970af03000-7f970af0a000 r--s 00000000 08:20 1640931                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f970af0a000-7f970af0c000 rw-p 00000000 00:00 0
7f970af0c000-7f970af0e000 r--p 00000000 08:20 1221990                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f970af0e000-7f970af38000 r-xp 00002000 08:20 1221990                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f970af38000-7f970af43000 r--p 0002c000 08:20 1221990                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f970af43000-7f970af44000 r--p 00000000 08:20 1221929                    /usr/lib/locale/C.utf8/LC_IDENTIFICATION
7f970af44000-7f970af46000 r--p 00037000 08:20 1221990                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7f970af46000-7f970af48000 rw-p 00039000 08:20 1221990                    /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
7fff053d2000-7fff053f3000 rw-p 00000000 00:00 0                          [stack]
7fff053f3000-7fff053f7000 r--p 00000000 00:00 0                          [vvar]
7fff053f7000-7fff053f9000 r-xp 00000000 00:00 0                          [vdso]

To summarize, I would use grep, sort, uniq, and awk command.

cat /proc/363/maps | awk '{print $6}' | grep . | sort | uniq
/usr/bin/bash
/usr/lib/locale/C.utf8/LC_ADDRESS
/usr/lib/locale/C.utf8/LC_COLLATE
/usr/lib/locale/C.utf8/LC_CTYPE
/usr/lib/locale/C.utf8/LC_IDENTIFICATION
/usr/lib/locale/C.utf8/LC_MEASUREMENT
/usr/lib/locale/C.utf8/LC_MESSAGES/SYS_LC_MESSAGES
/usr/lib/locale/C.utf8/LC_MONETARY
/usr/lib/locale/C.utf8/LC_NAME
/usr/lib/locale/C.utf8/LC_NUMERIC
/usr/lib/locale/C.utf8/LC_PAPER
/usr/lib/locale/C.utf8/LC_TELEPHONE
/usr/lib/locale/C.utf8/LC_TIME
/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
/usr/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libtinfo.so.6.3
[heap]
[stack]
[vdso]
[vvar]

This part is related with the part of the BusyBox document.