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

            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久99精品久久久大学生| 一本久久精品一区二区| 久久精品国产亚洲AV影院 | 久久人人爽人人爽人人片AV麻豆 | 久久精品国产一区二区| 99久久精品国产一区二区三区 | 久久亚洲2019中文字幕| 久久久午夜精品福利内容| 久久成人国产精品免费软件| 亚洲AV无码成人网站久久精品大| 久久久噜噜噜久久熟女AA片| 久久99精品国产麻豆宅宅| 精品久久久久久无码中文野结衣 | 人妻无码久久一区二区三区免费| 精品少妇人妻av无码久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久久久亚洲AV成人网人人软件| 亚洲色欲久久久综合网东京热| 国产精品久久久久国产A级| 7国产欧美日韩综合天堂中文久久久久| 色综合久久综合网观看| 亚洲国产一成久久精品国产成人综合 | 欧美久久久久久| 日韩久久久久久中文人妻| 久久91精品久久91综合| 久久免费视频网站| 亚洲国产日韩欧美久久| AV狠狠色丁香婷婷综合久久| 日本欧美国产精品第一页久久| 少妇久久久久久被弄高潮| 久久夜色撩人精品国产| 欧美久久精品一级c片片| 亚洲AV日韩精品久久久久久久| 精品久久久久国产免费| 久久国产精品无码一区二区三区| 成人亚洲欧美久久久久| 久久国产精品久久| 亚洲国产成人久久一区WWW| 精品久久久久久无码专区不卡| 久久青青草视频| 一本色道久久88综合日韩精品|