Commit 63350438 authored by Ondřej Surý's avatar Ondřej Surý Committed by Evan Hunt

Improve printing of the cmdline

- Print control characters in octal
- Shorten using an ellipsis when necessary
parent 31df6789
...@@ -110,7 +110,8 @@ LIBDNS_EXTERNAL_DATA extern unsigned int dns_zone_mkey_month; ...@@ -110,7 +110,8 @@ LIBDNS_EXTERNAL_DATA extern unsigned int dns_zone_mkey_month;
static bool want_stats = false; static bool want_stats = false;
static char program_name[NAME_MAX] = "named"; static char program_name[NAME_MAX] = "named";
static char absolute_conffile[PATH_MAX]; static char absolute_conffile[PATH_MAX];
static char saved_command_line[512]; static char saved_command_line[8192] = { 0 };
static char ellipsis[5] = { 0 };
static char version[512]; static char version[512];
static unsigned int maxsocks = 0; static unsigned int maxsocks = 0;
static int maxudp = 0; static int maxudp = 0;
...@@ -335,44 +336,76 @@ usage(void) { ...@@ -335,44 +336,76 @@ usage(void) {
static void static void
save_command_line(int argc, char *argv[]) { save_command_line(int argc, char *argv[]) {
int i; int i;
char *src; char *dst = saved_command_line;
char *dst; char *eob = saved_command_line + sizeof(saved_command_line) - 1;
char *eob; char *rollback;
const char truncated[] = "...";
bool quoted = false;
dst = saved_command_line;
eob = saved_command_line + sizeof(saved_command_line);
for (i = 1; i < argc && dst < eob; i++) { for (i = 1; i < argc && dst < eob; i++) {
char *src = argv[i];
bool quoted = false;
rollback = dst;
*dst++ = ' '; *dst++ = ' ';
src = argv[i];
while (*src != '\0' && dst < eob) { while (*src != '\0' && dst < eob) {
/* if (isalnum(*src) ||
* This won't perfectly produce a shell-independent
* pastable command line in all circumstances, but
* comes close, and for practical purposes will
* nearly always be fine.
*/
if (quoted || isalnum(*src & 0xff) ||
*src == '-' || *src == '_' || *src == '-' || *src == '_' ||
*src == '.' || *src == '/') { *src == '.' || *src == '/')
{
*dst++ = *src++; *dst++ = *src++;
quoted = false; } else if (isprint(*src)) {
} else { if (dst + 2 >= eob) {
goto add_ellipsis;
}
*dst++ = '\\'; *dst++ = '\\';
*dst++ = *src++;
} else {
/*
* Control character found in the input,
* quote the whole arg and restart
*/
if (!quoted) {
dst = rollback;
src = argv[i];
if (dst + 3 >= eob) {
goto add_ellipsis;
}
*dst++ = ' ';
*dst++ = '$';
*dst++ = '\'';
quoted = true; quoted = true;
continue;
} else {
char tmp[5];
int c = snprintf(tmp, sizeof(tmp),
"\\%03o", *src++);
if (dst + c >= eob) {
goto add_ellipsis;
} }
memmove(dst, tmp, c);
dst += c;
} }
} }
}
if (quoted) {
if (dst == eob) {
goto add_ellipsis;
}
*dst++ = '\'';
}
INSIST(sizeof(saved_command_line) >= sizeof(truncated)); }
if (dst == eob) if (dst < eob) {
strcpy(eob - sizeof(truncated), truncated); return;
else }
add_ellipsis:
dst = rollback;
*dst = '\0'; *dst = '\0';
strlcpy(ellipsis, " ...", sizeof(ellipsis));
} }
static int static int
...@@ -1005,8 +1038,8 @@ setup(void) { ...@@ -1005,8 +1038,8 @@ setup(void) {
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
NAMED_LOGMODULE_MAIN, ISC_LOG_NOTICE, NAMED_LOGMODULE_MAIN, ISC_LOG_NOTICE,
"running as: %s%s", "running as: %s%s%s",
program_name, saved_command_line); program_name, saved_command_line, ellipsis);
#ifdef __clang__ #ifdef __clang__
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
NAMED_LOGMODULE_MAIN, ISC_LOG_NOTICE, NAMED_LOGMODULE_MAIN, ISC_LOG_NOTICE,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment