Conver sudo_debug_write() to a macro

This commit is contained in:
Todd C. Miller
2014-07-21 09:13:47 -06:00
parent 2b104d6b3c
commit d7c309f702
2 changed files with 18 additions and 23 deletions

View File

@@ -202,6 +202,9 @@
#define sudo_debug_execve(pri, path, argv, envp) \
sudo_debug_execve2((pri)|sudo_debug_subsys, (path), (argv), (envp))
#define sudo_debug_write(str, len, errnum) \
sudo_debug_write2(NULL, NULL, 0, (str), (len), (errnum))
/*
* NULL-terminated string lists of priorities and subsystems.
*/
@@ -224,7 +227,6 @@ __dso_public int sudo_debug_init(const char *debugfile, const char *settings);
__dso_public void sudo_debug_printf_nvm(int pri, const char *fmt, ...) __printf0like(2, 3);
__dso_public void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *fmt, ...) __printf0like(5, 6);
__dso_public void sudo_debug_vprintf2(const char *func, const char *file, int line, int level, const char *fmt, va_list ap) __printf0like(5, 0);
__dso_public void sudo_debug_write(const char *str, int len, int errno_val);
__dso_public void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val);
__dso_public pid_t sudo_debug_fork(void);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2011-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -297,7 +297,7 @@ sudo_debug_exit_ptr(const char *func, const char *file, int line,
static void
sudo_debug_write_conv(const char *func, const char *file, int lineno,
const char *str, int len, int errno_val)
const char *str, int len, int errnum)
{
/* Remove trailing newlines. */
while (len > 0 && str[len - 1] == '\n')
@@ -305,35 +305,35 @@ sudo_debug_write_conv(const char *func, const char *file, int lineno,
if (len > 0) {
if (func != NULL && file != NULL) {
if (errno_val) {
if (errnum) {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s: %s @ %s() %s:%d",
len, str, strerror(errno_val), func, file, lineno);
len, str, strerror(errnum), func, file, lineno);
} else {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s @ %s() %s:%d",
len, str, func, file, lineno);
}
} else {
if (errno_val) {
if (errnum) {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s: %s",
len, str, strerror(errno_val));
len, str, strerror(errnum));
} else {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s", len, str);
}
}
} else if (errno_val) {
} else if (errnum) {
/* Only print error string. */
if (func != NULL && file != NULL) {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%s @ %s() %s:%d",
strerror(errno_val), func, file, lineno);
strerror(errnum), func, file, lineno);
} else {
sudo_printf(SUDO_CONV_DEBUG_MSG, "%s", strerror(errno_val));
sudo_printf(SUDO_CONV_DEBUG_MSG, "%s", strerror(errnum));
}
}
}
static void
sudo_debug_write_file(const char *func, const char *file, int lineno,
const char *str, int len, int errno_val)
const char *str, int len, int errnum)
{
char *timestr, numbuf[(((sizeof(int) * 8) + 2) / 3) + 2];
time_t now;
@@ -356,13 +356,13 @@ sudo_debug_write_file(const char *func, const char *file, int lineno,
}
/* Append error string if errno is specified. */
if (errno_val) {
if (errnum) {
if (len > 0) {
iov[iovcnt].iov_base = ": ";
iov[iovcnt].iov_len = 2;
iovcnt++;
}
iov[iovcnt].iov_base = strerror(errno_val);
iov[iovcnt].iov_base = strerror(errnum);
iov[iovcnt].iov_len = strlen(iov[iovcnt].iov_base);
iovcnt++;
}
@@ -410,25 +410,18 @@ sudo_debug_write_file(const char *func, const char *file, int lineno,
void
sudo_debug_write2(const char *func, const char *file, int lineno,
const char *str, int len, int errno_val)
const char *str, int len, int errnum)
{
switch (sudo_debug_mode) {
case SUDO_DEBUG_MODE_CONV:
sudo_debug_write_conv(func, file, lineno, str, len, errno_val);
sudo_debug_write_conv(func, file, lineno, str, len, errnum);
break;
case SUDO_DEBUG_MODE_FILE:
sudo_debug_write_file(func, file, lineno, str, len, errno_val);
sudo_debug_write_file(func, file, lineno, str, len, errnum);
break;
}
}
/* XXX - turn into a macro */
void
sudo_debug_write(const char *str, int len, int errno_val)
{
sudo_debug_write2(NULL, NULL, 0, str, len, errno_val);
}
void
sudo_debug_vprintf2(const char *func, const char *file, int lineno, int level,
const char *fmt, va_list ap)