execve函數的介紹與使用


#include<stdio.h>
#include<unistd.h>
int main()
{
        char *filename[]={"./BP",NULL};//BP是c文件編譯鏈接后產生的可執行文件,目的是打印一條語句
        char *envp[]={0,NULL};//如果依賴於新環境變量,這里才需要改
        execve("/root/BP",filename,envp);//字符串是文件的路徑需要具體到該運行文件名,直到文件夾貌似識別不了,或者命令的路徑
        /*注意,把下面的三個注釋遇到去掉,后面的ls命令也無法執行,因為*execve執行后,相當於結束了當前的程序,切換到execve里面的那個程序*去了*/
// char *filename1[]={"ls","-al","/root",NULL};//命令和參數,記得要加NULL
// char *envp1[]={0,NULL};
// execve("/bin/ls",filename1,envp1);
        return 0;
}

//BP.c
#include<stdio.h>
int main()
{
        printf("this is c process\n");
        return 0;
}

運行結果如下
沒修改前的運行結果
[22:41:10] gcc execvetest.c -o execvetest
[22:41:18] gcc BP.c -o BP
[22:41:24] ./execvetest
this is c process
注釋掉前三行,並且去掉后面的注釋,運行結果如下
[22:45:05] gcc execvetest.c -o execvetest
[22:45:11] gcc BP.c -o BP
[22:45:23] ./execvetest
total 64
dr-xr-x—. 15 root root 4096 May 10 21:06 .
dr-xr-xr-x. 17 root root 224 Apr 30 02:52 ..
-rw——-. 1 root root 314 Apr 30 07:08 .ICEauthority
-rw——-. 1 root root 3769 May 10 18:33 .bash_history
-rw-r–r–. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r–r–. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r–r–. 1 root root 208 May 10 18:19 .bashrc
drwx——. 10 root root 194 Apr 30 07:09 .cache
drwx——. 15 root root 276 Apr 30 07:09 .config
-rw-r–r–. 1 root root 100 Dec 28 2013 .cshrc
drwx——. 3 root root 25 Apr 30 06:53 .dbus
-rw——-. 1 root root 16 Apr 30 07:08 .esd_auth
drwx——. 3 root root 19 Apr 30 07:08 .local
-rw-r–r–. 1 root root 129 Dec 28 2013 .tcshrc
-rwxr-xr-x. 1 root root 8512 May 10 20:55 BP
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Desktop
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Documents
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Downloads
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Music
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Pictures
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Public
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Templates
drwxr-xr-x. 2 root root 6 Apr 30 07:08 Videos
-rw——-. 1 root root 2742 Apr 30 02:52 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 May 10 22:45 bp
-rw-r–r–. 1 root root 2750 Apr 30 06:55 initial-setup-ks.cfg
-rw——-. 1 root root 2033 Apr 30 02:52 original-ks.cfg


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM