예제 #1
0
/*
 * get_elf_class - get the target executable elf class
 *                 i.e. ELFCLASS64 or ELFCLASS32.
 */
static int
get_elf_class(char *filename)
{
	int	elfclass = -1;
	int	elffd = get_executable(filename);
	Elf	*elf;
	size_t	size;
	char	*ident;
	GElf_Ehdr	ehdr;

	if (elffd < 0)
		return (elfclass);
	if (elf_version(EV_CURRENT) == EV_NONE) {
		(void) close(elffd);
		return (elfclass);
	}
	elf = elf_begin(elffd, ELF_C_READ, (Elf *) 0);
	/*
	 * verify information in file header
	 */
	if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *) 0) {
		close(elffd);
		return (elfclass);
	}
	ident = elf_getident(elf, &size);
	if (ident[EI_CLASS] == ELFCLASS32)
		elfclass = ELFCLASS32;
	if (ident[EI_CLASS] == ELFCLASS64)
		elfclass = ELFCLASS64;
	close(elffd);
	return (elfclass);
}
예제 #2
0
/*
 * get_executable - return file descriptor for PATH-resolved
 *		    target file.
 *
 */
static int
get_executable(char *name) {
    int fd = -1;

    if (name != NULL) {
	char path[PATH_MAX + 1];
	char line[MAX_INPUT + 1];
	char *p = line;
	char *fname = name;
	int N = sizeof (line);
	struct stat file_att;

	while (*fname == ' ') fname++;
	if (fname[0] == '-' || strchr(fname, '/')) {
	    fd = open(fname, O_RDONLY);
	} else {
	    int len = strlen(fname);
	    char *dirlist = getenv("PATH");
	    char *dir = NULL;

	    if (dirlist != NULL) {
		dirlist = strdup(dirlist);
		dir = strtok(dirlist, ":");
	    }
	    while (fd < 0 && dir != NULL) {
		if ((strlen(dir) + len + 1) < sizeof (path)) {
		    strcat(strcat(strcpy(path, dir), "/"), fname);
		    fd = open(path, O_RDONLY);
		}
		dir = strtok(NULL, ":");
	    }
	    if (dirlist != NULL) free(dirlist);
	}
	if (fstat(fd, &file_att) || !S_ISREG(file_att.st_mode)) {
	    if (fd >= 0)
		close(fd);
	    return (-1);
	}
	if (read(fd, p, 2) && p[0] == '#' && p[1] == '!') {
	    while (N-- > 1 && read(fd, p, 1) && *p != '\n')
		p++;
	    *p = '\0';
	    close(fd);
	    return (get_executable(line));
	}
	if (fd >= 0) lseek(fd, 0, SEEK_SET);
	} /* %$#@! cstyle complaint */
    return (fd);
}
예제 #3
0
파일: main.c 프로젝트: Kjam-Victory/Hadoop
static void assert_valid_setup(char *current_executable) {
  char *executable_file = get_executable();

  char *orig_conf_file = HADOOP_CONF_DIR "/" CONF_FILENAME;
  char *conf_file = resolve_config_path(orig_conf_file, current_executable);

  if (conf_file == NULL) {
    fprintf(ERRORFILE, "Configuration file %s not found.\n", orig_conf_file);
    flush_and_close_log_files();
    exit(INVALID_CONFIG_FILE);
  }

  if (check_configuration_permissions(conf_file) != 0) {
    flush_and_close_log_files();
    exit(INVALID_CONFIG_FILE);
  }
  read_executor_config(conf_file);
  free(conf_file);

  // look up the node manager group in the config file
  char *nm_group = get_nodemanager_group();
  if (nm_group == NULL) {
    fprintf(ERRORFILE, "Can't get configured value for %s.\n", NM_GROUP_KEY);
    flush_and_close_log_files();
    exit(INVALID_CONFIG_FILE);
  }
  struct group *group_info = getgrnam(nm_group);
  if (group_info == NULL) {
    fprintf(ERRORFILE, "Can't get group information for %s - %s.\n", nm_group,
            strerror(errno));
    flush_and_close_log_files();
    exit(INVALID_CONFIG_FILE);
  }
  set_nm_uid(getuid(), group_info->gr_gid);
  // if we are running from a setuid executable, make the real uid root
  setuid(0);
  // set the real and effective group id to the node manager group
  setgid(group_info->gr_gid);

  if (check_executor_permissions(executable_file) != 0) {
    fprintf(ERRORFILE, "Invalid permissions on container-executor binary.\n");
    flush_and_close_log_files();
    exit(INVALID_CONTAINER_EXEC_PERMISSIONS);
  }
}
예제 #4
0
MWReturn 
MWDriver_blackbox::pack_worker_init_data( void ) 
{
	string exe = get_executable();
	list<string> l;
	l.push_back(exe);

	for (set<string>::const_iterator it = stageFiles_.begin(); 
		 it != stageFiles_.end(); ++it) {
		l.push_back(*it);
	}

	MWTask_blackbox::send_files(l);

	

	return OK;
}
예제 #5
0
파일: libhost.cpp 프로젝트: krwq/cli
host_mode_t detect_operating_mode(const int argc, const pal::char_t* argv[], pal::string_t* p_own_dir)
{
    pal::string_t own_path;
    if (!pal::get_own_executable_path(&own_path) || !pal::realpath(&own_path))
    {
        trace::error(_X("Failed to locate current executable"));
        return host_mode_t::invalid;
    }

    pal::string_t own_name = get_filename(own_path);
    pal::string_t own_dir = get_directory(own_path);
    if (p_own_dir)
    {
        p_own_dir->assign(own_dir);
    }

    pal::string_t own_dll_filename = get_executable(own_name) + _X(".dll");
    pal::string_t own_dll = own_dir;
    append_path(&own_dll, own_dll_filename.c_str());
    trace::info(_X("Own DLL path=[%s]"), own_dll.c_str());
    if (coreclr_exists_in_dir(own_dir) || pal::file_exists(own_dll))
    {
        pal::string_t own_deps_json = own_dir;
        pal::string_t own_deps_filename = strip_file_ext(own_name) + _X(".deps.json");
        pal::string_t own_config_filename = strip_file_ext(own_name) + _X(".runtimeconfig.json");
        append_path(&own_deps_json, own_deps_filename.c_str());
        if (trace::is_enabled())
        {
            trace::info(_X("Detecting mode... CoreCLR present in own dir [%s] and checking if [%s] file present=[%d]"),
                own_dir.c_str(), own_deps_filename.c_str(), pal::file_exists(own_deps_json));
        }
        return ((pal::file_exists(own_deps_json) || !pal::file_exists(own_config_filename)) && pal::file_exists(own_dll)) ? host_mode_t::standalone : host_mode_t::split_fx;
    }
    else
    {
        return host_mode_t::muxer;
    }
}
예제 #6
0
파일: main.c 프로젝트: SampathCA/platform
int main(int argc, char **argv) {
  //LOGFILE = stdout;
  LOGFILE = fopen("/tmp/taskcontroller.log", "a+");

  int i=0;
  for (i=0; i<argc; i++)
    fprintf(LOGFILE, "Arg[%i] = %s\n", i, argv[i]);

  //Minimum number of arguments required to run the task-controller
  if (argc < 4) {
    display_usage(LOGFILE);
    return INVALID_ARGUMENT_NUMBER;
  }

  int command;
  const char * job_id = NULL;
  const char * task_id = NULL;
  const char * cred_file = NULL;
  const char * script_file = NULL;
  const char * current_dir = NULL;
  const char * job_xml = NULL;

  int exit_code = 0;

  char * dir_to_be_deleted = NULL;

  char *executable_file = get_executable();

#ifndef HADOOP_CONF_DIR
  #error HADOOP_CONF_DIR must be defined
#endif

  char *orig_conf_file = STRINGIFY(HADOOP_CONF_DIR) "/" CONF_FILENAME;
  char *conf_file = realpath(orig_conf_file, NULL);

  if (conf_file == NULL) {
    fprintf(LOGFILE, "Configuration file %s not found.\n", orig_conf_file);
    return INVALID_CONFIG_FILE;
  }
  if (check_configuration_permissions(conf_file) != 0) {
    return INVALID_CONFIG_FILE;
  }
  read_config(conf_file);
  free(conf_file);

  // look up the task tracker group in the config file
  char *tt_group = get_value(TT_GROUP_KEY);
  if (tt_group == NULL) {
    fprintf(LOGFILE, "Can't get configured value for %s.\n", TT_GROUP_KEY);
    exit(INVALID_CONFIG_FILE);
  }
  struct group *group_info = getgrnam(tt_group);
  if (group_info == NULL) {
    fprintf(LOGFILE, "Can't get group information for %s - %s.\n", tt_group,
            strerror(errno));
    exit(INVALID_CONFIG_FILE);
  }
  set_tasktracker_uid(getuid(), group_info->gr_gid);
  // if we are running from a setuid executable, make the real uid root
  setuid(0);
  // set the real and effective group id to the task tracker group
  setgid(group_info->gr_gid);

  if (check_taskcontroller_permissions(executable_file) != 0) {
    fprintf(LOGFILE, "Invalid permissions on task-controller binary.\n");
    return INVALID_TASKCONTROLLER_PERMISSIONS;
  }

  //checks done for user name
  if (argv[optind] == NULL) {
    fprintf(LOGFILE, "Invalid user name \n");
    return INVALID_USER_NAME;
  }
  int ret = set_user(argv[optind]);
  if (ret != 0) {
    return ret;
  }

  optind = optind + 1;
  command = atoi(argv[optind++]);

  fprintf(LOGFILE, "main : command provided %d\n",command);
  fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);

  switch (command) {
  case INITIALIZE_JOB:
    if (argc < 7) {
      fprintf(LOGFILE, "Too few arguments (%d vs 7) for initialize job\n",
	      argc);
      return INVALID_ARGUMENT_NUMBER;
    }
    job_id = argv[optind++];
    cred_file = argv[optind++];
    job_xml = argv[optind++];
    exit_code = initialize_job(user_detail->pw_name, job_id, cred_file,
                               job_xml, argv + optind);
    break;
  case LAUNCH_TASK_JVM:
    if (argc < 7) {
      fprintf(LOGFILE, "Too few arguments (%d vs 7) for launch task\n",
	      argc);
      return INVALID_ARGUMENT_NUMBER;
    }
    job_id = argv[optind++];
    task_id = argv[optind++];
    current_dir = argv[optind++];
    script_file = argv[optind++];
    exit_code = run_task_as_user(user_detail->pw_name, job_id, task_id, 
                                 current_dir, script_file);
    break;
  case SIGNAL_TASK:
    if (argc < 5) {
      fprintf(LOGFILE, "Too few arguments (%d vs 5) for signal task\n",
	      argc);
      return INVALID_ARGUMENT_NUMBER;
    } else {
      char* end_ptr = NULL;
      char* option = argv[optind++];
      int task_pid = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(LOGFILE, "Illegal argument for task pid %s\n", option);
        return INVALID_ARGUMENT_NUMBER;
      }
      option = argv[optind++];
      int signal = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(LOGFILE, "Illegal argument for signal %s\n", option);
        return INVALID_ARGUMENT_NUMBER;
      }
      exit_code = signal_user_task(user_detail->pw_name, task_pid, signal);
    }
    break;
  case DELETE_AS_USER:
    dir_to_be_deleted = argv[optind++];
    exit_code= delete_as_user(user_detail->pw_name, dir_to_be_deleted);
    break;
  case DELETE_LOG_AS_USER:
    dir_to_be_deleted = argv[optind++];
    exit_code= delete_log_directory(dir_to_be_deleted);
    break;
  case RUN_COMMAND_AS_USER:
    exit_code = run_command_as_user(user_detail->pw_name, argv + optind);
    break;
  default:
    exit_code = INVALID_COMMAND_PROVIDED;
  }
  fclose(LOGFILE);
  return exit_code;
}
예제 #7
0
int main(int argc, char *argv[])
{
	int rtc = 0, file_arg = 0, do_what = 0, no_db = 1, allowopt = 1;
	char *config_file = NULL, *tmp_dir = NULL;
	char *domain = NULL, *sender = NULL;

	set_parm_logfun(&stderrlog);

	for (int i = 1; i < argc; ++i)
	{
		char *arg = argv[i];

		if (allowopt && arg[0] == '-')
		{
			if (arg[1] != '-')
			{
				char **target;
				switch (arg[1])
				{
					case 'f':
						target = &config_file;
						break;
					case 't':
						target = &tmp_dir;
						break;
					default:
						fprintf(stderr,
							"dkimsign: invalid option: %s\n", arg);
						return 1;
				}

				if (arg[2])
					*target = &arg[2];
				else
					*target = ++i < argc ? argv[i] : NULL;
			}
			else if (arg[2] == 0)
			{
				allowopt = 0;
			}
			else if (strcmp(arg, "--syslog") == 0)
			{
				do_what |= do_syslog;
			}
			else if (strcmp(arg, "--domain") == 0)
			{
				domain = ++i < argc ? argv[i] : NULL;
			}
			else if (strcmp(arg, "--sender") == 0)
			{
				sender = ++i < argc ? argv[i] : NULL;
			}
			else if (strcmp(arg, "--config") == 0)
			{
				do_what |= do_config;
			}
			else if (strcmp(arg, "--version") == 0)
			{
				do_what |= do_version;
			}
			else if (strcmp(arg, "--filter") == 0)
			{
				do_what |= do_filter;
				do_what |= do_syslog;
			}
			else if (strcmp(arg, "--db-filter") == 0)
			{
				do_what |= do_filter;
				do_what |= do_syslog;
				no_db = 0;
			}
			else if (strcmp(arg, "--help") == 0)
			{
				printf("This is a wrapper around the zdkimfilter executable.\n"
					"Usage:\n"
					"           dkimsign [opts] message-file...\n"
					"with opts:\n"
					"  -f config-filename  override %s\n"
					"  -t temp-dir         override the temporary directory\n"
					"  --syslog            use syslog (MAIL) rather than stderr\n"
					"  --filter            use stdin and ignore any message-file\n"
					"  --db-filter         same as filter, but enable db logging\n"
					"  --domain domain     signing domain, can be full address\n"
					"  --sender sender     envelope sender if different from domain\n"
					"  --config            have the exec check and print config\n"
					"  --help              print this stuff and exit\n"
					"  --version           have the exec print version and exit\n",
						default_config_file);
				return 0;
			}
			else
			{
				fprintf(stderr,
					"dkimsign: invalid option: %s\n", arg);
				return 1;
			}
		}
		else // message files
		{
			file_arg = i;
			break;
		}
	}

	if (file_arg == 0 && (do_what & ~do_syslog) == 0)
		return 1;

	// hack to ease auto tests
	if (strcmp(argv[argc-1], "--batch-test") == 0)
	{
		do_what &= ~do_syslog;
		no_needless_logging = 1;
	}

	if (do_what & do_syslog)
	{
		openlog("dkimsign", LOG_PID, LOG_MAIL);
		set_parm_logfun(do_report = &syslog);
	}

	char *xargv[argc - file_arg + 10];
	size_t xargc = 0;

	char *ctlfile = NULL;
	char *msgfile = NULL;
	char *execfile = get_executable(argv[0]);
	if (execfile == NULL)
	{
		(*do_report)(LOG_CRIT, "Cannot find the zdkimfilter executable");
		rtc = 1;
	}
	else
		xargv[xargc++] = execfile;

	if (config_file)
	{
		xargv[xargc++] = "-f";
		xargv[xargc++] = config_file;
	}

	if (do_what & do_version)
	{
		do_what &= ~do_config;
		xargv[xargc++] = "--version";
	}
	else if (do_what & do_config)
	{
		xargv[xargc++] = "--batch-test";
	}
	else if ((file_arg || (do_what & do_filter)) && rtc == 0)
	{
		do_what |= do_mail;
		rtc = create_tmpfiles(config_file, domain, sender, tmp_dir, &ctlfile,
			(do_what & do_filter)? &msgfile: NULL, no_db == 0);

		if (rtc == 0)
		{
			if (no_db)
				xargv[xargc++] = "--no-db";
			if ((do_what & do_filter) != 0)
				xargv[xargc++] = "--no-fork";
			xargv[xargc++] = "-t1,dkimsign";
			xargv[xargc++] = ctlfile;
			if (file_arg)
				for (int i = file_arg; i < argc; ++i)
					xargv[xargc++] = argv[i];
		}
	}

	if (rtc == 0)
	{
		xargv[xargc] = NULL;
		set_signal();

		rtc = run_zdkimfilter(xargv, do_what);
	}

	if (do_what & do_syslog)
		closelog();

	if (ctlfile)
	{
		if (!no_needless_logging)
			unlink(ctlfile);
		free(ctlfile);
	}

	if (msgfile)
	{
		unlink(msgfile);
		free(msgfile);
	}

	if (execfile && execfile != zdkimfilter_executable)
		free(execfile);

	return rtc;
}
예제 #8
0
파일: main.c 프로젝트: EddiePi/yarn-cpu
int main(int argc, char **argv) {
  int invalid_args = 0; 
  int do_check_setup = 0;
  int do_mount_cgroups = 0;
  
  LOGFILE = stdout;
  ERRORFILE = stderr;

  if (argc > 1) {
    if (strcmp("--mount-cgroups", argv[1]) == 0) {
      do_mount_cgroups = 1;
    }
  }

  // Minimum number of arguments required to run 
  // the std. container-executor commands is 4
  // 4 args not needed for checksetup option
  if (argc < 4 && !do_mount_cgroups) {
    invalid_args = 1;
    if (argc == 2) {
      const char *arg1 = argv[1];
      if (strcmp("--checksetup", arg1) == 0) {
        invalid_args = 0;
        do_check_setup = 1;        
      }      
    }
  }
  
  if (invalid_args != 0) {
    display_usage(stdout);
    return INVALID_ARGUMENT_NUMBER;
  }

  int command;
  const char * app_id = NULL;
  const char * container_id = NULL;
  const char * cred_file = NULL;
  const char * script_file = NULL;
  const char * current_dir = NULL;
  const char * pid_file = NULL;

  int exit_code = 0;

  char * dir_to_be_deleted = NULL;

  char *executable_file = get_executable();

  char *orig_conf_file = HADOOP_CONF_DIR "/" CONF_FILENAME;
  char *conf_file = resolve_config_path(orig_conf_file, argv[0]);
  char *local_dirs, *log_dirs;
  char *resources, *resources_key, *resources_value;

  if (conf_file == NULL) {
    fprintf(ERRORFILE, "Configuration file %s not found.\n", orig_conf_file);
    exit(INVALID_CONFIG_FILE);
  }
  if (check_configuration_permissions(conf_file) != 0) {
    exit(INVALID_CONFIG_FILE);
  }
  read_config(conf_file);
  free(conf_file);

  // look up the node manager group in the config file
  char *nm_group = get_value(NM_GROUP_KEY);
  if (nm_group == NULL) {
    fprintf(ERRORFILE, "Can't get configured value for %s.\n", NM_GROUP_KEY);
    exit(INVALID_CONFIG_FILE);
  }
  struct group *group_info = getgrnam(nm_group);
  if (group_info == NULL) {
    fprintf(ERRORFILE, "Can't get group information for %s - %s.\n", nm_group,
            strerror(errno));
    fflush(LOGFILE);
    exit(INVALID_CONFIG_FILE);
  }
  set_nm_uid(getuid(), group_info->gr_gid);
  // if we are running from a setuid executable, make the real uid root
  setuid(0);
  // set the real and effective group id to the node manager group
  setgid(group_info->gr_gid);

  if (check_executor_permissions(executable_file) != 0) {
    fprintf(ERRORFILE, "Invalid permissions on container-executor binary.\n");
    return INVALID_CONTAINER_EXEC_PERMISSIONS;
  }

  if (do_check_setup != 0) {
    // basic setup checks done
    // verified configs available and valid
    // verified executor permissions
    return 0;
  }

  if (do_mount_cgroups) {
    optind++;
    char *hierarchy = argv[optind++];
    int result = 0;

    while (optind < argc && result == 0) {
      result = mount_cgroup(argv[optind++], hierarchy);
    }

    return result;
  }

  //checks done for user name
  if (argv[optind] == NULL) {
    fprintf(ERRORFILE, "Invalid user name.\n");
    return INVALID_USER_NAME;
  }

  int ret = set_user(argv[optind]);
  if (ret != 0) {
    return ret;
  }

  // this string is used for building pathnames, the
  // process management is done based on the 'user_detail'
  // global, which was set by 'set_user()' above
  optind = optind + 1;
  char *yarn_user_name = argv[optind];
  if (yarn_user_name == NULL) {
    fprintf(ERRORFILE, "Invalid yarn user name.\n");
    return INVALID_USER_NAME;
  }
 
  optind = optind + 1;
  command = atoi(argv[optind++]);

  fprintf(LOGFILE, "main : command provided %d\n",command);
  fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);
  fprintf(LOGFILE, "main : requested yarn user is %s\n", yarn_user_name);
  fflush(LOGFILE);

  switch (command) {
  case INITIALIZE_CONTAINER:
    if (argc < 9) {
      fprintf(ERRORFILE, "Too few arguments (%d vs 9) for initialize container\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    app_id = argv[optind++];
    cred_file = argv[optind++];
    local_dirs = argv[optind++];// good local dirs as a comma separated list
    log_dirs = argv[optind++];// good log dirs as a comma separated list
    exit_code = initialize_app(yarn_user_name, app_id, cred_file,
                               extract_values(local_dirs),
                               extract_values(log_dirs), argv + optind);
    break;
  case LAUNCH_CONTAINER:
    if (argc != 13) {
      fprintf(ERRORFILE, "Wrong number of arguments (%d vs 13) for launch container\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    app_id = argv[optind++];
    container_id = argv[optind++];
    current_dir = argv[optind++];
    script_file = argv[optind++];
    cred_file = argv[optind++];
    pid_file = argv[optind++];
    local_dirs = argv[optind++];// good local dirs as a comma separated list
    log_dirs = argv[optind++];// good log dirs as a comma separated list
    resources = argv[optind++];// key,value pair describing resources
    char *resources_key = malloc(strlen(resources));
    char *resources_value = malloc(strlen(resources));
    if (get_kv_key(resources, resources_key, strlen(resources)) < 0 ||
        get_kv_value(resources, resources_value, strlen(resources)) < 0) {
        fprintf(ERRORFILE, "Invalid arguments for cgroups resources: %s",
                           resources);
        fflush(ERRORFILE);
        free(resources_key);
        free(resources_value);
        return INVALID_ARGUMENT_NUMBER;
    }
    char** resources_values = extract_values(resources_value);
    exit_code = launch_container_as_user(yarn_user_name, app_id,
                    container_id, current_dir, script_file, cred_file,
                    pid_file, extract_values(local_dirs),
                    extract_values(log_dirs), resources_key,
                    resources_values);
    free(resources_key);
    free(resources_value);
    break;
  case SIGNAL_CONTAINER:
    if (argc != 6) {
      fprintf(ERRORFILE, "Wrong number of arguments (%d vs 6) for " \
          "signal container\n", argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    } else {
      char* end_ptr = NULL;
      char* option = argv[optind++];
      int container_pid = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(ERRORFILE, "Illegal argument for container pid %s\n", option);
        fflush(ERRORFILE);
        return INVALID_ARGUMENT_NUMBER;
      }
      option = argv[optind++];
      int signal = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(ERRORFILE, "Illegal argument for signal %s\n", option);
        fflush(ERRORFILE);
        return INVALID_ARGUMENT_NUMBER;
      }
      exit_code = signal_container_as_user(yarn_user_name, container_pid, signal);
    }
    break;
  case DELETE_AS_USER:
    dir_to_be_deleted = argv[optind++];
    exit_code= delete_as_user(yarn_user_name, dir_to_be_deleted,
                              argv + optind);
    break;
  default:
    fprintf(ERRORFILE, "Invalid command %d not supported.",command);
    fflush(ERRORFILE);
    exit_code = INVALID_COMMAND_PROVIDED;
  }
  fclose(LOGFILE);
  fclose(ERRORFILE);
  return exit_code;
}
예제 #9
0
		TITANIUM_PROPERTY_GETTER(File, executable)
		{
			return get_context().CreateBoolean(get_executable());
		}
예제 #10
0
파일: main.c 프로젝트: hmcl/storm-apache
int main(int argc, char **argv) {
  int invalid_args = 0; 
  int do_check_setup = 0;
  
  LOGFILE = stdout;
  ERRORFILE = stderr;

  // Minimum number of arguments required to run 
  // the std. worker-launcher commands is 3
  // 3 args not needed for checksetup option
  if (argc < 3) {
    invalid_args = 1;
    if (argc == 2) {
      const char *arg1 = argv[1];
      if (strcmp("--checksetup", arg1) == 0) {
        invalid_args = 0;
        do_check_setup = 1;        
      }
    }
  }
  
  if (invalid_args != 0) {
    display_usage(stdout);
    return INVALID_ARGUMENT_NUMBER;
  }

  const char * command = NULL;
  const char * working_dir = NULL;

  int exit_code = 0;

  char *executable_file = get_executable();

  char *orig_conf_file = STRINGIFY(EXEC_CONF_DIR) "/" CONF_FILENAME;
  char *conf_file = realpath(orig_conf_file, NULL);

  if (conf_file == NULL) {
    fprintf(ERRORFILE, "Configuration file %s not found.\n", orig_conf_file);
    exit(INVALID_CONFIG_FILE);
  }
  if (do_check_setup) {
    fprintf(LOGFILE, "Using configuration file %s \n", conf_file);
  }
  if (check_configuration_permissions(conf_file) != 0) {
    exit(INVALID_CONFIG_FILE);
  }
  read_config(conf_file);

  // look up the worker launcher group in the config file
  char *wl_group = get_value(LAUNCHER_GROUP_KEY);
  if (wl_group == NULL) {
    fprintf(ERRORFILE, "Can't get configured value for %s.\n", LAUNCHER_GROUP_KEY);
    exit(INVALID_CONFIG_FILE);
  }
  struct group *group_info = getgrnam(wl_group);
  if (group_info == NULL) {
    fprintf(ERRORFILE, "Can't get group information for %s - %s.\n", wl_group,
            strerror(errno));
    fflush(LOGFILE);
    exit(INVALID_CONFIG_FILE);
  }

  set_launcher_uid(getuid(), group_info->gr_gid);
  // if we are running from a setuid executable, make the real uid root
  setuid(0);
  // set the real and effective group id to the node manager group
  setgid(group_info->gr_gid);

  if (check_executor_permissions(executable_file) != 0) {
    fprintf(LOGFILE, "worker-launcher config file: %s \n", conf_file);
    free(conf_file);
    conf_file = NULL;
    fprintf(ERRORFILE, "ERROR: Invalid permissions on worker-launcher binary.\n");
    return INVALID_CONTAINER_EXEC_PERMISSIONS;
  }

  free(conf_file);
  conf_file = NULL;

  if (do_check_setup != 0) {
    // basic setup checks done
    // verified configs available and valid
    // verified executor permissions
    return 0;
  }

  //checks done for user name
  if (argv[optind] == NULL) {
    fprintf(ERRORFILE, "Invalid user name.\n");
    return INVALID_USER_NAME;
  }

  int ret = set_user(argv[optind]);
  if (ret != 0) {
    return ret;
  }
 
  optind = optind + 1;
  command = argv[optind++];

  fprintf(LOGFILE, "main : command provided %s\n",command);
  fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);
  fflush(LOGFILE);

  if (strcasecmp("code-dir", command) == 0) {
    if (argc != 4) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 4) for code-dir\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    exit_code = setup_dir_permissions(argv[optind], 0);
  } else if (strcasecmp("artifacts-dir", command) == 0) {
    if (argc != 4) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 4) for artifacts-dir\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    exit_code = setup_dir_permissions(argv[optind], 1);
  } else if (strcasecmp("blob", command) == 0) {
      if (argc != 4) {
          fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 4) for blob\n",
                  argc);
          fflush(ERRORFILE);
          return INVALID_ARGUMENT_NUMBER;
      }
      exit_code = setup_dir_permissions(argv[optind], 0);
  } else if (strcasecmp("rmr", command) == 0) {
    if (argc != 4) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 4) for rmr\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    exit_code = recursive_delete(argv[optind], 1);
  } else if (strcasecmp("worker", command) == 0) {
    if (argc != 5) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 5) for worker\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    working_dir = argv[optind++];
    exit_code = setup_dir_permissions(working_dir, 1);
    if (exit_code == 0) {
      exit_code = exec_as_user(working_dir, argv[optind]);
    }
   } else if (strcasecmp("profiler", command) == 0) {
    if (argc != 5) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 5) for profiler\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    working_dir = argv[optind++];
    exit_code = exec_as_user(working_dir, argv[optind]);
  } else if (strcasecmp("signal", command) == 0) {
    if (argc != 5) {
      fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 5) for signal\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    char* end_ptr = NULL;
    char* option = argv[optind++];
    int container_pid = strtol(option, &end_ptr, 10);
    if (option == end_ptr || *end_ptr != '\0') {
      fprintf(ERRORFILE, "Illegal argument for container pid %s\n", option);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    option = argv[optind++];
    int signal = strtol(option, &end_ptr, 10);
    if (option == end_ptr || *end_ptr != '\0') {
      fprintf(ERRORFILE, "Illegal argument for signal %s\n", option);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    exit_code = signal_container_as_user(user_detail->pw_name, container_pid, signal);
  } else {
    fprintf(ERRORFILE, "Invalid command %s not supported.",command);
    fflush(ERRORFILE);
    exit_code = INVALID_COMMAND_PROVIDED;
  }
  fclose(LOGFILE);
  fclose(ERRORFILE);
  return exit_code;
}
예제 #11
0
/* static */
int fx_muxer_t::execute(const int argc, const pal::char_t* argv[])
{
    pal::string_t own_path;

    // Get the full name of the application
    if (!pal::get_own_executable_path(&own_path) || !pal::realpath(&own_path))
    {
        trace::error(_X("Failed to resolve full path of the current executable [%s]"), own_path.c_str());
        return StatusCode::LibHostCurExeFindFailure;
    }
    pal::string_t own_name = get_filename(own_path);
    pal::string_t own_dir = get_directory(own_path);

    pal::string_t own_dll_filename = get_executable(own_name) + _X(".dll");
    pal::string_t own_dll = own_dir;
    append_path(&own_dll, own_dll_filename.c_str());

    trace::info(_X("Own DLL path=[%s]"), own_dll.c_str());
    auto mode = detect_operating_mode(own_dir, own_dll, own_name);
    bool is_an_app = true;
    if (mode == host_mode_t::split_fx)
    {
        trace::verbose(_X("--- Executing in split/FX mode..."));
        return parse_args_and_execute(own_dir, own_dll, 1, argc, argv, false, host_mode_t::split_fx, &is_an_app);
    }
    if (mode == host_mode_t::standalone)
    {
        trace::verbose(_X("--- Executing in standalone mode..."));
        return parse_args_and_execute(own_dir, own_dll, 1, argc, argv, false, host_mode_t::standalone, &is_an_app);
    }

    trace::verbose(_X("--- Executing in muxer mode..."));

    if (argc <= 1)
    {
        return muxer_usage();
    }

    if (pal::strcasecmp(_X("exec"), argv[1]) == 0)
    {
        return parse_args_and_execute(own_dir, own_dll, 2, argc, argv, true, host_mode_t::muxer, &is_an_app); // arg offset 2 for dotnet, exec
    }

    int result = parse_args_and_execute(own_dir, own_dll, 1, argc, argv, false, host_mode_t::muxer, &is_an_app); // arg offset 1 for dotnet
    if (is_an_app)
    {
        return result;
    }

    // Could not execute as an app, try the CLI SDK dotnet.dll
    pal::string_t sdk_dotnet;
    if (!resolve_sdk_dotnet_path(own_dir, &sdk_dotnet))
    {
        assert(argc > 1);
        if (pal::strcasecmp(_X("--help"), argv[1]) == 0 ||
            pal::strcasecmp(_X("--version"), argv[1]) == 0 ||
            pal::strcasecmp(_X("-h"), argv[1]) == 0 ||
            pal::strcasecmp(_X("-v"), argv[1]) == 0)
        {
            return muxer_usage();
        }
        trace::error(_X("Did you mean to run dotnet SDK commands? Please install dotnet SDK from: "));
        trace::error(_X("  %s"), s_dotnet_sdk_download_url);
        return StatusCode::LibHostSdkFindFailure;
    }
    append_path(&sdk_dotnet, _X("dotnet.dll"));

    if (!pal::file_exists(sdk_dotnet))
    {
        trace::error(_X("Found dotnet SDK, but did not find dotnet.dll at [%s]"), sdk_dotnet.c_str());
        return StatusCode::LibHostSdkFindFailure;
    }

    // Transform dotnet [command] [args] -> dotnet dotnet.dll [command] [args]

    std::vector<const pal::char_t*> new_argv(argc + 1);
    memcpy(&new_argv.data()[2], argv + 1, (argc - 1) * sizeof(pal::char_t*));
    new_argv[0] = argv[0];
    new_argv[1] = sdk_dotnet.c_str();

    trace::verbose(_X("Using dotnet SDK dll=[%s]"), sdk_dotnet.c_str());
    return parse_args_and_execute(own_dir, own_dll, 1, new_argv.size(), new_argv.data(), false, host_mode_t::muxer, &is_an_app);
}
예제 #12
0
int main(int argc, char *argv[], char *env[]) {
    // Input buffer and and commands
    char buffer[BUFFER_LEN] = { 0 };
    char command[BUFFER_LEN] = { 0 };
    char arg[BUFFER_LEN] = { 0 };
    char delim[BUFFER_LEN];
    char* token = NULL;
    char* SHELL = NULL;

	setenv("SHELL", get_executable(SHELL), 1);
	free(SHELL);


    // Parse the commands provided using argc and argv
	FILE *in_stream = NULL;
	if(argc > 1){
		in_stream = fopen(argv[1], "r");
		if(in_stream == NULL){
			printf("Could not open file %s", argv[1]);
			return EXIT_FAILURE;
		}
		
	} else {
		in_stream = stdin;
		printf("%s> ", get_cwd());
	}

	
    // Perform an infinite loop getting command input from users
    while (fgets(buffer, BUFFER_LEN, in_stream) != NULL) {
        strcpy(arg, ""); // reset arg
        strcpy(command, ""); // reset command
        // Perform string tokenization to get 
		// the command and argument
        strcpy(delim, " ");
        token = strtok(buffer, delim); // get first word
		// remove newline character if at end of string
        if (token[strlen(token)-1] == '\n')
            token[strlen(token)-1] = '\0'; 
        strcpy(command, token); // copy token to command

        // loop to get second word in buffer
        while(token != NULL) {
            strcpy(delim, "\n");
            token = strtok(NULL, delim);

            if (token == NULL)
                break;
            strcpy(arg, token); // store second word in arg 
        }

        // Check command and execute operations for each command
        // cd command -- change the current directory
        if (strcmp(command, "cd") == 0) {
            if (strcmp(arg, " ") == 0 || strcmp(arg, "") == 0)
                printf("Error: cd: argument required\n");
            cd(arg);
        } else if (strcmp(command, "clr") == 0) {
            clr();
        } else if (strcmp(command, "dir") == 0) {
            dir(arg);
        } else if (strcmp(command, "environ") == 0) {
            environ(env);
        } else if (strcmp(command, "echo") == 0) {
            echo(arg);
        } else if (strcmp(command, "help") == 0) {
			help();
        } else if (strcmp(command, "pause") == 0) {
            pauses();
        } else if (strcmp(command, "") == 0) {
            
        } 
        
        // quit command -- exit the shell
        else if (strcmp(command, "quit") == 0) {
			free(token), fclose(in_stream);
            return EXIT_SUCCESS;
        }

        // Unsupported command
        else {
            fputs("Unsupported command, use help to display the manual\n", stderr);
        }
		if(argc == 1)
			printf("%s> ", get_cwd());

    }
    free(token), fclose(in_stream); // free the token pointer
    return EXIT_SUCCESS;
}
예제 #13
0
int main(int argc, char **argv) {
  int invalid_args = 0; 
  int do_check_setup = 0;
  
  LOGFILE = stdout;
  ERRORFILE = stderr;

  // Minimum number of arguments required to run 
  // the std. container-executor commands is 4
  // 4 args not needed for checksetup option
  if (argc < 4) {
    invalid_args = 1;
    if (argc == 2) {
      const char *arg1 = argv[1];
      if (strcmp("--checksetup", arg1) == 0) {
        invalid_args = 0;
        do_check_setup = 1;        
      }      
    }
  }
  
  if (invalid_args != 0) {
    display_usage(stdout);
    return INVALID_ARGUMENT_NUMBER;
  }

  int command;
  const char * app_id = NULL;
  const char * container_id = NULL;
  const char * cred_file = NULL;
  const char * script_file = NULL;
  const char * current_dir = NULL;
  const char * pid_file = NULL;

  int exit_code = 0;

  char * dir_to_be_deleted = NULL;

  char *executable_file = get_executable();

  char *orig_conf_file = STRINGIFY(HADOOP_CONF_DIR) "/" CONF_FILENAME;
  char *conf_file = realpath(orig_conf_file, NULL);

  if (conf_file == NULL) {
    fprintf(ERRORFILE, "Configuration file %s not found.\n", orig_conf_file);
    exit(INVALID_CONFIG_FILE);
  }
  if (check_configuration_permissions(conf_file) != 0) {
    exit(INVALID_CONFIG_FILE);
  }
  read_config(conf_file);
  free(conf_file);

  // look up the node manager group in the config file
  char *nm_group = get_value(NM_GROUP_KEY);
  if (nm_group == NULL) {
    fprintf(ERRORFILE, "Can't get configured value for %s.\n", NM_GROUP_KEY);
    exit(INVALID_CONFIG_FILE);
  }
  struct group *group_info = getgrnam(nm_group);
  if (group_info == NULL) {
    fprintf(ERRORFILE, "Can't get group information for %s - %s.\n", nm_group,
            strerror(errno));
    fflush(LOGFILE);
    exit(INVALID_CONFIG_FILE);
  }
  set_nm_uid(getuid(), group_info->gr_gid);
  // if we are running from a setuid executable, make the real uid root
  setuid(0);
  // set the real and effective group id to the node manager group
  setgid(group_info->gr_gid);

  if (check_executor_permissions(executable_file) != 0) {
    fprintf(ERRORFILE, "Invalid permissions on container-executor binary.\n");
    return INVALID_CONTAINER_EXEC_PERMISSIONS;
  }

  if (do_check_setup != 0) {
    // basic setup checks done
    // verified configs available and valid
    // verified executor permissions
    return 0;
  }

  //checks done for user name
  if (argv[optind] == NULL) {
    fprintf(ERRORFILE, "Invalid user name.\n");
    return INVALID_USER_NAME;
  }

  int ret = set_user(argv[optind]);
  if (ret != 0) {
    return ret;
  }
 
  optind = optind + 1;
  command = atoi(argv[optind++]);

  fprintf(LOGFILE, "main : command provided %d\n",command);
  fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);
  fflush(LOGFILE);

  switch (command) {
  case INITIALIZE_CONTAINER:
    if (argc < 6) {
      fprintf(ERRORFILE, "Too few arguments (%d vs 6) for initialize container\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    app_id = argv[optind++];
    cred_file = argv[optind++];
    exit_code = initialize_app(user_detail->pw_name, app_id, cred_file,
                               argv + optind);
    break;
  case LAUNCH_CONTAINER:
    if (argc < 9) {
      fprintf(ERRORFILE, "Too few arguments (%d vs 9) for launch container\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    }
    app_id = argv[optind++];
    container_id = argv[optind++];
    current_dir = argv[optind++];
    script_file = argv[optind++];
    cred_file = argv[optind++];
    pid_file = argv[optind++];
    exit_code = launch_container_as_user(user_detail->pw_name, app_id, container_id,
                                 current_dir, script_file, cred_file, pid_file);
    break;
  case SIGNAL_CONTAINER:
    if (argc < 5) {
      fprintf(ERRORFILE, "Too few arguments (%d vs 5) for signal container\n",
	      argc);
      fflush(ERRORFILE);
      return INVALID_ARGUMENT_NUMBER;
    } else {
      char* end_ptr = NULL;
      char* option = argv[optind++];
      int container_pid = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(ERRORFILE, "Illegal argument for container pid %s\n", option);
        fflush(ERRORFILE);
        return INVALID_ARGUMENT_NUMBER;
      }
      option = argv[optind++];
      int signal = strtol(option, &end_ptr, 10);
      if (option == end_ptr || *end_ptr != '\0') {
        fprintf(ERRORFILE, "Illegal argument for signal %s\n", option);
        fflush(ERRORFILE);
        return INVALID_ARGUMENT_NUMBER;
      }
      exit_code = signal_container_as_user(user_detail->pw_name, container_pid, signal);
    }
    break;
  case DELETE_AS_USER:
    dir_to_be_deleted = argv[optind++];
    exit_code= delete_as_user(user_detail->pw_name, dir_to_be_deleted,
                              argv + optind);
    break;
  default:
    fprintf(ERRORFILE, "Invalid command %d not supported.",command);
    fflush(ERRORFILE);
    exit_code = INVALID_COMMAND_PROVIDED;
  }
  fclose(LOGFILE);
  fclose(ERRORFILE);
  return exit_code;
}
예제 #14
0
파일: args.cpp 프로젝트: dsyme/cli
bool parse_arguments(const int argc, const pal::char_t* argv[], arguments_t& args)
{
    // Get the full name of the application
    if (!pal::get_own_executable_path(&args.own_path) || !pal::realpath(&args.own_path))
    {
        trace::error(_X("Failed to locate current executable"));
        return false;
    }

    auto own_name = get_filename(args.own_path);
    auto own_dir = get_directory(args.own_path);

    if (own_name.compare(HOST_EXE_NAME) == 0)
    {
        // corerun mode. First argument is managed app
        if (argc < 2)
        {
            display_help();
            return false;
        }
        args.managed_application = pal::string_t(argv[1]);
        if (!pal::realpath(&args.managed_application))
        {
            trace::error(_X("Failed to locate managed application: %s"), args.managed_application.c_str());
            return false;
        }
        args.app_dir = get_directory(args.managed_application);
        args.app_argc = argc - 2;
        args.app_argv = &argv[2];
    }
    else
    {
        // coreconsole mode. Find the managed app in the same directory
        pal::string_t managed_app(own_dir);
        managed_app.push_back(DIR_SEPARATOR);
        managed_app.append(get_executable(own_name));
        managed_app.append(_X(".dll"));
        args.managed_application = managed_app;
        if (!pal::realpath(&args.managed_application))
        {
            trace::error(_X("Failed to locate managed application: %s"), args.managed_application.c_str());
            return false;
        }
        args.app_dir = own_dir;
        args.app_argv = &argv[1];
        args.app_argc = argc - 1;
    }

    if(args.app_argc > 0)
    {
        auto depsfile_candidate = pal::string_t(args.app_argv[0]);
        
        if (starts_with(depsfile_candidate, s_depsArgPrefix))
        {
            args.deps_path = depsfile_candidate.substr(s_depsArgPrefix.length());
            if (!pal::realpath(&args.deps_path))
            {
                trace::error(_X("Failed to locate deps file: %s"), args.deps_path.c_str());
                return false;
            }            
            args.app_argc = args.app_argc - 1;
            args.app_argv = &args.app_argv[1];
        }
    }
    
    if (args.deps_path.empty())
    {
        const auto& app_base = args.app_dir;
        auto app_name = get_filename(args.managed_application);

        args.deps_path.reserve(app_base.length() + 1 + app_name.length() + 5);
        args.deps_path.append(app_base);
        args.deps_path.push_back(DIR_SEPARATOR);
        args.deps_path.append(app_name, 0, app_name.find_last_of(_X(".")));
        args.deps_path.append(_X(".deps"));
    }

    pal::getenv(_X("DOTNET_PACKAGES"), &args.dotnet_packages);
    pal::getenv(_X("DOTNET_PACKAGES_CACHE"), &args.dotnet_packages_cache);
    pal::getenv(_X("DOTNET_SERVICING"), &args.dotnet_servicing);
    pal::getenv(_X("DOTNET_RUNTIME_SERVICING"), &args.dotnet_runtime_servicing);
    pal::getenv(_X("DOTNET_HOME"), &args.dotnet_home);
    return true;
}
예제 #15
0
static void write_context(OutputStream * out, Context * ctx, int is_thread) {
    assert(!ctx->exited);

    write_stream(out, '{');

    json_write_string(out, "ID");
    write_stream(out, ':');
    json_write_string(out, is_thread ? thread_id(ctx) : container_id(ctx));

    if (is_thread) {
        write_stream(out, ',');
        json_write_string(out, "ParentID");
        write_stream(out, ':');
        json_write_string(out, container_id(ctx));
    }

#if !defined(_WRS_KERNEL)
    write_stream(out, ',');
    json_write_string(out, "ProcessID");
    write_stream(out, ':');
    json_write_string(out, pid2id(ctx->mem, 0));
#endif

#if !defined(WIN32) && !defined(_WRS_KERNEL)
    if (!ctx->exiting && !is_thread) {
        write_stream(out, ',');
        json_write_string(out, "File");
        write_stream(out, ':');
        json_write_string(out, get_executable(ctx->pid));
    }
#endif

    if (is_thread) {
        write_stream(out, ',');
        json_write_string(out, "CanSuspend");
        write_stream(out, ':');
        json_write_boolean(out, 1);

        write_stream(out, ',');
        json_write_string(out, "CanResume");
        write_stream(out, ':');
        json_write_long(out, (1 << RM_RESUME) | (1 << RM_STEP_INTO));

        write_stream(out, ',');
        json_write_string(out, "HasState");
        write_stream(out, ':');
        json_write_boolean(out, 1);
    }

#ifdef WIN32
    if (!is_thread)
#endif
    {
        write_stream(out, ',');
        json_write_string(out, "CanTerminate");
        write_stream(out, ':');
        json_write_boolean(out, 1);
    }

    write_stream(out, '}');
}