Beispiel #1
0
/*
 * General server example: accept a client connection and do something.
 * This program just outputs a short HTML page, but can be easily adapted
 * to do other things.
 *
 * This server creates a constant number of processes ("virtual processors"
 * or VPs) and replaces them when they die. Each virtual processor manages
 * its own independent set of state threads (STs), the number of which varies
 * with load against the server. Each state thread listens to exactly one
 * listening socket. The initial process becomes the watchdog, waiting for
 * children (VPs) to die or for a signal requesting termination or restart.
 * Upon receiving a restart signal (SIGHUP), all VPs close and then reopen
 * log files and reload configuration. All currently active connections remain
 * active. It is assumed that new configuration affects only request
 * processing and not the general server parameters such as number of VPs,
 * thread limits, bind addresses, etc. Those are specified as command line
 * arguments, so the server has to be stopped and then started again in order
 * to change them.
 *
 * Each state thread loops processing connections from a single listening
 * socket. Only one ST runs on a VP at a time, and VPs do not share memory,
 * so no mutual exclusion locking is necessary on any data, and the entire
 * server is free to use all the static variables and non-reentrant library
 * functions it wants, greatly simplifying programming and debugging and
 * increasing performance (for example, it is safe to ++ and -- all global
 * counters or call inet_ntoa(3) without any mutexes). The current thread on
 * each VP maintains equilibrium on that VP, starting a new thread or
 * terminating itself if the number of spare threads exceeds the lower or
 * upper limit.
 *
 * All I/O operations on sockets must use the State Thread library's I/O
 * functions because only those functions prevent blocking of the entire VP
 * process and perform state thread scheduling.
 */
int main(int argc, char *argv[])
{
  /* Parse command-line options */
  parse_arguments(argc, argv);

  /* Allocate array of server pids */
  if ((vp_pids = calloc(vp_count, sizeof(pid_t))) == NULL)
    err_sys_quit(errfd, "ERROR: calloc failed");

  /* Start the daemon */
  if (!interactive_mode)
    start_daemon();

  /* Initialize the ST library */
  if (st_init() < 0)
    err_sys_quit(errfd, "ERROR: initialization failed: st_init");

  /* Set thread throttling parameters */
  set_thread_throttling();

  /* Create listening sockets */
  create_listeners();

  /* Change the user */
  if (username)
    change_user();

  /* Open log files */
  open_log_files();

  /* Start server processes (VPs) */
  start_processes();

  /* Turn time caching on */
  st_timecache_set(1);

  /* Install signal handlers */
  install_sighandlers();

  /* Load configuration from config files */
  load_configs();

  /* Start all threads */
  start_threads();

  /* Become a signal processing thread */
  process_signals(NULL);

  /* NOTREACHED */
  return 1;
}
int
main(){
	void **value_ptr = NULL;
	short host_numbers[16];
    	short sockfd = create_server_socket();
	int i;
    	if(sockfd < 0){
    		printf("\n Arbiter Error. Arbiter closing ...\n");
    		return -1;
    	}

    	hostsockfd_init();

    	sigemptyset(&set);	
	sigaddset(&set, SIGINT);
	pthread_sigmask(SIG_BLOCK, &set, NULL);
	//printf("\n Going to Signal Handling .....\n");
	pthread_create(&signal_thread, 0, (void *)&signal_handler, &sockfd);

	open_log_files();
    	//printf("\n accept_connections thread ---------------\n");
   	pthread_create(&accept_connections_thread, 0, (void *)&accept_connections, &sockfd);
   	//printf("\n get_demands thread -------------------");
	for(i=0; i<16; i++){
		host_numbers[i] = i+1;	
        	pthread_create(&recieve_request_thread[i], 0, (void *)&get_demands, &(host_numbers[i]));
	}
        	//printf("\n process_demands thread ----------------------");
        	pthread_create(&process_request_thread, 0, (void *)&process_demands, NULL);
        //	pthread_create(&send_response_thread, 0, (void *)&send_demands_to_host, NULL);
        	
        	
        	
        	//pthread_create(&send_response_thread, 0, (void *)&get_demands, NULL);

    	pthread_join (accept_connections_thread, value_ptr);
	for(i=0; i<16; i++)
     	 pthread_join (recieve_request_thread[i], value_ptr);	
     //	pthread_join (process_request_thread, value_ptr);
     //	pthread_join (send_response_thread, value_ptr);
     	//close_log_files();	
     	return 0;
}
Beispiel #3
0
int run_program(CodeRunInstance *instance, const char *filename, char *const argv[], char *const envp[], const char *working_directory, const char *run_as_user, const char *datafilename_stdin, const char *logfilename_stdout, const char *logfilename_stderr, uint32_t max_running_second, uint32_t overtime_sigint_second, uint32_t overtime_sigterm_second)
{
	char *fullpath_working_directory;

	char *fullpath_datafile_stdin;
	char *fullpath_logfile_stdout;
	char *fullpath_logfile_stderr;

	uid_t runner_uid;
	gid_t runner_gid;

	pid_t child_pid;


	fullpath_working_directory = NULL;
	fullpath_datafile_stdin = NULL;
	fullpath_logfile_stdout = NULL;
	fullpath_logfile_stderr = NULL;

#define RELEASE_ALLOCATED_RESOURCE {	\
	release_allocated_memory(fullpath_working_directory, fullpath_datafile_stdin, fullpath_logfile_stdout, fullpath_logfile_stderr);	\
}


	memset(instance, 0, sizeof(CodeRunInstance));
