diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2015-07-17 14:55:15 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2015-07-17 14:55:15 +0200 |
commit | c5ff2059f619ac91c14d55412314d1c67de6a593 (patch) | |
tree | 18c324c5d2bbbe9a913ae693ff75b109d050a1e5 | |
parent | 5d6d5f85d8ed49698eb3d790308e05a50b784c54 (diff) |
flowtop, netsniff-ng: Move process name extraction to own function
flowtop and the netsniff-ng's netlink message dissector both need to get
the process name for a pid from /proc/<pid>/exe, thus move that
functionality to an own function.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | flowtop.c | 12 | ||||
-rw-r--r-- | flowtop/Makefile | 1 | ||||
-rw-r--r-- | proc.c | 15 | ||||
-rw-r--r-- | proc.h | 3 | ||||
-rw-r--r-- | proto_nlmsg.c | 13 |
5 files changed, 27 insertions, 17 deletions
@@ -38,6 +38,7 @@ #include "locking.h" #include "pkt_buff.h" #include "screen.h" +#include "proc.h" struct flow_entry { uint32_t flow_id, use, status; @@ -474,14 +475,9 @@ static int walk_process(unsigned int pid, struct flow_entry *n) continue; if (S_ISSOCK(statbuf.st_mode) && (ino_t) n->inode == statbuf.st_ino) { - memset(n->cmdline, 0, sizeof(n->cmdline)); - - snprintf(path, sizeof(path), "/proc/%u/exe", pid); - - ret = readlink(path, n->cmdline, - sizeof(n->cmdline) - 1); + ret = proc_get_cmdline(pid, n->cmdline, sizeof(n->cmdline)); if (ret < 0) - panic("readlink error: %s\n", strerror(errno)); + panic("Failed to get process cmdline: %s\n", strerror(errno)); n->procnum = pid; closedir(dir); @@ -501,7 +497,7 @@ static void walk_processes(struct flow_entry *n) /* n->inode must be set */ if (n->inode <= 0) { - memset(n->cmdline, 0, sizeof(n->cmdline)); + n->cmdline[0] = '\0'; return; } diff --git a/flowtop/Makefile b/flowtop/Makefile index 1bd5a0f..85cb0b6 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -15,6 +15,7 @@ flowtop-objs = xmalloc.o \ str.o \ sig.o \ sock.o \ + proc.o \ dev.o \ link.o \ hash.o \ @@ -64,3 +64,18 @@ int set_sched_status(int policy, int priority) return 0; } + +ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len) +{ + ssize_t ret; + char path[1024]; + + snprintf(path, sizeof(path), "/proc/%u/exe", pid); + ret = readlink(path, cmdline, len - 1); + if (ret < 0) + cmdline[0] = '\0'; + else + cmdline[ret] = '\0'; + + return ret; +} @@ -1,8 +1,11 @@ #ifndef PROC_H #define PROC_H +#include <stdlib.h> + extern void cpu_affinity(int cpu); extern int set_proc_prio(int prio); extern int set_sched_status(int policy, int priority); +extern ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len); #endif /* PROC_H */ diff --git a/proto_nlmsg.c b/proto_nlmsg.c index 22d50a8..1dde5e1 100644 --- a/proto_nlmsg.c +++ b/proto_nlmsg.c @@ -17,6 +17,7 @@ #include "dev.h" #include "pkt_buff.h" +#include "proc.h" #include "proto.h" #include "protos.h" #include "timer.h" @@ -226,7 +227,7 @@ static void rtnl_print_ifinfo(struct nlmsghdr *hdr) uint32_t attrs_len = IFLA_PAYLOAD(hdr); char flags[256]; char if_addr[64] = {}; - char *af_link = "Unknown"; + char *af_link = "unknown"; if (ifi->ifi_family == AF_UNSPEC) af_link = "unspec"; @@ -717,14 +718,8 @@ static void nlmsg_print(uint16_t family, struct nlmsghdr *hdr) * PID and the information will not be printed. */ if (hdr->nlmsg_pid != 0) { - char path[1024]; - int ret; - - snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid); - ret = readlink(path, procname, sizeof(procname) - 1); - if (ret < 0) - ret = 0; - procname[ret] = '\0'; + if (proc_get_cmdline(hdr->nlmsg_pid, procname, sizeof(procname)) < 0) + snprintf(procname, sizeof(procname), "unknown process"); } else snprintf(procname, sizeof(procname), "kernel"); |