How can I check if a memory area is being mapped to a file?

Is there a way to check if a memory area is mapped to some main file using mmap?

I would like to write a function:

int is_mmapped(void *ptr, size_t length);

      

Returns a nonzero value for the region of memory that is fully mapped to the file using the mmap tooltip.

+3


source to share


1 answer


How Ross Ridge suggested in the comments /proc/self/maps

can help you with this.

Each line looks something like this:

35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522  /usr/lib64/ld-2.15.so
START ADDR- END ADDR  PERM  OFFSET   DEV   INODE   PATHNAME

      

All we care about is the start and end addresses, so it doesn't take a lot of code:

#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int is_mmaped(void *ptr, size_t length) {
    FILE *file = fopen("/proc/self/maps", "r");
    char line[1024];
    int result = 0;
    while (!feof(file)) {
        if (fgets(line, sizeof(line) / sizeof(char), file) == NULL) {
            break;
        }
        unsigned long start, end;
        if (sscanf(line, "%lx-%lx", &start, &end) != 2) {
            continue; // could not parse. fail gracefully and try again on the next line.
        }
        unsigned long ptri = (long) ptr;
        if (ptri >= start && ptri + length <= end) {
            result = 1;
            break;
        }
    }
    fclose(file);
    return result;
}

      



And some tests:

int main(int argc, char *argv[]) {
    void *test = mmap(NULL, 16384, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    printf("T %d\n", is_mmaped(test, 16384));
    printf("F %d\n", is_mmaped(test, 16385));
    printf("F %d\n", is_mmaped(test + 1, 16384));
    printf("T %d\n", is_mmaped(test, 1024));
    printf("T %d\n", is_mmaped(test, 256));
    printf("T %d\n", is_mmaped(test, 8));
    printf("T %d\n", is_mmaped(test + 16383, 1));
    munmap(test, 16384);
    printf("F %d\n", is_mmaped(test, 16384));

    printf("T %d\n", is_mmaped(main, 32));
    return 0;
}

      

which prints:

T 1
F 0
F 0
T 1
T 1
T 1
T 1
F 0
T 1

      

as was expected.

+2


source







All Articles