From 0380a4309f826062a4ab772e65077c0566d8fbd4 Mon Sep 17 00:00:00 2001 From: Niobos Date: Sat, 31 Aug 2019 16:32:10 +0200 Subject: [PATCH] Change process name to indicate task of process --- Makefile | 6 ++++++ common.c | 22 ++++++++++++++++++++++ common.h | 1 + doc/INSTALL.md | 6 ++++++ sslh-fork.c | 24 ++++++++++++++++++++++++ sslh-main.c | 9 ++++++++- 6 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f3a0464..5cc30b2 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ USELIBPCRE=1 # Use libpcre? (needed for regex on musl) USELIBWRAP?= # Use libwrap? USELIBCAP= # Use libcap? USESYSTEMD= # Make use of systemd socket activation +USELIBBSD?= # Use libbsd (needed to update process name in `ps`) COV_TEST= # Perform test coverage? PREFIX?=/usr BINDIR?=$(PREFIX)/sbin @@ -59,6 +60,11 @@ ifneq ($(strip $(USESYSTEMD)),) CONDITIONAL_TARGETS+=systemd-sslh-generator endif +ifneq ($(strip $(USELIBBSD)),) + LIBS:=$(LIBS) -lbsd + CPPFLAGS+=-DLIBBSD +endif + all: sslh $(MAN) echosrv $(CONDITIONAL_TARGETS) diff --git a/common.c b/common.c index cf92272..8426e60 100644 --- a/common.c +++ b/common.c @@ -30,6 +30,10 @@ #include #endif +#ifdef LIBBSD +#include +#endif + /* * Settings that depend on the command line or the config file */ @@ -621,6 +625,24 @@ void log_connection(struct connection_desc* desc, const struct connection *cnx) desc->target); } +void set_proctitle_shovel(struct connection_desc* desc, const struct connection *cnx) +{ +#ifdef LIBBSD + struct connection_desc d; + + if (!desc) { + desc = &d; + get_connection_desc(desc, cnx); + } + setproctitle("shovel %s %s->%s => %s->%s", + cnx->proto->name, + desc->peer, + desc->service, + desc->local, + desc->target); +#endif +} + /* libwrap (tcpd): check the connection is legal. This is necessary because * the actual server will only see a connection coming from localhost and can't diff --git a/common.h b/common.h index bc1f16d..b39f895 100644 --- a/common.h +++ b/common.h @@ -123,6 +123,7 @@ char* sprintaddr(char* buf, size_t size, struct addrinfo *a); void resolve_name(struct addrinfo **out, char* fullname); int get_connection_desc(struct connection_desc* desc, const struct connection *cnx); void log_connection(struct connection_desc* desc, const struct connection *cnx); +void set_proctitle_shovel(struct connection_desc* desc, const struct connection *cnx); int check_access_rights(int in_socket, const char* service); void setup_signals(void); void setup_syslog(const char* bin_name); diff --git a/doc/INSTALL.md b/doc/INSTALL.md index a7e4fd7..40f01b5 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -27,6 +27,9 @@ distclean` for example), you will also need to add [conf2struct](https://www.rutschle.net/tech/conf2struct/README.html) (v1.0) to your path. +There is optional support to change the process name (as shown in `ps`), +which requires `libbsd` at runtime, and `libbsd-dev` at compile-time. + Compilation ----------- @@ -48,6 +51,9 @@ of the Makefile: * `USESYSTEMD` compiles support for using systemd socket activation. You will need `systemd` headers to compile (`systemd-devel` in Fedora). +* `USELIBBSD` compiles support for updating the process name (as shown + by `ps`). + Binaries -------- diff --git a/sslh-fork.c b/sslh-fork.c index 0c16af6..f3f2142 100644 --- a/sslh-fork.c +++ b/sslh-fork.c @@ -23,6 +23,10 @@ #include "common.h" #include "probe.h" +#ifdef LIBBSD +#include +#endif + const char* server_type = "sslh-fork"; #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -114,6 +118,7 @@ void start_shoveler(int in_socket) get_connection_desc(&desc, &cnx); log_connection(&desc, &cnx); + set_proctitle_shovel(&desc, &cnx); flush_deferred(&cnx.q[1]); @@ -140,6 +145,24 @@ void stop_listeners(int sig) } } +void set_listen_procname(int listen_socket) +{ +#ifdef LIBBSD + int res; + struct addrinfo addr; + struct sockaddr_storage ss; + char listen_addr[NI_MAXHOST+1+NI_MAXSERV+1]; + + addr.ai_addr = (struct sockaddr*)&ss; + addr.ai_addrlen = sizeof(ss); + res = getsockname(listen_socket, addr.ai_addr, &addr.ai_addrlen); + if (res != -1) { + sprintaddr(listen_addr, sizeof(listen_addr), &addr); + setproctitle("listener %s", listen_addr); + } +#endif +} + void main_loop(int listen_sockets[], int num_addr_listen) { int in_socket, i, res; @@ -160,6 +183,7 @@ void main_loop(int listen_sockets[], int num_addr_listen) case 0: /* Listening process just accepts a connection, forks, and goes * back to listening */ + set_listen_procname(listen_sockets[i]); while (1) { in_socket = accept(listen_sockets[i], 0, 0); diff --git a/sslh-main.c b/sslh-main.c index efe7f25..7e5c1bb 100644 --- a/sslh-main.c +++ b/sslh-main.c @@ -33,6 +33,10 @@ #endif #endif +#ifdef LIBBSD +#include +#endif + #include "common.h" #include "probe.h" @@ -247,7 +251,7 @@ void config_sanity_check(struct sslhcfg_item* cfg) { } -int main(int argc, char *argv[]) +int main(int argc, char *argv[], char* envp[]) { extern char *optarg; @@ -255,6 +259,9 @@ int main(int argc, char *argv[]) int res, num_addr_listen; int *listen_sockets; +#ifdef LIBBSD + setproctitle_init(argc, argv, envp); +#endif memset(&cfg, 0, sizeof(cfg)); sslhcfg_cl_parse(argc, argv, &cfg);