* Strip @ symbol from file names read from /proc/net/unix
* Above 2 changes close openSuSE bugs #536209, #529520, and #417841
and provided by Werner Fink
+ * Applied patch from Werner Fink to avoid stat(2) on NFS mounts
+ * Zeros process group memory - Patch by jgorig SF#3152925 RH#666213
+ * fuser -m -s flags work - Patch by jgorig SF#31110178 RH#651794
+ * fuser silent if /proc/swaps not available SF#3072134
+ * ppc 64 support for peekfd by jgorig SF#3166444
+ * jiffies now ULL in killall SF#3138538
+ * pstree can show parents of a process. Patch supplied by Curtis
+ Hawthorne SF#3135157
Changes in 22.13
================
-.TH KILLALL 1 2009-12-18 "Linux" "User Commands"
+.TH KILLALL 1 2011-02-22 "Linux" "User Commands"
.SH NAME
killall \- kill processes by name
.SH SYNOPSIS
them correctly.
.SH AUTHORS
Werner Almesberger <werner@almesberger.net> wrote the original version
-of psmisc. Since version 20 Craig Small <csmall@small.dropbear.id.au>
+of psmisc. Since version 20 Craig Small <csmall@enc.com.au>
can be blamed.
.SH "SEE ALSO"
.BR kill "(1), " fuser "(1), " pgrep "(1), " pidof "(1), " pkill "(1), "
-.TH PSTREE 1 2009-12-16 "Linux" "User Commands"
+.TH PSTREE 1 2011-02-22 "Linux" "User Commands"
.SH NAME
pstree \- display a tree of processes
.SH SYNOPSIS
.RB [ \-l | \-\-long ]
.RB [ \-n | \-\-numeric\-sort ]
.RB [ \-p | \-\-show\-pids ]
+.RB [ \-s | \-\-show\-parents ]
.RB [ \-u | \-\-uid\-changes ]
.RB [ \-Z | \-\-security\-context ]
.RB [ \-A | \-\-ascii | \-G | \-\-vt100 | \-U | \-\-unicode ]
.IP \fB\-p\fP
Show PIDs. PIDs are shown as decimal numbers in parentheses after each
process name. \fB\-p\fP implicitly disables compaction.
+.IP \fB\-s\fP
+Show parent processes of the specified process.
.IP \fB\-u\fP
Show uid transitions. Whenever the uid of a process differs from the uid of
its parent, the new uid is shown in parentheses after the process name.
.fi
.SH AUTHORS
Werner Almesberger <werner@almesberger.net>
-Craig Small <csmall@small.dropbear.id.au>
+Craig Small <csmall@enc.com.au>
.SH BUGS
Some character sets may be incompatible with the VT100 characters.
#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>
#include <mntent.h>
#include <signal.h>
#include <getopt.h>
+#include <setjmp.h>
#include "fuser.h"
#include "signals.h"
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)
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"),
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);
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 (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_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 (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);
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
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;
}
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:
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;
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);
+ }
}
}
}
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;
}
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;
}
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 */
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 */
struct stat st;
if ((fp = fopen(PROC_SWAPS, "r")) == NULL) {
- fprintf(stderr, "Cannot open %s\n", PROC_SWAPS);
+ /*fprintf(stderr, "Cannot open %s\n", PROC_SWAPS);*/
return;
}
/* lines are filename type */
if (*find_space == '\0')
continue;
}
- if (stat(line, &st) != 0) {
+ if (timeout(stat, line, &st, 5) != 0) {
continue;
}
/* Scan the devices */
}
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 != '/') { /* special 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 (mnt->nlen == 1) /* root fs is the last entry */
+ return mnt->dev;
+ 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;
+}
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
}
/* process age from jiffies to seconds via uptime */
-static double process_age(const unsigned jf)
+static double process_age(const unsigned long long jf)
{
double sc_clk_tck = sysconf(_SC_CLK_TCK);
assert(sc_clk_tck > 0);
pgids = NULL; /* silence gcc */
else
{
- pgids = malloc (pids * sizeof (pid_t));
+ pgids = calloc (pids, sizeof (pid_t));
if (!pgids)
{
perror ("malloc");
}
if ( younger_than || older_than ) {
rewind(file);
- unsigned int proc_stt_jf = 0;
+ unsigned long long proc_stt_jf = 0;
okay = fscanf(file, "%*d %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %ull",
&proc_stt_jf) == 1;
if (!okay) {
--- /dev/null
+/*
+ * 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 */
+/*
+ * 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 */
#define REG_PARAM1 orig_gpr3
#define REG_PARAM2 gpr[4]
#define REG_PARAM3 gpr[5]
+#ifndef PT_ORIG_R3
+ #define PT_ORIG_R3 34
+#endif
#elif defined(ARM)
#ifndef __ARM_EABI__
#error arm oabi not supported
#define REG_PARAM1 regs[4]
#define REG_PARAM2 regs[5]
#define REG_PARAM3 regs[6]
-#ifndef PT_ORIG_R3
- #define PT_ORIG_R3 34
-#endif
#endif
#define MAX_ATTACHED_PIDS 1024
if (WIFSTOPPED(status)) {
#ifdef PPC
struct pt_regs regs;
- regs.gpr[0] = ptrace(PTRACE_PEEKUSER, pid, 4 * PT_R0, 0);
- regs.gpr[3] = ptrace(PTRACE_PEEKUSER, pid, 4 * PT_R3, 0);
- regs.gpr[4] = ptrace(PTRACE_PEEKUSER, pid, 4 * PT_R4, 0);
- regs.gpr[5] = ptrace(PTRACE_PEEKUSER, pid, 4 * PT_R5, 0);
- regs.orig_gpr3 = ptrace(PTRACE_PEEKUSER, pid, 4 * PT_ORIG_R3, 0);
+ regs.gpr[0] = ptrace(PTRACE_PEEKUSER, pid, __WORDSIZE/8 * PT_R0, 0);
+ regs.gpr[3] = ptrace(PTRACE_PEEKUSER, pid, __WORDSIZE/8 * PT_R3, 0);
+ regs.gpr[4] = ptrace(PTRACE_PEEKUSER, pid, __WORDSIZE/8 * PT_R4, 0);
+ regs.gpr[5] = ptrace(PTRACE_PEEKUSER, pid, __WORDSIZE/8 * PT_R5, 0);
+ regs.orig_gpr3 = ptrace(PTRACE_PEEKUSER, pid, __WORDSIZE/8 * PT_ORIG_R3, 0);
#elif defined(ARM)
struct pt_regs regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
for (i = 0; i < regs.REG_PARAM3; i++) {
#ifdef _BIG_ENDIAN
+#if __WORDSIZE == 64
+ unsigned int a = bswap_64(ptrace(PTRACE_PEEKTEXT, pid, regs.REG_PARAM2 + i, 0));
+#else
unsigned int a = bswap_32(ptrace(PTRACE_PEEKTEXT, pid, regs.REG_PARAM2 + i, 0));
+#endif
#else
unsigned int a = ptrace(PTRACE_PEEKTEXT, pid, regs.REG_PARAM2 + i, 0);
#endif
static int *width = NULL;
static int *more = NULL;
-static int print_args = 0, compact = 1, user_change = 0, pids = 0, by_pid =
- 0, trunc = 1, wait_end = 0;
+static int print_args = 0, compact = 1, user_change = 0, pids = 0,
+ show_parents = 0, by_pid = 0, trunc = 1, wait_end = 0;
#ifdef WITH_SELINUX
static int show_scontext = 0;
#endif /*WITH_SELINUX */
dump_by_user(walk->child, uid);
}
+static void trim_tree_by_parent(PROC * current)
+{
+ if (!current)
+ return;
+
+ PROC * parent = current->parent;
+
+ if (!parent)
+ return;
+
+ parent->children = NULL;
+ add_child(parent, current);
+ trim_tree_by_parent(parent);
+}
+
/*
* read_proc now uses a similar method as procps for finding the process
" -l, --long don't truncate long lines\n"
" -n, --numeric-sort sort output by PID\n"
" -p, --show-pids show PIDs; implies -c\n"
+ " -s, --show-parents show parents of the selected process\n"
" -u, --uid-changes show uid transitions\n"
" -U, --unicode use UTF-8 (Unicode) line drawing characters\n"
" -V, --version display version information\n"));
{"vt100", 0, NULL, 'G'},
{"highlight-all", 0, NULL, 'h'},
{"highlight-pid", 1, NULL, 'H'},
+ {"long", 0, NULL, 'l'},
{"numeric-sort", 0, NULL, 'n'},
{"show-pids", 0, NULL, 'p'},
- {"long", 0, NULL, 'l'},
+ {"show-parents", 0, NULL, 's'},
{"uid-changes", 0, NULL, 'u'},
{"unicode", 0, NULL, 'U'},
{"version", 0, NULL, 'V'},
#ifdef WITH_SELINUX
while ((c =
- getopt_long(argc, argv, "aAcGhH:npluUVZ", options,
+ getopt_long(argc, argv, "aAcGhH:nplsuUVZ", options,
NULL)) != -1)
#else /*WITH_SELINUX */
while ((c =
- getopt_long(argc, argv, "aAcGhH:npluUV", options, NULL)) != -1)
+ getopt_long(argc, argv, "aAcGhH:nplsuUV", options, NULL)) != -1)
#endif /*WITH_SELINUX */
switch (c) {
case 'a':
pids = 1;
compact = 0;
break;
+ case 's':
+ show_parents = 1;
+ break;
case 'u':
user_change = 1;
break;
for (current = find_proc(highlight); current;
current = current->parent)
current->flags |= PFLAG_HILIGHT;
+
+ if(show_parents && pid != 0) {
+ trim_tree_by_parent(find_proc(pid));
+
+ pid = 1;
+ }
+
if (!pw)
dump_tree(find_proc(pid), 0, 1, 1, 1, 0, 0);
else {