Add variants of warn/error and sudo_debug_printf that take a va_list

instead of a variable number of args.
This commit is contained in:
Todd C. Miller
2012-11-08 15:37:42 -05:00
parent 56857f7f01
commit f8660f9988
5 changed files with 139 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2011-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -422,11 +422,10 @@ sudo_debug_write(const char *str, int len, int errno_val)
} }
void void
sudo_debug_printf2(const char *func, const char *file, int lineno, int level, sudo_debug_vprintf2(const char *func, const char *file, int lineno, int level,
const char *fmt, ...) const char *fmt, va_list ap)
{ {
int buflen, pri, subsys, saved_errno = errno; int buflen, pri, subsys, saved_errno = errno;
va_list ap;
char *buf; char *buf;
if (!sudo_debug_mode) if (!sudo_debug_mode)
@@ -438,7 +437,6 @@ sudo_debug_printf2(const char *func, const char *file, int lineno, int level,
/* Make sure we want debug info at this level. */ /* Make sure we want debug info at this level. */
if (subsys < NUM_SUBSYSTEMS && sudo_debug_settings[subsys] >= pri) { if (subsys < NUM_SUBSYSTEMS && sudo_debug_settings[subsys] >= pri) {
va_start(ap, fmt);
buflen = vasprintf(&buf, fmt, ap); buflen = vasprintf(&buf, fmt, ap);
va_end(ap); va_end(ap);
if (buflen != -1) { if (buflen != -1) {
@@ -454,6 +452,17 @@ sudo_debug_printf2(const char *func, const char *file, int lineno, int level,
errno = saved_errno; errno = saved_errno;
} }
void
sudo_debug_printf2(const char *func, const char *file, int lineno, int level,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sudo_debug_vprintf2(func, file, lineno, level, fmt, ap);
va_end(ap);
}
void void
sudo_debug_execve2(int level, const char *path, char *const argv[], char *const envp[]) sudo_debug_execve2(int level, const char *path, char *const argv[], char *const envp[])
{ {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2010 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2004, 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -35,6 +35,10 @@
# define warning(...) warning2(__VA_ARGS__) # define warning(...) warning2(__VA_ARGS__)
# define warningx(...) warningx2(__VA_ARGS__) # define warningx(...) warningx2(__VA_ARGS__)
# endif /* __GNUC__ == 2 */ # endif /* __GNUC__ == 2 */
# define verror(rval, fmt, ap) error2((rval), (fmt), (ap))
# define verrorx(rval, fmt, ap) errorx2((rval), (fmt), (ap))
# define vwarning(fmt, ap) warning2((fmt), (ap))
# define vwarningx(fmt, ap) warningx2((fmt), (ap))
#else /* SUDO_ERROR_WRAP */ #else /* SUDO_ERROR_WRAP */
# if defined(__GNUC__) && __GNUC__ == 2 # if defined(__GNUC__) && __GNUC__ == 2
# define error(rval, fmt...) do { \ # define error(rval, fmt...) do { \
@@ -83,11 +87,37 @@
warningx2(__VA_ARGS__); \ warningx2(__VA_ARGS__); \
} while (0) } while (0)
# endif /* __GNUC__ == 2 */ # endif /* __GNUC__ == 2 */
# define verror(rval, fmt, ap) do { \
sudo_debug_vprintf2(__func__, __FILE__, __LINE__, \
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO|sudo_debug_subsys, \
(fmt), (ap)); \
verror2((rval), (fmt), (ap)); \
} while (0)
# define verrorx(rval, fmt, ap) do { \
sudo_debug_vprintf2(__func__, __FILE__, __LINE__, \
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|sudo_debug_subsys, (fmt), (ap)); \
verrorx2((rval), (fmt), (ap)); \
} while (0)
# define vwarning(fmt, ap) do { \
sudo_debug_vprintf2(__func__, __FILE__, __LINE__, \
SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO|sudo_debug_subsys, \
(fmt), (ap)); \
vwarning2((fmt), (ap)); \
} while (0)
# define vwarningx(fmt, ap) do { \
sudo_debug_vprintf2(__func__, __FILE__, __LINE__, \
SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|sudo_debug_subsys, (fmt), (ap)); \
vwarningx2((fmt), (ap)); \
} while (0)
#endif /* SUDO_ERROR_WRAP */ #endif /* SUDO_ERROR_WRAP */
void error2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__)); void error2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void errorx2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__)); void errorx2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void verror2(int, const char *, va_list ap) __attribute__((__noreturn__));
void verrorx2(int, const char *, va_list ap) __attribute__((__noreturn__));
void warning2(const char *, ...) __printflike(1, 2); void warning2(const char *, ...) __printflike(1, 2);
void warningx2(const char *, ...) __printflike(1, 2); void warningx2(const char *, ...) __printflike(1, 2);
void vwarning2(const char *, va_list ap);
void vwarningx2(const char *, va_list ap);
#endif /* _SUDO_ERROR_H_ */ #endif /* _SUDO_ERROR_H_ */

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2011-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -201,7 +201,8 @@ void sudo_debug_exit_str_masked(const char *func, const char *file, int line, in
void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval); void sudo_debug_exit_ptr(const char *func, const char *file, int line, int subsys, const void *rval);
int sudo_debug_fd_set(int fd); int sudo_debug_fd_set(int fd);
int sudo_debug_init(const char *debugfile, const char *settings); int sudo_debug_init(const char *debugfile, const char *settings);
void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *format, ...) __printflike(5, 6); void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *fmt, ...) __printflike(5, 6);
void sudo_debug_vprintf2(const char *func, const char *file, int line, int level, const char *fmt, va_list ap);
void sudo_debug_write(const char *str, int len, int errno_val); void sudo_debug_write(const char *str, int len, int errno_val);
void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val); void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val);
pid_t sudo_debug_fork(void); pid_t sudo_debug_fork(void);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004-2005, 2010 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2004-2005, 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -63,6 +63,22 @@ errorx2(int eval, const char *fmt, ...)
siglongjmp(error_jmp, eval); siglongjmp(error_jmp, eval);
} }
void
verror2(int eval, const char *fmt, va_list ap)
{
_warning(1, fmt, ap);
sudoers_plugin_cleanup(0);
siglongjmp(error_jmp, eval);
}
void
verrorx2(int eval, const char *fmt, va_list ap)
{
_warning(0, fmt, ap);
sudoers_plugin_cleanup(0);
siglongjmp(error_jmp, eval);
}
void void
warning2(const char *fmt, ...) warning2(const char *fmt, ...)
{ {
@@ -82,6 +98,18 @@ warningx2(const char *fmt, ...)
va_end(ap); va_end(ap);
} }
void
vwarning2(const char *fmt, va_list ap)
{
_warning(1, fmt, ap);
}
void
vwarningx2(const char *fmt, va_list ap)
{
_warning(0, fmt, ap);
}
static void static void
_warning(int use_errno, const char *fmt, va_list ap) _warning(int use_errno, const char *fmt, va_list ap)
{ {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004-2005, 2010 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 2004-2005, 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -35,56 +35,84 @@ static void _warning(int, const char *, va_list);
void void
error2(int eval, const char *fmt, ...) error2(int eval, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
_warning(1, fmt, ap); _warning(1, fmt, ap);
va_end(ap); va_end(ap);
cleanup(0); cleanup(0);
exit(eval); exit(eval);
} }
void void
errorx2(int eval, const char *fmt, ...) errorx2(int eval, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
_warning(0, fmt, ap); _warning(0, fmt, ap);
va_end(ap); va_end(ap);
cleanup(0); cleanup(0);
exit(eval); exit(eval);
}
void
verror2(int eval, const char *fmt, va_list ap)
{
_warning(1, fmt, ap);
cleanup(0);
exit(eval);
}
void
verrorx2(int eval, const char *fmt, va_list ap)
{
_warning(0, fmt, ap);
cleanup(0);
exit(eval);
} }
void void
warning2(const char *fmt, ...) warning2(const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
_warning(1, fmt, ap); _warning(1, fmt, ap);
va_end(ap); va_end(ap);
} }
void void
warningx2(const char *fmt, ...) warningx2(const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
_warning(0, fmt, ap); _warning(0, fmt, ap);
va_end(ap); va_end(ap);
}
void
vwarning2(const char *fmt, va_list ap)
{
_warning(1, fmt, ap);
}
void
vwarningx2(const char *fmt, va_list ap)
{
_warning(0, fmt, ap);
} }
static void static void
_warning(int use_errno, const char *fmt, va_list ap) _warning(int use_errno, const char *fmt, va_list ap)
{ {
int serrno = errno; int serrno = errno;
fputs(getprogname(), stderr); fputs(getprogname(), stderr);
if (fmt != NULL) { if (fmt != NULL) {
fputs(_(": "), stderr); fputs(_(": "), stderr);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
if (use_errno) { if (use_errno) {
fputs(_(": "), stderr); fputs(_(": "), stderr);
fputs(strerror(serrno), stderr); fputs(strerror(serrno), stderr);
} }
putc('\n', stderr); putc('\n', stderr);
} }