Greenbone Vulnerability Management Libraries  22.8.0
proctitle.c File Reference

Implementation of an API to set process title. More...

#include "proctitle.h"
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/prctl.h>
Include dependency graph for proctitle.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "libgvm base"
 GLib log domain. More...
 

Functions

void proctitle_init (int argc, char **argv)
 Initializes the process setting variables. More...
 
static void proctitle_set_args (const char *new_title, va_list args)
 Sets the process' title. More...
 
void proctitle_set (const char *new_title,...)
 Sets the process' title. More...
 

Variables

const char * __progname
 Access to the executable's name. More...
 
const char * __progname_full
 
static char ** old_argv
 
static int old_argc
 
char ** environ
 
void * current_environ = NULL
 
static int max_prog_name = 0
 

Detailed Description

Implementation of an API to set process title.

Definition in file proctitle.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm base"

GLib log domain.

Definition at line 23 of file proctitle.c.

Function Documentation

◆ proctitle_init()

void proctitle_init ( int  argc,
char **  argv 
)

Initializes the process setting variables.

Parameters
[in]argcArgc argument from main.
[in]argvArgv argument from main.

Definition at line 45 of file proctitle.c.

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));
79  if (current_environ)
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 }

References __progname, __progname_full, current_environ, environ, max_prog_name, old_argc, and old_argv.

◆ proctitle_set()

void proctitle_set ( const char *  new_title,
  ... 
)

Sets the process' title.

Parameters
[in]new_titleFormat string for new process title.
[in]...Arguments for format string.

Definition at line 139 of file proctitle.c.

140 {
141  va_list args;
142 
143  va_start (args, new_title);
144  proctitle_set_args (new_title, args);
145  va_end (args);
146 }

References proctitle_set_args().

Here is the call graph for this function:

◆ proctitle_set_args()

static void proctitle_set_args ( const char *  new_title,
va_list  args 
)
static

Sets the process' title.

Parameters
[in]new_titleFormat string for new process title.
[in]argsFormat string arguments variable list.

Definition at line 102 of file proctitle.c.

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 }

References max_prog_name, old_argc, and old_argv.

Referenced by proctitle_set().

Here is the caller graph for this function:

Variable Documentation

◆ __progname

const char* __progname
extern

Access to the executable's name.

Referenced by proctitle_init().

◆ __progname_full

const char* __progname_full
extern

Referenced by proctitle_init().

◆ current_environ

void* current_environ = NULL

Definition at line 35 of file proctitle.c.

Referenced by proctitle_init().

◆ environ

char** environ
extern

Referenced by proctitle_init().

◆ max_prog_name

int max_prog_name = 0
static

Definition at line 36 of file proctitle.c.

Referenced by proctitle_init(), and proctitle_set_args().

◆ old_argc

int old_argc
static

Definition at line 33 of file proctitle.c.

Referenced by proctitle_init(), and proctitle_set_args().

◆ old_argv

char** old_argv
static

Definition at line 32 of file proctitle.c.

Referenced by proctitle_init(), and proctitle_set_args().

__progname_full
const char * __progname_full
old_argc
static int old_argc
Definition: proctitle.c:33
current_environ
void * current_environ
Definition: proctitle.c:35
old_argv
static char ** old_argv
Definition: proctitle.c:32
proctitle_set_args
static void proctitle_set_args(const char *new_title, va_list args)
Sets the process' title.
Definition: proctitle.c:102
environ
char ** environ
__progname
const char * __progname
Access to the executable's name.
max_prog_name
static int max_prog_name
Definition: proctitle.c:36