Be more efficient about allocating duplicate stuff and on the stack vs heap
This commit is contained in:
@@ -72,16 +72,20 @@ int str2int (char * ptr) {
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * getprogname (char * pid) {
|
// Not sure, but assuming there's no more PID's than go into 64 unsigned bits..
|
||||||
int filenamelen = 14 + strlen(pid) + 1;
|
#define MAX_PID_LENGTH 20
|
||||||
|
|
||||||
|
char * getprogname (pid_t pid) {
|
||||||
int bufsize = 80;
|
int bufsize = 80;
|
||||||
char buffer [bufsize];
|
char buffer [bufsize];
|
||||||
char * filename = (char *) malloc (filenamelen);
|
|
||||||
snprintf (filename, filenamelen, "/proc/%s/cmdline", pid);
|
int maxfilenamelen = 14 + MAX_PID_LENGTH + 1;
|
||||||
|
char filename[maxfilenamelen];
|
||||||
|
|
||||||
|
snprintf (filename, maxfilenamelen, "/proc/%d/cmdline", pid);
|
||||||
int fd = open(filename, O_RDONLY);
|
int fd = open(filename, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
fprintf (stderr, "Error opening %s: %s\n", filename, strerror(errno));
|
fprintf (stderr, "Error opening %s: %s\n", filename, strerror(errno));
|
||||||
free (filename);
|
|
||||||
exit(3);
|
exit(3);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -90,68 +94,51 @@ char * getprogname (char * pid) {
|
|||||||
std::cout << "Error closing file: " << strerror(errno) << std::endl;
|
std::cout << "Error closing file: " << strerror(errno) << std::endl;
|
||||||
exit(34);
|
exit(34);
|
||||||
}
|
}
|
||||||
free (filename);
|
|
||||||
if (length < bufsize - 1)
|
if (length < bufsize - 1)
|
||||||
buffer[length]='\0';
|
buffer[length]='\0';
|
||||||
|
|
||||||
char * retval = buffer;
|
return strdup(buffer);
|
||||||
|
|
||||||
/* this removed directory names, but that malfunctions
|
|
||||||
* when the program name is like "sshd: arnouten@pts/8"
|
|
||||||
if ((retval = strrchr(buffer, '/')))
|
|
||||||
retval++;
|
|
||||||
else
|
|
||||||
retval = buffer;
|
|
||||||
*/
|
|
||||||
// truncating is now done where it should be, in cui.cpp
|
|
||||||
|
|
||||||
return strdup(retval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setnode (unsigned long inode, prg_node * newnode)
|
void setnode (unsigned long inode, pid_t pid)
|
||||||
{
|
{
|
||||||
if (inodeproc[inode] != NULL)
|
prg_node * current_value = inodeproc[inode];
|
||||||
free (inodeproc[inode]);
|
|
||||||
|
if (current_value == NULL || current_value->pid != pid) {
|
||||||
|
prg_node * newnode = (prg_node *) malloc (sizeof (struct prg_node));
|
||||||
|
newnode->inode = inode;
|
||||||
|
newnode->pid = pid;
|
||||||
|
newnode->name = getprogname(pid);
|
||||||
|
|
||||||
inodeproc[inode] = newnode;
|
inodeproc[inode] = newnode;
|
||||||
|
free(current_value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_info_by_linkname (char * pid, char * linkname) {
|
void get_info_by_linkname (char * pid, char * linkname) {
|
||||||
if (strncmp(linkname, "socket:[", 8) == 0) {
|
if (strncmp(linkname, "socket:[", 8) == 0) {
|
||||||
char * ptr = linkname + 8;
|
setnode(str2ulong(linkname + 8), str2int(pid));
|
||||||
unsigned long inode = str2ulong(ptr);
|
|
||||||
|
|
||||||
char * progname = getprogname (pid);
|
|
||||||
|
|
||||||
//std::cout << "Found socket with inode " << inode << " and pid " << pid << " and progname " << progname << "\n";
|
|
||||||
prg_node * newnode = (prg_node *) malloc (sizeof (struct prg_node));
|
|
||||||
newnode->inode = inode;
|
|
||||||
newnode->pid = str2int(pid);
|
|
||||||
// TODO progname could be more memory-efficient
|
|
||||||
strncpy (newnode->name, progname, PROGNAME_WIDTH);
|
|
||||||
free (progname);
|
|
||||||
setnode (inode, newnode);
|
|
||||||
} else {
|
|
||||||
//std::cout << "Linkname looked like: " << linkname << endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Max length of filenames in /proc/<pid>/fd/*. These are numeric, so 10 digits seems like a safe assumption.
|
||||||
|
#define MAX_FDLINK 10
|
||||||
|
|
||||||
/* updates the `inodeproc' inode-to-prg_node
|
/* updates the `inodeproc' inode-to-prg_node
|
||||||
* for all inodes belonging to this PID
|
* for all inodes belonging to this PID
|
||||||
* (/proc/pid/fd/42)
|
* (/proc/pid/fd/42)
|
||||||
* */
|
* */
|
||||||
void get_info_for_pid(char * pid) {
|
void get_info_for_pid(char * pid) {
|
||||||
size_t dirlen = 10 + strlen(pid);
|
char dirname[10 + MAX_PID_LENGTH];
|
||||||
char * dirname = (char *) malloc (dirlen * sizeof(char));
|
|
||||||
snprintf(dirname, dirlen, "/proc/%s/fd", pid);
|
|
||||||
|
|
||||||
//std::cout << "Getting info for pid " << pid << std::endl;
|
size_t dirlen = 10 + strlen(pid);
|
||||||
|
snprintf(dirname, dirlen, "/proc/%s/fd", pid);
|
||||||
|
|
||||||
DIR * dir = opendir(dirname);
|
DIR * dir = opendir(dirname);
|
||||||
|
|
||||||
if (!dir)
|
if (!dir)
|
||||||
{
|
{
|
||||||
std::cout << "Couldn't open dir " << dirname << ": " << strerror(errno) << "\n";
|
std::cout << "Couldn't open dir " << dirname << ": " << strerror(errno) << "\n";
|
||||||
free (dirname);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,8 +149,8 @@ void get_info_for_pid(char * pid) {
|
|||||||
continue;
|
continue;
|
||||||
//std::cout << "Looking at: " << entry->d_name << std::endl;
|
//std::cout << "Looking at: " << entry->d_name << std::endl;
|
||||||
|
|
||||||
int fromlen = dirlen + strlen(entry->d_name) + 1;
|
size_t fromlen = dirlen + strlen(entry->d_name) + 1;
|
||||||
char * fromname = (char *) malloc (fromlen * sizeof(char));
|
char fromname[10 + MAX_PID_LENGTH + 1 + MAX_FDLINK];
|
||||||
snprintf (fromname, fromlen, "%s/%s", dirname, entry->d_name);
|
snprintf (fromname, fromlen, "%s/%s", dirname, entry->d_name);
|
||||||
|
|
||||||
//std::cout << "Linking from: " << fromname << std::endl;
|
//std::cout << "Linking from: " << fromname << std::endl;
|
||||||
@@ -173,17 +160,13 @@ void get_info_for_pid(char * pid) {
|
|||||||
int usedlen = readlink(fromname, linkname, linklen-1);
|
int usedlen = readlink(fromname, linkname, linklen-1);
|
||||||
if (usedlen == -1)
|
if (usedlen == -1)
|
||||||
{
|
{
|
||||||
free (fromname);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
assert (usedlen < linklen);
|
assert (usedlen < linklen);
|
||||||
linkname[usedlen] = '\0';
|
linkname[usedlen] = '\0';
|
||||||
//std::cout << "Linking to: " << linkname << std::endl;
|
|
||||||
get_info_by_linkname (pid, linkname);
|
get_info_by_linkname (pid, linkname);
|
||||||
free (fromname);
|
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
free (dirname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* updates the `inodeproc' inode-to-prg_node mapping
|
/* updates the `inodeproc' inode-to-prg_node mapping
|
||||||
@@ -203,10 +186,8 @@ void reread_mapping () {
|
|||||||
|
|
||||||
if (! is_number (entry->d_name)) continue;
|
if (! is_number (entry->d_name)) continue;
|
||||||
|
|
||||||
//std::cout << "Getting info for " << entry->d_name << std::endl;
|
|
||||||
get_info_for_pid(entry->d_name);
|
get_info_for_pid(entry->d_name);
|
||||||
}
|
}
|
||||||
//std::cout << "End...\n";
|
|
||||||
closedir(proc);
|
closedir(proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,11 @@
|
|||||||
* quickly, too :) */
|
* quickly, too :) */
|
||||||
|
|
||||||
#include "nethogs.h"
|
#include "nethogs.h"
|
||||||
// #define PROGNAME_WIDTH 200
|
|
||||||
|
|
||||||
struct prg_node {
|
struct prg_node {
|
||||||
long inode;
|
long inode;
|
||||||
int pid;
|
pid_t pid;
|
||||||
char name[PROGNAME_WIDTH];
|
char * name;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct prg_node * findPID (unsigned long inode);
|
struct prg_node * findPID (unsigned long inode);
|
||||||
|
|||||||
Reference in New Issue
Block a user