From: Steve Beck <stevebeck99@hotmail.com>
To: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr@ncbi.nlm.nih.gov>,
"cygwin@cygwin.com" <cygwin@cygwin.com>
Subject: Re: Setting process command name in forked process
Date: Fri, 26 Jan 2024 18:35:51 +0000 [thread overview]
Message-ID: <DM6PR20MB347644CA2D99E18B0F468578B1792@DM6PR20MB3476.namprd20.prod.outlook.com> (raw)
In-Reply-To: <DM8PR09MB7095E65F7C281621A1A7A63BA5792@DM8PR09MB7095.namprd09.prod.outlook.com>
[-- Attachment #1: Type: text/plain, Size: 3591 bytes --]
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
next prev parent reply other threads:[~2024-01-26 18:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-23 19:32 Steve Beck
2024-01-24 17:09 ` Brian Inglis
2024-01-26 15:47 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2024-01-26 18:35 ` Steve Beck [this message]
2024-01-29 9:58 ` Corinna Vinschen
2024-01-29 22:00 ` Steve Beck
2024-01-30 16:10 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2024-01-29 15:50 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2024-01-29 15:57 ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DM6PR20MB347644CA2D99E18B0F468578B1792@DM6PR20MB3476.namprd20.prod.outlook.com \
--to=stevebeck99@hotmail.com \
--cc=cygwin@cygwin.com \
--cc=lavr@ncbi.nlm.nih.gov \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).