2006-12-06 17:25:37 -06:00
|
|
|
/*
|
2011-08-25 05:07:12 -05:00
|
|
|
* set process title for ps (from sendmail)
|
|
|
|
*
|
|
|
|
* Clobbers argv of our main procedure so ps(1) will display the title.
|
|
|
|
*/
|
2006-12-06 17:25:37 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2011-08-25 05:07:12 -05:00
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
#include "setproctitle.h"
|
|
|
|
|
|
|
|
#ifndef SPT_BUFSIZE
|
2011-08-25 05:07:12 -05:00
|
|
|
# define SPT_BUFSIZE 2048
|
2006-12-06 17:25:37 -06:00
|
|
|
#endif
|
|
|
|
|
2011-08-25 05:07:12 -05:00
|
|
|
extern char **environ;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2011-08-25 05:07:12 -05:00
|
|
|
static char **argv0;
|
2017-09-25 14:55:34 -05:00
|
|
|
static size_t argv_lth;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2011-08-25 05:07:12 -05:00
|
|
|
void initproctitle (int argc, char **argv)
|
|
|
|
{
|
2006-12-06 17:25:37 -06:00
|
|
|
int i;
|
|
|
|
char **envp = environ;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the environment so we can reuse the memory.
|
|
|
|
* (Code borrowed from sendmail.)
|
|
|
|
* WARNING: ugly assumptions on memory layout here;
|
|
|
|
* if this ever causes problems, #undef DO_PS_FIDDLING
|
|
|
|
*/
|
|
|
|
for (i = 0; envp[i] != NULL; i++)
|
|
|
|
continue;
|
2011-08-25 05:07:12 -05:00
|
|
|
|
2016-02-26 04:10:24 -06:00
|
|
|
environ = malloc(sizeof(char *) * (i + 1));
|
2006-12-06 17:25:37 -06:00
|
|
|
if (environ == NULL)
|
|
|
|
return;
|
2011-08-25 05:07:12 -05:00
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
for (i = 0; envp[i] != NULL; i++)
|
|
|
|
if ((environ[i] = strdup(envp[i])) == NULL)
|
|
|
|
return;
|
|
|
|
environ[i] = NULL;
|
|
|
|
|
|
|
|
if (i > 0)
|
2017-09-25 14:55:34 -05:00
|
|
|
argv_lth = envp[i-1] + strlen(envp[i-1]) - argv[0];
|
2006-12-06 17:25:37 -06:00
|
|
|
else
|
2017-09-25 14:55:34 -05:00
|
|
|
argv_lth = argv[argc-1] + strlen(argv[argc-1]) - argv[0];
|
|
|
|
if (argv_lth > 1)
|
|
|
|
argv0 = argv;
|
2006-12-06 17:25:37 -06:00
|
|
|
}
|
2011-08-25 05:07:12 -05:00
|
|
|
|
|
|
|
void setproctitle (const char *prog, const char *txt)
|
|
|
|
{
|
2017-09-25 14:55:34 -05:00
|
|
|
size_t i;
|
2011-08-25 05:07:12 -05:00
|
|
|
char buf[SPT_BUFSIZE];
|
2006-12-06 17:25:37 -06:00
|
|
|
|
|
|
|
if (!argv0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE)
|
|
|
|
return;
|
|
|
|
|
2011-08-25 05:07:12 -05:00
|
|
|
sprintf(buf, "%s -- %s", prog, txt);
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2011-08-25 05:07:12 -05:00
|
|
|
i = strlen(buf);
|
2006-12-06 17:25:37 -06:00
|
|
|
if (i > argv_lth - 2) {
|
|
|
|
i = argv_lth - 2;
|
|
|
|
buf[i] = '\0';
|
|
|
|
}
|
2006-12-06 17:25:39 -06:00
|
|
|
memset(argv0[0], '\0', argv_lth); /* clear the memory area */
|
2011-08-25 05:07:12 -05:00
|
|
|
strcpy(argv0[0], buf);
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
argv0[1] = NULL;
|
|
|
|
}
|