Example #1
0
char const *TcallDep::realise(char const *target,
                              char const *targetMatch,
                              Tstr       *list,
                              int         maxIdx)
{
   static TstrBuf rtn = "";
   
   char const    *comm   = Tdep::realise(target, targetMatch, list, maxIdx);
   FILE          *input  = NULL;
   unsigned long  rtnPos = 0;
   
   // if (NULL != (input = popen(comm, "r")))
   if (NULL != (input = readPipe(comm)))
   {
      int ch;
      
      while (EOF != (ch = fgetc(input)))
      {
         if (isspace(ch))
         {
            rtn[rtnPos++] = ' ';
            while (isspace(ch)) ch = fgetc(input);
         }
         
         if (EOF != ch) rtn[rtnPos++] = ch; 
      }
      
      // pclose(input);
      closePipe(input);
   }
   
   rtn[rtnPos++] = '\0';
   
   return ((char const*)rtn);
}
Example #2
0
/*
 * Create pipe with either write- _or_ read-semantics.  Fortunately for us,
 * on SYS_MSDOS, we don't need both at the same instant.
 */
int
inout_popen(FILE **fr, FILE **fw, char *cmd)
{
    char *type = (fw != 0) ? "w" : "r";
    static FILE *pp[2] =
    {0, 0};
    int fd;

    TRACE(("inout_popen(fr=%p, fw=%p, cmd='%s')\n", fr, fw, cmd));

    ffstatus = file_is_pipe;
    fileeof = FALSE;
    append_libdir_to_path();

    /* Create the file that will hold the pipe's content */
    if ((fd = createTemp(type)) >= 0) {
        if (fw == 0) {
            *fr = pp[0] = readPipe(cmd, -1, fd);
            myWrtr = 0;
            myPipe = &pp[0];	/* "fr" may be stack-based. */
            myCmds = 0;
        } else {
            *fw = pp[1] = fdopen(fd, type);
            myPipe = fr;
            myWrtr = &pp[1];	/* "fw" may be stack-based. */
            myCmds = strmalloc(cmd);
        }
    }
    return TRUE;
}
Example #3
0
qword sys_read(qword file, qword buffer, qword size, qword r8, qword r9) {
    if (file == 0) {
        readFull((char*) buffer, (int) size);
    }else if(file>2 && file<8){
        pipe_t pipe=getMyProcessData()->fd[file-3];
        readPipe(pipe,buffer,size);
    }
    return 1;
}
Example #4
0
/** threadedCommunicate uses threads to read from the output streams.
 * It is intended to be used on Windows where there is no reasonable
 * way to carry out a non-blocking read on a pipe.  We compile and
 * test it on all platforms to make it easier to avoid regressions. */
std::pair<w_string, w_string> ChildProcess::threadedCommunicate(
    pipeWriteCallback writeCallback) {
  auto outFuture = readPipe(STDOUT_FILENO);
  auto errFuture = readPipe(STDERR_FILENO);

  auto it = pipes_.find(STDIN_FILENO);
  if (it != pipes_.end()) {
    auto& inPipe = pipes_[STDIN_FILENO];
    while (!writeCallback(inPipe->write)) {
      ; // keep trying to greedily write to the pipe
    }
    // Close the input stream; this typically signals the child
    // process that we're done and allows us to safely block
    // on the reads below.
    pipes_.erase(STDIN_FILENO);
  }

  return std::make_pair(outFuture.get(), errFuture.get());
}
Example #5
0
/**
 Executes the reporting script, captures stdout and stderr
 Only returns once the child exits
 @param pEvent
 @param resultLine - output returned
 @return true on success
 @exception on error
 */
bool scriptExec::execScript( baseEvent* pEvent, std::string& resultLine )
{
  bool bSuccess;
  try
  {
    spawnScript( pEvent, bPersistentApp );
    bSuccess = readPipe( resultLine );
  } // try
  catch( Exception e )
  {
    bSuccess = false;
  } // catch
  return bSuccess;
} // execScript
Example #6
0
int main(void)
{
	PIPE * pipe = NULL;
	int ret;
	pid_t pid;

	ret = createPipe(&pipe);
	if (ret == -1) {
		printf("create fail!\n");
		return -1;
	}

	pid = fork();
	if (pid == 0) {
		setRDWRflag(pipe, WRITE);
		ret = writePipe(pipe, "Hello world!\n", 
			13);
		if (ret == -1) {
			printf("write pipe fail!\n");
			closePipe(pipe);
			exit(1);
		}	
		closePipe(pipe);
		exit(0);
	}

	if (pid > 0) {
		wait(NULL);
		setRDWRflag(pipe, READ);
		char buf[32];
		bzero(buf, 32);
		ret = readPipe(pipe, buf, 13);
		if (ret == -1) {
			printf("read pipe fail!\n");
			closePipe(pipe);
			exit(1);
		}
		printf("%s", buf);
		closePipe(pipe);
		exit(0);
	}
}
Example #7
0
/*
 * If we were writing to a pipe, invoke the read-process with stdin set to the
 * temporary-file.  This is used in the filter-buffer code, which needs both
 * read- and write-pipes.
 */