#if ENABLE_RUNTIMEENV_PRESERVE
	instance->fullpath_working_directory = NULL;
	instance->fullpath_datafile_stdin = NULL;
	instance->fullpath_logfile_stdout = NULL;
	instance->fullpath_logfile_stderr = NULL;
	instance->runner_uid = 0;
	instance->runner_gid = 0;
#endif	/* ENABLE_RUNTIMEENV_PRESERVE */

	if(0 != check_working_directory(instance, working_directory, &fullpath_working_directory))
	{
		RECORD_ERR("cannot have real path of given working directory", __FILE__, __LINE__);
		RELEASE_ALLOCATED_RESOURCE;
		return 1;
	}

	if(0 != prepare_log_files(instance, datafilename_stdin, logfilename_stdout, logfilename_stderr, &fullpath_datafile_stdin, &fullpath_logfile_stdout, &fullpath_logfile_stderr))
	{
		RELEASE_ALLOCATED_RESOURCE;
		return 2;
	}

	if(0 != lookup_runner_account(instance, run_as_user, &runner_uid, &runner_gid))
	{
		RELEASE_ALLOCATED_RESOURCE;
		return 3;
	}

	child_pid = fork();
	if(-1 == child_pid)
	{
		RECORD_ERR("failed on perform fork()", __FILE__, __LINE__);
		RELEASE_ALLOCATED_RESOURCE;
		return 4;
	}
	else if(0 != child_pid)
	{
		fill_instance_structure(instance, max_running_second, overtime_sigint_second, overtime_sigterm_second, child_pid);
#if ENABLE_RUNTIMEENV_PRESERVE
		instance->fullpath_working_directory = fullpath_working_directory;
		instance->fullpath_datafile_stdin = fullpath_datafile_stdin;
		instance->fullpath_logfile_stdout = fullpath_logfile_stdout;
		instance->fullpath_logfile_stderr = fullpath_logfile_stderr;
		instance->runner_uid = runner_uid;
		instance->runner_gid = runner_gid;
#else	/* ENABLE_RUNTIMEENV_PRESERVE */
		RELEASE_ALLOCATED_RESOURCE;
#endif	/* ENABLE_RUNTIMEENV_PRESERVE */
		return 0;
	}

	/* {{{ child process code */

	if(0 != setpgid(0, 0))
	{
		RECORD_ERR("failed on creating independent process group", __FILE__, __LINE__);
	}

	if(0 != chdir(working_directory))
	{
		RECORD_ERR("failed on changing work directory", __FILE__, __LINE__);
		exit(17);
		return 1;
	}

	if(0 != open_log_files(instance, fullpath_datafile_stdin, fullpath_logfile_stdout, fullpath_logfile_stderr, runner_uid, runner_gid))
	{
		exit(18);
		return 2;
	}

	close_fd(instance);

	if( (NULL != run_as_user) && (0 != change_account(instance, runner_uid, runner_gid)) )
	{
		exit(19);
		return 3;
	}

	fprintf(stdout, "PID: %d\n", (int)(getpid()));
	fprintf(stdout, "WorkDirectory: [%s]\n", fullpath_working_directory);
	fprintf(stdout, "Runner: UID=%d; GID=%d\n", (int)(runner_uid), (int)(runner_gid));
	fprintf(stdout, "----------------\n");

	execve(filename, argv, envp);
	RECORD_ERR("cannot execute target program", __FILE__, __LINE__);
	exit(20);

	/* }}} child process code */

