--- src/killall5.c +++ src/killall5.c @@ -284,6 +284,7 @@ /* * Read the proc filesystem. + * CWD must be /proc. */ int readproc() { @@ -299,7 +300,7 @@ int pid, f; /* Open the /proc directory. */ - if ((dir = opendir("/proc")) == NULL) { + if ((dir = opendir(".")) == NULL) { nsyslog(LOG_ERR, "cannot opendir(/proc)"); return -1; } @@ -326,10 +327,10 @@ memset(p, 0, sizeof(PROC)); /* Open the status file. */ - snprintf(path, sizeof(path), "/proc/%s/stat", d->d_name); + snprintf(path, sizeof(path), "%s/stat", d->d_name); /* Read SID & statname from it. */ - if ((fp = fopen(path, "r")) != NULL) { + if ((fp = fopen(path, "r")) != NULL) { buf[0] = 0; fgets(buf, sizeof(buf), fp); @@ -343,7 +344,7 @@ if (q == NULL) { p->sid = 0; nsyslog(LOG_ERR, - "can't get program name from %s\n", + "can't get program name from /proc/%s\n", path); if (p->argv0) free(p->argv0); if (p->argv1) free(p->argv1); @@ -389,7 +390,7 @@ continue; } - snprintf(path, sizeof(path), "/proc/%s/cmdline", d->d_name); + snprintf(path, sizeof(path), "%s/cmdline", d->d_name); if ((fp = fopen(path, "r")) != NULL) { /* Now read argv[0] */ @@ -456,6 +457,39 @@ return 0; } +/* + * Scan the filedescriptors of pid for /dev/fuse + * CWD must be /proc. + */ +int is_fuse(int pid) { + DIR *dir; + char path[256]; + char buf[256]; + struct dirent *d; + ssize_t len; + + /* Open /proc/pid/fd/ */ + snprintf(path, sizeof(path), "%d/fd", pid); + if ((dir = opendir(path)) != NULL) { + /* Walk through the directory. */ + while ((d = readdir(dir)) != NULL) { + /* check for /dev/fuse */ + snprintf(path, sizeof(path), "%d/fd/%s", + pid, d->d_name); + if ((len = readlink(path, buf, sizeof(buf))) > 0) { + if (strncmp("/dev/fuse", buf, len) == 0) { + /* Fuse filesystem */ + return 1; + } + } + } + closedir(dir); + } + + /* Not a fuse filesystem */ + return 0; +} + PIDQ_HEAD *init_pid_q(PIDQ_HEAD *q) { q->head = q->next = q->tail = NULL; @@ -688,6 +722,11 @@ argc -= optind; argv += optind; + if (chdir("/proc") == -1) { + nsyslog(LOG_ERR, "chdir /proc failed"); + return(1); + } + /* Print out process-ID's one by one. */ readproc(); for(f = 0; f < argc; f++) { @@ -759,6 +798,11 @@ /* Which NFS partitions are online? */ init_nfs(); + if (chdir("/proc") == -1) { + nsyslog(LOG_ERR, "chdir /proc failed"); + return(1); + } + /* * Ignoring SIGKILL and SIGSTOP do not make sense, but * someday kill(-1, sig) might kill ourself if we don't @@ -779,11 +823,12 @@ exit(1); } - /* Now kill all processes except our session. */ + /* Now kill all processes except init (pid 1), our session, + and FUSE filesystems. */ sid = (int)getsid(0); pid = (int)getpid(); for (p = plist; p; p = p->next) { - if (p->pid == 1 || p->pid == pid || p->sid == sid || p->kernel) { + if (p->pid == 1 || p->pid == pid || p->sid == sid || p->kernel || is_fuse(p->pid)) { kill(p->pid, SIGCONT); continue; }