• <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)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久国产高潮流白浆免费观看| 久久久久18| 韩国无遮挡三级久久| 亚洲成色999久久网站| 婷婷久久综合九色综合98| 久久久久这里只有精品| 国内精品伊人久久久久av一坑 | 久久99精品久久久久久噜噜| 亚洲?V乱码久久精品蜜桃 | 亚洲欧美日韩精品久久| 色婷婷久久久SWAG精品| 日产精品久久久久久久| 无码8090精品久久一区| 久久国产免费观看精品| 亚洲精品无码久久久久去q| 一级做a爰片久久毛片16| 久久综合国产乱子伦精品免费| 久久噜噜久久久精品66| 久久99中文字幕久久| 日韩AV无码久久一区二区| 久久亚洲精品无码播放| 99久久www免费人成精品 | 国产日韩久久免费影院| 欧美亚洲另类久久综合| 久久人人爽爽爽人久久久| 久久久久免费精品国产| 久久久这里有精品中文字幕| 免费观看久久精彩视频| 久久综合88熟人妻| 久久精品极品盛宴观看| 人人狠狠综合88综合久久| 久久人妻少妇嫩草AV无码蜜桃| 国产精品综合久久第一页| 久久久久久狠狠丁香| 久久精品国产亚洲欧美| 青青青青久久精品国产h| 久久国产乱子精品免费女| 亚洲国产精品一区二区久久| 久久91综合国产91久久精品| 狠狠色丁香婷综合久久| 99久久精品九九亚洲精品|