diff options
author | Mike Frysinger <vapier@gentoo.org> | 2014-03-10 03:12:02 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2014-03-10 03:12:02 -0400 |
commit | 054269125460aadf20059019a7a106800160c69a (patch) | |
tree | 70a960da7c38d2ca2df705b06094a703e31e160a /qlop.c | |
parent | qmerge: fix color used in displaying downgrades (diff) | |
download | portage-utils-054269125460aadf20059019a7a106800160c69a.tar.gz portage-utils-054269125460aadf20059019a7a106800160c69a.tar.bz2 portage-utils-054269125460aadf20059019a7a106800160c69a.zip |
eat_file: convert API to work on dynamic buffers
Rather than use static allocated buffers everywhere where we assume we
picked a size big enough for real world uses (and just silently truncate
or die in large edge cases), convert the API to use malloc instead. We
will grow the buffers (never shrink) so that repeat calls should ramp up
to the max quickly and thus avoid having to call malloc() repeatedly.
This might report memory leaks with some funcs as we hold on to some
buffers indefinitely since we know the buffer isn't used outside the
context of the func. Helps out when the func is called repeatedly.
This should make future enhancements (like eating more than one element
in a vdb pkg dir) a lot easier.
Diffstat (limited to 'qlop.c')
-rw-r--r-- | qlop.c | 13 |
1 files changed, 7 insertions, 6 deletions
@@ -385,7 +385,8 @@ void show_current_emerge(void) DIR *proc; struct dirent *de; pid_t pid; - char buf[BUFSIZE], bufstat[300]; + static char *cmdline, *bufstat; + static size_t cmdline_len, bufstat_len; char path[50]; char *p, *q; unsigned long long start_time = 0; @@ -407,17 +408,17 @@ void show_current_emerge(void) /* portage renames the cmdline so the package name is first */ snprintf(path, sizeof(path), "/proc/%i/cmdline", pid); - if (!eat_file(path, buf, sizeof(buf))) + if (!eat_file(path, &cmdline, &cmdline_len)) continue; - if (buf[0] == '[' && (p = strchr(buf, ']')) != NULL && strstr(buf, "sandbox") != NULL) { + if (cmdline[0] == '[' && (p = strchr(cmdline, ']')) != NULL && strstr(cmdline, "sandbox") != NULL) { *p = '\0'; - p = buf+1; + p = cmdline + 1; q = p + strlen(p) + 1; /* open the stat file to figure out how long we have been running */ snprintf(path, sizeof(path), "/proc/%i/stat", pid); - if (!eat_file(path, bufstat, sizeof(bufstat))) + if (!eat_file(path, &bufstat, &bufstat_len)) continue; /* ripped from procps/proc/readproc.c */ @@ -435,7 +436,7 @@ void show_current_emerge(void) "%llu ", &start_time); /* get uptime */ - if (!eat_file("/proc/uptime", bufstat, sizeof(bufstat))) + if (!eat_file("/proc/uptime", &bufstat, &bufstat_len)) continue; sscanf(bufstat, "%lf", &uptime_secs); |