int main(int argc, char** argv) { if(init() == 0){ printf("Something went wrong, sorry..."); return 0; } int p = fork(); if(p == 0){ while(1){ process_heartbeat(); } }else{ if(p == -1){ logout(); return 1; } kill(p, SIGSTOP); set_child_pid(p); ui_main(); } return (EXIT_SUCCESS); }
static bool _FrExecProgram(const char *hostname, int portnum, int &pipe_in, int &pipe_out, istream *&stream_in, ostream *&stream_out, ostream &err, const char *progname, char **arglist) { set_child_pid(-1) ; if (progname && *progname) { bool result ; if (portnum > 0) { #ifdef FrUSING_POPEN result = popen_program(arglist,err) ; if (result) #else int sp ; sp = spawnvp(P_NOWAIT,arglist[0],(char const * const *)arglist) == 0 ; if (sp == 0) #endif /* FrUSING_POPEN */ { result = true ; // wait for the child program to contact us FrSocket sock = FrAwaitConnection(portnum, FrPOPEN_LOAD_TIMEOUT, err,false) ; if (sock != (FrSocket)INVALID_SOCKET) { pipe_in = pipe_out = sock ; stream_in = new FrISockStream(sock) ; stream_out = new FrOSockStream(sock) ; if (!stream_in || !stream_in->good()) result = false ; // the child program is supposed to send us a line identifying // itself if (FramepaC_verbose) { char line[MAX_CMDLINE] ; stream_in->getline(line,sizeof(line)) ; err << "; Connected: " << line << endl ; } else stream_in->ignore(MAX_CMDLINE,'\n') ; } else result = false ; // close the listening socket to avoid having it hang around (void)FrAwaitConnection(-1,0,err,false) ; } } else { result = fork_program(arglist,pipe_in,pipe_out) ; if (result) { stream_in = Fr_ifstream(pipe_in) ; stream_out = Fr_ofstream(pipe_out) ; } } return result ; } else return connect_to_port(hostname,portnum,pipe_in, pipe_out, stream_in,stream_out) ; }
static bool fork_program(char **arglist, int &pipe_in, int &pipe_out) { #if defined(MSDOS) || defined(__MSDOS__) || defined(__WINDOWS__) || defined(__NT__) FrWarning("Sorry, fork() is not available under MS-DOS or Windoze.\n" "Change the program's configuration to indicate a socket number\n" "other than -1 to permit the use of " #ifdef FrUSING_POPEN "popen" #else "spawn" #endif /* FrUSING_POPEN */ "(), which IS supported.") ; (void)arglist ; (void)pipe_in ; (void)pipe_out ; return false ; #else int pipe_desc[2] ; pipe_desc[0] = EOF ; pipe_desc[1] = EOF ; errno = 0 ; int pipe_stat = pipe( pipe_desc ) ; if (pipe_stat) { if (pipe_desc[0] != EOF) close(pipe_desc[0]) ; if (pipe_desc[1] != EOF) close(pipe_desc[1]) ; FrErrorVA("bad write pipe (errno=%d: %s)",errno,strerror(errno)) ; return false ; } int pipe_in_child = pipe_desc[0] ; int pipe_out_parent = pipe_desc[1] ; errno = 0 ; pipe_stat = pipe( pipe_desc ) ; if (pipe_stat) { close(pipe_in_child) ; close(pipe_out_parent) ; if (pipe_desc[0] != EOF && pipe_desc[0] != pipe_in_child) (void)close(pipe_desc[0]) ; if (pipe_desc[1] != EOF && pipe_desc[1] != pipe_out_parent) (void)close(pipe_desc[1]) ; FrErrorVA("bad read pipe (errno=%d: %s)",errno,strerror(errno)) ; return false ; } int pipe_in_parent = pipe_desc[0] ; int pipe_out_child = pipe_desc[1] ; errno = 0 ; int pid = fork() ; if (pid == -1) { close(pipe_in_parent) ; close(pipe_in_child) ; close(pipe_out_parent) ; close(pipe_out_child) ; FrErrorVA("unable to fork %s (errno=%d)",arglist[0],errno) ; return false ; } else if (pid == 0) { dup2( pipe_in_child, 0 ) ; // put the read end of the pipe on stdin dup2( pipe_out_child, 1 ) ; // put the write end of the pipe on stdout // dup2( 1, 2 ) ; // also put stderr thru the pipe if (!FramepaC_verbose) { close(2) ; open(FrNULL_DEVICE,O_WRONLY) ; } close(pipe_in_child) ; close(pipe_out_child) ; close( pipe_in_parent ) ; // close the unused ends of the pipes close( pipe_out_parent ) ; errno = 0 ; execvp( arglist[0], arglist) ; // not reached except when error FrErrorVA("couldn't exec program %s (errno=%d) -- check configuration file", arglist[0],errno) ; return false ; } else { close(pipe_in_child) ; // close the unused ends of the pipes close(pipe_out_child) ; pipe_in = pipe_in_parent ; pipe_out = pipe_out_parent ; set_child_pid(pid) ; } return true ; #endif /* __WINDOWS__ || __NT__ */ }