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

            鏈接

            最新隨筆

            搜索

            最新評論

            蜜臀久久99精品久久久久久| 日韩欧美亚洲综合久久| 99蜜桃臀久久久欧美精品网站| 亚洲精品久久久www| 日产精品久久久久久久| 欧美日韩精品久久免费| 久久精品国产亚洲AV无码娇色| 亚洲国产精品人久久| 亚洲日韩中文无码久久| 久久综合88熟人妻| 久久国产热这里只有精品| 久久久久波多野结衣高潮| 国产三级精品久久| 久久久免费精品re6| 国产午夜电影久久| 亚洲伊人久久精品影院| 久久se精品一区精品二区| 久久久www免费人成精品| 日本三级久久网| 国产99久久久久久免费看| 99久久夜色精品国产网站| 久久国产影院| 国产精品久久久99| 久久久久久国产精品免费无码| 亚洲国产成人久久笫一页| 亚洲国产成人久久精品影视| 久久人人爽爽爽人久久久| 三级三级久久三级久久| 亚洲欧美日韩精品久久亚洲区 | 国产精品免费久久久久影院| 久久国产欧美日韩精品| 亚洲国产精品一区二区三区久久 | 婷婷久久久亚洲欧洲日产国码AV| 久久久久国产一级毛片高清板| 国产69精品久久久久777| 国产精品永久久久久久久久久| 精品久久无码中文字幕| 97久久精品午夜一区二区| 99久久久国产精品免费无卡顿 | 91精品国产91热久久久久福利 | 国内精品久久久久国产盗摄|