commit 3e29be1: [Fix] Another try to fix setproctitle

Vsevolod Stakhov vsevolod at rspamd.com
Mon Nov 13 16:07:03 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-11-13 16:03:36 +0000
URL: https://github.com/rspamd/rspamd/commit/3e29be1422ae7e5303e2fda47ff93d1a458a9f75 (HEAD -> master)

[Fix] Another try to fix setproctitle

---
 src/libserver/worker_util.c |  6 +++---
 src/libutil/util.c          | 31 ++++++++++++++++++++++---------
 src/libutil/util.h          |  9 ++-------
 src/lua/lua_worker.c        | 10 +++++-----
 src/rspamadm/rspamadm.c     |  2 +-
 src/rspamd.c                | 12 ++++++------
 6 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 8edfe0cdb..74a3cf887 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -1120,11 +1120,11 @@ rspamd_handle_child_fork(struct rspamd_worker *wrk,
 	setrlimit(RLIMIT_STACK, &rlim);
 
 	if (cf->bind_conf) {
-		setproctitle("%s process (%s)", cf->worker->name,
-					 cf->bind_conf->bind_line);
+		rspamd_setproctitle("%s process (%s)", cf->worker->name,
+							cf->bind_conf->bind_line);
 	}
 	else {
-		setproctitle("%s process", cf->worker->name);
+		rspamd_setproctitle("%s process", cf->worker->name);
 	}
 
 	if (rspamd_main->pfh) {
diff --git a/src/libutil/util.c b/src/libutil/util.c
index f7e22e1bd..1c644c2cc 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -533,9 +533,7 @@ void rspamd_signals_init(struct sigaction *signals, void (*sig_handler)(gint))
 static gchar *title_buffer = NULL;
 static size_t title_buffer_size = 0;
 static gchar *title_progname, *title_progname_full;
-#endif
 
-#ifdef LINUX
 static void
 rspamd_title_dtor(gpointer d)
 {
@@ -548,12 +546,14 @@ rspamd_title_dtor(gpointer d)
 
 	g_free(env);
 }
-#endif
+#endif /* ifdef LINUX */
+
+#endif /* ifndef HAVE_SETPROCTITLE */
 
-gint init_title(rspamd_mempool_t *pool,
-				gint argc, gchar *argv[], gchar *envp[])
+gint rspamd_init_title(rspamd_mempool_t *pool,
+					   gint argc, gchar *argv[], gchar *envp[])
 {
-#ifdef LINUX
+#if defined(LINUX) && !defined(HAVE_SETPROCTITLE)
 	gchar *begin_of_buffer = 0, *end_of_buffer = 0;
 	gint i;
 
@@ -614,8 +614,20 @@ gint init_title(rspamd_mempool_t *pool,
 	return 0;
 }
 
-gint setproctitle(const gchar *fmt, ...)
+gint rspamd_setproctitle(const gchar *fmt, ...)
 {
+#ifdef HAVE_SETPROCTITLE
+	if (fmt) {
+		static char titlebuf[4096];
+		va_list ap;
+
+		va_start(ap, fmt);
+		rspamd_vsnprintf(titlebuf, sizeof(titlebuf), fmt, ap);
+		va_end(ap);
+
+		setproctitle("%s", titlebuf);
+	}
+#else
 #if defined(LINUX)
 	if (!title_buffer || !title_buffer_size) {
 		errno = ENOMEM;
@@ -669,11 +681,12 @@ gint setproctitle(const gchar *fmt, ...)
 	g_set_prgname(dest->str);
 	g_string_free(dest, TRUE);
 
-#endif
+#endif /* defined(LINUX) */
+
+#endif /* HAVE_SETPROCTITLE */
 	return 0;
 }
 
-#endif
 
 #ifndef HAVE_PIDFILE
 static gint _rspamd_pidfile_remove(rspamd_pidfh_t *pfh, gint freeit);
diff --git a/src/libutil/util.h b/src/libutil/util.h
index 4842b890e..17956acb7 100644
--- a/src/libutil/util.h
+++ b/src/libutil/util.h
@@ -126,16 +126,11 @@ void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint,
 void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint));
 #endif
 
-#ifndef HAVE_SETPROCTITLE
-
 /*
  * Process title utility functions
  */
-gint init_title(rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]);
-
-gint setproctitle(const gchar *fmt, ...);
-
-#endif
+gint rspamd_init_title(rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]);
+gint rspamd_setproctitle(const gchar *fmt, ...);
 
 #ifndef HAVE_PIDFILE
 /*
diff --git a/src/lua/lua_worker.c b/src/lua/lua_worker.c
index 47f27ca07..025b97ba1 100644
--- a/src/lua/lua_worker.c
+++ b/src/lua/lua_worker.c
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2019 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -828,10 +828,10 @@ lua_worker_spawn_process(lua_State *L)
 		ev_loop_destroy(cbdata->event_loop);
 
 		if (proctitle) {
-			setproctitle("lua process: %s", proctitle);
+			rspamd_setproctitle("lua process: %s", proctitle);
 		}
 		else {
-			setproctitle("lua process: unnamed");
+			rspamd_setproctitle("lua process: unnamed");
 		}
 
 		cbdata->event_loop = ev_loop_new(EVFLAG_SIGNALFD);
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 58f2da308..0e38dc326 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -470,7 +470,7 @@ gint main(gint argc, gchar **argv, gchar **env)
 	cfg->compiled_modules = modules;
 	cfg->compiled_workers = workers;
 
-	setproctitle("rspamdadm");
+	rspamd_setproctitle("rspamdadm");
 
 	L = cfg->lua_state;
 	rspamd_lua_set_path(L, NULL, ucl_vars);
diff --git a/src/rspamd.c b/src/rspamd.c
index 58a3f8783..df08f5410 100644
--- a/src/rspamd.c
+++ b/src/rspamd.c
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -1125,7 +1125,7 @@ rspamd_stat_update_handler(struct ev_loop *loop, ev_timer *w, int revents)
 						(new_spam - old_spam) / w->repeat,
 						(new_ham - old_ham) / w->repeat,
 						cnt > 0 ? sum / cnt : 0);
-		setproctitle("%s", proctitle);
+		rspamd_setproctitle("%s", proctitle);
 	}
 
 	memcpy(&old_stat, &cur_stat, sizeof(cur_stat));
@@ -1439,7 +1439,7 @@ gint main(gint argc, gchar **argv, gchar **env)
 	}
 
 #ifndef HAVE_SETPROCTITLE
-	init_title(rspamd_main->server_pool, argc, argv, env);
+	rspamd_init_title(rspamd_main->server_pool, argc, argv, env);
 #endif
 
 	rspamd_main->cfg->libs_ctx = rspamd_init_libs();
@@ -1584,7 +1584,7 @@ gint main(gint argc, gchar **argv, gchar **env)
 	sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL);
 
 	/* Set title */
-	setproctitle("main process");
+	rspamd_setproctitle("main process");
 
 	/* Open control socket if needed */
 	control_fd = -1;


More information about the Commits mailing list