#undef RELEASE_ALLOCATED_RESOURCE

	return -1;
}
Beispiel #4
0
int main(int argc, char **argv) {
  open_log_files();
  assert_valid_setup(argv[0]);

  int operation;
  int ret = validate_arguments(argc, argv, &operation);

  if (ret != 0) {
    flush_and_close_log_files();
    return ret;
  }

  int exit_code = 0;

  switch (operation) {
  case CHECK_SETUP:
    //we already did this
    exit_code = 0;
    break;
  case MOUNT_CGROUPS:
    exit_code = 0;

    while (optind < argc && exit_code == 0) {
      exit_code = mount_cgroup(argv[optind++], cmd_input.cgroups_hierarchy);
    }

    break;
  case TRAFFIC_CONTROL_MODIFY_STATE:
    exit_code = traffic_control_modify_state(cmd_input.traffic_control_command_file);
    break;
  case TRAFFIC_CONTROL_READ_STATE:
    exit_code = traffic_control_read_state(cmd_input.traffic_control_command_file);
    break;
  case TRAFFIC_CONTROL_READ_STATS:
    exit_code = traffic_control_read_stats(cmd_input.traffic_control_command_file);
    break;
  case RUN_DOCKER:
    exit_code = run_docker(cmd_input.docker_command_file);
    break;
  case RUN_AS_USER_INITIALIZE_CONTAINER:
    exit_code = set_user(cmd_input.run_as_user_name);
    if (exit_code != 0) {
      break;
    }

    exit_code = initialize_app(cmd_input.yarn_user_name,
                            cmd_input.app_id,
                            cmd_input.cred_file,
                            extract_values(cmd_input.local_dirs),
                            extract_values(cmd_input.log_dirs),
                            argv + optind);
    break;
  case RUN_AS_USER_LAUNCH_DOCKER_CONTAINER:
     if (cmd_input.traffic_control_command_file != NULL) {
        //apply tc rules before switching users and launching the container
        exit_code = traffic_control_modify_state(cmd_input.traffic_control_command_file);
        if( exit_code != 0) {
          //failed to apply tc rules - break out before launching the container
          break;
        }
      }

      exit_code = set_user(cmd_input.run_as_user_name);
      if (exit_code != 0) {
        break;
      }

      exit_code = launch_docker_container_as_user(cmd_input.yarn_user_name,
                      cmd_input.app_id,
                      cmd_input.container_id,
                      cmd_input.current_dir,
                      cmd_input.script_file,
                      cmd_input.cred_file,
                      cmd_input.pid_file,
                      extract_values(cmd_input.local_dirs),
                      extract_values(cmd_input.log_dirs),
                      cmd_input.docker_command_file,
                      cmd_input.resources_key,
                      cmd_input.resources_values);
      break;
  case RUN_AS_USER_LAUNCH_CONTAINER:
    if (cmd_input.traffic_control_command_file != NULL) {
      //apply tc rules before switching users and launching the container
      exit_code = traffic_control_modify_state(cmd_input.traffic_control_command_file);
      if( exit_code != 0) {
        //failed to apply tc rules - break out before launching the container
        break;
      }
    }

    exit_code = set_user(cmd_input.run_as_user_name);
    if (exit_code != 0) {
      break;
    }

    exit_code = launch_container_as_user(cmd_input.yarn_user_name,
                    cmd_input.app_id,
                    cmd_input.container_id,
                    cmd_input.current_dir,
                    cmd_input.script_file,
                    cmd_input.cred_file,
                    cmd_input.pid_file,
                    extract_values(cmd_input.local_dirs),
                    extract_values(cmd_input.log_dirs),
                    cmd_input.resources_key,
                    cmd_input.resources_values);
    free(cmd_input.resources_key);
    free(cmd_input.resources_value);
    free(cmd_input.resources_values);
    break;
  case RUN_AS_USER_SIGNAL_CONTAINER:
    exit_code = set_user(cmd_input.run_as_user_name);
    if (exit_code != 0) {
      break;
    }

    exit_code = signal_container_as_user(cmd_input.yarn_user_name,
                                  cmd_input.container_pid,
                                  cmd_input.signal);
    break;
  case RUN_AS_USER_DELETE:
    exit_code = set_user(cmd_input.run_as_user_name);
    if (exit_code != 0) {
      break;
    }

    exit_code = delete_as_user(cmd_input.yarn_user_name,
                        cmd_input.dir_to_be_deleted,
                        argv + optind);
    break;
  }

  flush_and_close_log_files();
  return exit_code;
}
Beispiel #5
0
/**
 * Main function.
 */
