Parsing /proc/maps

3458 views c

I wrote the following code in C. What I want to do is to print the PID of all the processes that have read and write permissions. My problem is that when the program is run nothing is printed out. What am I doing wrong?

Here is a snippet of my code:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
int main() {    
    FILE *file;
    unsigned int from, to, offset, major, minor;  
    char buf[512], flags[4];
    sprintf(buf, "/proc/%d/maps", getpid());
    file = fopen(buf, "r");
    while (fgets(buf, sizeof(buf), file)) {
        sscanf(buf, "%x-%x %4c %x %x:%x", &from, &to, flags, &offset, &major, &minor);
    if (flags[0] == 'r' && flags[1] == 'w') {
            printf("%d\n", getpid());

answered question

1 Answer


What I want to do is to print the PID of all the processes that are read and writable

A process is an abstraction provided by some operating system. It makes no sense to speak of a "readable" process (or a "writable" one), or one with "read" permissions or "write" permissions, since it is about file system permissions. A process is not a file, and its /proc/1234/ directory is just a view into that process given by the kernel. A process is using file descriptors to access files (and you might scan its /proc/1234/fd/ directory).

The proc(5) file system is a pseudo file system specific to Linux. The /proc/$pid/maps is a textual view showing the virtual address space of a process. You can read that pseudo-file sequentially.

To change its virtual address space, a Linux process would use system calls like execve(2) (which initializes a fresh virtual address space for a new program), mmap(2) and munmap, mprotect(2), etc...

Read also Operating Systems: Three Easy Pieces to get a broad view about OSes.

posted this

Have an answer?


Please login first before posting an answer.