Re: Changing argv[0] under Linux.

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Wed Jan 15 2003 - 11:43:01 EST


Well, I just can't give this up! Here is a procedure plus a test
program that reallocates and copies environment strings so the
process title can be altered. The procedure returns the amount
of space available for the new name so that strncpy() can be
used to prevent damage.

If you don't like me pretending that main() gets the environment
after args[], you can access environ directly anyway with the
same result. Have fun!

If you don't have enough string-space to make a large enough name,
just increase the size of your environment.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>

extern char **environ;

size_t init_settitle(char *argv[], char *env[])
{
   size_t i, len;
   char *limit;
   char *var;
   i = 0;
   while(env[i] != NULL)
   {
       len = strlen(env[i]) + 1; /* Room for the '\0' */
       var = (char *) malloc(len);
       memcpy(var, env[i], len); /* Copy the '\0' also */
       limit = env[i]; /* Start of last string */
       env[i++] = var; /* New environment ptr */
   }
   while(*limit) /* End of last string */
       limit++;
   return (limit - argv[0]); /* Space we can use */
}

int main(int c, char *argv[], char *env[])
{
    size_t len, i;
    len = init_settitle(argv, env);
    i = 0;
    while(environ[i] != NULL)
        puts(environ[i++]);
    printf("Length allowed = %u\n", len);
    fflush(stdout);
    strncpy(argv[0], "Antidesestablishmentarianism", len);
    i = 0;
    while(environ[i] != NULL)
        puts(environ[i++]);
    fflush(stdout);
    pause();
    return 0;
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Jan 15 2003 - 22:00:54 EST