Greenbone Vulnerability Management Libraries 22.32.0
proctitle.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "proctitle.h"
12
13#include <glib.h> /* for g_free, g_malloc0, g_strdup */
14#include <stdio.h>
15#include <string.h> /* for strlen, strdup, bzero, strncpy */
16#include <sys/param.h>
17#include <sys/prctl.h>
18
19#undef G_LOG_DOMAIN
23#define G_LOG_DOMAIN "libgvm base"
24
28extern const char *__progname;
29#ifndef __FreeBSD__
30extern const char *__progname_full;
31#endif
32static char **old_argv;
33static int old_argc;
34extern char **environ;
35void *current_environ = NULL;
36static int max_prog_name = 0;
37
44void
45proctitle_init (int argc, char **argv)
46{
47 int i;
48 char **envp = environ;
49#ifndef __FreeBSD__
50 char *new_progname, *new_progname_full;
51#else
52 char *new_progname;
53#endif
54 old_argc = argc;
55
56 if (argv == NULL)
57 return;
58 // according to c99 argv is defined as when argc is set it follows program
59 // parameter. Since we will override on set_proctitle we know that this
60 // memory is modifiable.
61 // Everything after that is unsafe and can lead to segmentation faults.
62 // Therefore we iterate through argv and append strlen to gather the maximum
63 // safe program name.
64 for (i = 0; i < argc; i++)
65 {
66 max_prog_name += strlen (argv[i]) + 1;
67 }
68 i = 0;
69
70 new_progname = strdup (__progname);
71#ifndef __FreeBSD__
72 new_progname_full = strdup (__progname_full);
73#endif
74
75 /* Move environ to new memory, to be able to reuse older one. */
76 while (envp[i])
77 i++;
78 environ = g_malloc0 (sizeof (char *) * (i + 1));
80 g_free (current_environ);
82 for (i = 0; envp[i]; i++)
83 environ[i] = g_strdup (envp[i]);
84 environ[i] = NULL;
85
86 old_argv = argv;
87 /* Seems like these are in the moved environment, so reset them. Idea from
88 * proctitle.cpp in KDE libs. */
89 __progname = new_progname;
90#ifndef __FreeBSD__
91 __progname_full = new_progname_full;
92#endif
93}
94
101static void
102proctitle_set_args (const char *new_title, va_list args)
103{
104 char *formatted;
105 int tmp;
106
107 if (old_argv == NULL)
108 /* Called setproctitle before initproctitle ? */
109 return;
110 if (max_prog_name == 0)
111 // there may no program name set
112 return;
113 // omit previous additional parameter
114
115 formatted = g_strdup_vprintf (new_title, args);
116
117 tmp = strlen (formatted);
118 if (tmp >= max_prog_name)
119 {
120 formatted[max_prog_name] = '\0';
121 tmp = max_prog_name;
122 }
123
124 // set display name
125 memset (old_argv[0], 0, max_prog_name);
126 memcpy (old_argv[0], formatted, tmp);
127 g_free (formatted);
128 if (old_argc > 1)
129 old_argv[1] = NULL;
130}
131
138void
139proctitle_set (const char *new_title, ...)
140{
141 va_list args;
142
143 va_start (args, new_title);
144 proctitle_set_args (new_title, args);
145 va_end (args);
146}
void proctitle_init(int argc, char **argv)
Initializes the process setting variables.
Definition proctitle.c:45
static void proctitle_set_args(const char *new_title, va_list args)
Sets the process' title.
Definition proctitle.c:102
const char * __progname_full
void * current_environ
Definition proctitle.c:35
static char ** old_argv
Definition proctitle.c:32
static int old_argc
Definition proctitle.c:33
char ** environ
void proctitle_set(const char *new_title,...)
Sets the process' title.
Definition proctitle.c:139
static int max_prog_name
Definition proctitle.c:36
const char * __progname
Access to the executable's name.
API for process title setting.