2021-03-24 03:43:51 +09:00
|
|
|
/*
|
|
|
|
* lsfd(1) - list file descriptors
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Red Hat, Inc. All rights reserved.
|
|
|
|
* Written by Masatake YAMATO <yamato@redhat.com>
|
|
|
|
*
|
|
|
|
* Very generally based on lsof(8) by Victor A. Abell <abe@purdue.edu>
|
|
|
|
* It supports multiple OSes. lsfd specializes to Linux.
|
|
|
|
*
|
|
|
|
* 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 would 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
2021-05-05 03:37:57 +09:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <linux/kcmp.h>
|
|
|
|
static int kcmp(pid_t pid1, pid_t pid2, int type,
|
|
|
|
unsigned long idx1, unsigned long idx2)
|
|
|
|
{
|
|
|
|
return syscall(SYS_kcmp, pid1, pid2, type, idx1, idx2);
|
|
|
|
}
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
#include "c.h"
|
|
|
|
#include "nls.h"
|
|
|
|
#include "xalloc.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "closestream.h"
|
|
|
|
#include "strutils.h"
|
|
|
|
#include "procutils.h"
|
|
|
|
#include "fileutils.h"
|
2021-03-26 16:55:57 +09:00
|
|
|
#include "idcache.h"
|
2021-03-24 03:43:51 +09:00
|
|
|
|
|
|
|
#include "libsmartcols.h"
|
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
#include "lsfd.h"
|
2021-03-24 03:43:51 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Multi-threading related stuffs
|
|
|
|
*/
|
2021-04-17 05:49:58 +09:00
|
|
|
#define NUM_COLLECTORS 1
|
2021-03-24 03:43:51 +09:00
|
|
|
static pthread_cond_t procs_ready = PTHREAD_COND_INITIALIZER;
|
|
|
|
static pthread_mutex_t procs_ready_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
static pthread_mutex_t procs_consumer_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static struct list_head *current_proc;
|
|
|
|
|
|
|
|
static void *fill_procs(void *arg);
|
|
|
|
|
2021-05-06 22:15:24 +09:00
|
|
|
/*
|
|
|
|
* /proc/$pid/maps entries
|
|
|
|
*/
|
|
|
|
struct map {
|
|
|
|
struct list_head maps;
|
|
|
|
unsigned long mem_addr_start;
|
|
|
|
unsigned long long file_offset;
|
|
|
|
unsigned int read:1, write:1, exec:1, shared:1;
|
|
|
|
};
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
/*
|
|
|
|
* Column related stuffs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* column names */
|
|
|
|
struct colinfo {
|
|
|
|
const char *name;
|
|
|
|
double whint;
|
|
|
|
int flags;
|
2021-05-03 15:33:56 +09:00
|
|
|
int json_type;
|
2021-03-24 03:43:51 +09:00
|
|
|
const char *help;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* columns descriptions */
|
|
|
|
static struct colinfo infos[] = {
|
2021-05-03 15:33:56 +09:00
|
|
|
[COL_ASSOC] = { "ASSOC", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("association between file and process") },
|
2021-05-08 03:51:25 +09:00
|
|
|
[COL_CHRDRV] = { "CHRDRV", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("charcter device driver name resolved by /proc/devices") },
|
2021-05-05 02:50:51 +09:00
|
|
|
[COL_COMMAND] = { "COMMAND", 15, SCOLS_FL_TRUNC, SCOLS_JSON_STRING,
|
2021-05-03 15:33:56 +09:00
|
|
|
N_("command of the process opening the file") },
|
2021-05-03 16:22:29 +09:00
|
|
|
[COL_DELETED] = { "DELETED", 0, SCOLS_FL_RIGHT, SCOLS_JSON_BOOLEAN,
|
|
|
|
N_("reachability from the file system") },
|
2021-05-03 15:33:56 +09:00
|
|
|
[COL_DEV] = { "DEV", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("ID of device containing file") },
|
|
|
|
[COL_DEVICE] = { "DEVICE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("device ID for special, or ID of device containing file") },
|
2021-05-06 14:28:58 +09:00
|
|
|
[COL_FLAGS] = { "FLAGS", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("flags specified when opening the file") },
|
2021-05-03 15:33:56 +09:00
|
|
|
[COL_FD] = { "FD", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("file descriptor for the file") },
|
|
|
|
[COL_INODE] = { "INODE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("inode number") },
|
2021-05-06 14:28:58 +09:00
|
|
|
[COL_MNT_ID] = { "MNTID", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("mount id") },
|
2021-05-06 14:29:43 +09:00
|
|
|
[COL_MODE] = { "MODE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("access mode (rwx)") },
|
2021-05-03 15:47:02 +09:00
|
|
|
[COL_NAME] = { "NAME", 45, 0, SCOLS_JSON_STRING,
|
2021-05-03 15:33:56 +09:00
|
|
|
N_("name of the file") },
|
2021-05-03 16:22:29 +09:00
|
|
|
[COL_NLINK] = { "NLINK", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("link count") },
|
2021-05-03 15:47:02 +09:00
|
|
|
[COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
2021-05-03 15:33:56 +09:00
|
|
|
N_("PID of the process opening the file") },
|
2021-05-08 03:28:46 +09:00
|
|
|
[COL_PARTITION]={ "PARTITION",0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("block device name resolved by /proc/partition") },
|
2021-05-06 14:28:58 +09:00
|
|
|
[COL_POS] = { "POS", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("file position") },
|
2021-05-07 03:57:45 +09:00
|
|
|
[COL_PROTONAME]={ "PROTONAME",0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("protocol name") },
|
2021-05-03 15:33:56 +09:00
|
|
|
[COL_RDEV] = { "RDEV", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("device ID (if special file)") },
|
2021-05-03 15:59:26 +09:00
|
|
|
[COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("file size"), },
|
2021-05-05 01:47:49 +09:00
|
|
|
[COL_TID] = { "TID", 5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("thread ID of the process opening the file") },
|
2021-05-03 15:33:56 +09:00
|
|
|
[COL_TYPE] = { "TYPE", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("file type") },
|
|
|
|
[COL_UID] = { "UID", 0, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
|
|
|
|
N_("user ID number") },
|
|
|
|
[COL_USER] = { "USER", 0, SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
|
|
|
|
N_("user of the process") },
|
2021-03-24 03:43:51 +09:00
|
|
|
};
|
|
|
|
|
2021-05-03 15:45:47 +09:00
|
|
|
static int default_columns[] = {
|
|
|
|
COL_COMMAND,
|
|
|
|
COL_PID,
|
|
|
|
COL_USER,
|
|
|
|
COL_ASSOC,
|
2021-05-06 14:29:43 +09:00
|
|
|
COL_MODE,
|
2021-05-03 15:45:47 +09:00
|
|
|
COL_TYPE,
|
|
|
|
COL_DEVICE,
|
2021-05-06 14:45:11 +09:00
|
|
|
COL_MNT_ID,
|
2021-05-03 15:45:47 +09:00
|
|
|
COL_INODE,
|
|
|
|
COL_NAME,
|
|
|
|
};
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
static int default_threads_columns[] = {
|
|
|
|
COL_COMMAND,
|
|
|
|
COL_PID,
|
|
|
|
COL_TID,
|
|
|
|
COL_USER,
|
|
|
|
COL_ASSOC,
|
2021-05-06 14:29:43 +09:00
|
|
|
COL_MODE,
|
2021-05-05 01:47:49 +09:00
|
|
|
COL_TYPE,
|
|
|
|
COL_DEVICE,
|
2021-05-06 14:45:11 +09:00
|
|
|
COL_MNT_ID,
|
2021-05-05 01:47:49 +09:00
|
|
|
COL_INODE,
|
|
|
|
COL_NAME,
|
|
|
|
};
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
static int columns[ARRAY_SIZE(infos) * 2] = {-1};
|
|
|
|
static size_t ncolumns;
|
|
|
|
|
|
|
|
struct lsfd_control {
|
|
|
|
struct libscols_table *tb; /* output */
|
|
|
|
|
|
|
|
unsigned int noheadings : 1,
|
|
|
|
raw : 1,
|
2021-05-05 01:47:49 +09:00
|
|
|
json : 1,
|
|
|
|
threads : 1;
|
2021-03-24 03:43:51 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
static int column_name_to_id(const char *name, size_t namesz)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++) {
|
|
|
|
const char *cn = infos[i].name;
|
|
|
|
|
|
|
|
if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
warnx(_("unknown column: %s"), name);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_column_id(int num)
|
|
|
|
{
|
|
|
|
assert(num >= 0);
|
|
|
|
assert((size_t) num < ncolumns);
|
|
|
|
assert(columns[num] < (int) ARRAY_SIZE(infos));
|
|
|
|
|
|
|
|
return columns[num];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct colinfo *get_column_info(int num)
|
|
|
|
{
|
|
|
|
return &infos[ get_column_id(num) ];
|
|
|
|
}
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
static struct proc *make_proc(pid_t pid, struct proc * leader)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
|
|
|
struct proc *proc = xcalloc(1, sizeof(*proc));
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
proc->pid = pid;
|
|
|
|
proc->leader = leader? leader: proc;
|
2021-03-24 03:43:51 +09:00
|
|
|
proc->command = NULL;
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_file(struct file *file)
|
|
|
|
{
|
|
|
|
const struct file_class *class = file->class;
|
|
|
|
|
|
|
|
while (class) {
|
|
|
|
if (class->free_content)
|
|
|
|
class->free_content(file);
|
|
|
|
class = class->super;
|
|
|
|
}
|
|
|
|
free(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_proc(struct proc *proc)
|
|
|
|
{
|
|
|
|
list_free(&proc->files, struct file, files, free_file);
|
|
|
|
|
|
|
|
free(proc->command);
|
|
|
|
free(proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void enqueue_proc(struct list_head *procs, struct proc * proc)
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&proc->procs);
|
|
|
|
list_add_tail(&proc->procs, procs);
|
|
|
|
}
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
static void collect_tasks(struct proc *leader,
|
|
|
|
DIR *task_dirp, struct list_head *procs)
|
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
long num;
|
|
|
|
|
|
|
|
while ((dp = xreaddir(task_dirp))) {
|
|
|
|
struct proc *proc;
|
|
|
|
|
|
|
|
/* care only for numerical entries.
|
|
|
|
* For a non-numerical entry, strtol returns 0.
|
|
|
|
* We can skip it because there is no task having 0 as pid. */
|
|
|
|
if (!(num = strtol(dp->d_name, (char **) NULL, 10)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (leader->pid == (pid_t) num) {
|
|
|
|
/* The leader is already queued. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc = make_proc((pid_t)num, leader);
|
|
|
|
enqueue_proc(procs, proc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void collect_procs(DIR *dirp, struct list_head *procs,
|
|
|
|
struct lsfd_control *ctl)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
long num;
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp))) {
|
|
|
|
struct proc *proc;
|
|
|
|
|
|
|
|
/* care only for numerical entries.
|
|
|
|
* For a non-numerical entry, strtol returns 0.
|
|
|
|
* We can skip it because there is no task having 0 as pid. */
|
|
|
|
if (!(num = strtol(dp->d_name, (char **) NULL, 10)))
|
|
|
|
continue;
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
proc = make_proc((pid_t)num, NULL);
|
2021-03-24 03:43:51 +09:00
|
|
|
enqueue_proc(procs, proc);
|
2021-05-05 01:47:49 +09:00
|
|
|
|
|
|
|
if (ctl->threads) {
|
|
|
|
DIR *task_dirp = opendirf("/proc/%s/task", dp->d_name);
|
|
|
|
if (task_dirp) {
|
|
|
|
collect_tasks(proc, task_dirp, procs);
|
|
|
|
closedir(task_dirp);
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 03:43:51 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void run_collectors(struct list_head *procs)
|
|
|
|
{
|
|
|
|
pthread_t collectors[NUM_COLLECTORS];
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_COLLECTORS; i++) {
|
|
|
|
errno = pthread_create(collectors + i, NULL, fill_procs, procs);
|
|
|
|
if (errno)
|
|
|
|
err(EXIT_FAILURE, _("failed to create a thread"));
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = pthread_mutex_lock(&procs_ready_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to lock a mutex"));
|
|
|
|
|
|
|
|
current_proc = procs->next;
|
|
|
|
|
|
|
|
errno = pthread_mutex_unlock(&procs_ready_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to unlock a mutex"));
|
|
|
|
|
|
|
|
errno = pthread_cond_broadcast(&procs_ready);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to broadcast a condvar"));
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_COLLECTORS; i++)
|
|
|
|
pthread_join(collectors[i], NULL);
|
|
|
|
}
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
static void collect(struct list_head *procs, struct lsfd_control *ctl)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
|
|
|
|
dirp = opendir("/proc");
|
|
|
|
if (!dirp)
|
|
|
|
err(EXIT_FAILURE, _("failed to open /proc"));
|
2021-05-05 01:47:49 +09:00
|
|
|
collect_procs(dirp, procs, ctl);
|
2021-03-24 03:43:51 +09:00
|
|
|
closedir(dirp);
|
|
|
|
|
|
|
|
run_collectors(procs);
|
|
|
|
}
|
|
|
|
|
2021-05-07 03:57:45 +09:00
|
|
|
static struct file *collect_file(struct proc *proc,
|
2021-05-07 03:57:03 +09:00
|
|
|
struct stat *sb, char *name, int assoc)
|
2021-04-17 06:50:00 +09:00
|
|
|
{
|
|
|
|
switch (sb->st_mode & S_IFMT) {
|
|
|
|
case S_IFCHR:
|
2021-05-06 14:54:27 +09:00
|
|
|
return make_cdev(NULL, sb, name, assoc);
|
2021-04-17 06:50:00 +09:00
|
|
|
case S_IFBLK:
|
2021-05-06 14:54:27 +09:00
|
|
|
return make_bdev(NULL, sb, name, assoc);
|
2021-05-06 15:10:49 +09:00
|
|
|
case S_IFSOCK:
|
2021-05-07 03:57:45 +09:00
|
|
|
return make_sock(NULL, sb, name, assoc, proc);
|
2021-05-06 15:10:49 +09:00
|
|
|
case S_IFLNK:
|
|
|
|
case S_IFREG:
|
|
|
|
case S_IFIFO:
|
|
|
|
case S_IFDIR:
|
|
|
|
return make_file(NULL, sb, name, assoc);
|
|
|
|
default:
|
|
|
|
return make_unkn(NULL, sb, name, assoc);
|
2021-04-17 06:50:00 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:25:56 +09:00
|
|
|
static void read_fdinfo(struct file *file, FILE *fdinfo)
|
|
|
|
{
|
|
|
|
const struct file_class *class;
|
|
|
|
char buf[1024];
|
|
|
|
char *val;
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fdinfo)) {
|
|
|
|
val = strchr(buf, ':');
|
|
|
|
if (!val)
|
|
|
|
continue;
|
|
|
|
*val++ = '\0';
|
|
|
|
while (*val == '\t' || *val == ' ')
|
|
|
|
val++;
|
|
|
|
|
|
|
|
class = file->class;
|
|
|
|
while (class) {
|
|
|
|
if (class->handle_fdinfo
|
|
|
|
&& class->handle_fdinfo(file, buf, val))
|
|
|
|
break;
|
|
|
|
class = class->super;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
static struct file *collect_fd_file(struct proc *proc, int dd, struct dirent *dp, void *data)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
|
|
|
long num;
|
|
|
|
char *endptr = NULL;
|
2021-05-06 14:29:43 +09:00
|
|
|
struct stat sb, lsb;
|
2021-03-24 03:43:51 +09:00
|
|
|
ssize_t len;
|
|
|
|
char sym[PATH_MAX];
|
2021-05-06 14:25:56 +09:00
|
|
|
struct file *f;
|
|
|
|
int *fdinfo_dd = data;
|
|
|
|
FILE *fdinfo_fp;
|
2021-03-24 03:43:51 +09:00
|
|
|
|
|
|
|
/* care only for numerical descriptors */
|
|
|
|
num = strtol(dp->d_name, &endptr, 10);
|
|
|
|
if (num == 0 && endptr == dp->d_name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fstatat(dd, dp->d_name, &sb, 0) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(sym, 0, sizeof(sym));
|
|
|
|
if ((len = readlinkat(dd, dp->d_name, sym, sizeof(sym) - 1)) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
f = collect_file(proc, &sb, sym, (int)num);
|
2021-05-06 14:25:56 +09:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-06 14:29:43 +09:00
|
|
|
if (fstatat(dd, dp->d_name, &lsb, AT_SYMLINK_NOFOLLOW) == 0)
|
|
|
|
f->mode = lsb.st_mode;
|
|
|
|
|
2021-05-06 14:25:56 +09:00
|
|
|
if (*fdinfo_dd < 0)
|
|
|
|
return f;
|
|
|
|
|
|
|
|
fdinfo_fp = fopen_at(*fdinfo_dd, dp->d_name, O_RDONLY, "r");
|
|
|
|
if (fdinfo_fp) {
|
|
|
|
read_fdinfo(f, fdinfo_fp);
|
|
|
|
fclose(fdinfo_fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
2021-03-24 03:43:51 +09:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:19:11 +09:00
|
|
|
static struct map *find_map(struct list_head *maps, unsigned long start_addr)
|
|
|
|
{
|
|
|
|
struct list_head *m;
|
|
|
|
|
|
|
|
list_for_each(m, maps) {
|
|
|
|
struct map *map = list_entry(m, struct map, maps);
|
|
|
|
if (map->mem_addr_start == start_addr)
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
static struct file *collect_mem_file(struct proc *proc, int dd, struct dirent *dp,
|
2021-05-06 22:19:11 +09:00
|
|
|
void *data)
|
2021-04-17 07:33:07 +09:00
|
|
|
{
|
2021-05-06 22:19:11 +09:00
|
|
|
struct list_head *maps = data;
|
2021-04-17 07:33:07 +09:00
|
|
|
struct stat sb;
|
|
|
|
ssize_t len;
|
|
|
|
char sym[PATH_MAX];
|
2021-05-06 22:19:11 +09:00
|
|
|
struct file *f;
|
|
|
|
unsigned long start, end;
|
|
|
|
struct map *map;
|
|
|
|
enum association assoc;
|
2021-04-17 07:33:07 +09:00
|
|
|
|
|
|
|
if (fstatat(dd, dp->d_name, &sb, 0) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(sym, 0, sizeof(sym));
|
|
|
|
if ((len = readlinkat(dd, dp->d_name, sym, sizeof(sym) - 1)) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-06 22:19:11 +09:00
|
|
|
|
|
|
|
map = NULL;
|
|
|
|
if (sscanf(dp->d_name, "%lx-%lx", &start, &end) == 2)
|
|
|
|
map = find_map(maps, start);
|
|
|
|
|
|
|
|
assoc = (map && map->shared)? ASSOC_SHM: ASSOC_MEM;
|
2021-05-07 03:57:03 +09:00
|
|
|
f = collect_file(proc, &sb, sym, -assoc);
|
2021-05-06 22:19:11 +09:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
2021-05-06 22:26:48 +09:00
|
|
|
|
|
|
|
if (map) {
|
|
|
|
f->mode = (map->read? S_IRUSR: 0) | (map->write? S_IWUSR: 0) | (map->exec? S_IXUSR: 0);
|
|
|
|
f->pos = map->file_offset;
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:19:11 +09:00
|
|
|
return f;
|
2021-04-17 07:33:07 +09:00
|
|
|
}
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
static void enqueue_file(struct proc *proc, struct file * file)
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&file->files);
|
|
|
|
list_add_tail(&file->files, &proc->files);
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:33:07 +09:00
|
|
|
|
|
|
|
static void collect_fd_files_generic(struct proc *proc, const char *proc_template,
|
2021-05-07 03:57:03 +09:00
|
|
|
struct file *(*collector)(struct proc *,int, struct dirent *, void *),
|
2021-05-06 14:25:56 +09:00
|
|
|
void *data)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
int dd;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
2021-04-17 07:33:07 +09:00
|
|
|
dirp = opendirf(proc_template, proc->pid);
|
2021-03-24 03:43:51 +09:00
|
|
|
if (!dirp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((dd = dirfd(dirp)) < 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((dp = xreaddir(dirp))) {
|
|
|
|
struct file *file;
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
if ((file = (* collector)(proc, dd, dp, data)) == NULL)
|
2021-04-17 06:50:00 +09:00
|
|
|
continue;
|
|
|
|
|
|
|
|
enqueue_file(proc, file);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:33:07 +09:00
|
|
|
static void collect_fd_files(struct proc *proc)
|
|
|
|
{
|
2021-05-06 14:25:56 +09:00
|
|
|
DIR *dirp;
|
|
|
|
int dd = -1;
|
|
|
|
|
|
|
|
dirp = opendirf("/proc/%d/fdinfo/", proc->pid);
|
|
|
|
if (dirp)
|
|
|
|
dd = dirfd(dirp);
|
|
|
|
|
|
|
|
collect_fd_files_generic(proc, "/proc/%d/fd/", collect_fd_file, &dd);
|
|
|
|
|
|
|
|
if (dirp)
|
|
|
|
closedir(dirp);
|
2021-04-17 07:33:07 +09:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:15:24 +09:00
|
|
|
static struct map* make_map(unsigned long mem_addr_start, unsigned long long file_offset,
|
|
|
|
int r, int w, int x, int s)
|
|
|
|
{
|
|
|
|
struct map *map = xcalloc(1, sizeof(*map));
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&map->maps);
|
|
|
|
|
|
|
|
map->mem_addr_start = mem_addr_start;
|
|
|
|
map->file_offset = file_offset;
|
|
|
|
|
|
|
|
map->read = r;
|
|
|
|
map->write = w;
|
|
|
|
map->exec = x;
|
|
|
|
map->shared = s;
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_map(struct map *map)
|
|
|
|
{
|
|
|
|
free(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_maps(struct list_head *maps_list, FILE *maps_fp)
|
|
|
|
{
|
|
|
|
/* Taken from proc(5):
|
|
|
|
* address perms offset dev inode pathname
|
|
|
|
* 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
|
|
|
|
* ... */
|
|
|
|
|
|
|
|
char line[PATH_MAX + 128];
|
|
|
|
unsigned long start, end;
|
|
|
|
char r, w, x, s;
|
|
|
|
unsigned long long file_offset;
|
|
|
|
unsigned long major;
|
|
|
|
unsigned long minor;
|
|
|
|
long unsigned inode;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), maps_fp)) {
|
|
|
|
struct map *map;
|
|
|
|
|
|
|
|
if (sscanf(line, "%lx-%lx %c%c%c%c %llx %lx:%lx %lu %*[^\n]",
|
|
|
|
&start, &end, &r, &w, &x, &s, &file_offset,
|
|
|
|
&major, &minor, &inode) != 10)
|
|
|
|
continue;
|
|
|
|
map = make_map(start, file_offset, r == 'r', w == 'w', x == 'x', s == 's');
|
|
|
|
list_add_tail(&map->maps, maps_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE* open_maps(pid_t pid)
|
|
|
|
{
|
|
|
|
return fopenf("r", "/proc/%d/maps", pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_maps(struct list_head *maps)
|
|
|
|
{
|
|
|
|
list_free(maps, struct map, maps, free_map);
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:33:07 +09:00
|
|
|
static void collect_mem_files(struct proc *proc)
|
|
|
|
{
|
2021-05-06 22:15:24 +09:00
|
|
|
|
|
|
|
struct list_head maps;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&maps);
|
|
|
|
fp = open_maps(proc->pid);
|
|
|
|
if (fp) {
|
|
|
|
read_maps(&maps, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
collect_fd_files_generic(proc, "/proc/%d/map_files/", collect_mem_file, &maps);
|
|
|
|
|
|
|
|
free_maps(&maps);
|
2021-04-17 07:33:07 +09:00
|
|
|
}
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
static struct file *collect_outofbox_file(struct proc *proc,
|
|
|
|
int dd, const char *name, int association)
|
2021-04-17 06:50:00 +09:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
ssize_t len;
|
|
|
|
char sym[PATH_MAX];
|
|
|
|
|
|
|
|
if (fstatat(dd, name, &sb, 0) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(sym, 0, sizeof(sym));
|
|
|
|
if ((len = readlinkat(dd, name, sym, sizeof(sym) - 1)) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
return collect_file(proc, &sb, sym, association);
|
2021-04-17 06:50:00 +09:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:36:09 +09:00
|
|
|
static void collect_proc_uid(struct proc *proc, int dd)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (fstat(dd, &sb) == 0)
|
|
|
|
proc->uid = sb.st_uid;
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:15:50 +09:00
|
|
|
static void collect_outofbox_files(struct proc *proc,
|
|
|
|
const char *proc_template,
|
|
|
|
enum association assocs[],
|
|
|
|
const char* assoc_names[],
|
|
|
|
unsigned int count)
|
2021-04-17 06:50:00 +09:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
int dd;
|
|
|
|
|
2021-04-17 07:15:50 +09:00
|
|
|
dirp = opendirf(proc_template, proc->pid);
|
2021-04-17 06:50:00 +09:00
|
|
|
if (!dirp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((dd = dirfd(dirp)) < 0 )
|
|
|
|
return;
|
|
|
|
|
2021-04-17 07:15:50 +09:00
|
|
|
for (unsigned int i = 0; i < count; i++) {
|
2021-04-17 06:50:00 +09:00
|
|
|
struct file *file;
|
|
|
|
|
2021-05-05 03:36:09 +09:00
|
|
|
if (assocs[i] == ASSOC_EXE)
|
|
|
|
collect_proc_uid(proc, dd);
|
|
|
|
|
2021-05-07 03:57:03 +09:00
|
|
|
if ((file = collect_outofbox_file(proc, dd,
|
2021-04-17 06:50:00 +09:00
|
|
|
assoc_names[assocs[i]],
|
|
|
|
assocs[i] * -1)) == NULL)
|
2021-03-24 03:43:51 +09:00
|
|
|
continue;
|
|
|
|
|
|
|
|
enqueue_file(proc, file);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
|
2021-05-05 03:37:57 +09:00
|
|
|
static void collect_execve_file(struct proc *proc)
|
2021-03-24 03:43:51 +09:00
|
|
|
{
|
2021-04-17 07:38:48 +09:00
|
|
|
const char *execve_template = "/proc/%d";
|
2021-05-05 03:37:57 +09:00
|
|
|
enum association execve_assocs[] = { ASSOC_EXE };
|
|
|
|
const char* execve_assoc_names[] = {
|
|
|
|
[ASSOC_EXE] = "exe",
|
|
|
|
};
|
|
|
|
collect_outofbox_files(proc, execve_template,
|
|
|
|
execve_assocs, execve_assoc_names,
|
|
|
|
ARRAY_SIZE(execve_assocs));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void collect_execve_and_fs_files(struct proc *proc)
|
|
|
|
{
|
|
|
|
const char *execve_template = "/proc/%d";
|
|
|
|
enum association execve_assocs[] = { ASSOC_EXE, ASSOC_CWD, ASSOC_ROOT };
|
2021-04-17 07:38:48 +09:00
|
|
|
const char* execve_assoc_names[] = {
|
2021-04-17 07:15:50 +09:00
|
|
|
[ASSOC_EXE] = "exe",
|
2021-05-05 03:37:57 +09:00
|
|
|
[ASSOC_CWD] = "cwd",
|
2021-04-17 07:15:50 +09:00
|
|
|
[ASSOC_ROOT] = "root",
|
|
|
|
};
|
2021-04-17 07:38:48 +09:00
|
|
|
collect_outofbox_files(proc, execve_template,
|
|
|
|
execve_assocs, execve_assoc_names,
|
|
|
|
ARRAY_SIZE(execve_assocs));
|
|
|
|
}
|
2021-04-17 07:15:50 +09:00
|
|
|
|
2021-04-17 07:38:48 +09:00
|
|
|
|
|
|
|
static void collect_namespace_files(struct proc *proc)
|
|
|
|
{
|
2021-04-17 07:15:50 +09:00
|
|
|
const char *namespace_template = "/proc/%d/ns";
|
|
|
|
enum association namespace_assocs[] = {
|
|
|
|
ASSOC_NS_CGROUP,
|
|
|
|
ASSOC_NS_IPC,
|
|
|
|
ASSOC_NS_MNT,
|
|
|
|
ASSOC_NS_NET,
|
|
|
|
ASSOC_NS_PID,
|
|
|
|
ASSOC_NS_PID4C,
|
|
|
|
ASSOC_NS_TIME,
|
|
|
|
ASSOC_NS_TIME4C,
|
|
|
|
ASSOC_NS_USER,
|
|
|
|
ASSOC_NS_UTS,
|
|
|
|
};
|
|
|
|
const char* namespace_assoc_names[] = {
|
|
|
|
[ASSOC_NS_CGROUP] = "cgroup",
|
|
|
|
[ASSOC_NS_IPC] = "ipc",
|
|
|
|
[ASSOC_NS_MNT] = "mnt",
|
|
|
|
[ASSOC_NS_NET] = "net",
|
|
|
|
[ASSOC_NS_PID] = "pid",
|
|
|
|
[ASSOC_NS_PID4C] = "pid_for_children",
|
|
|
|
[ASSOC_NS_TIME] = "time",
|
|
|
|
[ASSOC_NS_TIME4C] = "time_for_children",
|
|
|
|
[ASSOC_NS_USER] = "user",
|
|
|
|
[ASSOC_NS_UTS] = "uts",
|
|
|
|
};
|
|
|
|
collect_outofbox_files(proc, namespace_template,
|
|
|
|
namespace_assocs, namespace_assoc_names,
|
|
|
|
ARRAY_SIZE(namespace_assocs));
|
2021-04-17 07:38:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_proc(struct proc *proc)
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&proc->files);
|
2021-04-17 07:15:50 +09:00
|
|
|
|
2021-04-17 07:38:48 +09:00
|
|
|
proc->command = proc_get_command_name(proc->pid);
|
|
|
|
if (!proc->command)
|
2021-05-05 01:47:49 +09:00
|
|
|
proc->command = xstrdup(_("(unknown)"));
|
2021-04-17 07:38:48 +09:00
|
|
|
|
2021-05-05 03:37:57 +09:00
|
|
|
|
|
|
|
if (proc->pid == proc->leader->pid
|
|
|
|
|| kcmp(proc->leader->pid, proc->pid,
|
|
|
|
KCMP_FS, 0, 0) != 0)
|
|
|
|
collect_execve_and_fs_files(proc);
|
|
|
|
else
|
|
|
|
collect_execve_file(proc);
|
|
|
|
|
2021-04-17 07:38:48 +09:00
|
|
|
collect_namespace_files(proc);
|
2021-05-05 03:37:57 +09:00
|
|
|
|
|
|
|
/* If kcmp is not available,
|
|
|
|
* there is no way to no whether threads share resources.
|
|
|
|
* In such cases, we must pay the costs: call collect_mem_files()
|
|
|
|
* and collect_fd_files().
|
|
|
|
*/
|
|
|
|
if (proc->pid == proc->leader->pid
|
|
|
|
|| kcmp(proc->leader->pid, proc->pid,
|
|
|
|
KCMP_VM, 0, 0) != 0)
|
|
|
|
collect_mem_files(proc);
|
|
|
|
|
|
|
|
if (proc->pid == proc->leader->pid
|
|
|
|
|| kcmp(proc->leader->pid, proc->pid,
|
|
|
|
KCMP_FILES, 0, 0) != 0)
|
|
|
|
collect_fd_files(proc);
|
2021-03-24 03:43:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *fill_procs(void *arg)
|
|
|
|
{
|
|
|
|
struct list_head *procs = arg;
|
|
|
|
struct list_head *target_proc;
|
|
|
|
|
|
|
|
errno = pthread_mutex_lock(&procs_ready_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to lock a mutex"));
|
|
|
|
|
|
|
|
if (current_proc == NULL) {
|
|
|
|
errno = pthread_cond_wait(&procs_ready, &procs_ready_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to wait a condvar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = pthread_mutex_unlock(&procs_ready_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to unlock a mutex"));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
errno = pthread_mutex_lock(&procs_consumer_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to lock a mutex"));
|
|
|
|
|
|
|
|
target_proc = current_proc;
|
|
|
|
|
|
|
|
if (current_proc != procs)
|
|
|
|
current_proc = current_proc->next;
|
|
|
|
|
|
|
|
errno = pthread_mutex_unlock(&procs_consumer_lock);
|
|
|
|
if (errno != 0)
|
|
|
|
err(EXIT_FAILURE, _("failed to lock a mutex"));
|
|
|
|
|
|
|
|
if (target_proc == procs) {
|
|
|
|
/* All pids are processed. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fill_proc(list_entry(target_proc, struct proc, procs));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_column(struct proc *proc,
|
|
|
|
struct file *file,
|
|
|
|
struct libscols_line *ln,
|
|
|
|
int column_id,
|
|
|
|
size_t column_index)
|
|
|
|
{
|
|
|
|
const struct file_class *class = file->class;
|
|
|
|
|
|
|
|
while (class) {
|
|
|
|
if (class->fill_column
|
|
|
|
&& class->fill_column(proc, file, ln,
|
|
|
|
column_id, column_index))
|
|
|
|
break;
|
|
|
|
class = class->super;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert1(struct proc *proc,
|
|
|
|
struct file *file,
|
|
|
|
struct libscols_line *ln)
|
|
|
|
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ncolumns; i++)
|
|
|
|
fill_column(proc, file, ln, get_column_id(i), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert(struct list_head *procs, struct lsfd_control *ctl)
|
|
|
|
{
|
|
|
|
struct list_head *p;
|
|
|
|
|
|
|
|
list_for_each (p, procs) {
|
|
|
|
struct proc *proc = list_entry(p, struct proc, procs);
|
|
|
|
struct list_head *f;
|
|
|
|
|
|
|
|
list_for_each (f, &proc->files) {
|
|
|
|
struct file *file = list_entry(f, struct file, files);
|
|
|
|
struct libscols_line *ln = scols_table_new_line(ctl->tb, NULL);
|
|
|
|
if (!ln)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate output line"));
|
|
|
|
convert1(proc, file, ln);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete(struct list_head *procs, struct lsfd_control *ctl)
|
|
|
|
{
|
|
|
|
list_free(procs, struct proc, procs, free_proc);
|
|
|
|
|
|
|
|
scols_unref_table(ctl->tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit(struct lsfd_control *ctl)
|
|
|
|
{
|
|
|
|
scols_print_table(ctl->tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
|
|
|
{
|
|
|
|
FILE *out = stdout;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
|
|
|
|
|
|
|
|
fputs(USAGE_OPTIONS, out);
|
2021-05-05 01:47:49 +09:00
|
|
|
fputs(_(" -l, --threads list in threads level\n"), out);
|
2021-03-24 03:43:51 +09:00
|
|
|
fputs(_(" -J, --json use JSON output format\n"), out);
|
|
|
|
fputs(_(" -n, --noheadings don't print headings\n"), out);
|
|
|
|
fputs(_(" -o, --output <list> output columns\n"), out);
|
|
|
|
fputs(_(" -r, --raw use raw output format\n"), out);
|
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
printf(USAGE_HELP_OPTIONS(23));
|
|
|
|
|
|
|
|
fprintf(out, USAGE_COLUMNS);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(infos); i++)
|
|
|
|
fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
|
|
|
|
|
|
|
|
printf(USAGE_MAN_TAIL("lsfd(1)"));
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2021-05-06 23:51:00 +09:00
|
|
|
static void initialize_class(const struct file_class *class)
|
|
|
|
{
|
|
|
|
if (class->initialize_class)
|
|
|
|
class->initialize_class();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initialize_classes(void)
|
|
|
|
{
|
|
|
|
initialize_class(&file_class);
|
|
|
|
initialize_class(&cdev_class);
|
|
|
|
initialize_class(&bdev_class);
|
|
|
|
initialize_class(&sock_class);
|
|
|
|
initialize_class(&unkn_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finalize_class(const struct file_class *class)
|
|
|
|
{
|
|
|
|
if (class->finalize_class)
|
|
|
|
class->finalize_class();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finalize_classes(void)
|
|
|
|
{
|
|
|
|
finalize_class(&file_class);
|
|
|
|
finalize_class(&cdev_class);
|
|
|
|
finalize_class(&bdev_class);
|
|
|
|
finalize_class(&sock_class);
|
|
|
|
finalize_class(&unkn_class);
|
|
|
|
}
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *outarg = NULL;
|
|
|
|
|
|
|
|
struct list_head procs;
|
|
|
|
|
|
|
|
struct lsfd_control ctl = {};
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "noheadings", no_argument, NULL, 'n' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "json", no_argument, NULL, 'J' },
|
|
|
|
{ "raw", no_argument, NULL, 'r' },
|
2021-05-05 01:47:49 +09:00
|
|
|
{ "threads", no_argument, NULL, 'l' },
|
2021-03-24 03:43:51 +09:00
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
close_stdout_atexit();
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
while ((c = getopt_long(argc, argv, "no:JrVhl", longopts, NULL)) != -1) {
|
2021-03-24 03:43:51 +09:00
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
ctl.noheadings = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outarg = optarg;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
ctl.json = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ctl.raw = 1;
|
|
|
|
break;
|
2021-05-05 01:47:49 +09:00
|
|
|
case 'l':
|
|
|
|
ctl.threads = 1;
|
|
|
|
break;
|
2021-03-24 03:43:51 +09:00
|
|
|
case 'V':
|
|
|
|
print_version(EXIT_SUCCESS);
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
default:
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 01:47:49 +09:00
|
|
|
#define INITIALIZE_COLUMNS(COLUMN_SPEC) \
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(COLUMN_SPEC); i++) \
|
|
|
|
columns[ncolumns++] = COLUMN_SPEC[i]
|
|
|
|
if (!ncolumns) {
|
|
|
|
if (ctl.threads)
|
|
|
|
INITIALIZE_COLUMNS(default_threads_columns);
|
|
|
|
else
|
|
|
|
INITIALIZE_COLUMNS(default_columns);
|
|
|
|
}
|
2021-03-24 03:43:51 +09:00
|
|
|
|
|
|
|
if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
|
|
|
|
&ncolumns, column_name_to_id) < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
scols_init_debug(0);
|
|
|
|
ctl.tb = scols_new_table();
|
|
|
|
|
|
|
|
if (!ctl.tb)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate output table"));
|
|
|
|
|
|
|
|
scols_table_enable_noheadings(ctl.tb, ctl.noheadings);
|
|
|
|
scols_table_enable_raw(ctl.tb, ctl.raw);
|
|
|
|
scols_table_enable_json(ctl.tb, ctl.json);
|
|
|
|
if (ctl.json)
|
|
|
|
scols_table_set_name(ctl.tb, "lsfd");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ncolumns; i++) {
|
|
|
|
const struct colinfo *col = get_column_info(i);
|
|
|
|
struct libscols_column *cl;
|
|
|
|
|
|
|
|
cl = scols_table_new_column(ctl.tb, col->name, col->whint, col->flags);
|
|
|
|
if (!cl)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate output column"));
|
|
|
|
|
2021-05-03 15:33:56 +09:00
|
|
|
if (ctl.json)
|
|
|
|
scols_column_set_json_type(cl, col->json_type);
|
2021-03-24 03:43:51 +09:00
|
|
|
}
|
|
|
|
|
2021-05-06 23:51:00 +09:00
|
|
|
initialize_classes();
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
INIT_LIST_HEAD(&procs);
|
2021-05-05 01:47:49 +09:00
|
|
|
collect(&procs, &ctl);
|
2021-03-24 03:43:51 +09:00
|
|
|
|
|
|
|
convert(&procs, &ctl);
|
|
|
|
emit(&ctl);
|
|
|
|
delete(&procs, &ctl);
|
|
|
|
|
2021-05-06 23:51:00 +09:00
|
|
|
finalize_classes();
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-08 01:50:20 +09:00
|
|
|
struct name_manager {
|
|
|
|
struct idcache *cache;
|
|
|
|
pthread_rwlock_t rwlock;
|
|
|
|
unsigned long next_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct name_manager *new_name_manager(void)
|
|
|
|
{
|
|
|
|
struct name_manager *nm = xcalloc(1, sizeof(struct name_manager));
|
|
|
|
nm->cache = new_idcache();
|
|
|
|
if (!nm->cache)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate an idcache"));
|
|
|
|
|
|
|
|
pthread_rwlock_init(&nm->rwlock, NULL);
|
|
|
|
nm->next_id = 1; /* 0 is never issued as id. */
|
|
|
|
return nm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_name_manager(struct name_manager *nm)
|
|
|
|
{
|
|
|
|
free_idcache(nm->cache);
|
|
|
|
free(nm);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *get_name(struct name_manager *nm, unsigned long id)
|
|
|
|
{
|
|
|
|
struct identry *e;
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&nm->rwlock);
|
|
|
|
e = get_id(nm->cache, id);
|
|
|
|
pthread_rwlock_unlock(&nm->rwlock);
|
|
|
|
|
|
|
|
return e? e->name: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long add_name(struct name_manager *nm, const char *name)
|
|
|
|
{
|
|
|
|
struct identry *e = NULL, *tmp;
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&nm->rwlock);
|
|
|
|
for (tmp = nm->cache->ent; tmp; tmp = tmp->next) {
|
|
|
|
if (strcmp(tmp->name, name) == 0) {
|
|
|
|
e = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_rwlock_unlock(&nm->rwlock);
|
|
|
|
if (e)
|
|
|
|
return e->id;
|
|
|
|
|
|
|
|
pthread_rwlock_wrlock(&nm->rwlock);
|
|
|
|
for (tmp = nm->cache->ent; tmp; tmp = tmp->next) {
|
|
|
|
if (strcmp(tmp->name, name) == 0) {
|
|
|
|
e = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
e = xmalloc(sizeof(struct identry));
|
|
|
|
e->name = xstrdup(name);
|
|
|
|
e->id = nm->next_id++;
|
|
|
|
e->next = nm->cache->ent;
|
|
|
|
nm->cache->ent = e;
|
|
|
|
}
|
|
|
|
pthread_rwlock_unlock(&nm->rwlock);
|
|
|
|
|
|
|
|
return e->id;
|
|
|
|
}
|
|
|
|
|
2021-03-24 03:43:51 +09:00
|
|
|
DIR *opendirf(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
memset(path, 0, sizeof(path));
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vsprintf(path, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return opendir(path);
|
|
|
|
}
|
2021-05-10 02:29:31 +09:00
|
|
|
|
|
|
|
FILE *fopenf(const char *mode, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
memset(path, 0, sizeof(path));
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vsprintf(path, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return fopen(path, mode);
|
|
|
|
}
|