/*
* pstree.c - display process tree
*
* Copyright (C) 1993-2002 Werner Almesberger
* Copyright (C) 2002-2014 Craig Small
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <pwd.h>
#include <dirent.h>
#include <curses.h>
#include <term.h>
#include <termios.h>
#include <langinfo.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "i18n.h"
#include "comm.h"
#ifdef WITH_SELINUX
#include <selinux/selinux.h>
#else
typedef void* security_context_t; /* DUMMY to remove most ifdefs */
#endif /*WITH_SELINUX */
extern const char *__progname;
#define PROC_BASE "/proc"
#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
#define ROOT_PID 0
#else
#define ROOT_PID 1
#endif /* __FreeBSD__ */
/* UTF-8 defines by Johan Myreen, updated by Ben Winslow */
#define UTF_V "\342\224\202" /* U+2502, Vertical line drawing char */
#define UTF_VR "\342\224\234" /* U+251C, Vertical and right */
#define UTF_H "\342\224\200" /* U+2500, Horizontal */
#define UTF_UR "\342\224\224" /* U+2514, Up and right */
#define UTF_HD "\342\224\254" /* U+252C, Horizontal and down */
#define VT_BEG "\033(0\017" /* use graphic chars */
#define VT_END "\033(B" /* back to normal char set */
#define VT_V "x" /* see UTF definitions above */
#define VT_VR "t"
#define VT_H "q"
#define VT_UR "m"
#define VT_HD "w"
#define NUM_NS 6
#define THREAD_FORMAT "{%.*s}" /* Format for thread names */
typedef struct _proc {
char comm[COMM_LEN + 2 + 1]; /* add another 2 for thread brackets */
char **argv; /* only used : argv[0] is 1st arg; undef if argc < 1 */
int argc; /* with -a : number of arguments, -1 if swapped */
pid_t pid;
pid_t pgid;
uid_t uid;
security_context_t scontext;
ino_t ns[NUM_NS];
char flags;
struct _child *children;
struct _proc *parent;
struct _proc *next;
} PROC;
/* For flags above */
#define PFLAG_HILIGHT 0x01
#define PFLAG_THREAD 0x02
typedef struct _child {
PROC *child;
struct _child *next;
} CHILD;
static struct {
const char *empty_2; /* */
const char *branch_2; /* |- */
const char *vert_2; /* | */
const char *last_2; /* `- */
const char *single_3; /* --- */
const char *first_3; /* -+- */
} sym_ascii = {
" ", "|-", "| ", "`-", "---", "-+-"}
, sym_utf = {
" ",
UTF_VR UTF_H,
UTF_V " ",
UTF_UR UTF_H, UTF_H UTF_H UTF_H, UTF_H UTF_HD UTF_H}, sym_vt100 = {
" ",
VT_BEG VT_VR VT_H VT_END,
VT_BEG