• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            linux pexec函數

            #include <unistd.h>
            #include 
            <sys/mman.h>
            #include 
            <sys/types.h>
            #include 
            <fcntl.h>
            #include 
            <stdio.h>
            #include 
            <string.h>
            #include 
            <stdlib.h>
            #include 
            <sys/wait.h>

            int pexec(const char* command,int* result)
            {
                
            int pid;
                
            int fds[2= {-1,-1};
                
            int status = -1;

                
            if(pipe(fds) < 0
                {
                    
            return -1;
                }

                
            if( (pid=vfork()) < 0)
                {
                    close(fds[
            0]);
                    close(fds[
            1]);
                    
            return -1;
                }

                
            if (pid == 0 )
                {
                    dup2(fds[
            1],1);
                    close(fds[
            0]);
                    close(fds[
            1]);
                    execl(
            "/bin/sh""sh""-c", command,NULL);
                    _exit(
            -1);
                }
                
            else
                {
                    waitpid(pid,
            &status,0);
                    close(fds[
            1]);
                    
            if (WIFEXITED(status)==0
                    {
                        close(fds[
            0]);
                        
            return -1;
                    }
                    status  
            = WEXITSTATUS(status);
                    
            if (result!=NULL) 
                    {
                        
            *result = status;
                    }
                    
            //printf("pid(%d) exited,status:%d \r\n",pid,status);
                    return fds[0];
                } 
            }

            int main(int argc,char* argv[]) 
            {
                
            if (argc<2)
                {
                    printf(
            "Usage:%s <cmd> \r\n",argv[0]);
                    
            return -1;
                }
                
            int status = -1;
                
            int fd = pexec(argv[1],&status);
                
            if (fd==-1)
                {
                    printf(
            "pexec(%s) error \r\n",argv[1]);
                    
            return -1;
                }
                
            char buf[4097];
                
            int readed = 0;
                
            while( (readed = read(fd,buf,4096))>0)
                {
                    buf[readed] 
            = 0;
                    printf(
            "readed:%d\r\n%s",readed,buf);
                }
                close(fd);
                
            return 0;
            }

            posted on 2012-08-10 09:23 天下 閱讀(560) 評論(0)  編輯 收藏 引用 所屬分類: Linux編程

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            亚洲а∨天堂久久精品| 九九热久久免费视频| 2021国产精品久久精品| 区久久AAA片69亚洲| 久久99精品久久久久婷婷| 久久99精品久久只有精品| 久久精品国产WWW456C0M| 青青草原综合久久大伊人| www性久久久com| 久久国产AVJUST麻豆| 国产69精品久久久久777| 一本色道久久综合狠狠躁篇| 国产精品久久久天天影视| 看全色黄大色大片免费久久久| 人妻无码αv中文字幕久久| 久久国产影院| 久久不射电影网| 蜜臀久久99精品久久久久久小说| 麻豆国内精品久久久久久| 久久久久中文字幕| 精品久久久久久国产潘金莲| 日韩美女18网站久久精品| 一本一道久久精品综合| 久久ZYZ资源站无码中文动漫| 精品国产乱码久久久久软件| 久久国产精品波多野结衣AV| 国产精品久久久久久影院| 久久精品一本到99热免费| 免费精品久久久久久中文字幕| 日韩精品久久久久久| 久久青草国产精品一区| 2022年国产精品久久久久| 久久夜色精品国产噜噜亚洲AV| 久久亚洲国产精品成人AV秋霞| 久久久久综合国产欧美一区二区| 免费观看久久精彩视频| 久久99热精品| 日韩电影久久久被窝网| 一本色综合久久| 日本久久久久亚洲中字幕 | 91精品国产高清91久久久久久|