OpenVAS Libraries  9.0.3
openvas_proctitle.c
Go to the documentation of this file.
1 /* openvas-libraries/misc
2  * $Id$
3  * Description: Implementation of an API to set process title.
4  *
5  * Authors:
6  * Hani Benhabiles <hani.benhabiles@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2014 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #include <glib.h>
27 #include <string.h>
28 #include <stdio.h>
29 
30 #include "openvas_proctitle.h"
31 
32 extern const char *__progname;
33 extern const char *__progname_full;
34 static int argv_len;
35 static char **old_argv;
36 extern char **environ;
37 void *current_environ = NULL;
38 
45 void
46 proctitle_init (int argc, char **argv)
47 {
48  int i = 0;
49  char **envp = environ;
50  char *new_progname, *new_progname_full;
51 
52  if (argv == NULL)
53  return;
54 
55  new_progname = strdup (__progname);
56  new_progname_full = strdup (__progname_full);
57 
58  /* Move environ to new memory, to be able to reuse older one. */
59  while (envp[i]) i++;
60  environ = g_malloc0 (sizeof (char *) * (i + 1));
61  if (current_environ)
62  g_free (current_environ);
64  for (i = 0; envp[i]; i++)
65  environ[i] = g_strdup (envp[i]);
66  environ[i] = NULL;
67 
68  old_argv = argv;
69  if (i > 0)
70  argv_len = envp[i-1] + strlen (envp[i-1]) - old_argv[0];
71  else
72  argv_len = old_argv[argc-1] + strlen (old_argv[argc-1]) - old_argv[0];
73 
74  /* Seems like these are in the moved environment, so reset them. Idea from
75  * proctitle.cpp in KDE libs. */
76  __progname = new_progname;
77  __progname_full = new_progname_full;
78 }
79 
86 static void
87 proctitle_set_args (const char *new_title, va_list args)
88 {
89  int i;
90  char *formatted;
91 
92  if (old_argv == NULL)
93  /* Called setproctitle before initproctitle ? */
94  return;
95 
96  formatted = g_strdup_vprintf (new_title, args);
97 
98  i = strlen (formatted);
99  if (i > argv_len - 2)
100  {
101  i = argv_len - 2;
102  formatted[i] = '\0';
103  }
104  bzero (old_argv[0], argv_len);
105  strcpy (old_argv[0], formatted);
106  old_argv[1] = NULL;
107  g_free (formatted);
108 }
109 
116 void
117 proctitle_set (const char *new_title, ...)
118 {
119  va_list args;
120 
121  va_start (args, new_title);
122  proctitle_set_args (new_title, args);
123  va_end (args);
124 }
void * current_environ
const char * __progname_full
const char * __progname
void proctitle_init(int argc, char **argv)
Initializes the process setting variables.
void proctitle_set(const char *new_title,...)
Sets the process&#39; title.
char ** environ