Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505053
  • 博文数量: 140
  • 博客积分: 461
  • 博客等级: 下士
  • 技术积分: 878
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-28 10:06
文章分类

全部博文(140)

文章存档

2016年(1)

2015年(6)

2014年(20)

2013年(1)

2012年(16)

2011年(96)

分类: LINUX

2015-04-02 16:09:43

上午碰到个需求,打算修改子进程名字,之前其实没想过这个问题。
如果简单粗暴的修改argv[0],会对argv[1]及env参数造成影响。

参考一下nginx中的实现

点击(此处)折叠或打开

  1. * To change the process title in Linux andSolaris we have to set argv[1]
  2.  * to NULL and to copy the title to the sameplace where the argv[0] points to.
  3.  * However, argv[0] may be too small to hold anew title. Fortunately, Linux
  4.  * and Solaris store argv[] and environ[] oneafter another. So we should
  5.  * ensure that is the continuous memory andthen we allocate the new memory
  6.  * for environ[] and copy it. After this we could use the memory starting
  7.  * from argv[0] for our process title.
  8.  *
  9.  * The Solaris's standard /bin/ps does not showthe changed process title.
  10.  * You have to use "/usr/ucb/ps -w"instead. Besides, the UCB ps does not
  11.  * show a new title if its length less than theorigin command line length.
  12.  * To avoid it we append to a new title theorigin command line in the
  13.  * parenthesis.
  14.  */
  15.  
  16. extern char **environ;
  17.  
  18. static char *ngx_os_argv_last;
  19.  
  20. ngx_int_t
  21. ngx_init_setproctitle(ngx_log_t *log)
  22. {
  23.     u_char *p;
  24.     size_t size;
  25.     ngx_uint_t i;
  26.  
  27.     size = 0;
  28.  
  29.     for (i = 0; environ[i]; i++) {
  30.         size+= ngx_strlen(environ[i]) + 1;
  31.     }
  32.  
  33.     p = ngx_alloc(size, log);
  34.     if (p == NULL) {
  35.         return NGX_ERROR;
  36.     }
  37.  
  38.    /*
  39.    这是为了找出argv和environ指向连续内存空间结尾的位置,为了能处理argv[i]被修改后,指向非进程启动时所分配的连续内存,而采用了下面的算法。但是实际上,这样还是处理不了这种情况。仅仅是个人愚见!!!
  40.    */
  41.     ngx_os_argv_last= ngx_os_argv[0];
  42.  
  43.     for (i = 0; ngx_os_argv[i]; i++) {
  44.         if (ngx_os_argv_last == ngx_os_argv[i]) {
  45.             ngx_os_argv_last= ngx_os_argv[i]+ ngx_strlen(ngx_os_argv[i]) + 1;
  46.         }
  47.     }
  48.  
  49.     for (i = 0; environ[i]; i++) {
  50.         if (ngx_os_argv_last == environ[i]) {
  51.  
  52.             size= ngx_strlen(environ[i]) + 1;
  53.             ngx_os_argv_last= environ[i]+ size;
  54.  
  55.             ngx_cpystrn(p, (u_char *) environ[i], size);
  56.             environ[i] = (char *) p;
  57.             p+= size;
  58.         }
  59.     }
  60.  
  61.     ngx_os_argv_last--;
  62.  
  63.     return NGX_OK;
  64. }
  65.  
  66.  
  67. void
  68. ngx_setproctitle(char *title)
  69. {
  70.     u_char *p;
  71.  
  72. #if (NGX_SOLARIS)
  73.  
  74.     ngx_int_t i;
  75.     size_t size;
  76.  
  77. #endif
  78.  
  79.     ngx_os_argv[1]= NULL;
  80.  
  81.     p = ngx_cpystrn((u_char*) ngx_os_argv[0], (u_char*) "nginx: ",
  82.                     ngx_os_argv_last- ngx_os_argv[0]);
  83.  
  84.     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char*) p);
  85.  
  86. #if (NGX_SOLARIS)
  87.  
  88.     size = 0;
  89.  
  90.     for (i = 0; i < ngx_argc; i++) {
  91.         size+= ngx_strlen(ngx_argv[i]) + 1;
  92.     }
  93.  
  94.     if (size > (size_t)((char *) p - ngx_os_argv[0])) {
  95.  
  96.         /*
  97.          * ngx_setproctitle() is too rareoperation so we use
  98.          * the non-optimized copies
  99.          */
  100.  
  101.         p = ngx_cpystrn(p, (u_char *) " (",ngx_os_argv_last - (char*) p);
  102.  
  103.         for (i = 0; i < ngx_argc; i++) {
  104.             p= ngx_cpystrn(p,(u_char *) ngx_argv[i],
  105.                             ngx_os_argv_last - (char*) p);
  106.             p= ngx_cpystrn(p,(u_char *) "", ngx_os_argv_last - (char *) p);
  107.         }
  108.  
  109.         if (*(p - 1) == ' ') {
  110.             *(p- 1) = ')';
  111.         }
  112.     }
  113.  
  114. #endif
  115.  
  116.     if (ngx_os_argv_last - (char*) p) {
  117.         ngx_memset(p, NGX_SETPROCTITLE_PAD,ngx_os_argv_last - (char*) p);
  118.     }
  119.  
  120.     ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
  121.                    "setproctitle:\"%s\"", ngx_os_argv[0]);
  122. }


阅读(2881) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~