implement err.h

valgrind reported invalid reads/writes with glibc err[x]|warn[x] (in
glibc printf) when stdio had been redirected in between.

define _progname (pointing to argv[0])
This commit is contained in:
Ludwig Ortmann 2013-12-10 12:10:05 +01:00
parent 864267f238
commit 916757cf4c
3 changed files with 83 additions and 3 deletions

View File

@ -49,6 +49,8 @@ extern ucontext_t native_isr_context;
extern ucontext_t end_context;
extern ucontext_t *_native_cur_ctx, *_native_isr_ctx;
extern const char *_progname;
#ifdef MODULE_UART0
#include <sys/select.h>
extern fd_set _native_rfds;

View File

@ -41,9 +41,9 @@
#include "tap.h"
int (*real_printf)(const char *format, ...);
char *argv0;
int _native_null_in_pipe[2];
int _native_null_out_file;
const char *_progname;
/**
* initialize _native_null_in_pipe to allow for reading from stdin
@ -152,7 +152,7 @@ void daemonize()
void usage_exit()
{
real_printf("usage: %s", argv0);
real_printf("usage: %s", _progname);
#ifdef MODULE_NATIVENET
real_printf(" <tap interface>");
@ -189,7 +189,7 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
*(void **)(&real_write) = dlsym(RTLD_NEXT, "write");
*(void **)(&real_printf) = dlsym(RTLD_NEXT, "printf");
argv0 = argv[0];
_progname = argv[0];
int argp = 1;
char *stderrtype = "stdio";
char *stdouttype = "stdio";

View File

@ -24,6 +24,7 @@
#endif
#include <err.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
@ -163,3 +164,80 @@ int vprintf(const char *format, va_list argp)
return r;
}
void vwarn(const char *fmt, va_list args)
{
char *m, *e;
e = strerror(errno);
if ((m = make_message(fmt, args)) == NULL) {
write(STDERR_FILENO, "malloc\n", 7);
exit(EXIT_FAILURE);
}
write(STDERR_FILENO, _progname, strlen(_progname));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, m, strlen(m));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, e, strlen(e));
write(STDERR_FILENO, "\n", 1);
free(m);
}
void vwarnx(const char *fmt, va_list args)
{
char *m;
if ((m = make_message(fmt, args)) == NULL) {
write(STDERR_FILENO, "malloc\n", 7);
exit(EXIT_FAILURE);
}
write(STDERR_FILENO, _progname, strlen(_progname));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, m, strlen(m));
write(STDERR_FILENO, "\n", 1);
free(m);
}
void verr(int eval, const char *fmt, va_list args)
{
vwarn(fmt, args);
exit(eval);
}
void verrx(int eval, const char *fmt, va_list args)
{
vwarnx(fmt, args);
exit(eval);
}
void warn(const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vwarn(fmt, argp);
va_end(argp);
}
void warnx(const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vwarnx(fmt, argp);
va_end(argp);
}
void err(int eval, const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
verr(eval, fmt, argp);
}
void errx(int eval, const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
verrx(eval, fmt, argp);
}