Ejemplo n.º 1
0
Archivo: signal.c Proyecto: ANahr/mono
/*
 * returns: -1 on error:
 *          timeout on timeout
 *          index into _signals array of signal that was generated on success
 */
int
Mono_Unix_UnixSignal_WaitAny (void** _signals, int count, int timeout /* milliseconds */, Mono_Posix_RuntimeIsShuttingDown shutting_down)
{
	int r;
	int currfd = 0;
	struct pollfd fd_structs[NUM_SIGNALS];

	signal_info** signals = (signal_info**) _signals;

	if (count > NUM_SIGNALS)
		return -1;

	if (acquire_mutex (&signals_mutex) == -1)
		return -1;

	r = setup_pipes (signals, count, &fd_structs[0], &currfd);

	release_mutex (&signals_mutex);

	if (r == 0) {
		r = wait_for_any (signals, count, &currfd, &fd_structs[0], timeout, shutting_down);
	}

	if (acquire_mutex (&signals_mutex) == -1)
		return -1;

	teardown_pipes (signals, count);

	release_mutex (&signals_mutex);

	return r;
}
Ejemplo n.º 2
0
/* The checkEnv command. Sets up pipes and their identifiers
 * between printenv, (optionally) grep, sort, and pager, and
 * executes the pipeline.
 */
void checkenv(char* const* arguments, int argc) {
	pager = getenv("PAGER");
	if (setup_pipes(argc) == -1) {
		return;
	}
	set_pipe_identifiers(argc);

	if(pipe_printenv() == -1) {
		return;
	}
	if (argc > 1) {
		if(pipe_grep(arguments) == -1) {
			return;
		}
	}
	if(pipe_sort() == -1) {
		printf("sort failed.\n");
		return;
	}
	if(pipe_pager() == -1) {
		return;
	}
}
Ejemplo n.º 3
0
void interpret( cmd_list cmds ){
	int roop = 0;
	int c = count_non_pmode( cmds );
	int wait_res = 0;
	int result = 0;
	commands = (cmd_list) malloc( c * sizeof( cmd ) );
	
	setup_pipes( cmds );

	for( roop = 0; roop < c; roop++) {
	//printcmd(commands[roop], "next");	
		if ( commands[roop].pmode == C_FILE ) 
			continue;
		
		result = fork();
		switch( result ) {
			case 0:
					//child

					if ( commands[roop].fd_in[0] == -1) {
							freopen( commands[roop-1].list[0], "r", stdin );
					}else if ( commands[roop].fd_in[0] != 0  ) {
							//not reg stdin
							close( 0 );
							dup( commands[roop].fd_in[0] );
					}

					if ( commands[roop].fd_out[1] == -1 ) { 
							freopen( commands[roop+1].list[0], "a", stdout );
					}else if ( commands[roop].fd_out[1] != 1 ) {
							//not reg stdout
							close( 1 );
							dup( commands[roop].fd_out[1] );
					}
					execvp( commands[roop].list[0], commands[roop].list);
					printf("Unkown Command: %s\n", commands[roop].list[0]); 
					exit(0);
				break;
			case -1:
				//error
				perror("Spork: ");
				break;
			default:
				break;
		}
	}
	fsync(stdout);
	fflush(stdout);

	fsync(stdin);
	fflush(stdin);
	/*
	do {
			wait_res = wait(NULL);
	} while( wait_res != result);
	*/
	// grep hangs so we just let the 
	//waitpid( result, NULL, 0 );
	for ( roop = 0; roop < c; roop++ ) {
		if ( roop != c - 1 && commands[roop].pmode != C_FILE ) {
			close( commands[ roop ].fd_out[0] );
			close( commands[ roop ].fd_out[1] );
		}
	}
	free( commands );
}
Ejemplo n.º 4
0
/*****************************************************************************
 *  tc_initialize  -- called by test harness to initialize the testcase
 *****************************************************************************/
