日期:2014-05-16  浏览次数:20893 次

Linux修改进程名称(setproctitle())

1.1    介绍

每一个c程序都有个main函数,作为程序启动入口函数。main函数的原型是int main(int argc , char *argv[]);其中argc表示命令行参数的个数;argv是一个指针数组,保存所有命令行字符串。Linux进程名称是通过命令行参数argv[0]来表示的。

Linux 还有环境变量参数信息,表示进程执行需要的所有环境变量信息。通过全局变量

Char **environ;可以访问环境变量。

命令行参数argv和环境变量信息environ是在一块连续的内存中表示的,并且environ紧跟在argv后面。如下图:

 

1.2    验证argv和environ执行连续内存的测试程序

1 #include <stdio.h>
  2 #include <string.h>
  3
  4 extern char **environ;
  5 int main(int argc , char *argv[])
  6 {
  7    int i;
  8
  9    printf("argc:%d\n" , argc);
 10
 11    for (i = 0; i < argc; ++i){
 12        printf("%x\n" , argv[i]);
 13        printf("argv[%d]:%s\n" , i , argv[i]);
 14     }
 15
 16    printf("evriron=%x\n" , environ[0]);
 17
 18    return 0;
 19 }

 

 

root@VM-Ubuntu203001:~/test#gcc -o test main.c

root@VM-Ubuntu203001:~/test#./test -p /usr/local/nginx -n 32

argc:5

bfa0c8f2

argv[0]:./test

bfa0c8f9

argv[1]:-p

bfa0c8fc

argv[2]:/usr/local/nginx

bfa0c90d

argv[3]:-n

bfa0c910

argv[4]:32

evriron=bfa0c913

1.3    修改进程名称

按理说,修改进程名称,只需要修改argv[0]指向的内存的值为所需要的值即可。但是当我们要修改的值超过argv[0]所指向的内存空间大小时,再这样直接修改,就会覆盖掉一部分argv[1]的值,因为从上面的图中,很容易就可以看出。

这时候,该怎么做呢?

1、必须重新分配一块连续的内存空间,把argv和environ的参数都复制到新的空间。

2、修改argv[0]为所需要修改的值。

1.4    Nginx的做法

 * To change the process title in Linux andSolaris we have to set argv[1]
 * to NULL and to copy the title to the sameplace where the argv[0] points to.
 * However, argv[0] may be too small to hold anew title.  Fortunately, Linux
 * and Solaris store argv[] and environ[] oneafter another.  So we should
 * ensure that is the continuous memory andthen we allocate the new memory
 * for environ[] and copy it.  After this we could use the memory starting
 * from argv[0] for our process title.
 *
 * The Solaris's standard /bin/ps does not showthe changed process title.
 * You have to use "/usr/ucb/ps -w"instead.  Besides, the UCB ps does not
 * show a new title if its length less than theorigin command line length.
 * To avoid it we append to a new title theorigin command line in the
 * parenthesis.
 */
 
extern char **environ;
 
static char *ngx_os_argv_last;
 
ngx_int_t
ngx_init_setproctitle(ngx_log_t *log)
{
    u_char      *p;
    size_t       size;
    ngx_uint_t   i;
 
    size = 0;
 
    for (i = 0; environ[i]; i++) {
        size+= ngx_strlen(environ[i]) + 1;
    }
 
    p = ngx_alloc(size, log);
    if (p == NULL) {
        return NGX_ERROR;
    }
 
   /*
   这是为了找出argv和environ指向连续内存空间结尾的位置,为了能处理argv[i]被修改后,指向非进程启动时所分配的连续内存,而采用了下面的算法。但是实际上,这样还是处理不了这种情况。仅仅是个人愚见!!!
   */
    ngx_os_argv_last= ngx_os_argv[0];
 
    for (i = 0; ngx_os_argv[i]; i++) {
        if (ngx_os_argv_last == ngx_os_argv[i]) {
            ngx_os_argv_last= ngx_os_argv[i]+ ngx_strlen(ngx_os_argv[i]) + 1;
        }
    }
 
    for (i = 0; environ[i]; i++) {
        if (ngx_os_argv_last == environ[i]) {
 
            size= ngx_strlen(environ[i]) + 1;
            ngx_os_argv_last= environ[i]+ size;
 
            ngx_cpystrn(p, (u_char *) environ[i], size);
            environ[i] = (char *) p;
            p+= size;
        }
    }
 
    ngx_os_argv_last--;
 
    return NGX_OK;
}
 
 
void
ngx_setproctitle(char *title)
{
    u_char     *p;
 
#if (NGX_SOLARIS)
 
    ngx_int_t   i;
    size_t      size;
 
#endif
 
    ngx_os_argv[1]= NULL;
 
    p = ngx_cpystrn((u_char*) ngx_os_argv[0], (u_char*) "nginx: ",
                    ngx_os_argv_last- ngx_os_argv[0]);
 
    p = ngx_cpystrn(p, (u