• <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編程

            <2011年3月>
            272812345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            青青热久久国产久精品| 精品久久久久久国产91| 久久精品国产亚洲Aⅴ香蕉| 超级碰久久免费公开视频| 一本久久a久久精品综合夜夜| 91精品国产综合久久四虎久久无码一级| 无码人妻精品一区二区三区久久久 | 亚洲午夜久久久久久久久久| 久久人爽人人爽人人片AV| 久久99国产精品久久99果冻传媒| 精品久久久久久无码中文野结衣 | 伊人久久大香线蕉AV一区二区| 久久人人添人人爽添人人片牛牛| 成人妇女免费播放久久久| 国产精品免费看久久久香蕉| 久久久久波多野结衣高潮| www.久久精品| 无码久久精品国产亚洲Av影片| 天天爽天天爽天天片a久久网| 国产精品久久久久影院嫩草| 久久精品亚洲男人的天堂| 影音先锋女人AV鲁色资源网久久| 99久久精品国产一区二区蜜芽| 亚洲欧美日韩久久精品第一区| 国产精品欧美久久久久无广告| 亚洲愉拍99热成人精品热久久| 久久亚洲中文字幕精品一区| 精品精品国产自在久久高清| 久久久久久精品免费免费自慰 | 久久精品?ⅴ无码中文字幕| 久久精品国产亚洲AV无码娇色| 香蕉久久夜色精品国产2020 | 久久99国产精品久久99果冻传媒| 亚洲精品国产字幕久久不卡| 亚洲国产成人久久综合区| 久久se精品一区精品二区国产| 亚洲狠狠久久综合一区77777| 精品乱码久久久久久久| 久久亚洲精品成人AV| 狠狠色婷婷久久一区二区三区| 久久免费看黄a级毛片|