Dr. Werner Fink 2011-02-08 16:55:25 +00:00 committed by Git OBS Bridge
parent e9c9399aaf
commit c69d663e7f
6 changed files with 949 additions and 911 deletions

View File

@ -1,903 +0,0 @@
--- doc/fuser.1
+++ doc/fuser.1 2010-07-13 11:19:41.000000000 +0000
@@ -88,8 +88,14 @@ List all known signal names.
\fINAME\fR specifies a file on a mounted file system or a block device that
is mounted. All processes accessing files on that file system are listed.
If a directory file is specified, it is automatically changed to
-\fINAME\fR/. to use any file system that might be mounted on that
-directory.
+\fINAME\fR/. To use any file system that might be mounted on that
+directory. Please note that due the required device ID comparision all
+mounted file systems the
+.BR stat (2)
+system call will applied to every file system even on network file system
+(NFS). If the NFS server does not respond or the network is down the
+.BR stat (2)
+may hang forever.
.TP
\fB\-M\f, \fB\-\-ismountpoint\fR
Request will be fulfilled only if \fINAME\fR specifies a mountpoint.
@@ -174,10 +180,13 @@ The \fB\-k\fR option only works on proce
\fBfuser\fR will print an advice, but take no action beyond that.
.SH BUGS
.PP
-fuser \-m /dev/sgX will show (or kill with the \-k flag) all processes, even
+\fBfuser \-m \fI/dev/sgX\fR will show (or kill with the \fB\-k\fR flag) all processes, even
if you don't have that device configured. There may be other devices it
does this for too.
.PP
+\fBfuser \-m \fIname\fR may hang forever if there are NFS file systems mounted
+and one of the NFS servers do not respond or the corresponding network is down.
+.PP
.B fuser
cannot report on any processes that it doesn't have permission to look at
the file descriptor table for. The most common time this problem occurs
--- src/fuser.c
+++ src/fuser.c 2010-07-13 16:28:32.051424747 +0000
@@ -32,9 +32,11 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
+#include <sys/wait.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pwd.h>
@@ -45,6 +47,7 @@
#include <mntent.h>
#include <signal.h>
#include <getopt.h>
+#include <setjmp.h>
#include "fuser.h"
#include "signals.h"
@@ -67,7 +70,8 @@ static void check_map(const pid_t pid, c
struct device_list *dev_head,
struct inode_list *ino_head, const uid_t uid,
const char access);
-static struct stat *get_pidstat(const pid_t pid, const char *filename);
+static struct stat *get_pidstat(const opt_type opts, const pid_t pid,
+ const char *filename, char *real);
static uid_t getpiduid(const pid_t pid);
static int print_matches(struct names *names_head, const opt_type opts,
const int sig_number);
@@ -79,12 +83,12 @@ static void add_device(struct device_lis
struct names *this_name, dev_t device);
void fill_unix_cache(struct unixsocket_list **unixsocket_head);
static dev_t find_net_dev(void);
-static void scan_procs(struct names *names_head, struct inode_list *ino_head,
- struct device_list *dev_head,
+static void scan_procs(const opt_type opts, struct names *names_head,
+ struct inode_list *ino_head, struct device_list *dev_head,
struct unixsocket_list *sockets, dev_t netdev);
static void scan_knfsd(struct names *names_head, struct inode_list *ino_head,
struct device_list *dev_head);
-static void scan_mounts(struct names *names_head,
+static void scan_mounts(struct names *names_head, struct mount_list *mounts,
struct inode_list *ino_head,
struct device_list *dev_head);
static void scan_swaps(struct names *names_head, struct inode_list *ino_head,
@@ -95,6 +99,13 @@ static void debug_match_lists(struct nam
struct device_list *dev_head);
#endif
+static struct nfs_points *mnts;
+static void clear_mnt(void);
+static int check4nfs(const char * path, char * real);
+
+typedef int (*stat_t)(const char*, struct stat*);
+static int nfssafe(stat_t func, const char *path, struct stat *buf);
+
static void usage(const char *errormsg)
{
if (errormsg != NULL)
@@ -141,8 +152,15 @@ void print_version()
"For more information about these matters, see the files named COPYING.\n"));
}
+static int islocatedon(const char * path, const char * loc)
+{
+ if (!path || *path == '\0')
+ return 0;
+ return (strstr(path, loc) == path);
+}
+
static void
-scan_procs(struct names *names_head, struct inode_list *ino_head,
+scan_procs(const opt_type opts, struct names *names_head, struct inode_list *ino_head,
struct device_list *dev_head, struct unixsocket_list *sockets,
dev_t netdev)
{
@@ -153,6 +171,9 @@ scan_procs(struct names *names_head, str
pid_t pid, my_pid;
uid_t uid;
struct stat *cwd_stat, *exe_stat, *root_stat;
+ char root_real[PATH_MAX+1];
+ char cwd_real[PATH_MAX+1];
+ char exe_real[PATH_MAX+1];
if ((topproc_dir = opendir("/proc")) == NULL) {
fprintf(stderr, _("Cannot open /proc directory: %s\n"),
@@ -169,9 +190,10 @@ scan_procs(struct names *names_head, str
continue;
uid = getpiduid(pid);
- root_stat = get_pidstat(pid, "root");
- cwd_stat = get_pidstat(pid, "cwd");
- exe_stat = get_pidstat(pid, "exe");
+ root_real[0] = cwd_real[0] = exe_real[0] = '\0';
+ root_stat = get_pidstat(opts, pid, "root", root_real);
+ cwd_stat = get_pidstat(opts, pid, "cwd", cwd_real);
+ exe_stat = get_pidstat(opts, pid, "exe", exe_real);
/* Scan the devices */
for (dev_tmp = dev_head; dev_tmp != NULL;
dev_tmp = dev_tmp->next) {
@@ -187,6 +209,17 @@ scan_procs(struct names *names_head, str
&& cwd_stat->st_dev == dev_tmp->device)
add_matched_proc(dev_tmp->name, pid, uid,
ACCESS_CWD);
+ if ((dev_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (islocatedon(&exe_real[0], dev_tmp->name->filename))
+ add_matched_proc(dev_tmp->name, pid, uid,
+ ACCESS_EXE);
+ if (islocatedon(&root_real[0], dev_tmp->name->filename))
+ add_matched_proc(dev_tmp->name, pid, uid,
+ ACCESS_ROOT);
+ if (islocatedon(&cwd_real[0], dev_tmp->name->filename))
+ add_matched_proc(dev_tmp->name, pid, uid,
+ ACCESS_CWD);
}
for (ino_tmp = ino_head; ino_tmp != NULL;
ino_tmp = ino_tmp->next) {
@@ -212,14 +245,27 @@ scan_procs(struct names *names_head, str
uid, ACCESS_CWD);
}
}
+ if ((ino_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (islocatedon(&exe_real[0], ino_tmp->name->filename))
+ add_matched_proc(ino_tmp->name, pid, uid,
+ ACCESS_EXE);
+ if (islocatedon(&root_real[0], ino_tmp->name->filename))
+ add_matched_proc(ino_tmp->name, pid, uid,
+ ACCESS_ROOT);
+ if (islocatedon(&cwd_real[0], ino_tmp->name->filename))
+ add_matched_proc(ino_tmp->name, pid, uid,
+ ACCESS_CWD);
}
if (root_stat) free(root_stat);
if (cwd_stat) free(cwd_stat);
if (exe_stat) free(exe_stat);
+#ifndef __linux__
check_dir(pid, "lib", dev_head, ino_head, uid, ACCESS_MMAP,
sockets, netdev);
check_dir(pid, "mmap", dev_head, ino_head, uid, ACCESS_MMAP,
sockets, netdev);
+#endif
check_dir(pid, "fd", dev_head, ino_head, uid, ACCESS_FILE,
sockets, netdev);
check_map(pid, "maps", dev_head, ino_head, uid, ACCESS_MMAP);
@@ -394,6 +440,23 @@ add_special_proc(struct names *name_list
int parse_file(struct names *this_name, struct inode_list **ino_list)
{
+ char real[PATH_MAX+1] = "";
+
+ real[0] = '\0';
+ if (check4nfs(this_name->filename, real)) {
+ if (this_name->filename)
+ free(this_name->filename);
+ this_name->filename = strdup(real);
+ this_name->name_space |= NAMESPACE_NFS;
+ add_inode(ino_list, this_name, (dev_t)-1, (ino_t)-1);
+ return 0;
+ }
+ if (real[0] != '\0') {
+ if (this_name->filename)
+ free(this_name->filename);
+ this_name->filename = strdup(real);
+ }
+
if (stat(this_name->filename, &(this_name->st)) != 0) {
if (errno == ENOENT)
fprintf(stderr, _("Specified filename %s does not exist.\n"), this_name->filename);
@@ -411,12 +474,12 @@ int parse_file(struct names *this_name,
int
parse_unixsockets(struct names *this_name, struct inode_list **ino_list,
- struct unixsocket_list *sun_head)
+ struct unixsocket_list *sun_head, dev_t net_dev)
{
struct unixsocket_list *sun_tmp;
- dev_t net_dev;
- net_dev = find_net_dev();
+ if (this_name->name_space & NAMESPACE_NFS)
+ return 0;
for (sun_tmp = sun_head; sun_tmp != NULL; sun_tmp = sun_tmp->next) {
if (sun_tmp->dev == this_name->st.st_dev && sun_tmp->inode == this_name->st.st_ino) {
@@ -429,10 +492,14 @@ parse_unixsockets(struct names *this_nam
}
int
-parse_mounts(struct names *this_name, struct device_list **dev_list,
- const char opts)
+parse_mounts(struct names *this_name, struct mount_list *mounts,
+ struct device_list **dev_list)
{
dev_t match_device;
+ struct mount_list *mountptr;
+
+ if (this_name->name_space & NAMESPACE_NFS)
+ goto skip;
if (S_ISBLK(this_name->st.st_mode))
match_device = this_name->st.st_rdev;
@@ -440,6 +507,13 @@ parse_mounts(struct names *this_name, st
match_device = this_name->st.st_dev;
add_device(dev_list, this_name, match_device);
return 0;
+skip:
+ match_device = -1;
+ for (mountptr = mounts ; mountptr != NULL ; mountptr = mountptr->next) {
+ if (strcmp(mountptr->mountpoint, this_name->filename) == 0)
+ add_device(dev_list, this_name, match_device);
+ }
+ return 0;
}
#ifdef WITH_IPV6
@@ -758,34 +832,101 @@ find_net6_sockets(struct inode_list **in
}
#endif
+static inline int isnetfs(const char * type)
+{
+ static const char* netfs[] = {"nfs", "nfs4", "smbfs", "cifs", "afs", "ncpfs", (char*)0};
+ int n;
+ for (n = 0; netfs[n]; n++)
+ if (!strcasecmp(netfs[n], type))
+ return 1;
+ return 0;
+}
+
static void
read_proc_mounts(struct mount_list **mnt_list)
{
- FILE *fp;
- char line[BUFSIZ];
- char *find_mountp;
- char *find_space;
- struct mount_list *mnt_tmp;
+ FILE *mntfp;
+ struct mntent *mnt_ptr;
+ struct stat st;
+ const char * mtab;
+
+ if (stat("/proc/version", &st) < 0)
+ mtab = PROC_MOUNTS;
+ else
+ mtab = "/etc/mtab";
- if ((fp = fopen(PROC_MOUNTS, "r")) == NULL) {
- fprintf(stderr, "Cannot open %s\n", PROC_MOUNTS);
+ if ( (mntfp = setmntent(mtab,"r")) == NULL) {
+ fprintf(stderr, _("Cannot open %s: %s\n"), mtab,
+ strerror(errno));
return;
}
- while (fgets(line, BUFSIZ, fp) != NULL) {
- if ((find_mountp = strchr(line, ' ')) == NULL)
- continue;
- find_mountp++;
- if ((find_space = strchr(find_mountp, ' ')) == NULL)
- continue;
- *find_space = '\0';
- if ((mnt_tmp = malloc(sizeof(struct mount_list))) == NULL)
+ while ((mnt_ptr = getmntent(mntfp)) != NULL) {
+ struct mount_list *mnt_tmp;
+ if (isnetfs(mnt_ptr->mnt_type)) {
+ /*
+ * Remember all NFS typed partitions, required to make check4nfs() work.
+ */
+ size_t nlen = strlen(mnt_ptr->mnt_dir);
+ struct nfs_points *restrict p;
+ if (posix_memalign((void*)&p, sizeof(void*), alignof(struct nfs_points)+(nlen+1)) != 0)
+ goto out;
+ p->name = ((char*)p)+alignof(struct nfs_points);
+ p->nlen = nlen;
+ p->shadow = (struct shadow_list*)0;
+
+ strcpy(p->name, mnt_ptr->mnt_dir);
+ if (mnts)
+ mnts->prev = p;
+ p->next = mnts;
+ p->prev = (struct nfs_points*)0;
+ mnts = p;
+ }
+ if ((mnt_tmp = (struct mount_list*)malloc(sizeof(struct mount_list))) == NULL)
continue;
- if ((mnt_tmp->mountpoint = strdup(find_mountp)) == NULL)
+ if ((mnt_tmp->mountpoint = strdup(mnt_ptr->mnt_dir)) == NULL)
continue;
mnt_tmp->next = *mnt_list;
*mnt_list = mnt_tmp;
}
- fclose(fp);
+ endmntent(mntfp);
+
+ if (!mnts)
+ return;
+
+ if ((mntfp = setmntent(mtab, "r")) == NULL) {
+ fprintf(stderr, _("Cannot open %s: %s\n"), mtab,
+ strerror(errno));
+ return;
+ }
+
+ while ((mnt_ptr = getmntent(mntfp)) != NULL) {
+ struct nfs_points *p;
+
+ for (p = mnts; p; p = p->next) {
+ struct shadow_list *s;
+ size_t nlen;
+
+ if (strcmp(mnt_ptr->mnt_dir, p->name) == 0)
+ continue;
+ if (strncmp(mnt_ptr->mnt_dir, p->name, p->nlen) != 0)
+ continue;
+
+ nlen = strlen(mnt_ptr->mnt_dir);
+ if (posix_memalign((void*)&s, sizeof(void*), alignof(struct shadow_list)+(nlen+1)) != 0)
+ goto out;
+ s->name = ((char*)s)+alignof(struct shadow_list);
+ s->nlen = nlen;
+
+ strcpy(s->name, mnt_ptr->mnt_dir);
+ if (p->shadow)
+ p->shadow->prev = s;
+ s->next = p->shadow;
+ s->prev = (struct shadow_list*)0;
+ p->shadow = s;
+ }
+ }
+out:
+ endmntent(mntfp);
}
static int
@@ -808,6 +949,150 @@ is_mountpoint(struct mount_list **mnt_li
return 0;
}
+/*
+ * Remove struct nfs_points and its sahdows from memory
+ */
+static void clear_shadow(struct shadow_list *restrict shadow)
+{
+ struct shadow_list *s, *n, *l;
+
+ n = shadow;
+ l = (struct shadow_list*)0;
+ for (s = shadow; n; s = n) {
+ l = s->prev;
+ n = s->next;
+ if (s == shadow) {
+ if (n) n->prev = (struct shadow_list*)0;
+ shadow = n;
+ } else if (l) {
+ if (n) n->prev = l;
+ l->next = n;
+ }
+ free(s);
+ }
+}
+
+static void clear_mnt(void)
+{
+ struct nfs_points *p, *n, *l;
+
+ n = mnts;
+ l = (struct nfs_points*)0;
+ for (p = mnts; n; p = n) {
+ l = p->prev;
+ n = p->next;
+ if (p == mnts) {
+ if (n) n->prev = (struct nfs_points*)0;
+ mnts = n;
+ } else if (l) {
+ if (n) n->prev = l;
+ l->next = n;
+ }
+ if (p->shadow)
+ clear_shadow(p->shadow);
+ free(p);
+ }
+}
+
+/*
+ * Check if path is a shadow of a NFS partition.
+ */
+static int shadow(struct shadow_list *restrict this, const char *restrict name, const size_t nlen)
+{
+ struct shadow_list *s;
+
+ if (!this)
+ goto out;
+ for (s = this; s; s = s->next) {
+ if (nlen < s->nlen)
+ continue;
+ if (name[s->nlen] != '\0' && name[s->nlen] != '/')
+ continue;
+ if (strncmp(name, s->name, s->nlen) == 0)
+ return 1;
+ }
+out:
+ return 0;
+}
+
+/*
+ * Check path is located on a NFS partition.
+ */
+static int check4nfs(const char * path, char * real)
+{
+ char buf[PATH_MAX+1];
+ const char *curr;
+ int deep = MAXSYMLINKS;
+
+ if (!mnts) return 0;
+
+ curr = path;
+ do {
+ const char *prev;
+ int len;
+
+ if ((prev = strdupa(curr)) == NULL)
+ return 0;
+
+ errno = 0;
+ if ((len = readlink(curr, buf, PATH_MAX)) < 0)
+ break;
+ buf[len] = '\0'; /* Don't be fooled by readlink(2) */
+
+ if (strncmp(prev, "/proc/", 6) == 0) {
+ curr = &buf[0];
+ break; /* /proc/ provides the real path! */
+ }
+
+ if (len > 10) {
+ char *const ptr = &buf[len - 10];
+ if (strcmp(ptr, " (deleted)") == 0) {
+ *ptr = '\0';
+ curr = &buf[0];
+ break; /* Path is deleted from VFS cache */
+ }
+ }
+
+ if (buf[0] != '/') {
+ const char *slash;
+
+ if ((slash = strrchr(prev, '/'))) {
+ size_t off = slash - prev + 1;
+
+ if (off + len > PATH_MAX)
+ len = PATH_MAX - off;
+
+ memmove(&buf[off], &buf[0], len + 1);
+ memcpy(&buf[0], prev, off);
+ }
+ }
+ curr = &buf[0];
+
+ if (deep-- <= 0) return 0;
+
+ } while (1);
+
+ if (real) strcpy(real, curr);
+
+ if (errno == EINVAL) {
+ const size_t nlen = strlen(curr);
+ struct nfs_points *p;
+ for (p = mnts; p; p = p->next) {
+ if (nlen < p->nlen)
+ continue;
+ if (curr[p->nlen] != '\0' && curr[p->nlen] != '/')
+ continue;
+ if (!strncmp(curr, p->name, p->nlen)) {
+ if (shadow(p->shadow, curr, nlen))
+ continue;
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
opt_type opts;
@@ -835,6 +1120,7 @@ int main(int argc, char *argv[])
struct option *optr;
char *nsptr;
int skip_argv;
+ size_t len;
struct option options[] = {
{"all", 0, NULL, 'a'},
@@ -931,7 +1217,6 @@ int main(int argc, char *argv[])
break;
case 'M':
opts |= OPT_ISMOUNTPOINT;
- read_proc_mounts(&mounts);
break;
case 'n':
argc_cnt++;
@@ -979,6 +1264,9 @@ int main(int argc, char *argv[])
} /* an option */
/* Not an option, must be a file specification */
+ if (!mounts)
+ read_proc_mounts(&mounts);
+
if ((this_name = malloc(sizeof(struct names))) == NULL)
continue;
this_name->next = NULL;
@@ -1030,11 +1318,24 @@ int main(int argc, char *argv[])
break;
default: /* FILE */
this_name->filename = strdup(current_argv);
+ len = strlen(this_name->filename);
+ if (len > 1 && this_name->filename[len-1] == '/')
+ this_name->filename[len-1] = '\0';
+ if (len > 1 && this_name->filename[0] != '/') {
+ char pwd[MAX_PATHNAME];
+ if (getcwd(pwd, MAX_PATHNAME-1)) {
+ char *new, *old = this_name->filename;
+ if (asprintf(&new, "%s/%s", (strlen(pwd) > 1 ? pwd : ""), old) > 0) {
+ this_name->filename = new;
+ free(old);
+ }
+ }
+ }
if (parse_file(this_name, &match_inodes) == 0) {
- parse_unixsockets(this_name, &match_inodes, unixsockets);
+ parse_unixsockets(this_name, &match_inodes, unixsockets, netdev);
if (opts & OPT_MOUNTS)
- parse_mounts(this_name, &match_devices, opts);
- }
+ parse_mounts(this_name, mounts, &match_devices);
+ }
break;
}
@@ -1080,11 +1381,14 @@ int main(int argc, char *argv[])
#ifdef DEBUG
debug_match_lists(names_head, match_inodes, match_devices);
#endif
- scan_procs(names_head, match_inodes, match_devices, unixsockets,
- netdev);
- scan_knfsd(names_head, match_inodes, match_devices);
- scan_mounts(names_head, match_inodes, match_devices);
- scan_swaps(names_head, match_inodes, match_devices);
+ scan_procs(opts, names_head, match_inodes, match_devices,
+ unixsockets, netdev);
+ if (opts & OPT_VERBOSE) {
+ scan_knfsd(names_head, match_inodes, match_devices);
+ scan_mounts(names_head, mounts, match_inodes, match_devices);
+ scan_swaps(names_head, match_inodes, match_devices);
+ }
+ clear_mnt();
return print_matches(names_head, opts, sig_number);
}
@@ -1250,7 +1554,7 @@ print_matches(struct names *names_head,
}
-static struct stat *get_pidstat(const pid_t pid, const char *filename)
+static struct stat *get_pidstat(const opt_type opts, const pid_t pid, const char *filename, char *real)
{
char pathname[256];
struct stat *st;
@@ -1258,6 +1562,10 @@ static struct stat *get_pidstat(const pi
if ((st = (struct stat*)malloc(sizeof(struct stat))) == NULL)
return NULL;
snprintf(pathname, 256, "/proc/%d/%s", pid, filename);
+ if (check4nfs(pathname, real)) {
+ if ((opts & OPT_MOUNTS) == 0)
+ return NULL;
+ }
if (stat(pathname, st) != 0)
goto out;
return st;
@@ -1309,6 +1617,16 @@ check_dir(const pid_t pid, const char *d
}
for (dev_tmp = dev_head; dev_tmp != NULL;
dev_tmp = dev_tmp->next) {
+ if (dev_tmp->name->name_space & NAMESPACE_NFS) {
+ char buf[PATH_MAX+1];
+ ssize_t len;
+ if ((len = readlink(filepath, buf, PATH_MAX)) < 0)
+ continue;
+ buf[len] = '\0'; /* Don't be fooled by readlink(2) */
+ if (islocatedon(buf, dev_tmp->name->filename))
+ add_matched_proc(dev_tmp->name, pid,uid, access);
+ continue;
+ }
if (st.st_dev == dev_tmp->device) {
if (access == ACCESS_FILE
&& (lstat(filepath, &lst) == 0)
@@ -1326,6 +1644,16 @@ check_dir(const pid_t pid, const char *d
}
for (ino_tmp = ino_head; ino_tmp != NULL;
ino_tmp = ino_tmp->next) {
+ if (ino_tmp->name->name_space & NAMESPACE_NFS) {
+ char buf[PATH_MAX+1];
+ ssize_t len;
+ if ((len = readlink(filepath, buf, PATH_MAX)) < 0)
+ continue;
+ buf[len] = '\0'; /* Don't be fooled by readlink(2) */
+ if (islocatedon(buf, ino_tmp->name->filename))
+ add_matched_proc(ino_tmp->name, pid,uid, access);
+ continue;
+ }
if (st.st_dev == ino_tmp->device
&& st.st_ino == ino_tmp->inode) {
if (access == ACCESS_FILE
@@ -1372,18 +1700,41 @@ check_map(const pid_t pid, const char *f
while (fgets(line, BUFSIZ, fp)) {
if (sscanf(line, "%*s %*s %*s %x:%x %lld",
&tmp_maj, &tmp_min, &tmp_inode) == 3) {
+ const char * filepath = strchr(line, '/');
tmp_device = tmp_maj * 256 + tmp_min;
for (dev_tmp = dev_head; dev_tmp != NULL;
- dev_tmp = dev_tmp->next)
+ dev_tmp = dev_tmp->next) {
+ if (dev_tmp->name->name_space & NAMESPACE_NFS) {
+ char *nl;
+ if (!filepath)
+ continue;
+ if ((nl = strchr(filepath, '\n')))
+ nl = '\0';
+ if (islocatedon(filepath, dev_tmp->name->filename))
+ add_matched_proc(dev_tmp->name, pid,uid, access);
+ continue;
+ }
if (dev_tmp->device == tmp_device)
add_matched_proc(dev_tmp->name, pid,
uid, access);
+ }
for (ino_tmp = ino_head; ino_tmp != NULL;
- ino_tmp = ino_tmp->next)
+ ino_tmp = ino_tmp->next) {
+ if (ino_tmp->name->name_space & NAMESPACE_NFS) {
+ char *nl;
+ if (!filepath)
+ continue;
+ if ((nl = strchr(filepath, '\n')))
+ nl = '\0';
+ if (islocatedon(filepath, ino_tmp->name->filename))
+ add_matched_proc(ino_tmp->name, pid,uid, access);
+ continue;
+ }
if (ino_tmp->device == tmp_device
&& ino_tmp->inode == tmp_inode)
add_matched_proc(ino_tmp->name, pid,
uid, access);
+ }
}
}
fclose(fp);
@@ -1571,7 +1922,7 @@ scan_knfsd(struct names *names_head, str
if ((find_space = strpbrk(line, " \t")) == NULL)
continue;
*find_space = '\0';
- if (stat(line, &st) != 0) {
+ if (nfssafe(stat, line, &st) != 0) {
continue;
}
/* Scan the devices */
@@ -1580,6 +1931,11 @@ scan_knfsd(struct names *names_head, str
if (st.st_dev == dev_tmp->device)
add_special_proc(dev_tmp->name, PTYPE_KNFSD, 0,
line);
+ if ((dev_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (strcmp(line, dev_tmp->name->filename))
+ continue;
+ add_special_proc(dev_tmp->name, PTYPE_MOUNT, 0, line);
}
for (ino_tmp = ino_head; ino_tmp != NULL;
ino_tmp = ino_tmp->next) {
@@ -1587,53 +1943,55 @@ scan_knfsd(struct names *names_head, str
&& st.st_ino == ino_tmp->inode)
add_special_proc(ino_tmp->name, PTYPE_KNFSD, 0,
line);
+ if ((ino_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (strcmp(line, ino_tmp->name->filename))
+ continue;
+ add_special_proc(ino_tmp->name, PTYPE_MOUNT, 0, line);
}
}
fclose(fp);
}
static void
-scan_mounts(struct names *names_head, struct inode_list *ino_head,
- struct device_list *dev_head)
+scan_mounts(struct names *names_head, struct mount_list *mounts,
+ struct inode_list *ino_head, struct device_list *dev_head)
{
struct device_list *dev_tmp;
struct inode_list *ino_tmp;
- FILE *fp;
- char line[BUFSIZ];
- char *find_mountp;
- char *find_space;
+ struct mount_list *mountptr;
struct stat st;
- if ((fp = fopen(PROC_MOUNTS, "r")) == NULL) {
- fprintf(stderr, "Cannot open %s\n", PROC_MOUNTS);
- return;
- }
- while (fgets(line, BUFSIZ, fp) != NULL) {
- if ((find_mountp = strchr(line, ' ')) == NULL)
- continue;
- find_mountp++;
- if ((find_space = strchr(find_mountp, ' ')) == NULL)
+ for (mountptr = mounts ; mountptr != NULL ; mountptr = mountptr->next) {
+ if (nfssafe(stat, mountptr->mountpoint, &st) != 0)
continue;
- *find_space = '\0';
- if (stat(find_mountp, &st) != 0) {
- continue;
- }
/* Scan the devices */
for (dev_tmp = dev_head; dev_tmp != NULL;
dev_tmp = dev_tmp->next) {
if (st.st_dev == dev_tmp->device)
add_special_proc(dev_tmp->name, PTYPE_MOUNT, 0,
- find_mountp);
+ mountptr->mountpoint);
+ if ((dev_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (strcmp(mountptr->mountpoint, dev_tmp->name->filename))
+ continue;
+ add_special_proc(dev_tmp->name, PTYPE_MOUNT, 0,
+ mountptr->mountpoint);
}
for (ino_tmp = ino_head; ino_tmp != NULL;
ino_tmp = ino_tmp->next) {
if (st.st_dev == ino_tmp->device
&& st.st_ino == ino_tmp->inode)
add_special_proc(ino_tmp->name, PTYPE_MOUNT, 0,
- find_mountp);
+ mountptr->mountpoint);
+ if ((ino_tmp->name->name_space & NAMESPACE_NFS) == 0)
+ continue;
+ if (strcmp(mountptr->mountpoint, ino_tmp->name->filename))
+ continue;
+ add_special_proc(ino_tmp->name, PTYPE_MOUNT, 0,
+ mountptr->mountpoint);
}
}
- fclose(fp);
}
static void
@@ -1682,3 +2040,59 @@ scan_swaps(struct names *names_head, str
}
fclose(fp);
}
+
+static sigjmp_buf jenv;
+static int timeout = 5;
+static void
+sigalarm(int sig)
+{
+ if (sig == SIGALRM)
+ siglongjmp(jenv, 1);
+}
+static int
+nfssafe(stat_t func, const char *path, struct stat *buf)
+{
+ pid_t pid = 0;
+ int ret = 0, pipes[4];
+
+ if (pipe(&pipes[0]) < 0)
+ goto err;
+ switch ((pid = fork ())) {
+ case -1:
+ close(pipes[0]);
+ close(pipes[1]);
+ goto err;
+ case 0:
+ (void) signal(SIGALRM, SIG_DFL);
+ close(pipes[0]);
+ if ((ret = func(path, buf)) == 0)
+ write(pipes[1], buf, sizeof(struct stat));
+ close(pipes[1]);
+ exit(ret);
+ default:
+ close(pipes[1]);
+ if (sigsetjmp(jenv, 1)) {
+ (void) alarm(0);
+ (void) signal(SIGALRM, SIG_DFL);
+ if (waitpid(0, (int*)0, WNOHANG) == 0)
+ kill(pid, SIGKILL);
+ errno = ETIMEDOUT;
+ timeout = 1;
+ goto err;
+ }
+ (void) signal(SIGALRM, sigalarm);
+ (void) alarm(timeout);
+ if (read(pipes[0], buf, sizeof(struct stat)) == 0) {
+ errno = EFAULT;
+ ret = -1;
+ }
+ (void) alarm(0);
+ (void) signal(SIGALRM, SIG_DFL);
+ close(pipes[0]);
+ break;
+ }
+ return ret;
+err:
+ return -1;
+}
+
--- src/fuser.h
+++ src/fuser.h 2010-07-13 12:53:15.000000000 +0000
@@ -66,6 +66,11 @@ struct inode_list {
struct inode_list *next;
};
+struct mountdev_list {
+ char *dir;
+ struct mountdev_list *next;
+};
+
struct device_list {
struct names *name;
dev_t device;
@@ -85,9 +90,33 @@ struct mount_list {
struct mount_list *next;
};
+struct shadow_list
+{
+ struct shadow_list *next;
+ struct shadow_list *prev;
+ size_t nlen;
+ char * name;
+};
+
+struct nfs_points {
+ struct nfs_points *next, *prev;
+ struct shadow_list *shadow;
+ size_t nlen;
+ char * name;
+};
+
+#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
+# ifndef restrict
+# define restrict __restrict__
+# endif
+#endif
+#define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
+
+
#define NAMESPACE_FILE 0
#define NAMESPACE_TCP 1
#define NAMESPACE_UDP 2
+#define NAMESPACE_NFS 4
#define MAX_PATHNAME 200
#define MAX_CMDNAME 16

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:37c986a604ebfc72ddeea7dde48a5ddb0c0d8bdd7d81147626157e098243024b
size 6533

View File

@ -0,0 +1,931 @@
--- src/fuser.c
+++ src/fuser.c 2011-02-08 16:25:57.543927495 +0000
@@ -32,9 +32,11 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
+#include <sys/wait.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pwd.h>
@@ -45,6 +47,7 @@
#include <mntent.h>
#include <signal.h>
#include <getopt.h>
+#include <setjmp.h>
#include "fuser.h"
#include "signals.h"
@@ -95,6 +98,16 @@ static void debug_match_lists(struct nam
struct device_list *dev_head);
#endif
+#ifdef _LISTS_H
+static void clear_mntinfo(void) __attribute__((__destructor__));
+static void init_mntinfo(void) __attribute__((__constructor__));
+static dev_t device(const char *path);
+#endif
+static char *expandpath(const char *path);
+
+typedef int (*stat_t)(const char*, struct stat*);
+static int timeout(stat_t func, const char *path, struct stat *buf, unsigned int seconds);
+
static void usage(const char *errormsg)
{
if (errormsg != NULL)
@@ -152,7 +165,7 @@ scan_procs(struct names *names_head, str
struct device_list *dev_tmp;
pid_t pid, my_pid;
uid_t uid;
- struct stat *cwd_stat, *exe_stat, *root_stat;
+ int dfd;
if ((topproc_dir = opendir("/proc")) == NULL) {
fprintf(stderr, _("Cannot open /proc directory: %s\n"),
@@ -160,7 +173,17 @@ scan_procs(struct names *names_head, str
exit(1);
}
my_pid = getpid();
+ dfd = dirfd(topproc_dir);
while ((topproc_dent = readdir(topproc_dir)) != NULL) {
+ dev_t cwd_dev, exe_dev, root_dev;
+ struct stat *cwd_stat = NULL;
+ struct stat *exe_stat = NULL;
+ struct stat *root_stat = NULL;
+#ifdef _LISTS_H
+ char path[256] = "/proc/", *slash;
+ ssize_t len;
+#endif
+
if (topproc_dent->d_name[0] < '0' || topproc_dent->d_name[0] > '9') /* Not a process */
continue;
pid = atoi(topproc_dent->d_name);
@@ -169,57 +192,72 @@ scan_procs(struct names *names_head, str
continue;
uid = getpiduid(pid);
- root_stat = get_pidstat(pid, "root");
+#ifdef _LISTS_H
+ strcpy(&path[6], topproc_dent->d_name);
+ len = strlen(path);
+ slash = &path[len];
+
+ *slash = '\0';
+ strcat(slash, "/cwd");
+ cwd_dev = device(path);
+
+ *slash = '\0';
+ strcat(slash, "/exe");
+ exe_dev = device(path);
+
+ *slash = '\0';
+ strcat(slash, "/root");
+ root_dev = device(path);
+#else
cwd_stat = get_pidstat(pid, "cwd");
exe_stat = get_pidstat(pid, "exe");
+ root_stat = get_pidstat(pid, "root");
+ cwd_dev = cwd_stat ? cwd_stat->st_dev : 0;
+ exe_dev = exe_stat ? exe_stat->st_dev : 0;
+ root_dev = root_stat ? root_stat->st_dev : 0;
+#endif
+
/* Scan the devices */
- for (dev_tmp = dev_head; dev_tmp != NULL;
- dev_tmp = dev_tmp->next) {
- if (exe_stat != NULL
- && exe_stat->st_dev == dev_tmp->device)
- add_matched_proc(dev_tmp->name, pid, uid,
- ACCESS_EXE);
- if (root_stat != NULL
- && root_stat->st_dev == dev_tmp->device)
- add_matched_proc(dev_tmp->name, pid, uid,
- ACCESS_ROOT);
- if (cwd_stat != NULL
- && cwd_stat->st_dev == dev_tmp->device)
- add_matched_proc(dev_tmp->name, pid, uid,
- ACCESS_CWD);
- }
- for (ino_tmp = ino_head; ino_tmp != NULL;
- ino_tmp = ino_tmp->next) {
- if (exe_stat != NULL) {
- if (exe_stat->st_dev == ino_tmp->device
- && exe_stat->st_ino == ino_tmp->inode) {
- add_matched_proc(ino_tmp->name, pid,
- uid, ACCESS_EXE);
- }
+ for (dev_tmp = dev_head; dev_tmp != NULL; dev_tmp = dev_tmp->next) {
+ if (exe_dev == dev_tmp->device)
+ add_matched_proc(dev_tmp->name, pid, uid, ACCESS_EXE);
+ if (root_dev == dev_tmp->device)
+ add_matched_proc(dev_tmp->name, pid, uid, ACCESS_ROOT);
+ if (cwd_dev == dev_tmp->device)
+ add_matched_proc(dev_tmp->name, pid, uid, ACCESS_CWD);
+ }
+ for (ino_tmp = ino_head; ino_tmp != NULL; ino_tmp = ino_tmp->next) {
+ if (exe_dev == ino_tmp->device) {
+ if (!exe_stat)
+ exe_stat = get_pidstat(pid, "exe");
+ if (exe_stat && exe_stat->st_dev == ino_tmp->device
+ && exe_stat->st_ino == ino_tmp->inode)
+ add_matched_proc(ino_tmp->name, pid, uid, ACCESS_EXE);
}
- if (root_stat != NULL) {
- if (root_stat->st_dev == ino_tmp->device
- && root_stat->st_ino == ino_tmp->inode) {
- add_matched_proc(ino_tmp->name, pid,
- uid, ACCESS_ROOT);
- }
-
+ if (root_dev == ino_tmp->device) {
+ if (!root_stat)
+ root_stat = get_pidstat(pid, "root");
+ if (root_stat && root_stat->st_dev == ino_tmp->device
+ && root_stat->st_ino == ino_tmp->inode)
+ add_matched_proc(ino_tmp->name, pid, uid, ACCESS_ROOT);
}
- if (cwd_stat != NULL) {
- if (cwd_stat->st_dev == ino_tmp->device
- && cwd_stat->st_ino == ino_tmp->inode) {
- add_matched_proc(ino_tmp->name, pid,
- uid, ACCESS_CWD);
- }
+ if (cwd_dev == ino_tmp->device) {
+ if (!cwd_stat)
+ cwd_stat = get_pidstat(pid, "cwd");
+ if (cwd_stat && cwd_stat->st_dev == ino_tmp->device
+ && cwd_stat->st_ino == ino_tmp->inode)
+ add_matched_proc(ino_tmp->name, pid, uid, ACCESS_CWD);
}
}
if (root_stat) free(root_stat);
if (cwd_stat) free(cwd_stat);
if (exe_stat) free(exe_stat);
+#ifndef __linux__
check_dir(pid, "lib", dev_head, ino_head, uid, ACCESS_MMAP,
sockets, netdev);
check_dir(pid, "mmap", dev_head, ino_head, uid, ACCESS_MMAP,
sockets, netdev);
+#endif
check_dir(pid, "fd", dev_head, ino_head, uid, ACCESS_FILE,
sockets, netdev);
check_map(pid, "maps", dev_head, ino_head, uid, ACCESS_MMAP);
@@ -392,9 +430,24 @@ add_special_proc(struct names *name_list
pptr->command = strdup(command);
}
-int parse_file(struct names *this_name, struct inode_list **ino_list)
+int parse_file(struct names *this_name, struct inode_list **ino_list, const char opts)
{
- if (stat(this_name->filename, &(this_name->st)) != 0) {
+ char * new = expandpath(this_name->filename);
+ if (new) {
+ if (this_name->filename)
+ free(this_name->filename);
+ this_name->filename = strdup(new);
+ }
+#ifdef _LISTS_H
+ if (opts & OPT_MOUNTS) {
+ this_name->st.st_dev = device(this_name->filename);
+ this_name->st.st_ino = 0;
+ add_inode(ino_list, this_name, this_name->st.st_dev, this_name->st.st_ino);
+ return 0;
+ }
+#endif
+
+ if (timeout(stat, this_name->filename, &(this_name->st), 5) != 0) {
if (errno == ENOENT)
fprintf(stderr, _("Specified filename %s does not exist.\n"), this_name->filename);
else
@@ -1030,11 +1083,12 @@ int main(int argc, char *argv[])
break;
default: /* FILE */
this_name->filename = strdup(current_argv);
- if (parse_file(this_name, &match_inodes) == 0) {
- parse_unixsockets(this_name, &match_inodes, unixsockets);
+ if (parse_file(this_name, &match_inodes, opts) == 0) {
if (opts & OPT_MOUNTS)
parse_mounts(this_name, &match_devices, opts);
- }
+ else
+ parse_unixsockets(this_name, &match_inodes, unixsockets);
+ }
break;
}
@@ -1258,7 +1312,7 @@ static struct stat *get_pidstat(const pi
if ((st = (struct stat*)malloc(sizeof(struct stat))) == NULL)
return NULL;
snprintf(pathname, 256, "/proc/%d/%s", pid, filename);
- if (stat(pathname, st) != 0)
+ if (timeout(stat, pathname, st, 5) != 0)
goto out;
return st;
out:
@@ -1278,6 +1332,7 @@ check_dir(const pid_t pid, const char *d
struct device_list *dev_tmp;
struct unixsocket_list *sock_tmp;
struct stat st, lst;
+ dev_t thedev;
if ((dirpath = (char*)malloc(MAX_PATHNAME)) == NULL)
goto out;
@@ -1293,53 +1348,68 @@ check_dir(const pid_t pid, const char *d
snprintf(filepath, MAX_PATHNAME, "/proc/%d/%s/%s",
pid, dirname, direntry->d_name);
- if (stat(filepath, &st) != 0) {
+
+#ifdef _LISTS_H
+ st.st_ino = 0;
+ if ((thedev = device(filepath)) < 0)
+#else
+ if (!st.st_ino && timeout(stat, filepath, &st, 5) != 0)
+#endif
+ {
fprintf(stderr, _("Cannot stat file %s: %s\n"),
filepath, strerror(errno));
} else {
- if (st.st_dev == netdev) {
+ if (thedev == netdev) {
for (sock_tmp = sockets; sock_tmp != NULL;
sock_tmp = sock_tmp->next) {
if (sock_tmp->net_inode == st.st_ino) {
st.st_ino = sock_tmp->inode;
st.st_dev = sock_tmp->dev;
+ thedev = sock_tmp->dev;
break;
}
}
}
for (dev_tmp = dev_head; dev_tmp != NULL;
dev_tmp = dev_tmp->next) {
- if (st.st_dev == dev_tmp->device) {
- if (access == ACCESS_FILE
- && (lstat(filepath, &lst) == 0)
- && (lst.st_mode & S_IWUSR)) {
- add_matched_proc(dev_tmp->name,
- pid, uid,
- ACCESS_FILEWR |
- access);
- } else {
- add_matched_proc(dev_tmp->name,
- pid, uid,
- access);
- }
+ if (thedev != dev_tmp->device)
+ continue;
+ if (access == ACCESS_FILE
+ && (lstat(filepath, &lst) == 0)
+ && (lst.st_mode & S_IWUSR)) {
+ add_matched_proc(dev_tmp->name,
+ pid, uid,
+ ACCESS_FILEWR |
+ access);
+ } else {
+ add_matched_proc(dev_tmp->name,
+ pid, uid,
+ access);
}
}
for (ino_tmp = ino_head; ino_tmp != NULL;
ino_tmp = ino_tmp->next) {
- if (st.st_dev == ino_tmp->device
- && st.st_ino == ino_tmp->inode) {
- if (access == ACCESS_FILE
- && (lstat(filepath, &lst) == 0)
- && (lst.st_mode & S_IWUSR)) {
- add_matched_proc(ino_tmp->name,
- pid, uid,
- ACCESS_FILEWR |
- access);
- } else {
- add_matched_proc(ino_tmp->name,
- pid, uid,
- access);
- }
+ if (thedev != ino_tmp->device)
+ continue;
+ if (!st.st_ino && timeout(stat, filepath, &st, 5) != 0) {
+ fprintf(stderr,
+ _("Cannot stat file %s: %s\n"),
+ filepath, strerror(errno));
+ continue;
+ }
+ if (st.st_ino == ino_tmp->inode) {
+ if (access == ACCESS_FILE
+ && (lstat(filepath, &lst) == 0)
+ && (lst.st_mode & S_IWUSR)) {
+ add_matched_proc(ino_tmp->name,
+ pid, uid,
+ ACCESS_FILEWR |
+ access);
+ } else {
+ add_matched_proc(ino_tmp->name,
+ pid, uid,
+ access);
+ }
}
}
}
@@ -1396,7 +1466,7 @@ static uid_t getpiduid(const pid_t pid)
if (snprintf(pathname, MAX_PATHNAME, "/proc/%d", pid) < 0)
return 0;
- if (stat(pathname, &st) != 0)
+ if (timeout(stat, pathname, &st, 5) != 0)
return 0;
return st.st_uid;
}
@@ -1432,7 +1502,7 @@ void fill_unix_cache(struct unixsocket_l
path = scanned_path;
if (*scanned_path == '@')
scanned_path++;
- if (stat(scanned_path, &st) < 0) {
+ if (timeout(stat, scanned_path, &st, 5) < 0) {
free(path);
continue;
}
@@ -1571,7 +1641,7 @@ scan_knfsd(struct names *names_head, str
if ((find_space = strpbrk(line, " \t")) == NULL)
continue;
*find_space = '\0';
- if (stat(line, &st) != 0) {
+ if (timeout(stat, line, &st, 5) != 0) {
continue;
}
/* Scan the devices */
@@ -1615,7 +1685,7 @@ scan_mounts(struct names *names_head, st
if ((find_space = strchr(find_mountp, ' ')) == NULL)
continue;
*find_space = '\0';
- if (stat(find_mountp, &st) != 0) {
+ if (timeout(stat, find_mountp, &st, 5) != 0) {
continue;
}
/* Scan the devices */
@@ -1662,7 +1732,7 @@ scan_swaps(struct names *names_head, str
if (*find_space == '\0')
continue;
}
- if (stat(line, &st) != 0) {
+ if (timeout(stat, line, &st, 5) != 0) {
continue;
}
/* Scan the devices */
@@ -1682,3 +1752,279 @@ scan_swaps(struct names *names_head, str
}
fclose(fp);
}
+
+/*
+ * Execute stat(2) system call with timeout to avoid deadlock
+ * on network based file systems.
+ */
+static sigjmp_buf jenv;
+
+static void
+sigalarm(int sig)
+{
+ if (sig == SIGALRM)
+ siglongjmp(jenv, 1);
+}
+
+static int
+timeout(stat_t func, const char *path, struct stat *buf, unsigned int seconds)
+{
+ pid_t pid = 0;
+ int ret = 0, pipes[4];
+ ssize_t len;
+
+ if (pipe(&pipes[0]) < 0)
+ goto err;
+ switch ((pid = fork ())) {
+ case -1:
+ close(pipes[0]);
+ close(pipes[1]);
+ goto err;
+ case 0:
+ (void) signal(SIGALRM, SIG_DFL);
+ close(pipes[0]);
+ if ((ret = func(path, buf)) == 0)
+ do len = write(pipes[1], buf, sizeof(struct stat));
+ while (len < 0 && errno == EINTR);
+ close(pipes[1]);
+ exit(ret);
+ default:
+ close(pipes[1]);
+ if (sigsetjmp(jenv, 1)) {
+ (void) alarm(0);
+ (void) signal(SIGALRM, SIG_DFL);
+ if (waitpid(0, (int*)0, WNOHANG) == 0)
+ kill(pid, SIGKILL);
+ errno = ETIMEDOUT;
+ seconds = 1;
+ goto err;
+ }
+ (void) signal(SIGALRM, sigalarm);
+ (void) alarm(seconds);
+ if (read(pipes[0], buf, sizeof(struct stat)) == 0) {
+ errno = EFAULT;
+ ret = -1;
+ }
+ (void) alarm(0);
+ (void) signal(SIGALRM, SIG_DFL);
+ close(pipes[0]);
+ break;
+ }
+ return ret;
+err:
+ return -1;
+}
+
+#ifdef _LISTS_H
+/*
+ * Use /proc/self/mountinfo of modern linux system to determine
+ * the device numbers of the mount points. Use this to avoid the
+ * stat(2) system call wherever possible.
+ */
+
+static list_t mntinfo = {&mntinfo, &mntinfo};
+
+static void
+clear_mntinfo(void)
+{
+ list_t *ptr, *tmp;
+
+ list_for_each_safe(ptr, tmp, &mntinfo) {
+ mntinfo_t *mnt = list_entry(ptr, mntinfo_t);
+ delete(ptr);
+ free(mnt);
+ }
+}
+
+
+static void
+init_mntinfo(void)
+{
+ char mpoint[PATH_MAX+1];
+ int mid, parid, max = 0;
+ uint maj, min;
+ list_t sort;
+ FILE * mnt;
+
+ if (!list_empty(&mntinfo))
+ return;
+ if ((mnt = fopen("/proc/self/mountinfo", "r")) == (FILE*)0)
+ return;
+ while (fscanf(mnt, "%i %i %u:%u %*s %s %*[^\n]", &mid, &parid, &maj, &min, &mpoint[0]) == 5) {
+ const size_t nlen = strlen(mpoint);
+ mntinfo_t *restrict mnt;
+ if (posix_memalign((void*)&mnt, sizeof(void*), alignof(mntinfo_t)+(nlen+1)) != 0) {
+ fprintf(stderr, _("Cannot allocate memory for matched proc: %s\n"),
+ strerror(errno));
+ exit(1);
+ }
+ append(mnt, mntinfo);
+ mnt->mpoint = ((char*)mnt)+alignof(mntinfo_t);
+ strcpy(mnt->mpoint, mpoint);
+ mnt->nlen = nlen;
+ mnt->parid = parid;
+ mnt->dev = makedev(maj, min);
+ mnt->id = mid;
+ if (mid > max)
+ max = mid;
+ }
+ fclose(mnt);
+
+ /* Sort mount points accordingly to the reverse mount order */
+ initial(&sort);
+ for (mid = 1; mid <= max; mid++) {
+ list_t *ptr, *tmp;
+ list_for_each_safe(ptr, tmp, &mntinfo) {
+ mntinfo_t *mnt = list_entry(ptr, mntinfo_t);
+ if (mid != mnt->id)
+ continue;
+ move_head(ptr, &sort);
+ break;
+ }
+ list_for_each_safe(ptr, tmp, &mntinfo) {
+ mntinfo_t *mnt = list_entry(ptr, mntinfo_t);
+ if (mid != mnt->parid)
+ continue;
+ move_head(ptr, &sort);
+ }
+ }
+ if (!list_empty(&mntinfo)) {
+ errno = EBADE;
+ }
+ join(&sort, &mntinfo);
+}
+
+/*
+ * Determine device of links below /proc/
+ */
+static dev_t
+device(const char * path)
+{
+ char name[PATH_MAX+1];
+ const char *use;
+ ssize_t nlen;
+ list_t *ptr;
+
+ if ((nlen = readlink(path, name, PATH_MAX)) < 0) {
+ nlen = strlen(path);
+ use = &path[0];
+ } else {
+ name[nlen] = '\0';
+ use = &name[0];
+ }
+
+ if (*use != '/') { /* sepcial file (socket, pipe, inotify) */
+ struct stat st;
+ if (timeout(stat, path, &st, 5) != 0)
+ return (dev_t)-1;
+ return st.st_dev;
+ }
+
+ list_for_each(ptr, &mntinfo) {
+ mntinfo_t *mnt = list_entry(ptr, mntinfo_t);
+ if (nlen < mnt->nlen)
+ continue;
+ if (use[mnt->nlen] != '\0' && use[mnt->nlen] != '/')
+ continue;
+ if (strncmp(use, mnt->mpoint, mnt->nlen) == 0)
+ return mnt->dev;
+ }
+ return (dev_t)-1;
+}
+#endif /* _LISTS_H */
+
+/*
+ * Somehow the realpath(3) glibc function call, nevertheless
+ * it avoids lstat(2) system calls.
+ */
+static char real[PATH_MAX+1];
+char* expandpath(const char * path)
+{
+ char tmpbuf[PATH_MAX+1];
+ const char *start, *end;
+ char *curr, *dest;
+ int deep = MAXSYMLINKS;
+
+ if (!path || *path == '\0')
+ return (char*)0;
+
+ curr = &real[0];
+
+ if (*path != '/') {
+ if (!getcwd(curr, PATH_MAX))
+ return (char*)0;
+ dest = rawmemchr(curr, '\0');
+ } else {
+ *curr = '/';
+ dest = curr + 1;
+ }
+
+ for (start = end = path; *start; start = end) {
+
+ while (*start == '/')
+ ++start;
+
+ for (end = start; *end && *end != '/'; ++end)
+ ;
+
+ if (end - start == 0)
+ break;
+ else if (end - start == 1 && start[0] == '.') {
+ ;
+ } else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
+ if (dest > curr + 1)
+ while ((--dest)[-1] != '/')
+ ;
+ } else {
+ char lnkbuf[PATH_MAX+1];
+ size_t len;
+ ssize_t n;
+
+ if (dest[-1] != '/')
+ *dest++ = '/';
+
+ if (dest + (end - start) > curr + PATH_MAX) {
+ errno = ENAMETOOLONG;
+ return (char*)0;
+ }
+
+ dest = mempcpy(dest, start, end - start);
+ *dest = '\0';
+
+ if (deep-- < 0) {
+ errno = ELOOP;
+ return (char*)0;
+ }
+
+ errno = 0;
+ if ((n = readlink(curr, lnkbuf, PATH_MAX)) < 0) {
+ deep = MAXSYMLINKS;
+ if (errno == EINVAL)
+ continue; /* Not a symlink */
+ return (char*)0;
+ }
+ lnkbuf[n] = '\0'; /* Don't be fooled by readlink(2) */
+
+ len = strlen(end);
+ if ((n + len) > PATH_MAX) {
+ errno = ENAMETOOLONG;
+ return (char*)0;
+ }
+
+ memmove(&tmpbuf[n], end, len + 1);
+ path = end = memcpy(tmpbuf, lnkbuf, n);
+
+ if (lnkbuf[0] == '/')
+ dest = curr + 1;
+ else if (dest > curr + 1)
+ while ((--dest)[-1] != '/');
+
+ }
+ }
+
+ if (dest > curr + 1 && dest[-1] == '/')
+ --dest;
+ *dest = '\0';
+
+ return curr;
+}
--- src/fuser.h
+++ src/fuser.h 2011-02-08 16:25:57.575238622 +0000
@@ -85,6 +85,17 @@ struct mount_list {
struct mount_list *next;
};
+#if defined (__GNUC__) && defined(__OPTIMIZE__)
+# include "lists.h"
+typedef struct mntinfo_s {
+ list_t this;
+ int id, parid;
+ dev_t dev;
+ size_t nlen;
+ char *mpoint;
+} mntinfo_t;
+#endif
+
#define NAMESPACE_FILE 0
#define NAMESPACE_TCP 1
#define NAMESPACE_UDP 2
--- src/lists.h
+++ src/lists.h 2011-02-07 12:01:52.000000000 +0000
@@ -0,0 +1,249 @@
+/*
+ * lists.h Simple doubly linked list implementation,
+ * based on <linux/list.h> and <linux/prefetch.h>.
+ *
+ * Version: 0.1 01-Feb-2011 Fink
+ *
+ * Copyright 2011 Werner Fink, 2005 SUSE LINUX Products GmbH, Germany.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Author: Werner Fink <werner@suse.de>, 2011
+ */
+
+#ifndef _LISTS_H
+#define _LISTS_H
+
+#include <stddef.h>
+#include <sys/types.h>
+
+typedef enum _boolean {false, true} boolean;
+typedef unsigned char uchar;
+#ifndef __USE_MISC
+typedef unsigned short ushort;
+typedef unsigned int uint;
+#endif
+
+#ifndef __OPTIMIZE__
+# warning This will not compile without -O at least
+#endif
+#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
+# ifndef inline
+# define inline __inline__
+# endif
+# ifndef restrict
+# define restrict __restrict__
+# endif
+# ifndef volatile
+# define volatile __volatile__
+# endif
+# ifndef asm
+# define asm __asm__
+# endif
+# ifndef extension
+# define extension __extension__
+# endif
+#endif
+#ifndef attribute
+# define attribute(attr) __attribute__(attr)
+#endif
+
+/*
+ * This is lent from the kernel by e.g. using
+ *
+ * echo '#include <asm-i386/processor.h>\nint main () { prefetch(); return 0; }' | \
+ * gcc -I/usr/src/linux/include -D__KERNEL__ -x c -E -P - | \
+ * sed -rn '/void[[:blank:]]+prefetch[[:blank:]]*\(/,/^}/p'
+ *
+ * on the appropiate architecture (here on i686 for i586).
+ */
+extern inline void attribute((used,__gnu_inline__,always_inline,__artificial__)) prefetch(const void *restrict x)
+{
+#if defined(__x86_64__)
+ asm volatile ("prefetcht0 %0" :: "m" (*(unsigned long *)x))
+#elif defined(__ia64__)
+ asm volatile ("lfetch [%0]" :: "r" (x))
+#elif defined(__powerpc64__)
+ asm volatile ("dcbt 0,%0" :: "r" (x))
+#elif 1 && defined(__i386__)
+ asm volatile ("661:\n\t"
+ ".byte 0x8d,0x74,0x26,0x00\n"
+ "\n662:\n"
+ ".section .altinstructions,\"a\"\n"
+ " .align 4\n"
+ " .long 661b\n"
+ " .long 663f\n"
+ " .byte %c0\n"
+ " .byte 662b-661b\n"
+ " .byte 664f-663f\n"
+ ".previous\n"
+ ".section .altinstr_replacement,\"ax\"\n"
+ " 663:\n\t"
+ " prefetchnta (%1)"
+ " \n664:\n"
+ ".previous"
+ :: "i" ((0*32+25)), "r" (x))
+#else
+ __builtin_prefetch ((x), 0, 1);
+#endif
+ ;
+}
+
+#if defined(DEBUG) && (DEBUG > 0)
+# define __align attribute((packed))
+#else
+# define __align attribute((aligned(sizeof(struct list_struct*))))
+#endif
+#define __packed attribute((packed))
+
+#define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
+#define strsize(string) ((strlen(string)+1)*sizeof(char))
+
+typedef struct list_struct {
+ struct list_struct * next, * prev;
+} __align list_t;
+
+/*
+ * Linked list handling
+ * ====================
+ * The structures which will be linked into such lists have to be of the
+ * same type. The structures may have alway a list identifier of the type
+ * `list_t' as very first element. With this the macro list_entry() can
+ * be used to cast the memory address of a list member to the corresponding
+ * allocated structure.
+ */
+
+/*
+ * Insert new entry as next member.
+ */
+static inline void _insert(list_t *restrict new, list_t *restrict here) attribute((always_inline,nonnull(1,2)));
+static inline void _insert(list_t *restrict new, list_t *restrict here)
+{
+ list_t * prev = here;
+ list_t * next = here->next;
+
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+
+#define insert(new, list) _insert(&((new)->this), (&(list)));
+#define append(new, list) _insert(&((new)->this), (&(list))->prev);
+
+/*
+ * Set head
+ */
+static inline void initial(list_t *restrict head) attribute((always_inline,nonnull(1)));
+static inline void initial(list_t *restrict head)
+{
+ head->prev = head->next = head;
+}
+
+/*
+ * Remove entries, note that the pointer its self remains.
+ */
+static inline void delete(list_t *restrict entry) attribute((always_inline,nonnull(1)));
+static inline void delete(list_t *restrict entry)
+{
+ list_t * prev = entry->prev;
+ list_t * next = entry->next;
+
+ next->prev = prev;
+ prev->next = next;
+
+ initial(entry);
+}
+
+/*
+ * Replace an entry by a new one.
+ */
+static inline void replace(list_t *restrict old, list_t *restrict new) attribute((always_inline,nonnull(1,2)));
+static inline void replace(list_t *restrict old, list_t *restrict new)
+{
+ new->next = old->next;
+ new->next->prev = new;
+ new->prev = old->prev;
+ new->prev->next = new;
+}
+
+static inline void join(list_t *restrict list, list_t *restrict head) attribute((always_inline,nonnull(1,2)));
+static inline void join(list_t *restrict list, list_t *restrict head)
+{
+ list_t * first = list->next;
+
+ if (first != list) {
+ list_t * last = list->prev;
+ list_t * at = head->next;
+
+ first->prev = head;
+ head->next = first;
+
+ last->next = at;
+ at->prev = last;
+ }
+}
+
+static inline boolean list_empty(const list_t *restrict const head) attribute((always_inline,nonnull(1)));
+static inline boolean list_empty(const list_t *restrict const head)
+{
+ return head->next == head;
+}
+
+static inline void move_head(list_t *restrict entry, list_t *restrict head) attribute((always_inline,nonnull(1,2)));
+static inline void move_head(list_t *restrict entry, list_t *restrict head)
+{
+ list_t * prev = entry->prev;
+ list_t * next = entry->next;
+
+ next->prev = prev; /* remove entry from old list */
+ prev->next = next;
+
+ prev = head;
+ next = head->next;
+
+ next->prev = entry; /* and add it at head of new list */
+ entry->next = next;
+ entry->prev = prev;
+ prev->next = entry;
+}
+
+static inline void move_tail(list_t *restrict entry, list_t *restrict head) attribute((always_inline,nonnull(1,2)));
+static inline void move_tail(list_t *restrict entry, list_t *restrict head)
+{
+ list_t * prev = entry->prev;
+ list_t * next = entry->next;
+
+ next->prev = prev; /* remove entry from old list */
+ prev->next = next;
+
+ prev = head->prev;
+ next = head;
+
+ next->prev = entry; /* and add it at tail of new list */
+ entry->next = next;
+ entry->prev = prev;
+ prev->next = entry;
+}
+
+/*
+ * The handle of the list is named `this'
+ */
+#define list_entry(ptr, type) (__extension__ ({ \
+ const typeof( ((type *)0)->this ) *__mptr = (ptr); \
+ ((type *)( (char *)(__mptr) - offsetof(type,this) )); }))
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; prefetch(pos->next), pos != (head); pos = pos->next)
+#define np_list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+#define list_for_each_safe(pos, safe, head) \
+ for (pos = (head)->next, safe = pos->next; pos != (head); pos = safe, safe = pos->next)
+#define list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; prefetch(pos->prev), pos != (head); pos = pos->prev)
+#define np_list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; pos != (head); pos = pos->prev)
+
+#endif /* _LISTS_H */

View File

@ -10,18 +10,18 @@
AC_CHECK_MEMBERS([struct pt_regs.orig_gpr3, AC_CHECK_MEMBERS([struct pt_regs.orig_gpr3,
struct pt_regs.gpr], [],[], [#include <linux/ptrace.h>]) struct pt_regs.gpr], [],[], [#include <linux/ptrace.h>])
--- src/fuser.c --- src/fuser.c
+++ src/fuser.c 2010-07-13 15:01:58.000000000 +0000 +++ src/fuser.c 2011-02-08 16:08:59.823926228 +0000
@@ -1120,7 +1120,8 @@ int main(int argc, char *argv[]) @@ -888,7 +888,8 @@ int main(int argc, char *argv[])
char option_buf[3]; char option_buf[3];
struct option *optr; struct option *optr;
char *nsptr; char *nsptr;
- int skip_argv; - int skip_argv;
+ int skip_argv; + int skip_argv;
+ int seen_file; + int seen_file;
size_t len;
struct option options[] = { struct option options[] = {
@@ -1160,6 +1161,7 @@ int main(int argc, char *argv[]) {"all", 0, NULL, 'a'},
@@ -927,6 +928,7 @@ int main(int argc, char *argv[])
netdev = find_net_dev(); netdev = find_net_dev();
fill_unix_cache(&unixsockets); fill_unix_cache(&unixsockets);
@ -29,7 +29,7 @@
for (argc_cnt = 1; argc_cnt < argc; argc_cnt++) { for (argc_cnt = 1; argc_cnt < argc; argc_cnt++) {
current_argv = argv[argc_cnt]; current_argv = argv[argc_cnt];
if (current_argv[0] == '-') { /* its an option */ if (current_argv[0] == '-') { /* its an option */
@@ -1291,6 +1293,7 @@ int main(int argc, char *argv[]) @@ -1056,6 +1058,7 @@ int main(int argc, char *argv[])
} }
} }
this_name->matched_procs = NULL; this_name->matched_procs = NULL;
@ -37,7 +37,7 @@
if (opts & (OPT_MOUNTS|OPT_ISMOUNTPOINT) if (opts & (OPT_MOUNTS|OPT_ISMOUNTPOINT)
&& this_name->name_space != NAMESPACE_FILE) && this_name->name_space != NAMESPACE_FILE)
usage(_ usage(_
@@ -1350,7 +1353,7 @@ int main(int argc, char *argv[]) @@ -1103,7 +1106,7 @@ int main(int argc, char *argv[])
names_tail->next = this_name; names_tail->next = this_name;
names_tail = this_name; names_tail = this_name;
} /* for across the argvs */ } /* for across the argvs */

View File

@ -1,3 +1,10 @@
-------------------------------------------------------------------
Tue Feb 8 17:43:57 CET 2011 - werner@suse.de
- Now use /proc/self/mountinfo to minimize the needs of running
stat(2) system calls. The remaining stat(2) system calls are
wrapped by an timeout function.
------------------------------------------------------------------- -------------------------------------------------------------------
Fri Oct 15 12:36:16 CEST 2010 - werner@suse.de Fri Oct 15 12:36:16 CEST 2010 - werner@suse.de

View File

@ -35,7 +35,7 @@ Patch1: %name-22.12-tigetstr.patch
Patch2: %name-22.12-pstree_overflow.patch Patch2: %name-22.12-pstree_overflow.patch
Patch3: %name-22.12-pstree.patch Patch3: %name-22.12-pstree.patch
Patch4: %name-22.12-leaks.patch Patch4: %name-22.12-leaks.patch
Patch5: %name-22.12-nfs4fuser.patch Patch5: %name-22.13-nfs4fuser.patch
Patch6: %name-22.12-writeonly.patch Patch6: %name-22.12-writeonly.patch
Patch7: %name-22.13-peekfd-segv.patch Patch7: %name-22.13-peekfd-segv.patch
@ -59,7 +59,7 @@ processes that are using specified files or filesystems.
%patch5 -p0 -b .nfs4fuser %patch5 -p0 -b .nfs4fuser
%patch6 -p0 -b .wrtonly %patch6 -p0 -b .wrtonly
%patch7 -p1 -b .peekfd %patch7 -p1 -b .peekfd
%patch0 -p0 %patch0 -p0 -b .0
%build %build
autoreconf -fi autoreconf -fi