int ExecveX(PCStr(where),PCStr(path),ConstV av,ConstV ev,int flag) { const char *nav[2]; /**/ char *nev[1024]; char **oev; int rcode; BeforeExec(); endhostent(); if( av[0] == NULL ) { nav[0] = nav[1] = NULL; av = nav; } oev = (char**)ev; if( flag & EXP_NOENV ) { filterDGENV(oev,nev,elnumof(nev)); ev = nev; } if( flag & EXP_NOFDS ) { closeFds(0); } if( flag & EXP_PATH ) { environ = (char**)ev; rcode = execvp(path,(char**)av); } else { rcode = execve(path,(char**)av,(char**)ev); } execerror(where,path,rcode); return -1; }
void libmaus2::util::LogPipeMultiplexGeneric::join() { closeFds(); close(STDOUT_FILENO); close(STDERR_FILENO); int status; waitpid(pid,&status,0); }
static pid_t launchCcd(void) { LOG_INFO("start"); char* argv[] = { (char *)"/bin/ccd", (char *)NULL }; pid_t pid; pid = fork(); if (pid == 0) { closeFds(); LOG_INFO("executing /bin/ccd"); execv("/bin/ccd", argv); abort(); } LOG_INFO("CCD[pid %d] started", pid); return pid; }
libmaus2::util::LogPipeMultiplexGeneric::LogPipeMultiplexGeneric( std::string const & serverhostname, unsigned short port, std::string const & sid, uint64_t const id ) : pid(-1) { // reset stdoutpipe[0] = stdoutpipe[1] = -1; stderrpipe[0] = stderrpipe[1] = -1; // connect ::libmaus2::network::ClientSocket::unique_ptr_type tsock( new ::libmaus2::network::ClientSocket( port,serverhostname.c_str() ) ); sock = UNIQUE_PTR_MOVE(tsock); // no delay on socket sock->setNoDelay(); // write session id sock->writeString(0,sid); // id sock->writeSingle<uint64_t>(id); // connection type sock->writeString("log"); // create pipe for standard out if ( pipe(&stdoutpipe[0]) != 0 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "pipe() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } //create pipe for standard error if ( pipe(&stderrpipe[0]) != 0 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "pipe() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } // close previous standard output if ( close(STDOUT_FILENO) != 0 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "close() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } if ( close(STDERR_FILENO) != 0 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "close() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } if ( dup2(stdoutpipe[1],STDOUT_FILENO) == -1 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "dup2() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } if ( dup2(stderrpipe[1],STDERR_FILENO) == -1 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "dup2() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } pid = fork(); if ( pid < 0 ) { closeFds(); ::libmaus2::exception::LibMausException se; se.getStream() << "fork() failed: " << strerror(errno) << std::endl; se.finish(); throw se; } else if ( pid == 0 ) { // close write end close(stdoutpipe[1]); stdoutpipe[1] = -1; close(stderrpipe[1]); stderrpipe[1] = -1; // close copies close(STDOUT_FILENO); close(STDERR_FILENO); bool running = true; try { while ( running ) { running = false; fd_set fds; int maxfd = -1; FD_ZERO(&fds); if ( stdoutpipe[0] != -1 ) { FD_SET(stdoutpipe[0],&fds); maxfd = std::max(maxfd,stdoutpipe[0]); } if ( stderrpipe[0] != -1 ) { FD_SET(stderrpipe[0],&fds); maxfd = std::max(maxfd,stderrpipe[0]); } running = (maxfd != -1); if ( running ) { int r = ::select(maxfd+1,&fds,0,0,0); try { if ( r > 0 ) { if ( (stdoutpipe[0] != -1) && FD_ISSET(stdoutpipe[0],&fds) ) { ::libmaus2::autoarray::AutoArray<char> B(1024,false); ssize_t red = read(stdoutpipe[0],B.get(),B.size()); if ( red <= 0 ) { std::ostringstream errstream; errstream << "Failed to read from stdout pipe: " << strerror(errno) << std::endl; std::string errstring = errstream.str(); close(stdoutpipe[0]); stdoutpipe[0] = -1; sock->writeMessage<char>(STDERR_FILENO,errstring.c_str(),errstring.size()); sock->readSingle<uint64_t>(); } else { sock->writeMessage<char>(STDOUT_FILENO,B.get(),red); sock->readSingle<uint64_t>(); } } if ( stderrpipe[0] != -1 && FD_ISSET(stderrpipe[0],&fds) ) { ::libmaus2::autoarray::AutoArray<char> B(1024,false); ssize_t red = read(stderrpipe[0],B.get(),B.size()); if ( red <= 0 ) { std::ostringstream errstream; errstream << "Failed to read from stderr pipe: " << strerror(errno) << std::endl; std::string errstring = errstream.str(); close(stderrpipe[0]); stderrpipe[0] = -1; sock->writeMessage<char>(STDERR_FILENO,errstring.c_str(),errstring.size()); sock->readSingle<uint64_t>(); } else { sock->writeMessage<char>(STDERR_FILENO,B.get(),red); sock->readSingle<uint64_t>(); } } } } catch(std::exception const & ex) { } } } } catch(std::exception const & ex) { std::cerr << "LogPipeMultiplexGeneric " << ex.what() << std::endl; } catch(...) { std::cerr << "LogPipeMultiplexGeneric caught unknown exception." << std::endl; } try { std::ostringstream quitmsgstr; quitmsgstr << "\nLog process for id " << id << " is terminating." << std::endl; std::string const quitmsg = quitmsgstr.str(); sock->writeMessage<char>(std::max(STDOUT_FILENO,STDERR_FILENO)+1,quitmsg.c_str(),quitmsg.size()); sock->readSingle<uint64_t>(); } catch(...) { } _exit(0); } else { // close read ends close(stdoutpipe[0]); stdoutpipe[0] = -1; close(stderrpipe[0]); stderrpipe[0] = -1; } }
int main(int argc, char **argv) { char data[4]; int i, res= 0; res = inv_init_sysfs_attributes(); if (res) { printf("GT:ERR-Can't allocate mem"); return -1; } /* On Gesture/DMP supported features */ enableDMPFeatures(1); /* init Fds to poll for Gesture data */ initFds(); /* prompt user to make gesture and how to stop program */ printf("\n**Please make Gesture to see data. Press any key to stop Prog**\n\n"); do { for (i=0; i< numDMPFeatures; i++) { read(pfd[i].fd, data, 4); } poll(pfd, numDMPFeatures, POLL_TIME); for (i=0; i< numDMPFeatures; i++) { if(pfd[i].revents != 0) { switch(i) { case tap: tapHandler(); break; case flick: flickHandler(); break; case gOrient: googleOrientHandler(); break; case orient: orientHandler(); break; default: printf("GT:ERR-Not supported"); break; } pfd[i].revents= 0; //no need. reset anyway } } } while (!_kbhit()); /* Off DMP features */ enableDMPFeatures(0); /* release resources */ closeFds(); if (sysfs_names_ptr) { free(sysfs_names_ptr); } printf("\nThank You!\n"); return res; }
void MpiLauncher::launch(const vector<string>& slaveArgs, const boost::shared_ptr<const InstanceMembership>& membership, const size_t maxSlaves) { vector<string> args; { ScopedMutexLock lock(_mutex); if (_pid != 0 || _waiting) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__) << " MPI launcher is already running"; } boost::shared_ptr<Query> query = _query.lock(); Query::validateQueryPtr(query); buildArgs(args, slaveArgs, membership, query, maxSlaves); } pid_t pid = fork(); if (pid < 0) { // error int err = errno; throw (SYSTEM_EXCEPTION(SCIDB_SE_INTERNAL, SCIDB_LE_SYSCALL_ERROR) << "fork" << pid << err <<""); } else if (pid > 0) { // parent ScopedMutexLock lock(_mutex); if (_pid != 0 || _waiting) { throw InvalidStateException(REL_FILE, __FUNCTION__, __LINE__) << " MPI launcher is corrupted after launch"; } _pid = pid; LOG4CXX_DEBUG(logger, "MPI launcher process spawned, pid="<<_pid); return; } else { // child becomeProcGroupLeader(); recordPids(); setupLogging(); if (DBG) { std::cerr << "LAUNCHER pid="<<getpid() << ", pgid="<< ::getpgid(0) << ", ppid="<< ::getppid()<<std::endl; } closeFds(); boost::scoped_array<const char*> argv(new const char*[args.size()+1]); initExecArgs(args, argv); const char *path = argv[0]; if (DBG) { std::cerr << "LAUNCHER pid="<<::getpid()<<" args for "<<path<<" are ready" << std::endl; for (size_t i=0; i<args.size(); ++i) { const char * arg = argv[i]; if (!arg) break; cerr << "LAUNCHER arg["<<i<<"] = "<< argv[i] << std::endl; } } int rc = ::execv(path, const_cast<char* const*>(argv.get())); assert(rc == -1); rc=rc; // avoid compiler warning perror("LAUNCHER execv"); _exit(1); } throw SYSTEM_EXCEPTION(SCIDB_SE_INTERNAL, SCIDB_LE_UNREACHABLE_CODE); }