Compare commits

...

2 Commits

Author SHA1 Message Date
Damien Goutte-Gattat 69d4b60731 Remove malloc wrappers 6 years ago
Damien Goutte-Gattat 4c282b9d81 Fix compile-time warnings 7 years ago
  1. 2
      lib/Makefile.am
  2. 79
      lib/xmem.c
  3. 52
      lib/xmem.h
  4. 10
      src/wait4.c
  5. 10
      src/wait4pid.c

2
lib/Makefile.am

@ -1,5 +1,5 @@
noinst_LIBRARIES = libwait4.a
libwait4_a_SOURCES = err.compat.h compat.h xmem.c xmem.h
libwait4_a_SOURCES = err.compat.h compat.h
libwait4_a_LIBADD = $(LIBOBJS)

79
lib/xmem.c

@ -1,79 +0,0 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <xmem.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
void (*xmem_error)(const char *, unsigned int) = NULL;
#define mem_error(f, l) \
do { \
if ( xmem_error ) \
(*xmem_error)((f), (l)); \
err(EXIT_FAILURE, "%s, %d", (f), (l)); \
} while ( 0 )
void *
do_malloc(size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = malloc(s)) && s )
mem_error(file, line);
return p;
}
void *
do_calloc(size_t n, size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = calloc(n, s)) && n && s )
mem_error(file, line);
return p;
}
void *
do_realloc(void *p, size_t s, const char *file, unsigned int line)
{
void *np;
if ( ! (np = realloc(p, s)) && s )
mem_error(file, line);
return np;
}
char *
do_strdup(const char *s, const char *file, unsigned int line)
{
char *dup;
if ( ! (dup = strdup(s)) && s )
mem_error(file, line);
return dup;
}

52
lib/xmem.h

@ -1,52 +0,0 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20110203_XMEM_H
#define ICP20110203_XMEM_H
#include <stdlib.h>
#include <stdarg.h>
#ifdef __cpluscplus
extern "C" {
#endif
extern void (*xmem_error)(const char *, unsigned int);
void *
do_malloc(size_t, const char *, unsigned int);
void *
do_calloc(size_t, size_t, const char *, unsigned int);
void *
do_realloc(void *, size_t, const char *, unsigned int);
char *
do_strdup(const char *, const char *, unsigned int);
#define xmalloc(s) do_malloc(s, __FILE__, __LINE__)
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__)
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__)
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__)
#ifdef __cplusplus
}
#endif
#endif /* !ICP20110203_XMEM_H */

10
src/wait4.c

@ -23,10 +23,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>
#include <xmem.h>
#include <wait4pid.h>
@ -79,7 +79,7 @@ get_integer_or_die(const char *arg)
errno = 0;
val = strtol(arg, &endptr, 10);
if ( errno != 0 || endptr == arg )
errx(EXIT_FAILURE, "invalid argument, integer expected: %s");
errx(EXIT_FAILURE, "invalid argument, integer expected: %s", arg);
return val;
}
@ -87,6 +87,8 @@ get_integer_or_die(const char *arg)
static void
on_alarm(int sig)
{
(void)sig;
exit(EXIT_SUCCESS);
}
@ -142,7 +144,9 @@ main(int argc, char **argv)
alarm(timeout);
}
pids = xmalloc(len * sizeof(int));
if ( ! (pids = malloc(len * sizeof(int))) )
err(EXIT_FAILURE, "cannot allocate memory");
for ( i = 0; i < len; i++ )
pids[i] = get_integer_or_die(argv[optind + i]);

10
src/wait4pid.c

@ -147,7 +147,7 @@ wait4pid_init(void)
static int
set_filter(int sock, pid_t *pids, size_t len)
{
int i;
unsigned i;
struct sock_fprog flt;
struct sock_filter filter[256] = {
/* Load event type. */
@ -195,7 +195,7 @@ set_filter(int sock, pid_t *pids, size_t len)
int
wait4all(pid_t *pids, size_t len)
{
int ec, nproc, i;
unsigned ec, nproc, i;
struct cn_proc_msg *payload;
char buf[NLMSG_SPACE(sizeof(struct cn_proc_msg))];
@ -212,6 +212,7 @@ wait4all(pid_t *pids, size_t len)
return -1;
#endif
ec = 0;
nproc = len;
while ( nproc > 0 ) {
if ( do_recv(sock, buf, sizeof(buf), 0) <= 0 )
@ -244,7 +245,6 @@ wait4all(pid_t *pids, size_t len)
#elif defined HAVE_SYS_EVENT_H /* BSD Kevent */
#include <sys/event.h>
#include <xmem.h>
int
wait4all(pid_t *pids, size_t len)
@ -255,7 +255,9 @@ wait4all(pid_t *pids, size_t len)
if ( (fd = kqueue()) == -1 )
return -1;
evts = xmalloc(sizeof(struct kevent) * len);
if ( ! (evts = malloc(sizeof(struct kevent) * len)) )
return -1;
for ( i = 0; i < len; i++ ) {
EV_SET(&evts[i], pids[i], EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, 0);
}

Loading…
Cancel
Save