Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell in linux if a binary program that is running matches file on the disk?

Tags:

linux

Suppose a binary executable program is running:

For example: ps -eaf | grep someServer

shows that someServer is running.

Is it possible to tell if someServer executable on disk ( eg /usr/bin/someServer ) matches the program that was actually started ?

like image 528
steviekm3 Avatar asked Jan 12 '23 22:01

steviekm3


2 Answers

Yes: Use soft link /proc/$pid/exe to get the path which was used to load the code.

Look into /proc/$pid/maps. It will look like this (for /sbin/getty):

00400000-00407000 r-xp 00000000 08:01 3145779                            /sbin/getty
00606000-00607000 r--p 00006000 08:01 3145779                            /sbin/getty
00607000-00608000 rw-p 00007000 08:01 3145779                            /sbin/getty
... lots more ...

Filter the file using the path that you got from the soft link to find the lines that are interesting for you.

The last number (3145779) is the inode of the file. When you create a new file on disk, it gets a new inode.

To see the inode of a file, use ls --inode /sbin/getty:

3145779 /sbin/getty

Since the two numbers are still identical, the executable on disk is the same as in RAM.

Background: Linux doesn't load processes into RAM at once. Instead, the executable file is memory-mapped into RAM using the virtual memory subsystem. This means parts of the executable which you never use will never be loaded into memory. It also means that the kernel uses the executable on disk as a "cache".

When you overwrite the executable on disk, the original inode is not changed. Your existing process hangs on to it. Instead, a new inode is created and the directory node (which contains the file name and a pointer to the inode with the data) is updated. This is why you can overwrite files that are currently in use on Linux.

The original inode will be cleaned up when the last process which uses it dies.

like image 151
Aaron Digulla Avatar answered Jan 15 '23 11:01

Aaron Digulla


I am not sure what do you mean exactly.

  • use pid matching (compare ps output and getpid() in the server)
  • use /proc/$pid/environ and find _=path_to_someServer (there is the binary)
  • use diff /proc/$pid/exe someServer ("type someServer" to get full path)
like image 30
nopsoft Avatar answered Jan 15 '23 12:01

nopsoft