X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5179F3858419 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1706522391; bh=gPib+1p/gIrGTpJKQpV0o//r6x2zTh3vtEwrJUksp3E=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=C/MbaU0utwNvY1/tH+rX/OS3uS2ToE10WOjlwuJWgJ5x6zVeXjr9k2GIpZ8r1BYCT zsa7SQkDdfy39NCRcp5fbKwjOhYcR8Dcub629yuTh2Kj+X6qP06t0CYyNSG4wdtoQS 1ZWe6B5Uy9bmHN/OZb9ueIJNJo7+cOZuP+60BUho= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA3B13858435 Date: Mon, 29 Jan 2024 10:58:48 +0100 To: cygwin AT cygwin DOT com Subject: Re: Setting process command name in forked process Message-ID: Mail-Followup-To: cygwin AT cygwin DOT com, Steve Beck References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Corinna Vinschen via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Corinna Vinschen , Steve Beck Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" On Jan 26 18:35, Steve Beck via Cygwin wrote: > 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): > > [...] > if (fork() == 0) > { > extern char **__argv; > > strcpy(__argv[0], "bar"); Don't do that. It's much too dangerous. > __argv[0] = "bar"; That's ok, but see below. > printf("Retry in Child '%s' (argv[0]) with pid %d setting __argv[0] = 'bar': ps ...\n", argv[0], getpid()); > system("ps"); Make sure to install the procps-ng package, and then call system("procps -f"); > Here is the output when running foo.exe: > [...] > 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? Actually, that's not your fault. ps(1) from the cygwin base package does not support this kind of faking the process name, because it does not even get the command line of a process. The information given to ps(1) is pretty minimal and doesn't allow for stuff like that. The process name is always the real executable path the process has been started through. However, there's the ps(1) from the procps-ng package, which is called procps.exe so as not to collide with the basic Cygwin ps. This ps fetches process information from /proc, which *is* updated when you change __argv[0], because the info is fetched in realtime from the process itself. So with `procps -f', the output looks like this: Retry in Child './foo' (argv[0]) with pid 570 setting __argv[0] = 'bar': ps ... UID PID PPID C STIME TTY TIME CMD corinna 569 496 15 10:49 pty0 00:00:00 ./foo corinna 570 569 19 10:49 pty0 00:00:00 bar corinna 571 570 99 10:49 pty0 00:00:00 procps -f corinna 496 495 0 10:18 pty0 00:00:00 -tcsh Bottom line: Cygwin does not support this officially. Changing the global __argv[0] is a bad hack. It works, but is non-portable. In fact, there is no portable way to do this. SOme systems have prctl, some have setproctitle, and some have nothing like this at all, stock Windows for example. It's also too late to add something along these lines to Cygwin 3.5, which is due this week. What we can do is to support this in the next major version of Cygwin, in which case I'd prefer to implement this via setproctitle(3), given this API exists on BSD and Linux. Whether that implies changing Cygwin's very simple ps(1) as well, I can't say yet. HTH, Corinna -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple