From 3881a0844afe4d1b3cd512b2c2fd79e11bb0ed06 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Wed, 19 Aug 2015 00:00:00 -0500 Subject: [PATCH] old library: just some tweaks for transition to newlib A few minor changes are being made to position the old readproc logic for a transition to the newlib pid api. These changes will not impact current users beyond the the need to recompile such code. Hopefully this should be very last version change to the deprecated library. . most char arrays were replaced via char * to dynamic memory. this was done so that newlib could just assume ownership of such strings without using a strdup call. . former user and group name arrays also became char * but here the reason was because pwcache already cached those names. so, copying to an array never made sense. . the concept of QUICK_THREADS used to avoid duplicate overhead for string data was disabled. it could not be integrated with the newlib design, at least initially. . any #define which influenced the size of that proc_t was disable in the header. it was probably a poor idea to approach optional features in such a manner anyway. Signed-off-by: Jim Warner --- proc/readproc.c | 61 ++++++++++++++++++++++++++----------------------- proc/readproc.h | 26 ++++++++------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/proc/readproc.c b/proc/readproc.c index 1a69b27d..ede75582 100644 --- a/proc/readproc.c +++ b/proc/readproc.c @@ -95,7 +95,7 @@ static inline void free_acquired (proc_t *p, int reuse) { if (p->cgroup) free((void*)*p->cgroup); if (p->supgid) free(p->supgid); if (p->supgrp) free(p->supgrp); -#ifdef WITH_SYSTEMD + if (p->cmd) free(p->cmd); if (p->sd_mach) free(p->sd_mach); if (p->sd_ouid) free(p->sd_ouid); if (p->sd_seat) free(p->sd_seat); @@ -103,7 +103,6 @@ static inline void free_acquired (proc_t *p, int reuse) { if (p->sd_slice) free(p->sd_slice); if (p->sd_unit) free(p->sd_unit); if (p->sd_uunit) free(p->sd_uunit); -#endif #ifdef QUICK_THREADS } #endif @@ -264,8 +263,9 @@ ENTER(0x220); #endif case_Name: - { unsigned u = 0; - while(u < sizeof P->cmd - 1u){ + { char buf[16]; + unsigned u = 0; + while(u < sizeof(buf) - 1u){ int c = *S++; if(unlikely(c=='\n')) break; if(unlikely(c=='\0')) break; // should never happen @@ -275,9 +275,11 @@ ENTER(0x220); if(!c) break; // should never happen if(c=='n') c='\n'; // else we assume it is '\\' } - P->cmd[u++] = c; + buf[u++] = c; } - P->cmd[u] = '\0'; + buf[u] = '\0'; + if (!P->cmd) + P->cmd = strndup(buf, 15); S--; // put back the '\n' or '\0' continue; } @@ -562,9 +564,9 @@ ENTER(0x160); S = strchr(S, '(') + 1; tmp = strrchr(S, ')'); num = tmp - S; - if(unlikely(num >= sizeof P->cmd)) num = sizeof P->cmd - 1; - memcpy(P->cmd, S, num); - P->cmd[num] = '\0'; + if(unlikely(num >= 16)) num = 15; + if (!P->cmd) + P->cmd = strndup(S, num); S = tmp + 2; // skip ") " num = sscanf(S, @@ -938,22 +940,24 @@ static proc_t* simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons } /* some number->text resolving which is time consuming */ + /* ( names are cached, so memcpy to arrays was silly ) */ if (flags & PROC_FILLUSR){ - memcpy(p->euser, user_from_uid(p->euid), sizeof p->euser); + p->euser = user_from_uid(p->euid); if(flags & PROC_FILLSTATUS) { - memcpy(p->ruser, user_from_uid(p->ruid), sizeof p->ruser); - memcpy(p->suser, user_from_uid(p->suid), sizeof p->suser); - memcpy(p->fuser, user_from_uid(p->fuid), sizeof p->fuser); + p->ruser = user_from_uid(p->ruid); + p->suser = user_from_uid(p->suid); + p->fuser = user_from_uid(p->fuid); } } /* some number->text resolving which is time consuming */ + /* ( names are cached, so memcpy to arrays was silly ) */ if (flags & PROC_FILLGRP){ - memcpy(p->egroup, group_from_gid(p->egid), sizeof p->egroup); + p->egroup = group_from_gid(p->egid); if(flags & PROC_FILLSTATUS) { - memcpy(p->rgroup, group_from_gid(p->rgid), sizeof p->rgroup); - memcpy(p->sgroup, group_from_gid(p->sgid), sizeof p->sgroup); - memcpy(p->fgroup, group_from_gid(p->fgid), sizeof p->fgroup); + p->rgroup = group_from_gid(p->rgid); + p->sgroup = group_from_gid(p->sgid); + p->fgroup = group_from_gid(p->fgid); } } @@ -1052,22 +1056,24 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric } /* some number->text resolving which is time consuming */ + /* ( names are cached, so memcpy to arrays was silly ) */ if (flags & PROC_FILLUSR){ - memcpy(t->euser, user_from_uid(t->euid), sizeof t->euser); + t->euser = user_from_uid(t->euid); if(flags & PROC_FILLSTATUS) { - memcpy(t->ruser, user_from_uid(t->ruid), sizeof t->ruser); - memcpy(t->suser, user_from_uid(t->suid), sizeof t->suser); - memcpy(t->fuser, user_from_uid(t->fuid), sizeof t->fuser); + t->ruser = user_from_uid(t->ruid); + t->suser = user_from_uid(t->suid); + t->fuser = user_from_uid(t->fuid); } } /* some number->text resolving which is time consuming */ + /* ( names are cached, so memcpy to arrays was silly ) */ if (flags & PROC_FILLGRP){ - memcpy(t->egroup, group_from_gid(t->egid), sizeof t->egroup); + t->egroup = group_from_gid(t->egid); if(flags & PROC_FILLSTATUS) { - memcpy(t->rgroup, group_from_gid(t->rgid), sizeof t->rgroup); - memcpy(t->sgroup, group_from_gid(t->sgid), sizeof t->sgroup); - memcpy(t->fgroup, group_from_gid(t->fgid), sizeof t->fgroup); + t->rgroup = group_from_gid(t->rgid); + t->sgroup = group_from_gid(t->sgid); + t->fgroup = group_from_gid(t->fgid); } } @@ -1124,10 +1130,8 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric t->cmdline = p->cmdline; // better not free these until done with all threads! t->environ = p->environ; t->cgroup = p->cgroup; - if (t->supgid) free(t->supgid); t->supgid = p->supgid; t->supgrp = p->supgrp; -#ifdef WITH_SYSTEMD t->sd_mach = p->sd_mach; t->sd_ouid = p->sd_ouid; t->sd_seat = p->sd_seat; @@ -1135,7 +1139,6 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric t->sd_slice = p->sd_slice; t->sd_unit = p->sd_unit; t->sd_uunit = p->sd_uunit; -#endif t->lxcname = p->lxcname; MK_THREAD(t); } @@ -1429,7 +1432,9 @@ void look_up_our_self(proc_t *p) { fprintf(stderr, "Error, do this: mount -t proc proc /proc\n"); _exit(47); } + memset(p, 0, sizeof(*p)); stat2proc(ub.buf, p); // parse /proc/self/stat + free_acquired(p, 0); free(ub.buf); } diff --git a/proc/readproc.h b/proc/readproc.h index b1ebbc7e..5d4fce87 100644 --- a/proc/readproc.h +++ b/proc/readproc.h @@ -14,7 +14,7 @@ #include #define SIGNAL_STRING -#define QUICK_THREADS /* copy (vs. read) some thread info from parent proc_t */ +//#define QUICK_THREADS /* copy (vs. read) some thread info from parent proc_t */ __BEGIN_DECLS @@ -137,19 +137,15 @@ typedef struct proc_t { *supgid, // status supplementary gids as comma delimited str *supgrp; // supp grp names as comma delimited str, derived from supgid char - // Be compatible: Digital allows 16 and NT allows 14 ??? - euser[P_G_SZ], // stat(),status effective user name - ruser[P_G_SZ], // status real user name - suser[P_G_SZ], // status saved user name - fuser[P_G_SZ], // status filesystem user name - rgroup[P_G_SZ], // status real group name - egroup[P_G_SZ], // status effective group name - sgroup[P_G_SZ], // status saved group name - fgroup[P_G_SZ], // status filesystem group name - cmd[16]; // stat,status basename of executable file in call to exec(2) - struct proc_t - *ring, // n/a thread group ring - *next; // n/a various library uses + *euser, // stat(),status effective user name + *ruser, // status real user name + *suser, // status saved user name + *fuser, // status filesystem user name + *rgroup, // status real group name + *egroup, // status effective group name + *sgroup, // status saved group name + *fgroup, // status filesystem group name + *cmd; // stat,status basename of executable file in call to exec(2) int pgrp, // stat process group id session, // stat session id @@ -171,7 +167,6 @@ typedef struct proc_t { #endif long ns[NUM_NS]; // (ns subdir) inode number of namespaces -#ifdef WITH_SYSTEMD char *sd_mach, // n/a systemd vm/container name *sd_ouid, // n/a systemd session owner uid @@ -180,7 +175,6 @@ typedef struct proc_t { *sd_slice, // n/a systemd slice unit *sd_unit, // n/a systemd system unit id *sd_uunit; // n/a systemd user unit id -#endif const char *lxcname; // n/a lxc container name } proc_t;