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

来源:互联网 发布:源码本地测试 编辑:程序博客网 时间:2024/05/16 12:24

1、 Linux修改进程名称简介

c语言程序运行时,进程的名称通常就是argv[0],修改进程名称就是修改内存中argv[0]存储的内容。下面介绍nginx修改进程名称的方法。

2、C程序命令行参数(argc, argv)介绍

每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点。当执行程序时,命令行参数通过两个入参提供给main()函数。第一个参数int argc,表示命令行参数的个数。第二个参数char *argv[],是一个指向命令行参数的指针数组,每一参数又都是以空字符(null)结尾的字符串。第一个字符串,亦即argv[0]指向的,(通常)是该程序的名称。argv中的指针列表以NULL指针结尾(即argv[argc]为NULL)。如下图所示:

argc和argv内存存储

3、C程序环境变量environ介绍

每一个进程都有与其相关的称之为环境列表(environment list)的字符串数组。其中每个字符串都以名称=值(name=value)形式定义。新进程在创建时,会继承其父进程的环境副本,子进程创建后,父、子进程均可更改各自的环境变量,且这些变更对对方而言不再可见。

C语言程序中,可以使用全局变量char **environ访问环境列表。(C运行时启动代码定义了该变量并以环境列表位置为其赋值。)environ与argv参数类似,指向一个以NULL结尾的指针列表,每个指针又指向一个以空字符终止的字符串。下图所示为environ在内存中的存储:

environ在内存中的存储

argv和environ数组,以及这些参数最初指向的字符串,都驻留在进程栈之上的一个单一、连续的内存区域。

4、nginx修改进程名称方法

nginx首先将命令行参数解析并保存,然后malloc开辟出新内存,将environ复制到新开辟的内存中,然后将environ指向新开辟的内存。这么做的目的有两个:1、保存经进程原有的environ;2、能够接受相对较长的新的进程名称。

下面给出nginx中修改进程名称的源码:

/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */#include <ngx_config.h>#include <ngx_core.h>#if (NGX_SETPROCTITLE_USES_ENV)/* * To change the process title in Linux and Solaris we have to set argv[1] * to NULL and to copy the title to the same place where the argv[0] points to. * However, argv[0] may be too small to hold a new title.  Fortunately, Linux * and Solaris store argv[] and environ[] one after another.  So we should * ensure that is the continuous memory and then 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 show the 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 the origin command line length. * To avoid it we append to a new title the origin command line in the * parenthesis. */extern char **environ;static char *ngx_os_argv_last;ngx_int_tngx_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);  //malloc 开辟一整块内存用于存放environ的整个数组内容    if (p == NULL) {        return NGX_ERROR;    }    ngx_os_argv_last = ngx_os_argv[0];   //ngx_os_argv = argv    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);  //ngx_cpystrn逐个将environ[i]复制给p            environ[i] = (char *) p;  //将environ[i]指向新开辟的内存            p += size;        }    }    ngx_os_argv_last--;    return NGX_OK;}voidngx_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_char *) title, ngx_os_argv_last - (char *) p);#if (NGX_SOLARIS)    size = 0;    for (i = 0; i < ngx_argc; i++) {        size += ngx_strlen(ngx_argv[i]) + 1;    }    if (size > (size_t) ((char *) p - ngx_os_argv[0])) {        /*         * ngx_setproctitle() is too rare operation so we use         * the non-optimized copies         */        p = ngx_cpystrn(p, (u_char *) " (", ngx_os_argv_last - (char *) p);        for (i = 0; i < ngx_argc; i++) {            p = ngx_cpystrn(p, (u_char *) ngx_argv[i],                            ngx_os_argv_last - (char *) p);            p = ngx_cpystrn(p, (u_char *) " ", ngx_os_argv_last - (char *) p);        }        if (*(p - 1) == ' ') {            *(p - 1) = ')';        }    }#endif    if (ngx_os_argv_last - (char *) p) {        ngx_memset(p, NGX_SETPROCTITLE_PAD, ngx_os_argv_last - (char *) p);    }    ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,                   "setproctitle: \"%s\"", ngx_os_argv[0]);}#endif /* NGX_SETPROCTITLE_USES_ENV */

参考书籍或代码:
1、Linux_UNIX系统编程手册(上)
2、nginx源码

0 0