IBG
IBG

Reputation: 465

Altering argv[0] not working in Ubuntu (C)

So I have this task of changing the process name during runtime in C, and I've stumbled upon this: http://www.uofr.net/~greg/processname.html

Now before anything else, I am aware of the dangers of altering argv[0] (but have a long way to go about Linux and UNIX stuffs) so please don't lecture about it, I just want to know why it isn't working on Ubuntu.

Here is the test source I've used:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main(int argc, char *argv[]) {

    int argv0size = strlen(argv[0]);
    int onetwothree = 1;
    char* abc = (char *) malloc(sizeof(char) * (17 + 1));
    sprintf(abc,"ApplicationName%.3d",onetwothree); 
    strncpy(argv[0], abc ,argv0size);     
    getchar(); 
}

I've tested it on Mac OS X and Ubuntu only, and it's working on Mac OS X. I don't know why it's not working on Ubuntu. What could be the possible reasons for it not working?

I'm using gcc as compiler.

Upvotes: 2

Views: 520

Answers (2)

sree
sree

Reputation: 183

Unfortunately, there's no guarantee in C that argv[0] is actually the process name. In Unix/Linux, it can be any arbitrary value that the parent process chooses to use (see 'execv/execve'). Which means its doesn't necessarily map to the process name although it may based on implementation.

For Ubuntu (and probably MacOS?) the following should be more universal:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __linux__
#include <sys/prctl.h>
#endif

void main(int argc, char *argv[]) {

  int argv0size = strlen(argv[0]);
  int onetwothree = 1;
  char* abc = (char *) malloc(sizeof(char) * (17 + 1));
  sprintf(abc,"ApplicationName%.3d",argv0size); 
  #ifdef __linux__
    prctl(PR_SET_NAME, abc, 0, 0, 0);
  #else
    strncpy(argv[0], abc ,argv0size);     
  #endif
  getchar(); 
}

EDIT: removed actual application name.
EDIT_2: prctl not available on OS X

Upvotes: 2

autistic
autistic

Reputation: 15642

So I have this task of changing the process name during runtime in C, and I've stumbled upon this: http://www.uofr.net/~greg/processname.html

I would suggest that the reference you used wasn't written by an expert in OSes or C. Perhaps it works on some OSes, but it's not required to work that way in C.

Now before anything else, I am aware of the dangers of altering argv[0] (but have a long way to go about Linux and UNIX stuffs) so please don't lecture about it, I just want to know why it isn't working on Ubuntu.

As WhozCraig said in his comment, you're certainly allowed to modify the argv array, as well as the arrays that it points to. That doesn't mean Ubuntu is required to use that modification in its process list.

I've tested it on Mac OS X and Ubuntu only, and it's working on Mac OS X. I don't know why it's not working on Ubuntu. What could be the possible reasons for it not working?

If you're looking for a rationale, think of the potential for abuse. If your own copy of argv[0] were used in the process list, trojans such as kaiten would be able to forge their process name as they used to, to make them more difficult to detect and remove.

Upvotes: 3

Related Questions