summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c78
1 files changed, 46 insertions, 32 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index a87828d451..0161f3e957 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -905,13 +905,10 @@ int get_process_comm(pid_t pid, char **name) {
}
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
- char *r, *k;
+ char *r = NULL, *k;
int c;
- bool space = false;
- size_t left;
FILE *f;
- assert(max_length > 0);
assert(line);
if (pid == 0)
@@ -927,47 +924,64 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
if (!f)
return -errno;
+ if (max_length == 0) {
+ size_t len = 1;
+ while ((c = getc(f)) != EOF) {
+ k = realloc(r, len+1);
+ if (k == NULL) {
+ free(r);
+ fclose(f);
+ return -ENOMEM;
+ }
+ r = k;
+ r[len-1] = isprint(c) ? c : ' ';
+ r[len] = 0;
+ len++;
+ }
+ } else {
+ bool space = false;
+ size_t left;
+ r = new(char, max_length);
+ if (!r) {
+ fclose(f);
+ return -ENOMEM;
+ }
- r = new(char, max_length);
- if (!r) {
- fclose(f);
- return -ENOMEM;
- }
+ k = r;
+ left = max_length;
+ while ((c = getc(f)) != EOF) {
+
+ if (isprint(c)) {
+ if (space) {
+ if (left <= 4)
+ break;
- k = r;
- left = max_length;
- while ((c = getc(f)) != EOF) {
+ *(k++) = ' ';
+ left--;
+ space = false;
+ }
- if (isprint(c)) {
- if (space) {
if (left <= 4)
break;
- *(k++) = ' ';
+ *(k++) = (char) c;
left--;
- space = false;
- }
-
- if (left <= 4)
- break;
+ } else
+ space = true;
+ }
- *(k++) = (char) c;
- left--;
- } else
- space = true;
+ if (left <= 4) {
+ size_t n = MIN(left-1, 3U);
+ memcpy(k, "...", n);
+ k[n] = 0;
+ } else
+ *k = 0;
}
- if (left <= 4) {
- size_t n = MIN(left-1, 3U);
- memcpy(k, "...", n);
- k[n] = 0;
- } else
- *k = 0;
-
fclose(f);
/* Kernel threads have no argv[] */
- if (r[0] == 0) {
+ if (r == NULL || r[0] == 0) {
char *t;
int h;