From d2aa64df5cac6af4b94c44c723c40f900ccada38 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Fri, 29 Mar 2019 16:24:14 +0100 Subject: [PATCH] add kore_proctitle(). manipulates the argv+environ pointers to get a sensible process title under linux / darwin. --- include/kore/kore.h | 2 ++ src/bsd.c | 4 +++- src/kore.c | 42 +++++++++++++++++++++++++++++++++++++++--- src/linux.c | 4 +--- src/worker.c | 4 ++-- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/include/kore/kore.h b/include/kore/kore.h index 44c3dc8..a7eb460 100644 --- a/include/kore/kore.h +++ b/include/kore/kore.h @@ -578,6 +578,8 @@ extern struct kore_pool nb_pool; void kore_signal(int); void kore_shutdown(void); void kore_signal_setup(void); +void kore_proctitle(const char *); + void kore_worker_reap(void); void kore_worker_init(void); void kore_worker_make_busy(void); diff --git a/src/bsd.c b/src/bsd.c index 07b40f2..8c416b4 100644 --- a/src/bsd.c +++ b/src/bsd.c @@ -223,7 +223,9 @@ kore_platform_disable_write(int fd) void kore_platform_proctitle(char *title) { -#ifndef __MACH__ +#ifdef __MACH__ + kore_proctitle(title); +#else setproctitle("%s", title); #endif } diff --git a/src/kore.c b/src/kore.c index 3296d3f..23928e3 100644 --- a/src/kore.c +++ b/src/kore.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -39,6 +40,7 @@ volatile sig_atomic_t sig_recv; struct listener_head listeners; u_int8_t nlisteners; +int kore_argc = 0; pid_t kore_pid = -1; u_int16_t cpu_count = 1; int foreground = 0; @@ -47,12 +49,15 @@ int kore_quiet = 0; int skip_runas = 0; int skip_chroot = 0; u_int8_t worker_count = 0; +char **kore_argv = NULL; +char *kore_progname = NULL; char *kore_root_path = NULL; char *kore_runas_user = NULL; u_int32_t kore_socket_backlog = 5000; char *kore_pidfile = KORE_PIDFILE_DEFAULT; char *kore_tls_cipher_list = KORE_DEFAULT_CIPHER_LIST; +extern char **environ; extern char *__progname; static void usage(void); @@ -126,6 +131,8 @@ main(int argc, char *argv[]) int ch, flags; flags = 0; + kore_argc = argc; + kore_argv = argv; #if !defined(KORE_SINGLE_BINARY) while ((ch = getopt(argc, argv, "c:dfhnqrv")) != -1) { @@ -167,11 +174,12 @@ main(int argc, char *argv[]) } } + kore_mem_init(); + kore_progname = kore_strdup(argv[0]); + argc -= optind; argv += optind; - kore_mem_init(); - #if !defined(KORE_SINGLE_BINARY) if (argc > 0) fatal("did you mean to run `kodev' instead?"); @@ -581,6 +589,34 @@ kore_shutdown(void) fatal("kore_shutdown: called from parent"); } +void +kore_proctitle(const char *title) +{ + char *p; + size_t len; + int i, slen; + + len = 0; + + for (i = 0; environ[i] != NULL; i++) { + p = kore_strdup(environ[i]); + len += strlen(environ[i]) + 1; + environ[i] = p; + } + + for (i = 0; kore_argv[i] != NULL; i++) + len += strlen(kore_argv[i]) + 1; + + kore_argv[1] = NULL; + + slen = snprintf(kore_argv[0], len, "%s %s", + basename(kore_progname), title); + if (slen == -1 || (size_t)slen >= len) + fatal("proctitle '%s' too large", title); + + memset(kore_argv[0] + slen, 0, len - slen); +} + static void kore_server_sslstart(void) { @@ -639,7 +675,7 @@ kore_server_start(int argc, char *argv[]) } #endif - kore_platform_proctitle("kore [parent]"); + kore_platform_proctitle("[parent]"); kore_msg_init(); kore_worker_init(); diff --git a/src/linux.c b/src/linux.c index 4c1c9e3..1fe75d1 100644 --- a/src/linux.c +++ b/src/linux.c @@ -211,9 +211,7 @@ kore_platform_disable_accept(void) void kore_platform_proctitle(char *title) { - if (prctl(PR_SET_NAME, title) == -1) { - kore_debug("prctl(): %s", errno_s); - } + kore_proctitle(title); } #if defined(KORE_USE_PLATFORM_SENDFILE) diff --git a/src/worker.c b/src/worker.c index f69e263..a992ca6 100644 --- a/src/worker.c +++ b/src/worker.c @@ -325,10 +325,10 @@ kore_worker_entry(struct kore_worker *kw) worker = kw; - (void)snprintf(buf, sizeof(buf), "kore [wrk %d]", kw->id); + (void)snprintf(buf, sizeof(buf), "[wrk %d]", kw->id); #if !defined(KORE_NO_TLS) if (kw->id == KORE_WORKER_KEYMGR) - (void)snprintf(buf, sizeof(buf), "kore [keymgr]"); + (void)snprintf(buf, sizeof(buf), "[keymgr]"); #endif kore_platform_proctitle(buf);