Setting process command name in forked process
Steve Beck
stevebeck99@hotmail.com
Fri Jan 26 18:35:51 GMT 2024
Thanks so much for the reply, Anton! Really appreciate it.
I tried what you proposed. Here is the code trying both ways (overwriting what is referenced by __argv[0] and then reassigning the reference). I compile this code (foo.c) simply as follows: gcc -o foo foo.c (with no errors or warnings):
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
extern pid_t getpid(void), fork(void);
extern int sleep(int);
int
main (int argc, char *argv[])
{
printf("%s with pid %d: Entered. About to fork child ...\n",
argv[0], getpid());
if (fork() == 0)
{
extern char **__argv;
strcpy(__argv[0], "bar");
printf("Child '%s' (argv[0]) with pid %d: ps ...\n",
argv[0], getpid());
system("ps");
__argv[0] = "bar";
printf("Retry in Child '%s' (argv[0]) with pid %d setting __argv[0] = 'bar': ps ...\n", argv[0], getpid());
system("ps");
exit(0);
}
printf("%s parent with pid %d: sleep(5) ...\n", argv[0], getpid());
sleep(5);
exit(0);
}
Here is the output when running foo.exe:
foo with pid 497: Entered. About to fork child ...
foo parent with pid 497: sleep(5) ...
Child 'bar' (argv[0]) with pid 498: ps ...
PID PPID PGID WINPID TTY UID STIME COMMAND
446 445 446 108276 pty0 1207519 10:19:37 /usr/bin/bash
445 1 445 126652 ? 1207519 10:19:36 /usr/bin/mintty
499 498 497 125904 pty0 1207519 10:26:41 /usr/bin/ps
498 497 497 128424 pty0 1207519 10:26:41 /home/sbeck/foo
497 446 497 123772 pty0 1207519 10:26:41 /home/sbeck/foo
Retry in Child 'bar' (argv[0]) with pid 498 setting __argv[0] = 'bar': ps ...
PID PPID PGID WINPID TTY UID STIME COMMAND
446 445 446 108276 pty0 1207519 10:19:37 /usr/bin/bash
445 1 445 126652 ? 1207519 10:19:36 /usr/bin/mintty
498 497 497 128424 pty0 1207519 10:26:41 /home/sbeck/foo
497 446 497 123772 pty0 1207519 10:26:41 /home/sbeck/foo
500 498 497 128948 pty0 1207519 10:26:41 /usr/bin/ps
As you can see, in neither case does the ps command seem to accurately reflect the change in __argv[0] (although within the program, the change occurs to argv[0]).
Can you see what I'm doing wrong?
Many thanks,
Steve
________________________________
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] <lavr@ncbi.nlm.nih.gov>
Sent: Friday, January 26, 2024 7:47 AM
To: Steve Beck <stevebeck99@hotmail.com>; cygwin@cygwin.com <cygwin@cygwin.com>
Subject: RE: Setting process command name in forked process
> (I'm assuming it's too late by the time the /proc entry has been set up).
Actually, it's not. But beware, the suggested solution is absolutely NOT portable:
These are the externals that /proc is referring to... (Not argc, argv passed to main().)
extern char** __argv;
extern int __argc;
So you can change everything that it shows by reassigning __argv(and/or __argc) with a totally
different set of arguments, or just modify __argv[0], provided that it fits into the space
occupied by the original argument [0].
HTH,
Anton Lavrentiev
Contractor NIH/NLM/NCBI
More information about the Cygwin
mailing list