includes: add xalloc.h to unify memory allocations
The xalloc.h provides necessary error checking. Signed-off-by: Jim Warner <james.warner@comcast.net> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
4bd36d1483
commit
1ef14f4bf9
56
include/xalloc.h
Normal file
56
include/xalloc.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* General memory allocation wrappers for malloc, realloc, calloc
|
||||||
|
* and strdup.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PROCPS_NG_XALLOC_H
|
||||||
|
#define PROCPS_NG_XALLOC_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "c.h"
|
||||||
|
|
||||||
|
#ifndef XALLOC_EXIT_CODE
|
||||||
|
# define XALLOC_EXIT_CODE EXIT_FAILURE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static inline __ul_alloc_size(1)
|
||||||
|
void *xmalloc(const size_t size)
|
||||||
|
{
|
||||||
|
void *ret = malloc(size);
|
||||||
|
if (!ret && size)
|
||||||
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline __ul_alloc_size(2)
|
||||||
|
void *xrealloc(void *ptr, const size_t size)
|
||||||
|
{
|
||||||
|
void *ret = realloc(ptr, size);
|
||||||
|
if (!ret && size)
|
||||||
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline __ul_calloc_size(1, 2)
|
||||||
|
void *xcalloc(const size_t nelems, const size_t size)
|
||||||
|
{
|
||||||
|
void *ret = calloc(nelems, size);
|
||||||
|
if (!ret && size && nelems)
|
||||||
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *xstrdup(const char *str)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
if (!str)
|
||||||
|
return NULL;
|
||||||
|
ret = strdup(str);
|
||||||
|
if (!ret)
|
||||||
|
err(XALLOC_EXIT_CODE, "cannot duplicate string");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* PROCPS_NG_XALLOC_H */
|
44
pgrep.c
44
pgrep.c
@ -28,19 +28,21 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
|
// EXIT_SUCCESS is 0
|
||||||
|
// EXIT_FAILURE is 1
|
||||||
|
#define EXIT_USAGE 2
|
||||||
|
#define EXIT_FATAL 3
|
||||||
|
#define XALLOC_EXIT_CODE EXIT_FATAL
|
||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "xalloc.h"
|
||||||
#include "proc/readproc.h"
|
#include "proc/readproc.h"
|
||||||
#include "proc/sig.h"
|
#include "proc/sig.h"
|
||||||
#include "proc/devname.h"
|
#include "proc/devname.h"
|
||||||
#include "proc/sysinfo.h"
|
#include "proc/sysinfo.h"
|
||||||
#include "proc/version.h" /* procps_version */
|
#include "proc/version.h" /* procps_version */
|
||||||
|
|
||||||
// EXIT_SUCCESS is 0
|
|
||||||
// EXIT_FAILURE is 1
|
|
||||||
#define EXIT_USAGE 2
|
|
||||||
#define EXIT_FATAL 3
|
|
||||||
|
|
||||||
static int i_am_pkill = 0;
|
static int i_am_pkill = 0;
|
||||||
static const char *progname = "pgrep";
|
static const char *progname = "pgrep";
|
||||||
|
|
||||||
@ -114,7 +116,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
|
|||||||
|
|
||||||
static union el *split_list (const char *restrict str, int (*convert)(const char *, union el *))
|
static union el *split_list (const char *restrict str, int (*convert)(const char *, union el *))
|
||||||
{
|
{
|
||||||
char *copy = strdup (str);
|
char *copy = xstrdup (str);
|
||||||
char *ptr = copy;
|
char *ptr = copy;
|
||||||
char *sep_pos;
|
char *sep_pos;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -125,9 +127,7 @@ static union el *split_list (const char *restrict str, int (*convert)(const char
|
|||||||
if (i == size) {
|
if (i == size) {
|
||||||
size = size * 5 / 4 + 4;
|
size = size * 5 / 4 + 4;
|
||||||
// add 1 because slot zero is a count
|
// add 1 because slot zero is a count
|
||||||
list = realloc (list, 1 + size * sizeof *list);
|
list = xrealloc (list, 1 + size * sizeof *list);
|
||||||
if (list == NULL)
|
|
||||||
exit (EXIT_FATAL);
|
|
||||||
}
|
}
|
||||||
sep_pos = strchr (ptr, ',');
|
sep_pos = strchr (ptr, ',');
|
||||||
if (sep_pos)
|
if (sep_pos)
|
||||||
@ -238,7 +238,7 @@ static union el *read_pidfile(void)
|
|||||||
goto out;
|
goto out;
|
||||||
if(*endp && !isspace(*endp))
|
if(*endp && !isspace(*endp))
|
||||||
goto out;
|
goto out;
|
||||||
list = malloc(2 * sizeof *list);
|
list = xmalloc(2 * sizeof *list);
|
||||||
list[0].num = 1;
|
list[0].num = 1;
|
||||||
list[1].num = pid;
|
list[1].num = pid;
|
||||||
out:
|
out:
|
||||||
@ -322,7 +322,7 @@ static int conv_num (const char *restrict name, union el *restrict e)
|
|||||||
|
|
||||||
static int conv_str (const char *restrict name, union el *restrict e)
|
static int conv_str (const char *restrict name, union el *restrict e)
|
||||||
{
|
{
|
||||||
e->str = strdup (name);
|
e->str = xstrdup (name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,9 +396,7 @@ static PROCTAB *do_openproc (void)
|
|||||||
if (opt_euid && !opt_negate) {
|
if (opt_euid && !opt_negate) {
|
||||||
int num = opt_euid[0].num;
|
int num = opt_euid[0].num;
|
||||||
int i = num;
|
int i = num;
|
||||||
uid_t *uids = malloc (num * sizeof (uid_t));
|
uid_t *uids = xmalloc (num * sizeof (uid_t));
|
||||||
if (uids == NULL)
|
|
||||||
exit (EXIT_FATAL);
|
|
||||||
while (i-- > 0) {
|
while (i-- > 0) {
|
||||||
uids[i] = opt_euid[i+1].num;
|
uids[i] = opt_euid[i+1].num;
|
||||||
}
|
}
|
||||||
@ -419,13 +417,9 @@ static regex_t * do_regcomp (void)
|
|||||||
char errbuf[256];
|
char errbuf[256];
|
||||||
int re_err;
|
int re_err;
|
||||||
|
|
||||||
preg = malloc (sizeof (regex_t));
|
preg = xmalloc (sizeof (regex_t));
|
||||||
if (preg == NULL)
|
|
||||||
exit (EXIT_FATAL);
|
|
||||||
if (opt_exact) {
|
if (opt_exact) {
|
||||||
re = malloc (strlen (opt_pattern) + 5);
|
re = xmalloc (strlen (opt_pattern) + 5);
|
||||||
if (re == NULL)
|
|
||||||
exit (EXIT_FATAL);
|
|
||||||
sprintf (re, "^(%s)$", opt_pattern);
|
sprintf (re, "^(%s)$", opt_pattern);
|
||||||
} else {
|
} else {
|
||||||
re = opt_pattern;
|
re = opt_pattern;
|
||||||
@ -543,14 +537,12 @@ static union el * select_procs (int *num)
|
|||||||
}
|
}
|
||||||
if (matches == size) {
|
if (matches == size) {
|
||||||
size = size * 5 / 4 + 4;
|
size = size * 5 / 4 + 4;
|
||||||
list = realloc(list, size * sizeof *list);
|
list = xrealloc(list, size * sizeof *list);
|
||||||
if (list == NULL)
|
|
||||||
exit (EXIT_FATAL);
|
|
||||||
}
|
}
|
||||||
if (opt_long) {
|
if (opt_long) {
|
||||||
char buff[5096]; // FIXME
|
char buff[5096]; // FIXME
|
||||||
sprintf (buff, "%d %s", task.XXXID, cmd);
|
sprintf (buff, "%d %s", task.XXXID, cmd);
|
||||||
list[matches++].str = strdup (buff);
|
list[matches++].str = xstrdup (buff);
|
||||||
} else {
|
} else {
|
||||||
list[matches++].num = task.XXXID;
|
list[matches++].num = task.XXXID;
|
||||||
}
|
}
|
||||||
@ -631,7 +623,7 @@ static void parse_opts (int argc, char **argv)
|
|||||||
// case 'D': // FreeBSD: print info about non-matches for debugging
|
// case 'D': // FreeBSD: print info about non-matches for debugging
|
||||||
// break;
|
// break;
|
||||||
case 'F': // FreeBSD: the arg is a file containing a PID to match
|
case 'F': // FreeBSD: the arg is a file containing a PID to match
|
||||||
opt_pidfile = strdup (optarg);
|
opt_pidfile = xstrdup (optarg);
|
||||||
++criteria_count;
|
++criteria_count;
|
||||||
break;
|
break;
|
||||||
case 'G': // Solaris: match rgid/rgroup
|
case 'G': // Solaris: match rgid/rgroup
|
||||||
@ -676,7 +668,7 @@ static void parse_opts (int argc, char **argv)
|
|||||||
opt_count = 1;
|
opt_count = 1;
|
||||||
break;
|
break;
|
||||||
case 'd': // Solaris: change the delimiter
|
case 'd': // Solaris: change the delimiter
|
||||||
opt_delim = strdup (optarg);
|
opt_delim = xstrdup (optarg);
|
||||||
break;
|
break;
|
||||||
case 'f': // Solaris: match full process name (as in "ps -f")
|
case 'f': // Solaris: match full process name (as in "ps -f")
|
||||||
opt_full = 1;
|
opt_full = 1;
|
||||||
|
6
pmap.c
6
pmap.c
@ -26,6 +26,7 @@
|
|||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
#include "proc/escape.h"
|
#include "proc/escape.h"
|
||||||
|
#include "xalloc.h"
|
||||||
#include "proc/readproc.h"
|
#include "proc/readproc.h"
|
||||||
#include "proc/version.h"
|
#include "proc/version.h"
|
||||||
|
|
||||||
@ -449,10 +450,7 @@ int main(int argc, char **argv)
|
|||||||
if (d_option && x_option)
|
if (d_option && x_option)
|
||||||
errx(EXIT_FAILURE, _("options -d and -x cannot coexist"));
|
errx(EXIT_FAILURE, _("options -d and -x cannot coexist"));
|
||||||
|
|
||||||
pidlist = malloc(sizeof(unsigned) * argc);
|
pidlist = xmalloc(sizeof(unsigned) * argc);
|
||||||
if (pidlist == NULL)
|
|
||||||
err(EXIT_FAILURE, _("cannot allocate %zu bytes"),
|
|
||||||
sizeof(unsigned) * argc);
|
|
||||||
|
|
||||||
while (*argv) {
|
while (*argv) {
|
||||||
char *walk = *argv++;
|
char *walk = *argv++;
|
||||||
|
4
skill.c
4
skill.c
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "xalloc.h"
|
||||||
#include "proc/pwcache.h"
|
#include "proc/pwcache.h"
|
||||||
#include "proc/sig.h"
|
#include "proc/sig.h"
|
||||||
#include "proc/devname.h"
|
#include "proc/devname.h"
|
||||||
@ -39,8 +40,7 @@ static const char **cmds;
|
|||||||
static int *pids;
|
static int *pids;
|
||||||
|
|
||||||
#define ENLIST(thing,addme) do{ \
|
#define ENLIST(thing,addme) do{ \
|
||||||
if(!thing##s) thing##s = malloc(sizeof(*thing##s)*saved_argc); \
|
if(!thing##s) thing##s = xmalloc(sizeof(*thing##s)*saved_argc); \
|
||||||
if(!thing##s) fprintf(stderr,_("No memory.\n")),exit(2); \
|
|
||||||
thing##s[thing##_count++] = addme; \
|
thing##s[thing##_count++] = addme; \
|
||||||
}while(0)
|
}while(0)
|
||||||
|
|
||||||
|
19
sysctl.c
19
sysctl.c
@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "xalloc.h"
|
||||||
#include "proc/procps.h"
|
#include "proc/procps.h"
|
||||||
#include "proc/version.h"
|
#include "proc/version.h"
|
||||||
|
|
||||||
@ -155,7 +156,7 @@ static int ReadSetting(const char *restrict const name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* used to display the output */
|
/* used to display the output */
|
||||||
outname = strdup(name);
|
outname = xstrdup(name);
|
||||||
slashdot(outname,'/','.'); /* change / to . */
|
slashdot(outname,'/','.'); /* change / to . */
|
||||||
|
|
||||||
if (pattern && !pattern_match(outname, pattern)){
|
if (pattern && !pattern_match(outname, pattern)){
|
||||||
@ -164,13 +165,13 @@ static int ReadSetting(const char *restrict const name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* used to open the file */
|
/* used to open the file */
|
||||||
tmpname = malloc(strlen(name)+strlen(PROC_PATH)+2);
|
tmpname = xmalloc(strlen(name)+strlen(PROC_PATH)+2);
|
||||||
strcpy(tmpname, PROC_PATH);
|
strcpy(tmpname, PROC_PATH);
|
||||||
strcat(tmpname, name);
|
strcat(tmpname, name);
|
||||||
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
||||||
|
|
||||||
/* used to display the output */
|
/* used to display the output */
|
||||||
outname = strdup(name);
|
outname = xstrdup(name);
|
||||||
slashdot(outname,'/','.'); /* change / to . */
|
slashdot(outname,'/','.'); /* change / to . */
|
||||||
|
|
||||||
if (stat(tmpname, &ts) < 0) {
|
if (stat(tmpname, &ts) < 0) {
|
||||||
@ -285,7 +286,7 @@ static int DisplayAll(const char *restrict const path) {
|
|||||||
readdir(dp); // skip ..
|
readdir(dp); // skip ..
|
||||||
while (( de = readdir(dp) )) {
|
while (( de = readdir(dp) )) {
|
||||||
char *restrict tmpdir;
|
char *restrict tmpdir;
|
||||||
tmpdir = (char *restrict)malloc(strlen(path)+strlen(de->d_name)+2);
|
tmpdir = (char *restrict)xmalloc(strlen(path)+strlen(de->d_name)+2);
|
||||||
sprintf(tmpdir, "%s%s", path, de->d_name);
|
sprintf(tmpdir, "%s%s", path, de->d_name);
|
||||||
rc2 = stat(tmpdir, &ts);
|
rc2 = stat(tmpdir, &ts);
|
||||||
if (rc2 != 0) {
|
if (rc2 != 0) {
|
||||||
@ -339,14 +340,14 @@ static int WriteSetting(const char *setting) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* used to open the file */
|
/* used to open the file */
|
||||||
tmpname = malloc(equals-name+1+strlen(PROC_PATH));
|
tmpname = xmalloc(equals-name+1+strlen(PROC_PATH));
|
||||||
strcpy(tmpname, PROC_PATH);
|
strcpy(tmpname, PROC_PATH);
|
||||||
strncat(tmpname, name, (int)(equals-name));
|
strncat(tmpname, name, (int)(equals-name));
|
||||||
tmpname[equals-name+strlen(PROC_PATH)] = 0;
|
tmpname[equals-name+strlen(PROC_PATH)] = 0;
|
||||||
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
|
||||||
|
|
||||||
/* used to display the output */
|
/* used to display the output */
|
||||||
outname = malloc(equals-name+1);
|
outname = xmalloc(equals-name+1);
|
||||||
strncpy(outname, name, (int)(equals-name));
|
strncpy(outname, name, (int)(equals-name));
|
||||||
outname[equals-name] = 0;
|
outname[equals-name] = 0;
|
||||||
slashdot(outname,'/','.'); /* change / to . */
|
slashdot(outname,'/','.'); /* change / to . */
|
||||||
@ -544,9 +545,9 @@ static int PreloadSystem(void) {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (ncfgs % nprealloc == 0) {
|
if (ncfgs % nprealloc == 0) {
|
||||||
cfgs = realloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
|
cfgs = xrealloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
|
||||||
}
|
}
|
||||||
cfgs[ncfgs] = malloc(sizeof(struct pair) + strlen(de->d_name)*2+2 + strlen(dirs[di])+1);
|
cfgs[ncfgs] = xmalloc(sizeof(struct pair) + strlen(de->d_name)*2+2 + strlen(dirs[di])+1);
|
||||||
cfgs[ncfgs]->name = (char*)cfgs[ncfgs]+sizeof(struct pair);
|
cfgs[ncfgs]->name = (char*)cfgs[ncfgs]+sizeof(struct pair);
|
||||||
strcpy(cfgs[ncfgs]->name, de->d_name);
|
strcpy(cfgs[ncfgs]->name, de->d_name);
|
||||||
cfgs[ncfgs]->value = (char*)cfgs[ncfgs]+sizeof(struct pair) + strlen(cfgs[ncfgs]->name)+1;
|
cfgs[ncfgs]->value = (char*)cfgs[ncfgs]+sizeof(struct pair) + strlen(cfgs[ncfgs]->name)+1;
|
||||||
@ -660,7 +661,7 @@ int main(int argc, char *argv[])
|
|||||||
IgnoreError = true;
|
IgnoreError = true;
|
||||||
return PreloadSystem();
|
return PreloadSystem();
|
||||||
case 'r':
|
case 'r':
|
||||||
pattern = strdup(optarg);
|
pattern = xstrdup(optarg);
|
||||||
break;
|
break;
|
||||||
case 'V':
|
case 'V':
|
||||||
printf(PROCPS_NG_VERSION);
|
printf(PROCPS_NG_VERSION);
|
||||||
|
8
tload.c
8
tload.c
@ -13,6 +13,7 @@
|
|||||||
#include "proc/sysinfo.h"
|
#include "proc/sysinfo.h"
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
|
#include "xalloc.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -54,12 +55,9 @@ static void setsize(int i)
|
|||||||
}
|
}
|
||||||
scr_size = nrows * ncols;
|
scr_size = nrows * ncols;
|
||||||
if (screen == NULL)
|
if (screen == NULL)
|
||||||
screen = (char *)malloc(scr_size);
|
screen = (char *)xmalloc(scr_size);
|
||||||
else
|
else
|
||||||
screen = (char *)realloc(screen, scr_size);
|
screen = (char *)xrealloc(screen, scr_size);
|
||||||
|
|
||||||
if (screen == NULL)
|
|
||||||
err(EXIT_FAILURE, _("cannot allocate %zu bytes"), scr_size);
|
|
||||||
|
|
||||||
memset(screen, ' ', scr_size - 1);
|
memset(screen, ' ', scr_size - 1);
|
||||||
*(screen + scr_size - 2) = '\0';
|
*(screen + scr_size - 2) = '\0';
|
||||||
|
5
watch.c
5
watch.c
@ -17,6 +17,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "nls.h"
|
#include "nls.h"
|
||||||
#include "proc/procps.h"
|
#include "proc/procps.h"
|
||||||
|
#include "xalloc.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -366,13 +367,13 @@ int main(int argc, char *argv[])
|
|||||||
/* save for later */
|
/* save for later */
|
||||||
command_argv = &(argv[optind]);
|
command_argv = &(argv[optind]);
|
||||||
|
|
||||||
command = strdup(argv[optind++]);
|
command = xstrdup(argv[optind++]);
|
||||||
command_length = strlen(command);
|
command_length = strlen(command);
|
||||||
for (; optind < argc; optind++) {
|
for (; optind < argc; optind++) {
|
||||||
char *endp;
|
char *endp;
|
||||||
int s = strlen(argv[optind]);
|
int s = strlen(argv[optind]);
|
||||||
/* space and \0 */
|
/* space and \0 */
|
||||||
command = realloc(command, command_length + s + 2);
|
command = xrealloc(command, command_length + s + 2);
|
||||||
endp = command + command_length;
|
endp = command + command_length;
|
||||||
*endp = ' ';
|
*endp = ' ';
|
||||||
memcpy(endp + 1, argv[optind], s);
|
memcpy(endp + 1, argv[optind], s);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user