Update recyclix.c
[hband-ld-preload-libs.git] / src / xdev.c
blobd2e6af8073ba6ec8fba6f2d86ed95b28d53f0619
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #include <err.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <dirent.h>
10 #include <sys/stat.h>
11 #include <ftw.h>
13 #define TRUE 1
14 #define FALSE 0
15 #define MBUF_LENGTH 1024
17 int xdev_allow(const char *pathname)
19 char realpathname[PATH_MAX];
20 FILE* proc_mounts = NULL;
21 char mbuf[MBUF_LENGTH+1];
22 char* space;
23 char* mntdev;
24 char* mntpnt;
25 char* mntype;
26 char* mntopt;
28 warnx("xdev? %s", pathname);
30 proc_mounts = fopen("/proc/mounts", "r");
31 if(proc_mounts == NULL)
33 err(1, "/proc/mounts: open");
36 if(realpath(pathname, realpathname) != NULL)
38 while(fgets(mbuf, MBUF_LENGTH, proc_mounts) != NULL)
40 mntdev = mbuf;
41 space = strchr(mbuf, ' ');
42 if(space != NULL)
44 *space = 0;
45 mntpnt = space + 1;
46 space = strchr(mntpnt, ' ');
47 if(space != NULL)
49 *space = 0;
50 mntype = space + 1;
51 space = strchr(mntype, ' ');
52 if(space != NULL)
54 *space = 0;
55 mntopt = space + 1;
56 space = strchr(mntopt, ' ');
57 if(space != NULL)
59 *space = 0;
60 if(strncmp(realpathname, mntpnt, strlen(mntpnt)) == 0)
62 /* It's a mountpoint, or under a mountpoint */
64 /* Deny if it is likely a network device */
65 if(strncmp(mntdev, "//", 2) == 0 || strchr(mntdev, ':') != NULL)
66 goto deny;
68 //TODO XDEV_FSTYPE
69 //TODO XDEV_OPTION
77 goto allow;
79 deny:
80 fclose(proc_mounts);
81 errno = ECONNREFUSED;
82 return FALSE;
84 allow:
85 fclose(proc_mounts);
86 return TRUE;
90 int __lxstat64(int ver, const char *path, struct stat64 *buf)
92 int (*real___lxstat64)(int, const char *, struct stat64 *) = dlsym(RTLD_NEXT, "__lxstat64");
93 if(xdev_allow(path)) return real___lxstat64(ver, path, buf);
94 return -1;
97 int __openat_2(int fd, const char *path, int oflag, mode_t mode)
99 int (*real___openat_2)(int, const char*, int, mode_t) = dlsym(RTLD_NEXT, "__openat_2");
100 if(xdev_allow(path)) return real___openat_2(fd, path, oflag, mode);
101 return -1;
104 DIR* opendir(const char *pathname)
106 DIR* (*real_opendir)(const char *) = dlsym(RTLD_NEXT, "opendir");
107 if(xdev_allow(pathname)) return real_opendir(pathname);
108 return NULL;
111 int xdev_ftw_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf
112 // ,int (*real_cb)(const char*, const struct stat*, int, struct FTW*)
115 // if(xdev_allow(fpath)) return real_cb(fpath, sb, typeflag, ftwbuf);
116 // return FTW_STOP;
117 return 0;
120 int nftw64(const char *dirpath,
121 // int (*fgv)(const char*, const struct stat*, int, struct FTW*),
122 __nftw64_func_t fgv,
123 int nopenfd, int flags)
125 int (*real_nftw64)(const char *, int (*)(const char*, const struct stat*, int, struct FTW*), int, int) = dlsym(RTLD_NEXT, "nftw64");
126 return real_nftw64(dirpath, xdev_ftw_cb, nopenfd, flags);