--- orig/vproc-0.01/vproc.c 2004-01-13 13:03:12.000000000 -0500 +++ vproc.c 2004-01-13 13:19:43.000000000 -0500 @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef O_LARGEFILE #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE) @@ -107,8 +108,63 @@ return r; } +void dump_proc_flags() +{ + DIR *dp; + struct dirent *de; + struct stat st; + int fd, flags, r; + char *name, cwd[PATH_MAX + 1], *rel_cwd; + + if (!getcwd(cwd, sizeof(cwd))) + return; + + rel_cwd = cwd + strlen("/proc/"); + + dp = opendir("."); + while ((de = readdir(dp))) { + name = de->d_name; + if (!strcmp(name, "..") || !strcmp(name, ".")) + continue; + if (stat(name, &st) != 0) + continue; + fd = open(name, OPEN_FLAGS); + if (fd < 0) + continue; + r = !getxflg(fd, &flags); + close(fd); + if (!r) + continue; + printf("%s%s %d\n", rel_cwd, name, flags); + if (S_ISDIR(st.st_mode)) { + chdir(name); + dump_proc_flags(); + fchdir(dirfd(dp)); + } + } + closedir(dp); +} + +void restore_proc_flags() +{ + char input[PATH_MAX + 50 + 1], path[PATH_MAX + 1]; + int flags, fd; + fprintf(stderr, "Reading restore file from stdin\n"); + while (fgets(input, sizeof(input), stdin)) { + if (sscanf(input, "%s %d", path, &flags) == 2) { + fd = open(path, OPEN_FLAGS); + if (fd < 0) + continue; + if (setxflg(fd, flags)) { + perror("setxflags"); + } + close(fd); + } + } +} -#define OPTIONS "+hdDeEf:" + +#define OPTIONS "+hdRSDeEf:" int main(int argc, char *argv[]) { @@ -117,7 +173,6 @@ char c, errflg = 0; int r; - cmd_name = argv[0]; while ((c = getopt(argc, argv, OPTIONS)) != EOF) { switch (c) { @@ -131,9 +186,25 @@ "-e show everywhere\n" "-E show only in context 1\n" "-f flag value in decimal\n" + "-S dump all flags in /proc/\n" + "-R restore all flags to /proc/\n" "-- end of options\n", cmd_name); exit(0); break; + case 'S': + if (chdir("/proc/")) { + perror("chdir"); + exit(2); + } + dump_proc_flags("."); + break; + case 'R': + if (chdir("/proc/")) { + perror("chdir"); + exit(2); + } + restore_proc_flags(); + break; case 'd': num_flag = 3; opt_flag = 1;