This function implements the API to change process title:
http://lxr.evanmiller.org/http/source/os/unix/ngx_setproctitle.c
59 for (i = 0; environ[i]; i++) {
60 if (ngx_os_argv_last == environ[i]) {
61
62 size = ngx_strlen(environ[i]) + 1;
63 ngx_os_argv_last = environ[i] + size;
64
65 ngx_cpystrn(p, (u_char *) environ[i], size);
66 environ[i] = (char *) p;
67 p += size;
68 }
69 }
70
71 ngx_os_argv_last--;
72
73 return NGX_OK;
74 }
What I don’t understand is ,after copy the environment variables to block allocated by ngx_alloc(size, log);,how is that block connected with argv[] block?
I don’t see such logic there.
And there’s one line I don’t understand:
ngx_os_argv_last--;
What’s it for, is this line that connects the allocated block with argv[]?
The comment at the beginning of the module seems to explain a lot, have you read it?
http://lxr.evanmiller.org/http/source/os/unix/ngx_setproctitle.c#L14
It appears the
ngx_init_setproctitle()function simply sets up the memory for setting the process title and does no real changes to the title. The comment at the beginning of the module states that it needs to setup the memory for setting the process title because argv[0] may not have the space for the new title (which is what needs to be set in order to change the title).The
ngx_os_argv_lastvariable simply points to the end of the contiguous chunk forargv[]andenviron[]. It’s used later in the copy process inngx_setproctitle(). The lastngx_os_argv_last--is probably to account for the'\0'at the end of the string.The
ngx_init_setproctitle()create the space withngx_alloc()then copiesenviron[]into the new space. Thengx_setproctitle()function simply copies overngx_os_argv[0]with the new valuetitle.