Skip to content

Commit

Permalink
Add a quick test program to detect file system type
Browse files Browse the repository at this point in the history
Intended to help with identifying shared tmp dirs.

Signed-off-by: Ralph Castain <rhc@pmix.org>
  • Loading branch information
rhc54 committed Dec 14, 2023
1 parent 23bbb82 commit 5b5388b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ test/filegen
test/iostress
test/spawn_multiple
test/clichk
test/chkfs

docs/_build
docs/_static
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ TESTS = \
qspawn \
iostress \
filegen \
clichk
clichk \
chkfs

all: $(TESTS)

Expand Down
83 changes: 83 additions & 0 deletions test/chkfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <mntent.h>
#include <string.h>
#include <fcntl.h>

//#define MOUNT_FILE "/etc/mtab"
#define MOUNT_FILE "/proc/mounts"

static int mountpoint(char *filename, char **fstype)
{
struct stat s;
struct mntent mnt;
FILE *fp;
dev_t dev;
char buf[1024];
int fd;

fd = open(filename, O_RDONLY);
if (0 > fd) {
fprintf(stderr, "CANNOT OPEN %s\n", filename);
return EINVAL;
}
if (fstat(fd, &s) != 0) {
return EINVAL;
}
close(fd);

dev = s.st_dev;

if ((fp = setmntent(MOUNT_FILE, "r")) == NULL) {
return EINVAL;
}

while (getmntent_r(fp, &mnt, buf, sizeof(buf))) {
fprintf(stderr, "MNT: %s %s\n", mnt.mnt_fsname, mnt.mnt_dir);
fd = open(mnt.mnt_dir, O_RDONLY);
if (0 > fd) {
// probably lack permissions
continue;
}
if (fstat(fd, &s) != 0) {
close(fd);
continue;
}

if (s.st_dev == dev) {
*fstype = strdup(mnt.mnt_type);
close(fd);
return 0;
}
close(fd);
}

endmntent(fp);

// Should never reach here.
return EINVAL;
}

int main(int argc, char **argv)
{
int n, rc;
char *fstype;

if (argc < 2) {
fprintf(stderr, "Usage: %s file1 file2 ...\n", argv[0]);
return 1;
}

for (n=1; NULL != argv[n]; n++) {
rc = mountpoint(argv[n], &fstype);
fprintf(stdout, "Return: %d File: %s FStype: %s\n", rc, argv[n], fstype);
free(fstype);
}

return 0;
}

0 comments on commit 5b5388b

Please sign in to comment.