This might be a dumb question, but suppose I'm given a file path and I'd like to know if it points to a file on tmpfs (that is, it's an in-memory file). How can I do that using only Linux system calls? (That is, I can't go to the shell.)
Use the statfs
syscall and see if the returned f_type
field is TMPFS_MAGIC
.
Here's a small utility demonstrating this:
#include <sys/vfs.h>
#include <linux/magic.h>
#include <stdio.h>
int main(int argc, char** argv) {
struct statfs info;
statfs(argv[1], &info);
if (info.f_type == TMPFS_MAGIC) {
printf("It's tmpfs\n");
return 0;
} else {
printf("It's not tmpfs\n");
return 1;
}
}
Example:
$ ./isittmpfs /etc/passwd
It's not tmpfs
$ ./isittmpfs /dev/shm/pulse-shm-1358569836
It's tmpfs
(NB: This is just an example of how to determine if a file is on tmpfs through syscalls. This answer does not suggest dropping to a shell even though the example code is invoked from a shell)