Greenbone Vulnerability Management Libraries  10.0.0
proctitle.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #include "proctitle.h"
26 
27 #include <glib.h> /* for g_free, g_malloc0, g_strdup */
28 #include <stdio.h>
29 #include <string.h> /* for strlen, strdup, bzero, strncpy */
30 #include <sys/param.h>
31 
35 extern const char *__progname;
36 #ifndef __FreeBSD__
37 extern const char *__progname_full;
38 #endif
39 static int argv_len;
40 static char **old_argv;
41 extern char **environ;
42 void *current_environ = NULL;
43 
50 void
51 proctitle_init (int argc, char **argv)
52 {
53  int i = 0;
54  char **envp = environ;
55 #ifndef __FreeBSD__
56  char *new_progname, *new_progname_full;
57 #else
58  char *new_progname;
59 #endif
60 
61  if (argv == NULL)
62  return;
63 
64  new_progname = strdup (__progname);
65 #ifndef __FreeBSD__
66  new_progname_full = strdup (__progname_full);
67 #endif
68 
69  /* Move environ to new memory, to be able to reuse older one. */
70  while (envp[i])
71  i++;
72  environ = g_malloc0 (sizeof (char *) * (i + 1));
73  if (current_environ)
74  g_free (current_environ);
76  for (i = 0; envp[i]; i++)
77  environ[i] = g_strdup (envp[i]);
78  environ[i] = NULL;
79 
80  old_argv = argv;
81  if (i > 0)
82  argv_len = envp[i - 1] + strlen (envp[i - 1]) - old_argv[0];
83  else
84  argv_len = old_argv[argc - 1] + strlen (old_argv[argc - 1]) - old_argv[0];
85 
86  /* Seems like these are in the moved environment, so reset them. Idea from
87  * proctitle.cpp in KDE libs. */
88  __progname = new_progname;
89 #ifndef __FreeBSD__
90  __progname_full = new_progname_full;
91 #endif
92 }
93 
100 static void
101 proctitle_set_args (const char *new_title, va_list args)
102 {
103  int i;
104  char *formatted;
105 
106  if (old_argv == NULL)
107  /* Called setproctitle before initproctitle ? */
108  return;
109 
110  formatted = g_strdup_vprintf (new_title, args);
111 
112  i = strlen (formatted);
113  if (i > argv_len - 2)
114  {
115  i = argv_len - 2;
116  formatted[i] = '\0';
117  }
118  bzero (old_argv[0], argv_len);
119  strncpy (old_argv[0], formatted, argv_len);
120  old_argv[1] = NULL;
121  g_free (formatted);
122 }
123 
130 void
131 proctitle_set (const char *new_title, ...)
132 {
133  va_list args;
134 
135  va_start (args, new_title);
136  proctitle_set_args (new_title, args);
137  va_end (args);
138 }
void proctitle_init(int argc, char **argv)
Initializes the process setting variables.
Definition: proctitle.c:51
static void proctitle_set_args(const char *new_title, va_list args)
Sets the process' title.
Definition: proctitle.c:101
char ** environ
const char * __progname_full
void proctitle_set(const char *new_title,...)
Sets the process' title.
Definition: proctitle.c:131
const char * __progname
Access to the executable's name.
static char ** old_argv
Definition: proctitle.c:40
API for process title setting.
void * current_environ
Definition: proctitle.c:42
static int argv_len
Definition: proctitle.c:39