]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Change process title to show status
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 17:33:06 +0000 (17:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 17:33:06 +0000 (17:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/pakfire-daemon.c
src/pakfire/daemon.c
src/pakfire/job.c
src/pakfire/proctitle.c [new file with mode: 0644]
src/pakfire/proctitle.h [new file with mode: 0644]

index c02ca54eee4c10de8990afec309f656503d5d069..7b4cf9b26ab64ddae6fc93916f236442a50c0eee 100644 (file)
@@ -271,6 +271,8 @@ libpakfire_la_SOURCES = \
        src/pakfire/path.h \
        src/pakfire/problem.c \
        src/pakfire/problem.h \
+       src/pakfire/proctitle.c \
+       src/pakfire/proctitle.h \
        src/pakfire/progress.c \
        src/pakfire/progress.h \
        src/pakfire/pty.c \
index a818a3028763b6076afe68ade7bb0df63872b1db..c8072b73c7c017ff64d20b18b5776757144204d3 100644 (file)
@@ -24,6 +24,7 @@
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
 #include <pakfire/logging.h>
+#include <pakfire/proctitle.h>
 
 #include "lib/command.h"
 #include "lib/daemon.h"
@@ -64,6 +65,11 @@ int main(int argc, char* argv[]) {
        struct pakfire_ctx* ctx = NULL;
        int r;
 
+       // Initialize changing the process title
+       r = pakfire_proctitle_init(argc, argv);
+       if (r < 0)
+               goto ERROR;
+
        // Setup the context
        r = pakfire_ctx_create(&ctx, NULL);
        if (r)
index e2e65d080868f1594b974463de33b8c8c1761b26..25c4dbe7964012ae8beac97063539d133a61d002 100644 (file)
@@ -38,6 +38,7 @@
 #include <pakfire/httpclient.h>
 #include <pakfire/job.h>
 #include <pakfire/json.h>
+#include <pakfire/proctitle.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
 
@@ -632,6 +633,11 @@ static int pakfire_daemon_connected(struct pakfire_xfer* xfer, void* data) {
 
        DEBUG(daemon->ctx, "Connected!\n");
 
+       // Update the process title
+       r = pakfire_proctitle_set("pakfire-daemon: Connected\n");
+       if (r < 0)
+               return r;
+
        // Store a reference to the control connection
        daemon->control = pakfire_xfer_ref(xfer);
 
@@ -666,6 +672,11 @@ static int pakfire_daemon_connect(sd_event_source* s, uint64_t usec, void* data)
 
        DEBUG(daemon->ctx, "Connecting...\n");
 
+       // Update the process title
+       r = pakfire_proctitle_set("pakfire-daemon: Connecting...\n");
+       if (r < 0)
+               return r;
+
        // Fetch the distro
        const struct pakfire_distro* distro = pakfire_ctx_get_distro(daemon->ctx);
 
index 170e4ef84f38ced2824d84d03939e816510c0b83..ea7d92256f11488d0fd9c87f335b468982b0cd76 100644 (file)
@@ -43,6 +43,7 @@
 #include <pakfire/log_buffer.h>
 #include <pakfire/log_stream.h>
 #include <pakfire/logging.h>
+#include <pakfire/proctitle.h>
 #include <pakfire/string.h>
 #include <pakfire/syscalls.h>
 #include <pakfire/util.h>
@@ -632,6 +633,14 @@ static int pakfire_job_child(struct pakfire_job* job) {
        if (r < 0)
                goto ERROR;
 
+       // Set the process title
+       r = pakfire_proctitle_set("Job: %s (%s)", job->name, job->id);
+       if (r < 0) {
+               ERROR(job->ctx, "Failed to set process name: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
        // Replace the context with a new one
        r = pakfire_ctx_create(&ctx, NULL);
        if (r < 0) {
diff --git a/src/pakfire/proctitle.c b/src/pakfire/proctitle.c
new file mode 100644 (file)
index 0000000..e68ebf4
--- /dev/null
@@ -0,0 +1,110 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#include <pakfire/proctitle.h>
+#include <pakfire/string.h>
+
+extern char** environ;
+
+static char** argv0 = NULL;
+static size_t argv0_length = 0;
+
+int pakfire_proctitle_init(int argc, char** argv) {
+       char** e = NULL;
+       size_t length = 0;
+       int r;
+
+       // Count the length of the current environment
+       length = pakfire_strings_length(environ);
+
+       // Allocate a new environment
+       e = calloc(length + 1, sizeof(*e));
+       if (!e)
+               return -errno;
+
+       // Copy the entire environment
+       for (unsigned int i = 0; i < length; i++) {
+               e[i] = strdup(environ[i]);
+
+               // Fail if we could not copy
+               if (!e[i]) {
+                       r = -errno;
+                       goto ERROR;
+               }
+       }
+
+       // Determine how much space we have for a new process title
+       if (length > 0) {
+               argv0_length = environ[length - 1] + strlen(environ[length - 1]) + 1 - argv[0];
+       } else {
+               argv0_length = argv[argc - 1] + strlen(argv[argc - 1]) + 1 - argv[0];
+       }
+
+       // If there is some space, we remember the start address
+       if (!argv0_length) {
+               r = -ENOBUFS;
+               goto ERROR;
+       }
+
+       // Replace the environment
+       environ = e;
+
+       // Store argv
+       argv0 = argv;
+
+       return 0;
+
+ERROR:
+       if (e)
+               pakfire_strings_free(e);
+
+       return r;
+}
+
+int pakfire_proctitle_set(const char* format, ...) {
+       char buffer[2048];
+       va_list args;
+       int r;
+
+       // Ensure argv0 is initialized
+       if (!argv0 || !argv0_length)
+               return -ENOTSUP;
+
+       va_start(args, format);
+       r = pakfire_string_vformat(buffer, format, args);
+       va_end(args);
+       if (r < 0)
+               return r;
+
+       // Truncate the buffer
+       pakfire_string_truncate(buffer, argv0_length - 1);
+
+       // Copy the new content
+       memcpy(argv0[0], buffer, strlen(buffer) + 1);
+
+       // Reset any other arguments
+       argv0[1] = NULL;
+
+       return 0;
+}
diff --git a/src/pakfire/proctitle.h b/src/pakfire/proctitle.h
new file mode 100644 (file)
index 0000000..43b34eb
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_SETPROCTITLE_H
+#define PAKFIRE_SETPROCTITLE_H
+
+int pakfire_proctitle_init(int argc, char** argv);
+
+int pakfire_proctitle_set(const char* format, ...) __attribute__((format(printf, 1, 2)));;
+
+#endif /* PAKFIRE_SETPROCTITLE_H */