android中不可行的方法prctl
#include <sys/prctl.h>
int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
這個(gè)方法不知道為什么竟然不行,返回值是0.待查
直接修改argv[0]
測(cè)試代碼:
int main(int argc, char **argv)
{
strcpy(*argv,"world");
while(1)
{
sleep(1);
}
}
驗(yàn)證可行钠绍,但是有缺陷秆吵。具體問(wèn)題請(qǐng)看下文。
原理
c程序的main函數(shù)五慈,作為程序啟動(dòng)入口函數(shù)纳寂。main函數(shù)的原型是
int main(int argc , char *argv[]);
其中
argc表示命令行參數(shù)的個(gè)數(shù)主穗;
argv是一個(gè)指針數(shù)組,保存所有命令行字符串毙芜。
Linux進(jìn)程名稱是通過(guò)命令行參數(shù)argv[0]來(lái)表示的忽媒。
Linux 還有環(huán)境變量參數(shù)信息,表示進(jìn)程執(zhí)行需要的所有環(huán)境變量信息腋粥。通過(guò)全局變量 Char **environ;可以訪問(wèn)環(huán)境變量晦雨。
命令行參數(shù)argv和環(huán)境變量信息environ是在一塊連續(xù)的內(nèi)存中表示的,并且environ緊跟在argv后面隘冲。如下圖:
驗(yàn)證argv和environ執(zhí)行連續(xù)內(nèi)存的測(cè)試程序
#include <stdio.h>
#include <string.h>
extern char **environ;
int main(int argc , char *argv[])
{
int i;
printf("argc:%d\n" , argc);
for (i = 0; i < argc; ++i){
printf("%x\n" , argv[i]);
printf("argv[%d]:%s\n" , i , argv[i]);
}
printf("evriron=%x\n" , environ[0]);
return 0;
}
按理說(shuō)闹瞧,修改進(jìn)程名稱,只需要修改argv[0]指向的內(nèi)存的值為所需要的值即可展辞。但是當(dāng)我們要修改的值超過(guò)argv[0]所指向的內(nèi)存空間大小時(shí)奥邮,再這樣直接修改,就會(huì)覆蓋掉一部分argv[1]的值罗珍,因?yàn)閺纳厦娴膱D中洽腺,很容易就可以看出。
這時(shí)候覆旱,該怎么做呢蘸朋?
必須重新分配一塊連續(xù)的內(nèi)存空間,把a(bǔ)rgv和environ的參數(shù)都復(fù)制到新的空間扣唱。
修改argv[0]為所需要修改的值藕坯。
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指向連續(xù)內(nèi)存空間結(jié)尾的位置,為了能處理argv[i]被修改后噪沙,指向非進(jìn)程啟動(dòng)時(shí)所分配的連續(xù)內(nèi)存炼彪,而采用了下面的算法。但是實(shí)際上曲聂,這樣還是處理不了這種情況。僅僅是個(gè)人愚見(jiàn)S踊荨E笠浮!
*/
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_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]);
}