[common] proctitle: add new module to set process title

This module requires lgInitProcessTitle to be called on startup, which will
preserve argv and environ by duplicating them onto the heap, and allow the
the original memory of argv[...] and envp[...] to be reused for the new
process title.
This commit is contained in:
Quantum
2026-07-29 00:27:08 -04:00
committed by Geoffrey McRae
parent f2cc3f1959
commit 0f0f96394a
5 changed files with 196 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ add_library(lg_common_platform_code STATIC
paths.c
open.c
cpuinfo.c
proctitle.c
)
if(ENABLE_BACKTRACE)

View File

@@ -0,0 +1,132 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* 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 2 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, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/debug.h"
#include "common/proctitle.h"
#include <string.h>
#include <unistd.h>
static bool initialized;
static char * buffer;
static size_t totalSize = 0;
static char ** duplicateArray(char ** array, int count)
{
size_t size = (count + 1) * sizeof(char *);
char ** new = malloc(size);
if (!new)
return false;
memset(new, 0, size);
for (int i = 0; i < count; ++i)
{
new[i] = strdup(array[i]);
if (!new[i])
goto fail;
}
new[count] = NULL;
return new;
fail:
for (int i = 0; i < count; ++i)
if (new[i])
free(new[i]);
return NULL;
}
static int extendContiguous(char ** end, char ** array)
{
int i;
for (i = 0; array[i]; ++i)
if (array[i] == *end)
*end += strlen(array[i]) + 1;
return i;
}
void lgInitProcessTitle(int argc, char ***pargv)
{
if (initialized)
return;
char **argv = *pargv;
// it is technically possible to have no arguments, but then no titles for you
if (!argv[0])
{
initialized = true;
return;
}
char * start = argv[0];
char * end = start;
extendContiguous(&end, argv);
char ** newArgv = duplicateArray(argv, argc);
if (!newArgv)
{
initialized = true;
return;
}
*pargv = argv;
char * envEnd = end;
int vars = extendContiguous(&envEnd, environ);
if (envEnd != end)
{
char ** newEnv = duplicateArray(environ, vars);
if (newEnv)
{
environ = newEnv;
end = envEnd;
}
}
initialized = true;
buffer = start;
totalSize = end - start;
}
bool lgCanSetProcessTitle(void)
{
return initialized && totalSize > 0;
}
void lgSetProcessTitle(const char * title)
{
if (!initialized)
{
DEBUG_ERROR("Attempt to use lgSetProcessTitle without lgInitProcessTitle");
return;
}
if (!totalSize)
return;
size_t effective = strlen(title) + 1;
if (effective > totalSize)
effective = totalSize;
memcpy(buffer, title, effective - 1);
buffer[effective - 1] = '\0';
}

View File

@@ -40,6 +40,7 @@ add_library(lg_common_platform_code STATIC
time.c
cpuinfo.c
display.c
proctitle.c
)
target_link_libraries(lg_common_platform_code

View File

@@ -0,0 +1,36 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* 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 2 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, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/proctitle.h"
void lgInitProcessTitle(int argc, char **pargv[])
{
// not implemented on Windows
}
bool lgCanSetProcessTitle(void)
{
return false;
}
void lgSetProcessTitle(const char * title)
{
// not implemented on Windows
}