首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

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

2012-08-31 
Linux修改进程名称(setproctitle())1.2验证argv和environ执行连续内存的测试程序 * To change the process

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

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

 * 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_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);    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;}  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 rareoperation 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]);}


热点排行