• <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 天下 閱讀(554) 評論(0)  編輯 收藏 引用 所屬分類: Linux編程

            <2011年11月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            一本久久久久久久| 熟妇人妻久久中文字幕| 91麻豆精品国产91久久久久久| 久久精品国产亚洲沈樵| 国产日韩久久久精品影院首页| 久久久久国产视频电影| 国产色综合久久无码有码| 国内精品久久人妻互换| 久久精品?ⅴ无码中文字幕| 狠狠色综合网站久久久久久久高清 | 中文精品久久久久人妻不卡| 久久精品国产亚洲AV大全| 亚洲综合婷婷久久| 亚洲精品无码久久久影院相关影片| 久久婷婷五月综合97色| 综合久久一区二区三区| 亚洲成人精品久久| 国产精品99久久久精品无码| 久久最近最新中文字幕大全| 少妇久久久久久久久久| 久久亚洲精品成人无码网站| 国产精品久久久久乳精品爆| 久久国产精品99国产精| 日产精品久久久久久久| 久久久久亚洲精品中文字幕| 久久久国产精品福利免费| 久久99精品久久久久久hb无码 | 精品综合久久久久久88小说| jizzjizz国产精品久久| 久久青青色综合| 久久久久久午夜精品| 久久综合给合综合久久| 久久人人爽人爽人人爽av| 国产精品久久久天天影视香蕉 | 久久久久国产精品嫩草影院| AA级片免费看视频久久| 久久av高潮av无码av喷吹| 久久婷婷五月综合成人D啪| 久久精品无码一区二区三区日韩| 九九久久精品无码专区| 午夜精品久久影院蜜桃|