int
main( int argc, char *argv[] )
{
    const char* FNAME = __FUNCTION__;
    int       listenSock;   // Server listening socket.
    int       cliSock;      // Client remote socket.
    socklen_t clientAddressLength;
    struct sockaddr_in clientAddress;
    struct sockaddr_in serverAddress;
    SocketInfo* pSocketInfo = NULL;
    pthread_t   serviceThread;

    /* Parse command-line options */
    parse_arguments( argc, argv );

    /* Change the current directory. */
    //if ( chdir( s_logdir ) < 0 )
    //    err_sys_quit( s_errfd, "ERROR: can't change directory to %s: chdir", s_logdir );

    /* Open log files */
    open_log_files();

    hoxLog(LOG_INFO, "%s: Starting [dbagent]...", FNAME);

    /* Install signal handlers */
    Signal( SIGTERM, wdog_sighandler );  /* terminate */

    /* Create socket for listening for client connection requests.  */
    listenSock = socket(AF_INET, SOCK_STREAM, 0);
    if (listenSock < 0) 
    {
        hoxLog(LOG_SYS_ERROR, "%s: cannot create listen socket", FNAME);
        exit(1);
    }
  
    /* Bind listen socket to listen port. */
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = inet_addr(s_serverIP);
    serverAddress.sin_port = htons(s_nListenPort);

    if ( bind( listenSock,
               (struct sockaddr *) &serverAddress,
               sizeof(serverAddress) ) < 0 ) 
    {
        hoxLog(LOG_SYS_ERROR, "%s: cannot bind socket", FNAME);
        exit(1);
    }

    /* Wait for connections from clients.
     * This is a non-blocking call; i.e., it registers this program with
     * the system as expecting connections on this socket, and then
     * this thread of execution continues on.
     */
    listen(listenSock, 5);
  
    for (;;)
    {
        hoxLog(LOG_INFO, "%s: Waiting for connections on [%s:%d]...", 
            FNAME, s_serverIP, s_nListenPort);

        /* Accept a connection with a client that is requesting one.
         */
        clientAddressLength = sizeof(clientAddress);
        cliSock = accept( listenSock,
                          (struct sockaddr *) &clientAddress,
                          &clientAddressLength);
        if ( cliSock < 0 ) 
        {
            hoxLog(LOG_SYS_ERROR, "%s: cannot accept connection", FNAME);
            exit(1);
        }
    
        /* (1) Show the IP address of the client.
         *     inet_ntoa() converts an IP address from binary form to the
         *     standard "numbers and dots" notation.
         *
         * (2) Show the client's port number.
         *     ntohs() converts a short int from network byte order (which is
         *     Most Significant Byte first) to host byte order (which on x86,
         *     for example, is Least Significant Byte first).
         */
        hoxLog(LOG_DEBUG, "%s: connected to [%s:%d]", FNAME, 
            inet_ntoa(clientAddress.sin_addr),
            ntohs(clientAddress.sin_port));

        /* Create a separate thread to handle this client. */

        pSocketInfo = new SocketInfo;
        pSocketInfo->nSocket = cliSock;
        pSocketInfo->iaFrom = clientAddress.sin_addr;

        if ( 0 != pthread_create( &serviceThread,
                                  NULL,   /* Use Default Attributes */
                                  handle_client_thread, 
                                  (void*) pSocketInfo ) )
        {
            hoxLog(LOG_SYS_ERROR, "%s: Failed to create service Thread", FNAME);
            delete pSocketInfo;
            continue;  // NOTE: *** Still continue...
        }

    } /* for(...) */

    /* Close log files. */
    close( s_errfd );
    close( s_serverfd );

    return 0;
}