示例#1
0
int url_connect(const char *url)
{
    const char *ptr;
    char *helper_argv[2];
    int count;

    ptr = strstr(url, "//");
    if (!ptr)
        return -1;

    if (strncmp("dir:", url, ptr - url) == 0 ||
        strncmp("ssh:", url, ptr - url) == 0)
    {
        ptr = session_getcfg(CFGKEY_HARMONY_HOME);
        if (!ptr)
            return -1;

        count = snprintf_grow(&buf, &buflen, "%s/libexec/codegen-helper", ptr);
        if (count < 0)
            return -1;

        helper_argv[0] = buf;
        helper_argv[1] = NULL;
        return socket_launch(buf, helper_argv, NULL);
    }
    else if (strncmp("tcp:", url, ptr - url) == 0) {
        /* Not implemented yet. */
    }
    return -1;
}
示例#2
0
int parse_strategies(const char *list) {
  /* Much code stolen from session-core.c's load_layers() */
  const char *prefix, *end;
  char *path;
  int path_len, retval;
  void *lib;
  strategy_t *strat;
  char *name;

  path = NULL;
  name = NULL;
  path_len = 0;
  retval = 0;
  while (list && *list) {
    if (strat_count == strat_cap) {
      if (array_grow(&strategies, &strat_cap, sizeof(strategy_t)) < 0) {
	retval = -1;
	goto cleanup;
      }
    }
    end = strchr(list, ':');
    if (!end)
      end = list + strlen(list);

    name = (char *) malloc((end - list + 1) * sizeof(char));
    if (!name) {
      retval = -1;
      goto cleanup;
    }
    strncpy(name, list, end - list);
    name[end-list] = '\0';

    prefix = session_getcfg(CFGKEY_HARMONY_HOME);
    if (snprintf_grow(&path, &path_len, "%s/libexec/%.*s",
		      prefix, end - list, list) < 0)
      {
	retval = -1;
	goto cleanup;
      }

    lib = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
    if (!lib) {
      errmsg = dlerror();
      retval = -1;
      goto cleanup;
    }

    strat->name = name;
    strat->lib = lib;

    // load function pointers into the data structure
    strat = strategies + strat_count;
    strat->generate = (strategy_generate_t) dlfptr(lib, "strategy_generate");
    strat->rejected = (strategy_rejected_t) dlfptr(lib, "strategy_rejected");
    strat->analyze  =  (strategy_analyze_t) dlfptr(lib, "strategy_analyze");
    strat->hint     =  (strategy_analyze_t) dlfptr(lib, "strategy_hint");
    strat->best     =     (strategy_best_t) dlfptr(lib, "strategy_best");

    // make sure the required functions are present
    if (!(strat->generate
	  && strat->rejected
	  && strat->analyze
	  && strat->best)) {
      errmsg = "Sub-strategy does not define all required functions";
      retval = -1;
      goto cleanup;
    }

    strat->init     =         (hook_init_t) dlfptr(lib, "strategy_init");
    strat->join     =         (hook_join_t) dlfptr(lib, "strategy_join");
    strat->getcfg   =       (hook_getcfg_t) dlfptr(lib, "strategy_getcfg");
    strat->setcfg   =       (hook_setcfg_t) dlfptr(lib, "strategy_setcfg");
    strat->fini     =         (hook_fini_t) dlfptr(lib, "strategy_fini");

    name = NULL; // to avoid freeing it in cleanup
    lib = NULL;  // avoid closing in cleanup

    // success; prepare for the loop test and next iteration
    ++strat_count;
    if (*end)
      list = end + 1;
    else
      list = NULL;
  }

 cleanup:
  free(path);
  free(name);
  if (lib)
    dlclose(lib);
  if (retval == -1) {
    // error exit; free allocated space for names, close shared libs
    int i;
    for (i = 0; i < strat_count; i++)
      free_strat(strategies+i);
  }
  return retval;
}