File: pidfd.c

package info (click to toggle)
util-linux 2.41.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,852 kB
  • sloc: ansic: 179,050; sh: 22,693; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (95 lines) | stat: -rw-r--r-- 2,274 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * lsfd-pidfd.c - handle pidfd (from anon_inode or pidfs)
 *
 * Copyright (C) 2024 Xi Ruoyao <xry111@xry111.site>
 *
 * Refactored and moved out from lsfd-unkn.c (originally authored by
 * Masatake YAMATO <yamato@redhat.com>).
 *
 * 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 <string.h>

#include "strutils.h"
#include "xalloc.h"

#include "lsfd.h"
#include "pidfd.h"

int pidfd_handle_fdinfo(struct pidfd_data *data, const char *key,
			const char *value)
{
	if (strcmp(key, "Pid") == 0) {
		uint64_t pid;
		int rc = ul_strtou64(value, &pid, 10);

		if (rc < 0)
			return 0; /* ignore -- parse failed */

		data->pid = (pid_t)pid;
		return 1;
	} else if (strcmp(key, "NSpid") == 0) {
		data->nspid = xstrdup(value);
		return 1;
	}

	return 0;
}

char *pidfd_get_name(struct pidfd_data *data)
{
	char *str = NULL;
	char *comm = NULL;
	struct proc *proc = get_proc(data->pid);

	if (proc)
		comm = proc->command;

	xasprintf(&str, "pid=%d comm=%s nspid=%s",
			  data->pid,
			  comm ? comm : "",
			  data->nspid ? data->nspid : "");
	return str;
}

bool pidfd_fill_column(struct pidfd_data *data, int column_id, char **str)
{
	switch(column_id) {
	case COL_PIDFD_COMM: {
		struct proc *pidfd_proc = get_proc(data->pid);
		char *pidfd_comm = NULL;

		if (pidfd_proc)
			pidfd_comm = pidfd_proc->command;
		if (pidfd_comm) {
			*str = xstrdup(pidfd_comm);
			return true;
		}
		break;
	}
	case COL_PIDFD_NSPID:
		if (data->nspid) {
			*str = xstrdup(data->nspid);
			return true;
		}
		break;
	case COL_PIDFD_PID:
		xasprintf(str, "%d", (int)data->pid);
		return true;
	}

	return false;
}