int do_command_upload_sandbox(void *arg, Stream*) { dprintf(D_ALWAYS, "FTGAHP: upload sandbox\n"); Gahp_Args args; parse_gahp_command ((char*)arg, &args); // first two args: result id and sandbox id: std::string rid = args.argv[1]; std::string sid = args.argv[2]; // third arg: job ad ClassAd ad; classad::ClassAdParser my_parser; if (!(my_parser.ParseClassAd(args.argv[3], ad))) { // FAIL write_to_pipe( ChildErrorPipe, "Failed to parse job ad" ); return 1; } // rewrite the IWD to the actual sandbox dir std::string iwd; define_sandbox_path(sid, iwd); ad.Assign(ATTR_JOB_IWD, iwd.c_str()); char ATTR_SANDBOX_ID[] = "SandboxId"; ad.Assign(ATTR_SANDBOX_ID, sid.c_str()); // directory was created, let's set up the FileTransfer object FileTransfer ft; if (!ft.Init(&ad)) { // FAIL write_to_pipe( ChildErrorPipe, "Failed to initialize FileTransfer" ); return 1; } // lookup ATTR_VERSION and set it. this changes the wire // protocol and it is important that this happens before // calling UploadFiles. char* peer_version = NULL; ad.LookupString(ATTR_VERSION, &peer_version); ft.setPeerVersion(peer_version); free (peer_version); dprintf(D_ALWAYS, "BOSCO: calling upload files\n"); // the "true" param to UploadFiles here means blocking (i.e. "in the foreground") if (!ft.UploadFiles(true)) { // FAIL write_to_pipe( ChildErrorPipe, ft.GetInfo().error_desc.Value() ); return 1; } // SUCCEED return 0; }
int main(void) { pid_t pid; int mypipe[2]; /* Create the pipe. */ if (pipe(mypipe)) { write(STDOUT_FILENO, ERR_PIPE, sizeof(ERR_PIPE) - 1); return -1; } /* Create the child process. */ pid = fork(); if (pid == (pid_t) 0) { /* This is the child process. Close other end first. */ close(mypipe[1]); read_from_pipe(mypipe[0]); return -1; } else if (pid < (pid_t) 0) { /* The fork failed. */ write(STDOUT_FILENO, ERR_FORK, sizeof(ERR_FORK) - 1); return -1; } else { /* This is the parent process. Close other end first. */ close(mypipe[0]); write_to_pipe(mypipe[1]); return -1; } }
/**************************************************************************** reply to a write on a pipe ****************************************************************************/ int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize, int threadid) { pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0, threadid); uint32 numtowrite = SVAL(inbuf,smb_vwv1); int nwritten; int outsize; char *data; if (!p) return(ERROR(ERRDOS,ERRbadfid)); data = smb_buf(inbuf) + 3; if (numtowrite == 0) nwritten = 0; else nwritten = write_to_pipe(p, data, numtowrite); if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) //0507 return (UNIXERROR(ERRDOS,ERRnoaccess)); return 0; outsize = set_message(outbuf,1,0,True); SSVAL(outbuf,smb_vwv0,nwritten); //0507 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", //0507 p->pnum, nwritten)); return(outsize); }
int main(void) { int fd[2]; pid_t pid; int stat_val; if(pipe(fd)) { printf("creat pipe failed!\n"); exit(1); } pid = fork(); switch(pid) { case -1: printf("fork error!\n"); exit(1); case 0: //子进程关闭fd0 close(fd[0]); write_to_pipe(fd[1]); exit(0); default: //父进程关闭fd1 close(fd[1]); read_from_pipe(fd[0]); wait(&stat_val); exit(0); } return 0; }
int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize) { smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2); uint16 vuid = SVAL(inbuf,smb_uid); size_t numtowrite = SVAL(inbuf,smb_vwv10); int nwritten = -1; int smb_doff = SVAL(inbuf, smb_vwv11); BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) == (PIPE_START_MESSAGE|PIPE_RAW_MODE)); char *data; if (!p) { return(ERROR_DOS(ERRDOS,ERRbadfid)); } if (p->vuid != vuid) { return ERROR_NT(NT_STATUS_INVALID_HANDLE); } data = smb_base(inbuf) + smb_doff; if (numtowrite == 0) { nwritten = 0; } else { if(pipe_start_message_raw) { /* * For the start of a message in named pipe byte mode, * the first two bytes are a length-of-pdu field. Ignore * them (we don't trust the client). JRA. */ if(numtowrite < 2) { DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n", (unsigned int)numtowrite )); return (UNIXERROR(ERRDOS,ERRnoaccess)); } data += 2; numtowrite -= 2; } nwritten = write_to_pipe(p, data, numtowrite); } if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) { return (UNIXERROR(ERRDOS,ERRnoaccess)); } set_message(outbuf,6,0,True); nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten); SSVAL(outbuf,smb_vwv2,nwritten); DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten)); return chain_reply(inbuf,outbuf,length,bufsize); }
//#if 0 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize, int ProcSockID, int threadid) { pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2, threadid); uint32 numtowrite = SVAL(inbuf,smb_vwv10); int nwritten = -1; int smb_doff = SVAL(inbuf, smb_vwv11); int pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) == (PIPE_START_MESSAGE|PIPE_RAW_MODE)); char *data; if (!p) return(ERROR(ERRDOS,ERRbadfid)); data = smb_base(inbuf) + smb_doff; if (numtowrite == 0) nwritten = 0; else { if(pipe_start_message_raw) { /* * For the start of a message in named pipe byte mode, * the first two bytes are a length-of-pdu field. Ignore * them (we don't trust the client. JRA. */ if(numtowrite < 2) { //0507 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n", //0507 (unsigned int)numtowrite )); //0507 return (UNIXERROR(ERRDOS,ERRnoaccess)); return 0; } data += 2; numtowrite -= 2; } nwritten = write_to_pipe(p, data, numtowrite); } if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) //0507 return (UNIXERROR(ERRDOS,ERRnoaccess)); return 0; set_message(outbuf,6,0,True); nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten); SSVAL(outbuf,smb_vwv2,nwritten); //0507 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", //0507 p->pnum, nwritten)); return chain_reply(inbuf,outbuf,length,bufsize, ProcSockID, threadid); }
void Server::manipulateData(string data, socket_ptr sock){ shm->mutexThread.lock(); /* Send data from client to fork by PIPE */ write_to_pipe(data); shm->mutexProcess.post(); shm->mutexSend.wait(); string res = read_from_pipe(); boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(res), error); cout << "Message answered!" << endl; shm->mutexAnswer.post(); }
int handle_output (ACE_HANDLE handle) { ACE_UNUSED_ARG (handle); ACE_DEBUG ((LM_DEBUG, "Different_Handler::handle_output\n")); // Add for reading int result = this->reactor ()->mask_ops (this, ACE_Event_Handler::READ_MASK, ACE_Reactor::ADD_MASK); ACE_ASSERT (result != -1); ACE_Reactor_Mask old_masks = ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; ACE_ASSERT (old_masks == static_cast<ACE_Reactor_Mask> (result)); ACE_UNUSED_ARG (old_masks); // Get new masks result = this->reactor ()->mask_ops (this, ACE_Event_Handler::NULL_MASK, ACE_Reactor::GET_MASK); ACE_ASSERT (result != -1); ACE_Reactor_Mask current_masks = ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; ACE_ASSERT (current_masks == static_cast<ACE_Reactor_Mask> (result)); ACE_UNUSED_ARG (current_masks); // Remove for writing ACE_Reactor_Mask mask = ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::DONT_CALL; result = this->reactor ()->remove_handler (this, mask); ACE_ASSERT (result == 0); // Write to the pipe; this causes handle_input to get called. if (!write_to_pipe_in_main) write_to_pipe (this->pipe_); return 0; }
int handle_output (ACE_HANDLE handle) { ACE_DEBUG ((LM_DEBUG, "Handler::handle_output\n")); // Optionally cancel reads if (cancel_reads) { int result = ACE_Reactor::instance ()->cancel_wakeup (this, ACE_Event_Handler::READ_MASK); ACE_ASSERT (result != -1); } // Write to the pipe; this causes handle_input to get called. if (!write_to_pipe_in_main) write_to_pipe (this->pipe_); // Remove for writing return -1; }
int main() { #ifdef FORKING_ENABLED int pipefd[2]; pipe(pipefd); pid_t child = fork(); if (child == 0) { close(pipefd[0]); #ifdef TRACING_ENABLED ptrace(PTRACE_TRACEME, 0, NULL, NULL); raise(SIGSTOP); #endif #endif SqlModelling modelling; std::vector<std::string> tortured = modelling.process(); #ifdef FORKING_ENABLED write_to_pipe(pipefd[1], tortured); close(pipefd[1]); } else { close(pipefd[1]); #ifdef TRACING_ENABLED std::vector<std::string> files; std::mutex mutex; std::thread thread(reader_func, pipefd[0], std::ref(files), std::ref(mutex)); trace(child, files, mutex); close(pipefd[0]); thread.join(); #else for (;;) { int status; waitpid(child, &status, __WNOTHREAD); if (WIFEXITED(status)) { break; } } #endif } #endif return 0; }
int main(int argc, char** argv) { pid_t pid; int mypipe[2]; /* Create the pipe. */ printf("Create Pipe in parent process!\n"); if (pipe(mypipe)) { fprintf(stderr, "Pipe failed.\n"); return EXIT_FAILURE; } printf("Fork this process now!\n"); pid = fork(); if (pid == (pid_t) 0) { /*child*/ printf("This is the child process!\n"); printf("%d\n", pid); /* This is the child process. Close other end first. */ close(mypipe[1]); printf("Read from Pipe!\n"); read_from_pipe(mypipe[0]); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { /* The fork failed. */ fprintf(stderr, "Fork failed.\n"); return EXIT_FAILURE; } else { printf("This is the parent process!\n"); printf("%d\n", pid); /* This is the parent process. Close other end first. */ close(mypipe[0]); printf("Write to Pipe!\n"); write_to_pipe(mypipe[1]); return EXIT_SUCCESS; } }
int main (void) { pid_t pid; int mypipe[2]; /*@group*/ /* Create the pipe. */ if (pipe (mypipe)) { fprintf (stderr, "Pipe failed.\n"); return EXIT_FAILURE; } /*@end group*/ /* Create the child process. */ pid = fork (); if (pid == (pid_t) 0) { /* This is the child process. Close other end first. */ close (mypipe[1]); read_from_pipe (mypipe[0]); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { /* The fork failed. */ fprintf (stderr, "Fork failed.\n"); return EXIT_FAILURE; } else { /* This is the parent process. Close other end first. */ close (mypipe[0]); write_to_pipe (mypipe[1]); return EXIT_SUCCESS; } }
int do_command_destroy_sandbox(void *arg, Stream*) { dprintf(D_ALWAYS, "FTGAHP: destroy sandbox\n"); Gahp_Args args; parse_gahp_command ((char*)arg, &args); // first two args: result id and sandbox id: std::string rid = args.argv[1]; std::string sid = args.argv[2]; std::string err; if(!destroy_sandbox(sid, err)) { // FAIL dprintf( D_ALWAYS, "BOSCO: destroy_sandbox error: %s\n", err.c_str()); write_to_pipe( ChildErrorPipe, err.c_str() ); return 1; } // SUCCEED return 0; }
void reply_pipe_write(struct smb_request *req) { smb_np_struct *p = get_rpc_pipe_p(SVAL(req->inbuf,smb_vwv0)); size_t numtowrite = SVAL(req->inbuf,smb_vwv1); int nwritten; char *data; if (!p) { reply_doserror(req, ERRDOS, ERRbadfid); return; } if (p->vuid != req->vuid) { reply_nterror(req, NT_STATUS_INVALID_HANDLE); return; } data = smb_buf(req->inbuf) + 3; if (numtowrite == 0) { nwritten = 0; } else { nwritten = write_to_pipe(p, data, numtowrite); } if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) { reply_unixerror(req, ERRDOS, ERRnoaccess); return; } reply_outbuf(req, 1, 0); SSVAL(req->outbuf,smb_vwv0,nwritten); DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten)); return; }
int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize) { smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0); uint16 vuid = SVAL(inbuf,smb_uid); size_t numtowrite = SVAL(inbuf,smb_vwv1); int nwritten; int outsize; char *data; if (!p) { return(ERROR_DOS(ERRDOS,ERRbadfid)); } if (p->vuid != vuid) { return ERROR_NT(NT_STATUS_INVALID_HANDLE); } data = smb_buf(inbuf) + 3; if (numtowrite == 0) { nwritten = 0; } else { nwritten = write_to_pipe(p, data, numtowrite); } if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) { return (UNIXERROR(ERRDOS,ERRnoaccess)); } outsize = set_message(outbuf,1,0,True); SSVAL(outbuf,smb_vwv0,nwritten); DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten)); return(outsize); }
int main (int argc, char *argv[]) { int result = 0; // Parse args ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("swmc"), 1); for (int c; (c = getopt ()) != -1; ) switch (c) { case 's': opt_select_reactor = 1; break; case 'w': opt_wfmo_reactor = 1; break; case 'm': write_to_pipe_in_main = 1; break; case 'c': cancel_reads = 1; break; } // Create pipes ACE_Pipe pipe1, pipe2; result = pipe1.open (); ACE_ASSERT (result == 0); result = pipe2.open (); ACE_ASSERT (result == 0); // Create handlers Handler handler (pipe1); Different_Handler different_handler (pipe2); // Create reactor create_reactor (); // Manage memory automagically. auto_ptr<ACE_Reactor> reactor (ACE_Reactor::instance ()); auto_ptr<ACE_Reactor_Impl> impl; // If we are using other that the default implementation, we must // clean up. if (opt_select_reactor || opt_wfmo_reactor) impl = auto_ptr<ACE_Reactor_Impl> (ACE_Reactor::instance ()->implementation ()); // Register handlers ACE_Reactor_Mask handler_mask = ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; ACE_Reactor_Mask different_handler_mask = ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; result = ACE_Reactor::instance ()->register_handler (&handler, handler_mask); ACE_ASSERT (result == 0); result = ACE_Reactor::instance ()->register_handler (&different_handler, different_handler_mask); ACE_ASSERT (result == 0); // Write to the pipe; this causes handle_input to get called. if (write_to_pipe_in_main) { write_to_pipe (pipe1); write_to_pipe (pipe2); } // Note that handle_output will get called automatically since the // pipe is writable! // Run for three seconds ACE_Time_Value time (3); ACE_Reactor::instance ()->run_event_loop (time); ACE_DEBUG ((LM_DEBUG, "\nClosing down the application\n\n")); return 0; }
int main(int c, char *argv[]) { // pgrep other procnannies applications ... // for each one, if it's not your current pid, kill process // there's a key combination which sends a hangup /* DO THIS AT THE BEGINNING OF THE ASSIGNMENT*/ signal(SIGHUP, ignore_function); signal(SIGINT, catch_function); deleteProcnannies(); getParentPID(); initialisationOP(); const char* s = getenv("PROCNANNYLOGS"); FILE* logfile = fopen(s, "w"); fclose(logfile); FILE* file = fopen ( argv[1], "r" ); if (file == NULL) { printf("ERROR: Nanny.config file presented was not found. Program exiting..."); exit(EXIT_FAILURE); } int counter = 0; char * pch; int countval = 0; if (file != NULL) { char line[100]; while (fgets(line, sizeof line, file) != NULL) { /* read a line from a file */ // reads sample text: testa 120 strcpy(test[counter - 1], strtok(line, "\n")); pch = strtok (test[counter-1]," ,.-"); while (pch != NULL) { if (countval == 0) { strcpy(appdata[counter], pch); countval++; } else if (countval == 1) { timedata[counter] = atoi(pch); countval = 0; break; } pch = strtok (NULL, " ,.-"); } counter++; } } fclose(file); FILE* f[1000]; // may have to change this, original value counter - 1 ; need to realloc then. int i; int totalProcessCounter = 0; pid_t pid; int fd[counter][2][2]; for (i = 0; i < counter; i++) { if (appdata[i][0] != '\0') { //printf("Application Testing: %s\n", test[i]); char grepip[1000]; strcpy(grepip, "pgrep "); strcat(grepip, appdata[i]); if ( ( f[i] = popen( grepip, "r" ) ) == NULL ) { perror( "popen" ); } else { char pidval[150]; pid_t pidint; int haspid = 0; if (pipe(fd[i][PARENT]) < 0) { exit(EXIT_FAILURE); } if (pipe(fd[i][CHILD]) < 0) { exit(EXIT_FAILURE); } while (fgets(pidval, 150, f[i]) != NULL) { haspid = 1; pid = fork(); if (pid < 0) { // error process fprintf(stderr, "can't fork, error %d\n", errno); exit(EXIT_FAILURE); } else if (pid > 0) { // parent process totalProcessCounter = totalProcessCounter + 1; } else if (pid == 0 ) { // child process signal(SIGINT, fail_function); strcpy(appProcessed, appdata[i]); timeProcessed = timedata[i]; childMonitoring:; int count = 0; char buff[1000]; bzero(buff, 1000); char byte = 0; strtok(pidval, "\n"); initProcOP(appProcessed, pidval); pidint = (pid_t) strtol(pidval, NULL, 10); fcntl(fd[i][PARENT][READ], F_SETFL, O_NONBLOCK); int sleepLeft = timeProcessed; while(sleepLeft > 0) { sleep(5); // Need to start the reading at every 5s break; if its (-1) then end the program, if its new pid then clear pid and // write to inform the child is busy. while (SIFLAG == 1 || read(fd[i][PARENT][READ], &byte, 1) == 1) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } if (ioctl(fd[i][PARENT][READ], FIONREAD, &count) != -1) { buff[0] = byte; char printWrite[150]; if (read(fd[i][PARENT][READ], buff+1, count) == count) { int charbuf = atoi(buff); if (charbuf == 1) { sprintf(printWrite, "2"); // sends for onwait process write_to_pipe(fd[i][CHILD][WRITE], printWrite); } else if (charbuf == -1) { exit(EXIT_FAILURE); } } } } // inform busy ... sleepLeft = sleepLeft - 5; } pidint = (pid_t) strtol(pidval, NULL, 10); char timeStr[30]; sprintf(timeStr, "%d", timeProcessed); char prntChild[150]; int killresult = kill(pidint, SIGKILL); if (killresult == 0) { //printf("You killed the process (PID: %d) (Application: %s)\n", pidint, test[i] ); pidKilledOP(pidval, appProcessed, timeStr); sprintf(prntChild, "1 %s", appProcessed); } else if (killresult == -1) { //printf("ERROR: Process already killed (PID: %d) (Application: %s)\n", pidint, test[i] ); sprintf(prntChild, "0 %s", appProcessed); } //pclose(f[i]); write_to_pipe(fd[i][CHILD][WRITE], prntChild); // writing to parent that is polling int ops; ops = fcntl(fd[i][PARENT][READ],F_GETFL); // reenable blocking fcntl(fd[i][PARENT][READ], F_SETFL, ops & ~O_NONBLOCK); while (SIFLAG == 1 || read(fd[i][PARENT][READ], &byte, 1) == 1) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } if (ioctl(fd[i][PARENT][READ], FIONREAD, &count) != -1) { buff[0] = byte; if (read(fd[i][PARENT][READ], buff+1, count) == count) { int bufval = atoi(buff); if (bufval == -1) { goto waitingProc; } else if (bufval == 1) { // new process started! char prntReady[150]; sprintf(prntReady, "1"); // sends for onwait process write_to_pipe(fd[i][CHILD][WRITE], prntReady); // NEEDS TO WAIT @ THIS POINT while (SIFLAG == 1 || read(fd[i][PARENT][READ], &byte, 1) == 1) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } if (ioctl(fd[i][PARENT][READ], FIONREAD, &count) != -1) { buff[0] = byte; if (read(fd[i][PARENT][READ], buff+1, count) == count) { char * bch; int countvalb = 0; // read the buff value // parse buff into 2 parts; bch = strtok (buff," ,.-"); while (bch != NULL) { if (countvalb == 0) { strcpy(appProcessed, bch); countvalb++; } else if (countvalb == 1) { timeProcessed = atoi(bch); countvalb = 0; strcpy(grepip, "pgrep "); strcat(grepip, appProcessed); if ( ( f[i] = popen( grepip, "r" ) ) == NULL ) { perror( "popen" ); } else { if (fgets(pidval, 150, f[i]) != NULL) { goto childMonitoring; } else { noProcessOP(appdata[i]); goto waitingProc; } } } bch = strtok (NULL, " ,.-"); } } } } } } } } goto waitingProc; } } if (haspid == 0) { //printf("No processes found for %s.\n", test[i]); noProcessOP(appdata[i]); } } //pclose(f[i]); } } int k; int signum = 0; char buff[1000]; bzero(buff, 1000); char byte = 0; int count = 0; int h = 0; int pidstatus; char * bch; int countvalb = 0; int countProcCompleted = 0; if (totalProcessCounter == 0) { goto completeProcess; } parentMonitoring:; for (k = 0; k < totalProcessCounter; k++ ) { fcntl(fd[k][CHILD][READ], F_SETFL, O_NONBLOCK); } k = 0; while(1) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } if (k < totalProcessCounter) { k++; } else { k = 0; } if (SHFLAG == 1 || read(fd[k][CHILD][READ], &byte, 1) == 1) { // if the ioctl is -1 then switch to the next k value... if (SHFLAG == 1) { // REREAD FILE int validChild = 0; char switchProc[1000]; char buff2[1000]; bzero(buff2, 1000); char byte2 = 0; char test2[280][1000]; int count2 = 0; consoleOP("Info: Caught SIGHUP. Configuration file 'nanny.config' re-read."); genericOP("Info: Caught SIGHUP. Configuration file 'nanny.config' re-read."); // if there's a new program then search, so search w/ respect to the current appname list FILE* file2 = fopen ( argv[1], "r" ); if (file2 != NULL) { char line [1000]; while (fgets(line, sizeof line, file2) != NULL) { // read a line from a file // reads sample text: testa 120 validChild = 0; counter = totalProcessCounter + 1; strcpy(test2[counter - 1], strtok(line, "\n")); pch = strtok (test2[counter-1]," ,.-"); int validAppData = 0; while (pch != NULL) { if (countval == 0) { int m; for (m = 0; m < totalProcessCounter; m++) { validAppData = 0; if (strcmp(appdata[m], pch) == 0) { validAppData = 1; } } if (validAppData == 0) { strcpy(appdata[counter], pch); countval++; pch = strtok (NULL, " ,.-"); } } if (countval == 1 && validAppData == 0) { timedata[counter] = atoi(pch); // this is where you query, because everything is cleared. for (h = 0; h < totalProcessCounter; h++) { //h = h + 1; // sync w child process? if (validChild == 0) { sprintf(switchProc, "1"); write_to_pipe(fd[h][PARENT][WRITE], switchProc); /* int ops; ops = fcntl(fd[h][CHILD][READ],F_GETFL); // reenable blocking fcntl(fd[h][CHILD][READ], F_SETFL, ops & ~O_NONBLOCK); */ while (SIFLAG == 1 || read(fd[h][CHILD][READ], &byte2, 1) == 1) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } if (ioctl(fd[h][CHILD][READ], FIONREAD, &count2) != -1) { buff2[0] = byte2; if (SIFLAG == 1 || read(fd[h][CHILD][READ], buff2+1, count2) == count2) { if (SIFLAG == 1) { //setup to prevent early completion via sighup... sigintProcnannies(); goto completeProcess; } int tmpinitval = atoi(buff2); if (tmpinitval == 2) { fcntl(fd[h][CHILD][READ], F_SETFL, O_NONBLOCK); } else if (tmpinitval == 1) { // pass all the variables required here. // the child can: a. pass the variables and simply open a new process to monitor, or // b. reset the process UP THERE and rerun the forked process - which is probably better imo... char idToMonitor[150]; sprintf(idToMonitor, "%s %d", appdata[counter], timedata[counter]); write_to_pipe(fd[h][PARENT][WRITE], idToMonitor); validChild = 1; fcntl(fd[h][CHILD][READ], F_SETFL, O_NONBLOCK); } else { // it got a bad value in the pipe... send it back! write_to_pipe(fd[h][CHILD][READ], buff2); fcntl(fd[h][CHILD][READ], F_SETFL, O_NONBLOCK); } } } } fcntl(fd[h][CHILD][READ], F_SETFL, O_NONBLOCK); } } if (validChild == 0) { // FORK PROCESS HERE @ THIS POINT. HAVE TO FORK...? // set the i value (counter val) as the one you're using // fork here; the child is redirected to the child process UP THERE // the parent is just going to pass and do nothing i guess. char grepip[1000]; strcpy(grepip, "pgrep "); strcat(grepip, appdata[counter]); if ( ( f[counter] = popen( grepip, "r" ) ) == NULL ) { perror( "popen" ); } else { char pidval[150]; bzero(pidval, 150); if (pipe(fd[counter][PARENT]) < 0) { exit(EXIT_FAILURE); } if (pipe(fd[counter][CHILD]) < 0) { exit(EXIT_FAILURE); } fcntl(fd[counter][CHILD][READ], F_SETFL, O_NONBLOCK); while (fgets(pidval, 150, f[counter]) != NULL) { pid = fork(); if (pid < 0) { // error process fprintf(stderr, "can't fork, error %d\n", errno); exit(EXIT_FAILURE); } else if (pid > 0) { // parent process totalProcessCounter = totalProcessCounter + 1; } else if (pid == 0 ) { // child process signal(SIGINT, fail_function); strcpy(appProcessed, appdata[counter]); timeProcessed = timedata[counter]; goto childMonitoring; } } } } } countval = 0; break; } counter++; } } fclose(file2); SHFLAG = 0; goto parentMonitoring; } if (ioctl(fd[k][CHILD][READ], FIONREAD, &count) == -1) { buff[0] = byte; if (k < totalProcessCounter) { k++; } else { k = 0; } } else if (ioctl(fd[k][CHILD][READ], FIONREAD, &count) != -1) { //buff = malloc(count+1); //bzero(buff, count+1); buff[0] = byte; if (read(fd[k][CHILD][READ], buff+1, count) == count) { bch = strtok (buff," ,.-"); while (bch != NULL) { if (countvalb == 0) { pidstatus = atoi(bch); if (pidstatus == 1) { countProcCompleted++; strcpy(appdata[k], ""); signum++; } else if (pidstatus == 0) { countProcCompleted++; } countvalb++; } else if (countvalb == 1) { char procCompleted[150]; strcpy(procCompleted, bch); int p; for (p = 0; p < (sizeof(appdata) / sizeof(appdata[0])); p++) { if (strcmp(appdata[p], procCompleted) == 0) { //matches strcpy(appdata[p], ""); } } countvalb = 0; } bch = strtok (NULL, " ,.-"); } } if (countProcCompleted == totalProcessCounter) { goto parentMonitoring; } k++; } if (countProcCompleted == totalProcessCounter) { goto parentMonitoring; } } } completeProcess: //consoleOP("Operations have concluded for this process (all iterations have gone through)."); killProcessOP(signum); return 0; waitingProc: for (k = 0; k < totalProcessCounter; k++) { close(fd[k][PARENT][READ]); close(fd[k][PARENT][WRITE]); close(fd[k][CHILD][READ]); close(fd[k][CHILD][WRITE]); } return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { int result = 0; //FUZZ: disable check_for_lack_ACE_OS // Parse args ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("swmc"), 1); for (int c; (c = getopt ()) != -1; ) //FUZZ: enable check_for_lack_ACE_OS switch (c) { case 's': opt_select_reactor = 1; break; case 'w': opt_wfmo_reactor = 1; break; case 'm': write_to_pipe_in_main = 1; break; case 'c': cancel_reads = 1; break; } // Create pipes ACE_Pipe pipe1, pipe2; result = pipe1.open (); ACE_ASSERT (result == 0); result = pipe2.open (); ACE_ASSERT (result == 0); // Create handlers Handler handler (pipe1); Different_Handler different_handler (pipe2); // Manage memory automagically. auto_ptr<ACE_Reactor> reactor (create_reactor ()); // Register handlers ACE_Reactor_Mask handler_mask = ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; ACE_Reactor_Mask different_handler_mask = ACE_Event_Handler::WRITE_MASK | ACE_Event_Handler::EXCEPT_MASK; result = reactor->register_handler (&handler, handler_mask); ACE_ASSERT (result == 0); result = reactor->register_handler (&different_handler, different_handler_mask); ACE_ASSERT (result == 0); // Write to the pipe; this causes handle_input to get called. if (write_to_pipe_in_main) { write_to_pipe (pipe1); write_to_pipe (pipe2); } // Note that handle_output will get called automatically since the // pipe is writable! //FUZZ: disable check_for_lack_ACE_OS // Run for three seconds ACE_Time_Value time (3); //FUZZ: enable check_for_lack_ACE_OS reactor->run_reactor_event_loop (time); ACE_DEBUG ((LM_DEBUG, "\nClosing down the application\n\n")); return 0; }
void reply_pipe_write_and_X(struct smb_request *req) { smb_np_struct *p = get_rpc_pipe_p(SVAL(req->inbuf,smb_vwv2)); size_t numtowrite = SVAL(req->inbuf,smb_vwv10); int nwritten = -1; int smb_doff = SVAL(req->inbuf, smb_vwv11); bool pipe_start_message_raw = ((SVAL(req->inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) == (PIPE_START_MESSAGE|PIPE_RAW_MODE)); char *data; if (!p) { reply_doserror(req, ERRDOS, ERRbadfid); return; } if (p->vuid != req->vuid) { reply_nterror(req, NT_STATUS_INVALID_HANDLE); return; } data = smb_base(req->inbuf) + smb_doff; if (numtowrite == 0) { nwritten = 0; } else { if(pipe_start_message_raw) { /* * For the start of a message in named pipe byte mode, * the first two bytes are a length-of-pdu field. Ignore * them (we don't trust the client). JRA. */ if(numtowrite < 2) { DEBUG(0,("reply_pipe_write_and_X: start of " "message set and not enough data " "sent.(%u)\n", (unsigned int)numtowrite )); reply_unixerror(req, ERRDOS, ERRnoaccess); return; } data += 2; numtowrite -= 2; } nwritten = write_to_pipe(p, data, numtowrite); } if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) { reply_unixerror(req, ERRDOS,ERRnoaccess); return; } reply_outbuf(req, 6, 0); nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten); SSVAL(req->outbuf,smb_vwv2,nwritten); DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten)); chain_reply(req); }
/* * pipe_writer_write * * This function is used as the write handler for file handles of type * FH_PIPE_WRITER, and is invoked from the write system call. All this needs to do * is to call through to the write_to_pipe function to add the data. */ static ssize_t pipe_writer_write(filehandle * fh, const void *buf, size_t count) { write_to_pipe(fh->p, buf, count); return count; }
int do_command_upload_sandbox(void *arg, Stream*) { dprintf(D_ALWAYS, "FTGAHP: upload sandbox\n"); Gahp_Args args; parse_gahp_command ((char*)arg, &args); // first two args: result id and sandbox id: std::string rid = args.argv[1]; std::string sid = args.argv[2]; // third arg: job ad ClassAd ad; classad::ClassAdParser my_parser; if (!(my_parser.ParseClassAd(args.argv[3], ad))) { // FAIL write_to_pipe( ChildErrorPipe, "Failed to parse job ad" ); return 1; } // rewrite the IWD to the actual sandbox dir std::string iwd; define_sandbox_path(sid, iwd); ad.Assign(ATTR_JOB_IWD, iwd.c_str()); char ATTR_SANDBOX_ID[] = "SandboxId"; ad.Assign(ATTR_SANDBOX_ID, sid.c_str()); // directory was created, let's set up the FileTransfer object FileTransfer ft; if (!ft.Init(&ad)) { // FAIL write_to_pipe( ChildErrorPipe, "Failed to initialize FileTransfer" ); return 1; } // Set the Condor version of our peer, as given by the CONDOR_VERSION // command. // If we don't have a version, then assume it's pre-8.1.0. // In 8.1, the file transfer protocol changed and we added // the CONDOR_VERSION command. if ( !peer_condor_version.empty() ) { ft.setPeerVersion( peer_condor_version.c_str() ); } else { CondorVersionInfo ver( 8, 0, 0 ); ft.setPeerVersion( ver ); } if ( !sec_session_id.empty() ) { ft.setSecuritySession( sec_session_id.c_str() ); } dprintf(D_ALWAYS, "BOSCO: calling upload files\n"); // the "true" param to UploadFiles here means blocking (i.e. "in the foreground") if (!ft.UploadFiles(true)) { // FAIL write_to_pipe( ChildErrorPipe, ft.GetInfo().error_desc.Value() ); return 1; } // SUCCEED return 0; }
void Server::execMsg(string msg){ message_ptr message = messageParse(msg); string res = interpretarMsg(message->reason,message->atributes); write_to_pipe(res); }