小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

linux下C程序插入執(zhí)行shell腳本

 quasiceo 2013-01-14

linux下C程序插入執(zhí)行shell腳本

最近在看深入理解計(jì)算機(jī)系統(tǒng),看到一個(gè)函數(shù)叫做execve(),這個(gè)函數(shù)很有意思,可以在一個(gè)進(jìn)程插入另外一個(gè)進(jìn)程執(zhí)行,但是又不像fork()一樣產(chǎn)生一個(gè)子進(jìn)程,execve()插入的進(jìn)程和原進(jìn)程共享進(jìn)程號(hào),就好像執(zhí)行這進(jìn)程就像執(zhí)行過程調(diào)用一般隨意。

函數(shù)原型如下:

int execve(const char *filename, char *const argv[], char *const envp[]);

復(fù)制代碼
EXAMPLE
       The following program is designed to be execed by the second program below.  It just echoes its command-line one per line.

           /* myecho.c */

           #include <stdio.h>
           #include <stdlib.h>

           int
           main(int argc, char *argv[])
           {
               int j;

               for (j = 0; j < argc; j++)
                   printf("argv[%d]: %s\n", j, argv[j]);

               exit(EXIT_SUCCESS);
           }

       This program can be used to exec the program named in its command-line argument:

           /* execve.c */

           #include <stdio.h>
           #include <stdlib.h>
           #include <unistd.h>

           int
           main(int argc, char *argv[])
           {
               char *newargv[] = { NULL, "hello", "world", NULL };
               char *newenviron[] = { NULL };

               if (argc != 2) {
                fprintf(stderr, "Usage: %s <file-to-exec>\n", argv[0]);
                exit(EXIT_FAILURE);
               }

               newargv[0] = argv[1];

               execve(argv[1], newargv, newenviron);
               perror("execve");   /* execve() only returns on error */
               exit(EXIT_FAILURE);
           }

       We can use the second program to exec the first as follows:

           $ cc myecho.c -o myecho
           $ cc execve.c -o execve
           $ ./execve ./myecho
           argv[0]: ./myecho
           argv[1]: hello
           argv[2]: world
復(fù)制代碼

插入一個(gè)shell腳本執(zhí)行:

復(fù)制代碼
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    char *newargv[] = { "/etc" };
    char *newenviron[] = { NULL };
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s <file-to-exec>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    newargv[0] = argv[1];

    execve(argv[1], newargv, newenviron);
    perror("execve");   /* execve() only returns on error */
    exit(EXIT_FAILURE);
}
復(fù)制代碼

script.sh如下:

#!/bin/bash
ls 

執(zhí)行:

./execve ./script.sh

會(huì)在當(dāng)前終端下輸出所有的文件

復(fù)制代碼
yca@ubuntu:~/桌面/hello$ ./execve ./script.sh 
1          execve     hello1    hello3      hello5      hello_lex
1.txt          execve.c     hello1.c  hello3.cpp  hello5.c   k_max
Bubble          hello     hello1.o  hello3.o    hello5.o   k_max.c
Bubble.c      hello.c     hello2.c  hello3.s    hello5.s   lex.yy.c
QuickSort.c   hello.lex  hello2.o  hello4      hello5.s1  script.sh
Quicksort1.c  hello.sh     hello2.s  hello4.c    hello51.s
復(fù)制代碼

很好很強(qiáng)大~~

3
0
(請(qǐng)您對(duì)文章做出評(píng)價(jià))
博主上一篇:疑問:進(jìn)程間通信
博主下一篇:文件描述符與進(jìn)程間通信之關(guān)聯(lián)
首頁上一篇:基于Heritrix+Lucene的搜索引擎構(gòu)建(1)——網(wǎng)絡(luò)蜘蛛Heritrix
首頁下一篇:選擇HttpHandler還是HttpModule?

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多