tc_status tc_initialize(void)
{
  tc_status status = TC_OK;
  int i = 0;
  tc_task_body_fptr_t fp = NULL;

  if (op_type == USE_UNDEFINED) {
    op_type = USE_MESSAGES;
  }

  /* check the operation size & async-ness */
  switch (op_type) 
    {
    case USE_MESSAGES:
      if (op_size < 1 || op_size > MCAPI_MAX_MSG_SIZE || op_size > MAX_ECHO_SIZE) {
        th_log_error("number of bytes to send is out of range: %d\n",op_size);
        status = TC_ERROR;
      }
      break;
    case USE_PACKETS:
      if (op_size < 1 || op_size > MCAPI_MAX_PKT_SIZE || op_size > MAX_ECHO_SIZE) {
        th_log_error("number of bytes to send is out of range: %d\n",op_size);
        status = TC_ERROR;
      }
      break;
    case USE_SCALARS:
      if (op_size != 1 && op_size != 2 && op_size != 4 && op_size != 8) {
        th_log_error("number of bytes to send is out of range: %d\n",op_size);
        status = TC_ERROR;
      } else if (use_async) {
        th_log_error("async cannot be used with scalars\n");
        status = TC_ERROR;
      }
      break;
    case USE_PIPES:
      if (op_size < 1 || op_size > MAX_ECHO_SIZE) {
        th_log_error("number of bytes to send is out of range: %d\n",op_size);
        status = TC_ERROR;
      } else if (use_async) {
        th_log_error("async cannot be used with pipes\n");
        status = TC_ERROR;
      }      
      break;
    case USE_SOCKETS:
      if (op_size < 1 || op_size > MAX_ECHO_SIZE) {
        th_log_error("number of bytes to send is out of range: %d\n",op_size);
        status = TC_ERROR;
      } else if (use_async) {
        th_log_error("async cannot be used with sockets\n");
        status = TC_ERROR;
      }      
      break;
    default:
      break;
    }
  
  /* check the operation count */
  if (op_count < 1) {
    th_log_error("number of sends must be > 0\n");
    status = TC_ERROR;
  }

  /* check the number of hops */
  if (num_tasks < 2 || num_tasks > CONKER_MAX_TASKS) {
    th_log_error("number of hops out of range [2-%d]\n", CONKER_MAX_TASKS);
    status = TC_ERROR;
  }

  /* if we got here we must have good args ... */
  th_log_info("[TC_ECHO]    -- tc_initialize, pid = %d\n", getpid());

  /* additional initialization items */
  for (i = 0; i < CONKER_MAX_TASKS; i++) {
    fds_array[i].fds[READ_IDX] = fds_array[i].fds[WRITE_IDX] = -1;
  }

  if (strcmp(th_get_impl_string(),"pthread") == 0) {
    using_pthreads = 1;
  }

  /* print config */
  switch (op_type) {
    case USE_MESSAGES:
      th_log("[TC_ECHO]    -- using MCAPI messages\n");
      break;
    case USE_PACKETS:
      th_log("[TC_ECHO]    -- using MCAPI packets\n");
      break;
    case USE_SCALARS:
      th_log("[TC_ECHO]    -- using MCAPI scalars\n");
      break;
    case USE_PIPES:
      th_log("[TC_ECHO]    -- using pipes\n");
      setup_pipes();
      break;
    case USE_SOCKETS:
      th_log("[TC_ECHO]    -- using sockets\n");
      setup_sockets();
      break;
    default:
      break;
  }
  if (use_async) {
    th_log("[TC_ECHO]    -- using non-blocking send/recv\n");
  }
  th_log("[TC_ECHO]    -- node offset:\t%d\n", th_get_node_offset());
  th_log("[TC_ECHO]    -- #sends:\t%d\n", op_count);
  th_log("[TC_ECHO]    -- #bytes/send:\t%d\n", op_size);
  th_log("[TC_ECHO]    -- #hops:\t%d\n", num_tasks - 1);
  if (filename) {
    /* TODO -- how to deal with output log files in bare metal */
    th_log("[TC_ECHO]    -- results will be appended to file: %s\n",filename);
    outfile = fopen(filename, "a+");
  }

  for (i = 0; status == TC_OK && i < num_tasks; i++) {
    
    switch (op_type) 
      {
      case USE_MESSAGES:
        if (!use_async) {
          fp = task_body_msg_blocking;
        } else {
          fp = task_body_msg_non_blocking;
        }
        break;
      case USE_PACKETS:
        if (!use_async) {
          fp = task_body_pkt_blocking;
        } else {
          fp = task_body_pkt_non_blocking;
        }
        break;
      case USE_SCALARS:
        fp = task_body_scalar_blocking;
        break;
      case USE_PIPES:
        fp = task_body_pipes_blocking;
        break;
      case USE_SOCKETS:
        fp = task_body_sockets_blocking;
        break;
      default:
        /* TODO - this is an error and should be handled !! */
        break;
      }
        
    if (th_register_task(i,"echo_task",fp,NULL,0) != TH_OK) {
      th_log_error("Unable to register task number %d.  Status = %d\n",
                   i, status);
      status = TC_ERROR;
    } else {
      th_log_info("[TC_ECHO]    -- task registered, id = %d\n", i);
    }
  }

  return status;
}