From c5ff2059f619ac91c14d55412314d1c67de6a593 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 17 Jul 2015 14:55:15 +0200 Subject: 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//exe, thus move that functionality to an own function. Signed-off-by: Tobias Klauser --- flowtop.c | 12 ++++-------- flowtop/Makefile | 1 + proc.c | 15 +++++++++++++++ proc.h | 3 +++ proto_nlmsg.c | 13 ++++--------- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/flowtop.c b/flowtop.c index 7fd9055..a522daf 100644 --- a/flowtop.c +++ b/flowtop.c @@ -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 \ diff --git a/proc.c b/proc.c index 39d661e..672f00d 100644 --- a/proc.c +++ b/proc.c @@ -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; +} diff --git a/proc.h b/proc.h index 0a31f36..996ce06 100644 --- a/proc.h +++ b/proc.h @@ -1,8 +1,11 @@ #ifndef PROC_H #define PROC_H +#include + 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"); -- cgit v1.2.3-54-g00ecf