public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jakob Oestergaard <jakob@unthought.net>
To: DervishD <raul@pleyades.net>
Cc: jw schultz <jw@pegasys.ws>, Linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: Changing argv[0] under Linux.
Date: Wed, 15 Jan 2003 17:47:31 +0100	[thread overview]
Message-ID: <20030115164731.GB8621@unthought.net> (raw)
In-Reply-To: <20030115162219.GB86@DervishD>

On Wed, Jan 15, 2003 at 05:22:19PM +0100, DervishD wrote:
>     Hi Jakob :)
> 
> > >     Really, I'm thinking seriously about not rewritting argv[0] at
> > > all. The problem is that may confuse the user when issuing 'ps' or
> > > looking at /proc :((
> > What about
> [...]
> 
>     Mmm, interesting idea. A simpler solution is just copy the needed
> argv0. In your example, about 500 bytes are wasted ;))) By using the
> needed size at argv0, we have the space needed.

The point of the program is to exec() it once again, but with a larger
argv[0].

So what happens is that if the program is not called with --very-magic,
then it will call itself, with the following changes:
1)  argv[0] is 512 bytes long (511 bytes usable)
2)  --very-magic is supplied

Change 512 to whatever the limit you need is.  But the program should
(well that was the idea at least) never reach later parts of main()
unless it has a 512 (or whatever) byte argv[0].

> 
>     If execv doesn't do any magic with the supplied argv array, then
> this should work.

Yep, that's what I though.  Can anyone shoot this down?  ;)

> 
> > For the same effect without the --very-magic argument, you could simply
> > do an "if (argc != 2 || strlen(argv[0]) != 511)" instead.
> 
>     No, because the len of argv[0] being 'my proggy' is 9, that is not
> 511 ;)) We cannot tell the allocated space, I'm afraid O:)

Ok, I commented it to try to explain what it was I really meant to
say...   ;)

int main(int argc, char **argv)
{
 /* Check the length of argv[0] - we wat to make sure that 512 bytes are
  * allocated */
 if (strlen(argv[0]) != 511) {

   /* argv[0] is shorter than 511 non-zero bytes, so we can't be sure
    * it's really allocated big enough.  Now build an argv[0] buffer the
    * size we want it to be */

   char argv0[512];
   memcpy(argv0, 'a', 511);
   argv0[511] = 0;
   char *const args[] = { argv0, 0 };

   /* Exec ourselves, with the new argv[0] supplied - this means, we
    * will actually just re-execute ourselves, but our new "self" will
    * have a bigger argv[0] - since it's filled with 511 'a' characters,
    * our initial if() statement will evaluate to false and we will
    * not enter this block again */

   execv(argv[0], args);

   /* never to return... Let's play it safe and make sure nothing bad
    * happens */
   perror("You're fscked... execv says"); abort();
 }

 /* Cool - argv[0] was big - let's put a meaningful name in there */

 strcpy(argv[0], "my proggy");

  /* your code here - argv[0] is now 512 bytes long - putting the short
   * string into it just above, doesn't change the allocation of course */

}

Can anyone point out a problem in the above? I'd be happy to see it shot
down, mainly because it's ugly - and I hate programs that mess with
argv[0].

-- 
................................................................
:   jakob@unthought.net   : And I see the elder races,         :
:.........................: putrid forms of man                :
:   Jakob Østergaard      : See him rise and claim the earth,  :
:        OZ9ABN           : his downfall is at hand.           :
:.........................:............{Konkhra}...............:

  reply	other threads:[~2003-01-15 16:38 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-14 18:59 Changing argv[0] under Linux DervishD
2003-01-14 19:10 ` Richard B. Johnson
2003-01-14 19:14   ` DervishD
2003-01-14 19:43     ` Richard B. Johnson
2003-01-14 19:50       ` DervishD
2003-01-14 19:56         ` Richard B. Johnson
2003-01-14 20:23           ` Mark Mielke
2003-01-14 20:28             ` Richard B. Johnson
2003-01-14 21:21               ` Mark Mielke
2003-01-15 14:00                 ` Richard B. Johnson
2003-01-15 16:43                 ` Richard B. Johnson
2003-01-15 16:57                   ` DervishD
2003-01-14 22:00             ` DervishD
2003-01-21 14:16     ` Bill Davidsen
2003-01-21 15:33       ` DervishD
2003-01-14 20:25   ` Philippe Troin
2003-01-14 20:56     ` Richard B. Johnson
2003-01-14 22:04     ` DervishD
2003-01-14 23:04       ` Bob Miller
2003-01-14 23:11         ` Bob Miller
2003-01-15  4:46           ` Mark Mielke
2003-01-15  8:25             ` jw schultz
2003-01-15 11:41               ` DervishD
2003-01-15 13:16                 ` Jakob Oestergaard
2003-01-15 16:22                   ` DervishD
2003-01-15 16:47                     ` Jakob Oestergaard [this message]
2003-01-15 17:10                       ` DervishD
2003-01-15 17:36                         ` Changing argv[0] under Linux. This MUST work DervishD
2003-01-15 21:26                       ` Changing argv[0] under Linux Andreas Schwab
2003-01-15 21:36                         ` Jesse Pollard
2003-01-15 22:03                         ` DervishD
2003-01-16  9:19                           ` Dorin Lazar
2003-01-15 11:35         ` DervishD
2003-01-14 21:55   ` Miquel van Smoorenburg
2003-01-14 22:04     ` Valdis.Kletnieks
2003-01-15 11:28     ` DervishD
2003-01-27  7:47 ` Anuradha Ratnaweera
     [not found] <122203493@toto.iv>
2003-01-14 22:55 ` Peter Chubb
  -- strict thread matches above, loose matches on Subject: below --
2003-01-16 10:12 Jon Burgess
2003-01-16 10:32 ` DervishD
2003-01-16 11:31 Jon Burgess
2003-01-16 12:58 ` DervishD
     [not found] <20030116130013.GE1358@DervishD>
     [not found] ` <200301161315.h0GDFLM27487@isengard.accucard.com>
2003-01-17 10:08   ` DervishD

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=20030115164731.GB8621@unthought.net \
    --to=jakob@unthought.net \
    --cc=jw@pegasys.ws \
    --cc=linux-kernel@vger.kernel.org \
    --cc=raul@pleyades.net \
    /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