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

            鏈接

            最新隨筆

            搜索

            最新評論

            日韩人妻无码精品久久久不卡| 无码任你躁久久久久久久| 久久影院亚洲一区| 久久综合久久综合九色| www.久久热| 久久综合丝袜日本网| 久久er国产精品免费观看2| 69SEX久久精品国产麻豆| 狠狠色婷婷久久一区二区三区| 久久久噜噜噜www成人网| 亚洲国产精品无码成人片久久| 国产精品久久久久蜜芽| 伊人伊成久久人综合网777| 久久影视国产亚洲| 久久婷婷色综合一区二区| 国产69精品久久久久9999APGF | 99久久免费国产精品| 久久精品无码一区二区WWW| 97久久国产露脸精品国产| 亚洲香蕉网久久综合影视| 99久久精品国产高清一区二区 | 久久er国产精品免费观看2| 国产高清国内精品福利99久久| 精品人妻伦九区久久AAA片69| 久久精品成人| 亚洲国产精品无码久久久不卡| 久久婷婷五月综合色奶水99啪| 久久久国产精品福利免费 | 性做久久久久久免费观看| 中文字幕人妻色偷偷久久| 久久99热精品| 久久亚洲国产成人影院| 国内精品久久九九国产精品| 伊人久久大香线蕉精品不卡| 精品国产福利久久久| 伊人久久精品影院| 久久96国产精品久久久| 久久婷婷五月综合国产尤物app | 久久久久AV综合网成人| 亚洲伊人久久综合影院| 国产成人无码精品久久久免费|