psmisc/psmisc-22.13-nfs4fuser.patch

932 lines
25 KiB
Diff
Raw Normal View History

--- 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 */