library: eliminate all dependencies on alloc.h/alloc.c

While that old master branch library may utilize those
memory allocation functions found in the alloc module,
it was inappropriate for this newlib branch to subject
callers to a stderr message followed by an early exit.

Of course, the old libprocps offered a message handler
override provision (xalloc_err_handler) but that, too,
would seem to be inappropriate for our modern library.

[ remember the battles fought with that damn libnuma ]

So, this commit will tweak those old inherited sources
setting the stage for standardized return values/errno
settings in connection with a memory allocation error.

------------------------------------------------------
Along the way, we'll address the following miscellany:

. Completely eliminate usage of anything from alloc.h.
This, of course, entails our own error checking of the
alternative allocation calls from stdlib.h & string.h.

. Eliminate use of the strdup function where possible,
as with 'procps_uptime' and 'procps_loadavg' routines.

. Whack some obsolete code (getslabinfo) in sysinfo.c.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2017-11-15 00:00:00 -05:00
committed by Craig Small
parent 2d5b7e580f
commit 18e684d65d
7 changed files with 169 additions and 155 deletions

View File

@ -28,14 +28,15 @@
#include <pwd.h>
#include <grp.h>
#include "alloc.h"
#include "pwcache.h"
#include "procps-private.h"
// might as well fill cache lines... else we waste memory anyway
#define HASHSIZE 64 /* power of 2 */
#define HASH(x) ((x) & (HASHSIZE - 1))
#define HASHSIZE 64 /* power of 2 */
#define HASH(x) ((x) & (HASHSIZE - 1))
static char ERRname[] = "?";
static struct pwbuf {
struct pwbuf *next;
@ -49,15 +50,16 @@ char *pwcache_get_user(uid_t uid) {
p = &pwhash[HASH(uid)];
while (*p) {
if ((*p)->uid == uid)
return((*p)->name);
p = &(*p)->next;
if ((*p)->uid == uid)
return((*p)->name);
p = &(*p)->next;
}
*p = (struct pwbuf *) xmalloc(sizeof(struct pwbuf));
if (!(*p = (struct pwbuf *)malloc(sizeof(struct pwbuf))))
return ERRname;
(*p)->uid = uid;
pw = getpwuid(uid);
if(!pw || strlen(pw->pw_name) >= P_G_SZ)
sprintf((*p)->name, "%u", uid);
sprintf((*p)->name, "%u", uid);
else
strcpy((*p)->name, pw->pw_name);
@ -81,7 +83,8 @@ char *pwcache_get_group(gid_t gid) {
return((*g)->name);
g = &(*g)->next;
}
*g = (struct grpbuf *) xmalloc(sizeof(struct grpbuf));
if (!(*g = (struct grpbuf *)malloc(sizeof(struct grpbuf))))
return ERRname;;
(*g)->gid = gid;
gr = getgrgid(gid);
if (!gr || strlen(gr->gr_name) >= P_G_SZ)