void
npflush(void)
{
    if (myCmds != 0) {
        if (myWrtr != 0) {
            int fd;
            static FILE *pp;

            (void) fflush(*myWrtr);
#if 0
            (void) fclose(*myWrtr);
            *myWrtr = fopen(myName[0], "r");
#else
            rewind(*myWrtr);
#endif
            fd = createTemp("r");
            pp = fdopen(fd, "r");
            myRead = &pp;
            *myPipe = readPipe(myCmds, fileno(*myWrtr), fd);
        }
        FreeAndNull(myCmds);
    }
}
Example #8
0
void init(void){
  object_list * L = NULL;
  L = readPipe(L);

  SDL_Surface *startScreen = NULL;
  SDL_Rect startScreenCoord; 
  SDL_Event event;

  /* initialize SDL */
  SDL_Init(SDL_INIT_VIDEO);
  SDL_WM_SetCaption("get Rekt", "SDL Animation");

  /* create window */
  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);


  startScreen = SDL_LoadBMP("startScreen.bmp");
  startScreenCoord.x = 0;
  startScreenCoord.y = 0;
  SDL_BlitSurface(startScreen, NULL, screen, &startScreenCoord);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&event)){
    switch(event.type){
    case SDL_QUIT: 
      //SDL_Quit(); 
      break;
      
    case SDL_KEYDOWN:
      switch(event.key.keysym.sym){
      case SDLK_SPACE: 
	handle_events(L);
	break;
      }
    }
  }
  SDL_FreeSurface(startScreen);
}
Example #9
0
void executePipe(char *line, char *cmd) {
	int bgFg = getFgBg(cmd);	/* fg(0) or bg(1) */
	int status;

	pid_t pid = fork();

	if(pid < 0) {
		perror("Fork error, pid < 0");
	/* parent */
	} else if(pid > 0) {
		/* set process group ID of child to self pid */
		/* will suspend when receive SIGTTIN and SIGTTOU signals */
		setpgid(pid, pid);

		/* set terminal control to new process group with child */
		tcsetpgrp(STDIN_FILENO, pid);

		/* insert process in the list */
		insertProcess(pid, bgFg, cmd);


		if(processList.current->process.status == FOREGROUND) {
			/* wait child finish */
			waitpid(pid, &status, WUNTRACED);
		}

		/* parent regains terminal control */
		tcsetpgrp(STDIN_FILENO, getpgid(0));
	/* child */
	} else {
		/* signals SIGTSTP and SIGINT will show default behavior in child */
		defaultSignals();
		
		/* set group id of child to self pid */
		/* suspend when receive signals SIGTTIN and SIGTTOUT */
		/* the child could go through exec before the parent set the group */
		setpgid(0, 0);

        int startLine = 0;	/* search begin of next expression */
        int start, end;		/* expression delimiters */
        char *previousCmd;
		
		/* while find commands between pipes */
        while(setExpressionDelimiters(line + startLine,
        	"[^| ]([^|]*[^| ])?", &start, &end)) {
        	
            previousCmd = cmd;
            
            /* get current command */
            cmd = getExpression(line + startLine, start, end, 0);
            
            /* go to next search */
            startLine += end;

			/* file input and output of pipe */
            int file[2];
            pid_t pid;

            newPipe(file, &pid);

			/* parent */
			if(pid > 0) {
				readPipe(file);
			/* child */
			} else {
				writePipe(file);
				internalExternalPipe(previousCmd, NO_JOBS_CONTROL);
			}
        }

		/* execute last command */
        internalExternalPipe(cmd, NO_JOBS_CONTROL);
    }
}
Example #10
0
int readPipeTimed(PIPE_PTR fd, char *buffer, int size, int timeout)
{
    int retval = -1;
    fd_set fdsin, fdserr;
    struct timeval tv = {0,0}, *tvp = NULL;
    int64_t stoptime = (int64_t)microsSinceX() + timeout * 1000;

    /* prepare the fds for the select call */
    eintr_loop:
    FD_ZERO(&fdsin);
    FD_SET(fd, &fdsin);
    fdserr = fdsin;

    /* configure the timeout if there was one*/
    if (timeout >= 0)
    {
        int64_t timeremaining = stoptime - (int64_t)microsSinceX();
        tvp = &tv;
        if (timeremaining < 0) timeremaining = 0;
        tv.tv_sec = timeremaining / 1000000;
        tv.tv_usec = timeremaining % 1000000;
    }

    switch(select(fd + 1, &fdsin, NULL, &fdserr, tvp))
    {
    /* error */
    case -1:
        if (errno == EINTR)
            /* I hate goto, but it's the right move here */
            goto eintr_loop;
        break;

    /* timeout */
    case 0:
        retval = 0;
        break;

    default:
        if (FD_ISSET(fd, &fdsin))
        {
            int goal = size;

            retval = 0;
            while(goal > 0)
            {
                int amount = readPipe(fd, buffer + retval, goal);
                switch(amount)
                {
                case -1:
                    retval = -1;
                case 0:
                    /* break out on error or EOF */
                    if (retval == 0)
                    {
                        retval = -1;
                        errno = EPIPE;
                    }
                    goal = 0;
                    break;

                default:
                    retval += amount;
                    goal -= amount;
                }
            }
        }
        else
            errno = EIO;
        break;
    }
    return retval;
}
Example #11
0
static void workLoop()
{
    deviceList *list;
    int ctlSock = INVALID_PIPE;

    /* initialize the driver and device list */
    if ((list = initServer(&srvSettings)) == NULL)
        message(LOG_ERROR, "failed to initialize the device list.\n");
    else if (signal(SIGINT, quitHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGINT handler.\n");
    else if (signal(SIGTERM, quitHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGTERM handler.\n");
    else if (signal(SIGHUP, scanHandler) == SIG_ERR)
        message(LOG_ERROR, "failed to install SIGHUP handler.\n");
    else if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        message(LOG_ERROR, "failed to ignore SIGPIPE messages.\n");
    else if (! srvSettings.justDescribe && (ctlSock = startListening(NULL)) == INVALID_PIPE)
        message(LOG_ERROR, "failed to open the control socket.\n");
    else
    {
        bool quit = false;

#if DEBUG
printf("OPEN %d %s(%d)\n", srvSettings.commPipe[0], __FILE__, __LINE__);
printf("OPEN %d %s(%d)\n", srvSettings.commPipe[1], __FILE__, __LINE__);
#endif

        /* trigger the initial device scan */
        scanHandler(SIGHUP);

#ifdef __APPLE__
        /* Support hot plug in on Mac OS X -- returns non-zero for error */
        daemon_osx_support(ids);
#endif

        /* loop, waiting for commands */
        while(! quit)
        {
            THREAD_PTR thread = INVALID_THREAD_PTR;
            void *exitVal;

            /* wait for a new ctl connection, a command from an
               existing ctl connection, or a message from an exiting
               child thread. */

            /* read a command and check for error */
            if (readPipe(srvSettings.commPipe[READ], &thread,
                         sizeof(THREAD_PTR)) != sizeof(THREAD_PTR))
            {
                message(LOG_ERROR,
                        "CommPipe read failed: %s\n", translateError(errno));
                quit = true;
            }
            /* threads trigger a join by telling the main thread their id */
            else if (thread != INVALID_THREAD_PTR)
                joinThread(thread, &exitVal);
            /* read the actual command (came from a signal handler) */
            else if (readPipe(srvSettings.commPipe[READ], &thread,
                              sizeof(THREAD_PTR)) != sizeof(THREAD_PTR))
            {
                message(LOG_ERROR,
                        "Command read failed: %s\n", translateError(errno));
                quit = true;
            }
            /* handle the shutdown command */
            else if (thread == QUIT_TRIGGER)
                quit = true;
            /* complain about unknown commands */
            else if (thread != SCAN_TRIGGER)
                message(LOG_ERROR,
                        "Unknown command from commPipe: %d\n", thread);
            /* handle the scan/rescan command */
            else
            {
                if (srvSettings.justDescribe)
                    message(LOG_NORMAL, "Detected Iguanaworks devices:\n");
                if (! updateDeviceList(list))
                    message(LOG_ERROR, "scan failed.\n");
                if (srvSettings.justDescribe)
                    break;
            }
        }

        /* wait for all the workers to finish */
        reapAllChildren(list);

        /* close up the server socket */
        stopListening(ctlSock, NULL);
    }
}
Example #12
0
ssize_t __pread(int fd, void *data, size_t size) {
    idStream *is = getStreamData(getpid(), fd);
    if(!is)
        return -1;
    return readPipe(is->fd, data, size);
}