int my_open(const char* pathname, int mode) { int fd, sockfd[2], status; pid_t childpid; char c, argsockfd[10], argmode[10]; Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); if ((childpid = Fork()) == 0) { /* child process */ Close(sockfd[0]); snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]); snprintf(argmode, sizeof(argmode), "%d", mode); execl("./openfile", "openfile", argsockfd, pathname, argmode, (char*)NULL); err_sys("execl error"); } /* parent process - wait for the child to terminate */ Close(sockfd[1]); /* close the end we don't use */ Waitpid(childpid, &status, 0); if (WIFEXITED(status) == 0) err_quit("child did not terminate"); if ((status = WEXITSTATUS(status)) == 0) Read_fd(sockfd[0], &c, 1, &fd); else { errno = status; /* set errno value from child's status */ fd = -1; } Close(sockfd[0]); return (fd); }
int connectToSftp(const char *host,int port,const char *user,int fdc,int fdv[]){ int socks[2]; socks[1] = CC_connect("sftp",host,port,user); if( 0 <= socks[1] ){ DEBUG("---- SFTPCC HIT[%d] %s@%s:%d\n",socks[1],user,host,port); if( !sftpIsAlive(socks[1]) ){ close(socks[1]); }else return socks[1]; } DEBUG("---- SFTPCC MISS %s@%s:%d\n",user,host,port); Socketpair(socks); if( Fork("SftpGW") == 0 ){ int fi; for( fi = 0; fi < fdc; fi++ ) close(fdv[fi]); close(socks[1]); closeServPorts(); signal(SIGINT,sigTERM); signal(SIGTERM,sigTERM); SftpGW(host,port,socks[0]); _exit(0); } close(socks[0]); return socks[1]; }
int my_open(const char *pathname, int mode) { int fd, sockfd[2], status; pid_t childpid; char c, argsockfd[10], argmode[10]; Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); //child process if ((childpid = Fork()) == 0) { Close(sockfd[0]); snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]); /* what is in sockfd[1] */ snprintf(argmode, sizeof(argmode), "%d", mode); execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *)NULL); err_sys("execl error"); } //parent process Close(sockfd[1]); Waitpid(childpid, &status, 0); if (WIFEXITED(status) == 0) err_quit("child did not terminate"); if ((status = WEXITSTATUS(status)) == 0) read_fd(sockfd[0], &c, 1, &fd); else { errno = status; fd = -1; } Close(sockfd[0]); return (fd); }
FILE *openFilter(Connection *Conn,PCStr(fname),iFUNCP func,FILE *out,PCStr(args)) { int pid; int toc[2]; FILE *ioin; if( lSINGLEP() ){ FILE *ioout; Connection *dupConn; extern int BREAK_STICKY; BREAK_STICKY = 1; Socketpair(toc); /* Socketpair(fth.f_sync); */ ioin = fdopen(toc[0],"r"); ioout = fdopen(toc[1],"w"); dupConn = (Connection*)malloc(sizeof(Connection)); *dupConn = *Conn; fth.f_Conn = dupConn; fth.f_ioin = ioin; fth.f_ptid = STX_tid; /* fth.f_tid = thread_fork(0x80000,STX_tid,"threadFilter",(IFUNCP)threadFilter, */ fth.f_tid = thread_fork(0x100000,STX_tid,"threadFilter",(IFUNCP)threadFilter, dupConn,fname,func,ioin,out,args); return ioout; } /* pipe(toc); */ pipeX(toc,8*1024); /* if( INHERENT_fork() ){ */ /* if( INHERENT_fork() && threadSafeFork() ){ */ if( INHERENT_fork() && threadSafeFork() && !lEXECFILTER() ){ /* if( Fork("openFilter") == 0 ){ */ /* if( (pid = Fork("openFilter")) == 0 ){ */ if( (pid = ForkX("openFilter",xproc)) == 0 ){ close(toc[1]); ioin = fdopen(toc[0],"r"); (*func)(Conn,ioin,out,args); Finish(0); } }else{ fflush(out); pid = spawnFilter(Conn,1,toc,fileno(out),func,args); } Conn->fi_pid = pid; close(toc[0]); return fdopen(toc[1],"w"); }
int main(int argc, char **argv) { if (argc != 1) err_quit("usage: qlen"); Socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd); bzero(&serv, sizeof(serv)); serv.sin_family = AF_INET; serv.sin_port = htons(PORT); Inet_pton(AF_INET, ADDR, &serv.sin_addr); if ( (pid = Fork()) == 0) do_child(); else do_parent(); exit(0); }
pid_t child_make(int i, int listenfd, int addrlen) { int sockfd[2]; pid_t pid; void child_main(int, int, int); Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); if ( (pid = Fork()) > 0) { Close(sockfd[1]); cptr[i].child_pid = pid; cptr[i].child_pipefd = sockfd[0]; cptr[i].child_status = 0; return(pid); /* parent */ } Dup2(sockfd[1], STDERR_FILENO); /* child's stream pipe to parent */ Close(sockfd[0]); Close(sockfd[1]); Close(listenfd); /* child does not need this open */ child_main(i, listenfd, addrlen); /* never returns */ }