Пример #1
0
static struct test *read_tests(void) {
    char *fname;
    FILE *fp;
    char line[BUFSIZ];
    struct test *result = NULL, *t = NULL;
    int lc = 0;
    bool append_cmd = true;

    if (asprintf(&fname, "%s/tests/run.tests", abs_top_srcdir) < 0)
        die("asprintf fname");

    if ((fp = fopen(fname, "r")) == NULL)
        die("fopen run.tests");

    while (fgets(line, BUFSIZ, fp) != NULL) {
        lc += 1;
        char *s = skipws(line);
        if (*s == '#' || *s == '\0')
            continue;
        if (*s == ':')
            s += 1;
        char *eos = s + strlen(s) - 2;
        if (eos >= s && *eos == ':') {
            *eos++ = '\n';
            *eos = '\0';
        }

        if (looking_at(s, KW_TEST)) {
            if (ALLOC(t) < 0)
                die_oom();
            list_append(result, t);
            append_cmd = true;
            s = token(s + strlen(KW_TEST), &(t->name));
            s = inttok(s, &t->result);
            s = errtok(s, &t->errcode);
        } else if (looking_at(s, KW_PRINTS)) {
            s = skipws(s + strlen(KW_PRINTS));
            t->out_present = looking_at(s, KW_SOMETHING);
            append_cmd = false;
        } else if (looking_at(s, KW_USE)) {
            if (t->module !=NULL)
                die("Can use at most one module in a test");
            s = token(s + strlen(KW_USE), &(t->module));
        } else {
            char **buf = append_cmd ? &(t->cmd) : &(t->out);
            if (*buf == NULL) {
                *buf = strdup(s);
                if (*buf == NULL)
                    die_oom();
            } else {
                if (REALLOC_N(*buf, strlen(*buf) + strlen(s) + 1) < 0)
                    die_oom();
                strcat(*buf, s);
            }
        }
        if (t->out != NULL)
            t->out_present = true;
    }
    return result;
}
Пример #2
0
struct xenviron*
xenviron_create(const char* const* copy_from)
{
    struct cleanup* cl = cleanup_allocate();
    struct xenviron* xe = calloc(1, sizeof (char*));
    if (xe == NULL)
        die_oom();
    cleanup_commit(cl, xenviron_cleanup, xe);

    if (copy_from == NULL) {
        xe->env = calloc(1, xenviron_allocsz(0));
        if (xe->env == NULL)
            die_oom();
    } else {
        size_t nr = 0;
        for (const char* const* pos = copy_from; *pos; ++pos)
            nr += 1;

        xe->env = calloc(1, xenviron_allocsz(nr));
        if (xe->env == NULL)
            die_oom();

        for (size_t i = 0; i < nr; ++i) {
            xe->env[i] = strdup(copy_from[i]);
            if (xe->env[i] == NULL)
                die_oom();
        }
    }

    return xe;
}
Пример #3
0
CuSuite* CuSuiteNew(void) {
	CuSuite* testSuite = NULL;
    if (ALLOC(testSuite) < 0)
        die_oom();
	CuSuiteInit(testSuite);
	return testSuite;
}
Пример #4
0
void
xenviron_set(struct xenviron* xe,
             const char* name,
             const char* value)
{
    char* new_entry = xenviron_allocate_entry(name, value);
    char** slot;

    for (slot = xe->env;  *slot; ++slot)
        if (xenviron_match(*slot, name) != NULL)
            break;

    if (*slot != NULL) {
        free(*slot);
        *slot = new_entry;
    } else {
        size_t nr_slots = (slot - xe->env) + 1;
        char** new_env = realloc(xe->env, xenviron_allocsz(nr_slots));
        if (new_env == NULL) {
            free(new_entry);
            die_oom();
        }

        xe->env = new_env;
        new_env[nr_slots] = NULL;
        new_env[nr_slots - 1] = new_entry;
    }
}
Пример #5
0
CuTest* CuTestNew(const char* name, TestFunction function) {
	CuTest* tc = NULL;
    if (ALLOC(tc) < 0)
        die_oom();
	CuTestInit(tc, name, function);
	return tc;
}
Пример #6
0
void session_setstr(const char* name, const char* value)
{
  /* FIXME: use _set when bglibs 1.103 is released */
  session_strs_remove(&session.nums, &name);
  if (session_strs_add(&session.strs, &name, &value) == 0)
    die_oom(111);
}
Пример #7
0
void session_setnum(const char* name, unsigned long value)
{
  /* FIXME: use _set when bglibs 1.103 is released */
  session_nums_remove(&session.nums, &name);
  if (session_nums_add(&session.nums, &name, &value) == 0)
    die_oom(111);
}
Пример #8
0
static void exec_cmd(int fdin, int fdout,
		     const char** argv,
		     const str* env,
		     const struct passwd* pw)
{
  selfpipe_close();
  dup2(fdin, 0);
  dup2(fdout, 1);
  dup2(fdout, 2);
  close(fdin);
  close(fdout);
  if (!testmode) {
    if (initgroups(pw->pw_name, pw->pw_gid) != 0)
      die1sys(111, "Could not initgroups");
    if (setgid(pw->pw_gid) != 0)
      die1sys(111, "Could not setgid");
    if (setuid(pw->pw_uid) != 0)
      die1sys(111, "Could not setuid");
  }
  if (chdir(pw->pw_dir) != 0)
    die1sys(111, "Could not change directory");
  if (env)
    if ((environ = envstr_make_array(env)) == 0)
      die_oom(111);
  execv(argv[0], (char**)argv);
  die3sys(111, "Could not execute '", argv[0], "'");
  exit(111);
}
Пример #9
0
void* mem_calloc(size_t n, size_t size)
{
    void* ptr = calloc(n, size);
    
    if (!ptr)
        die_oom(size);
    
    return ptr;
}
Пример #10
0
void* mem_alloc(size_t size)
{
    void* ptr = malloc(size);
    
    if (!ptr)
        die_oom(size);
    
    return ptr;
}
Пример #11
0
void *
xrealloc (void *ptr, size_t size)
{
  void *res = realloc (ptr, size);

  if (size != 0 && res == NULL)
    die_oom ();
  return res;
}
Пример #12
0
void *
xcalloc (size_t size)
{
  void *res = calloc (1, size);

  if (res == NULL)
    die_oom ();
  return res;
}
Пример #13
0
void* mem_realloc(void* ptr, size_t size)
{
    void* newptr = realloc(ptr, size);
    
    if (!newptr)
        die_oom(size);
    
    return newptr;
}
Пример #14
0
static void string_append(char **s, const char *p) {
    if (*s == NULL) {
        *s = strdup(p);
    } else {
        int len = strlen(*s) + strlen(p) + 1;
        *s = realloc(*s, len);
        if (*s == NULL)
            die_oom();
        strcat(*s, p);
    }
}
Пример #15
0
const char* temppath(const char* prefix, str* path)
{
  struct timeval tv;
  gettimeofday(&tv, 0);
  if (!str_copy2s(path, prefix, ".tmp.barch.") ||
      !str_catu(path, pid) ||
      !str_catc(path, '.') ||
      !str_catu(path, tv.tv_sec) ||
      !str_catc(path, '.') ||
      !str_catuw(path, tv.tv_usec, 6, '0'))
    die_oom(1);
  return path->s;
}
Пример #16
0
char *
xstrdup (const char *str)
{
  char *res;

  assert (str != NULL);

  res = strdup (str);
  if (res == NULL)
    die_oom ();

  return res;
}
Пример #17
0
static char *
get_mountinfo (int proc_fd,
               const char *mountpoint)
{
  char *line_mountpoint, *line_mountpoint_end;
  cleanup_free char *mountinfo = NULL;
  cleanup_free char *free_me = NULL;
  char *line, *line_start;
  char *res = NULL;
  int i;

  if (mountpoint[0] != '/')
    {
      cleanup_free char *cwd = getcwd (NULL, 0);
      if (cwd == NULL)
        die_oom ();

      mountpoint = free_me = strconcat3 (cwd, "/", mountpoint);
    }

  mountinfo = load_file_at (proc_fd, "self/mountinfo");
  if (mountinfo == NULL)
    return NULL;

  line = mountinfo;

  while (*line != 0)
    {
      cleanup_free char *unescaped = NULL;

      line_start = line;
      for (i = 0; i < 4; i++)
        line = skip_token (line, TRUE);
      line_mountpoint = line;
      line = skip_token (line, FALSE);
      line_mountpoint_end = line;
      line = skip_line (line);

      unescaped = unescape_mountpoint (line_mountpoint, line_mountpoint_end - line_mountpoint);
      if (strcmp (mountpoint, unescaped) == 0)
        {
          res = line_start;
          line[-1] = 0;
          break;
        }
    }

  if (res)
    return xstrdup (res);
  return NULL;
}
Пример #18
0
char *
xasprintf (const char *format,
           ...)
{
  char *buffer = NULL;
  va_list args;

  va_start (args, format);
  if (vasprintf (&buffer, format, args) == -1)
    die_oom ();
  va_end (args);

  return buffer;
}
Пример #19
0
void *
xrealloc(void *ptr, size_t size)
{
  void *result;
  obfs_assert (size < SIZE_T_CEILING);
  if (size == 0)
    size = 1;

  result = realloc(ptr, size);
  if (result == NULL)
    die_oom();

  return result;
}
Пример #20
0
int main(int argc, char* argv[])
{
  struct connection conn;
  iopoll_fd fds[2];
  int selfpipe;
  int i;

  msg_debug_init();
  testmode = getenv("TESTMODE") != 0;

  if ((shell_argv = malloc((argc + 3) * sizeof *argv)) == 0)
    die_oom(111);
  for (i = 1; i < argc; ++i)
    shell_argv[i-1] = argv[i];
  for (; i < argc + 4; ++i)
    shell_argv[i-1] = 0;
  shell_argc = argc - 1;

  if ((path = getenv("PATH")) == 0)
    die1(111, "No PATH is set");
  if ((devnull = open("/dev/null", O_RDWR)) == -1)
    die1sys(111, "Could not open \"/dev/null\"");
  if (!nonblock_on(0))
    die1sys(111, "Could not set non-blocking status");
  if ((selfpipe = selfpipe_init()) == -1)
    die1sys(111, "Could not create self-pipe");
  init_slots();
  connection_init(&conn, 0, 0);
  fds[0].fd = 0;
  fds[0].events = IOPOLL_READ;
  fds[1].fd = selfpipe;
  fds[1].events = IOPOLL_READ;
  for (;;) {
    if (iopoll_restart(fds, 2, -1) == -1)
      die1sys(111, "Poll failed");
    if (fds[0].revents)
      if (connection_read(&conn, handle_packet) <= 0)
	break;
    if (fds[1].revents) {
      read(selfpipe, &i, 1);
      handle_child(WNOHANG);
    }
  }
  msg1("Waiting for remaining slots to complete");
  while (slots_used > 0)
    handle_child(0);
  return 0;
}
Пример #21
0
static void print_regerror(int err, const char *regexp) {
    size_t size;
    char *errbuf;
    size = regerror(err, NULL, NULL, 0);
    if (ALLOC_N(errbuf, size) < 0)
        die_oom();
    regerror(err, NULL, errbuf, size);
    if (strlen(regexp) > 40) {
        char *s = strndup(regexp, 40);
        fprintf(stderr, "Error building fa from %s...:\n", s);
        free(s);
    } else {
        fprintf(stderr, "Error building fa from %s:\n", regexp);
    }
    fprintf(stderr, "  %s\n", errbuf);
    free(errbuf);
}
Пример #22
0
int base64decode(const char* data, unsigned long size, str* dest)
{
  unsigned char bin[3];
  int decoded;
  
  dest->len = 0;
  while (size) {
    if (data[0] == CR || data[0] == LF) size = 0;
    if (size < 4) break;
    if ((decoded = base64_decode_part(data, bin)) <= 0) break;
    data += 4;
    size -= 4;
    if (!str_catb(dest, (char*)bin, decoded))
      die_oom(111);
  }
  return size ? 0 : 1;
}
Пример #23
0
static char*
xenviron_allocate_entry(const char* name, const char* value)
{
    size_t name_length = strlen(name);
    size_t value_length = strlen(value);
    // If name and value do not alias, this value cannot overflow.
    size_t entry_alloc = name_length + 1 + value_length + 1;
    char* entry = malloc(entry_alloc);
    if (entry == NULL)
        die_oom();

    memcpy(&entry[0], name, name_length);
    entry[name_length] = '=';
    memcpy(&entry[name_length+1], value, value_length);
    entry[name_length + 1 + value_length] = '\0';
    return entry;
}
Пример #24
0
void *
xmalloc(size_t size)
{
  void *result;

  obfs_assert(size < SIZE_T_CEILING);

  /* Some malloc() implementations return NULL when the input argument
     is zero. We don't bother detecting whether the implementation we're
     being compiled for does that, because it should hardly ever come up,
     and avoiding it unconditionally does no harm. */
  if (size == 0)
    size = 1;

  result = malloc(size);
  if (result == NULL)
    die_oom();

  return result;
}
Пример #25
0
static struct property_vector*
find_all_properties_raw(void)
{
    for (;;) {
        SCOPED_RESLIST(rl);

        struct find_all_properties_context ctx = {
            .pv = property_vector_new(),
            .oom = false,
        };

        if (property_foreach(find_all_properties_propfn, &ctx) == 1)
            continue;

        if (ctx.oom)
            die_oom();
        reslist_xfer(rl->parent, rl);
        property_vector_sort(ctx.pv);
        return ctx.pv;
    }
}

static void
property_vector_swap(struct property_vector* a,
                     struct property_vector* b)
{
    struct property_vector tmp = *a;
    *a = *b;
    *b = tmp;
}

static bool
property_vector_equal(const struct property_vector* a,
                      const struct property_vector* b)
{
    return a->size == b->size &&
        memcmp(a->props, b->props, a->size * sizeof (a->props[0])) == 0;
}

static struct property_vector*
find_all_properties(void)
{
    struct property_vector* pv1 = find_all_properties_raw();
    for (;;) {
        SCOPED_RESLIST(pv);
        struct property_vector* pv2 = find_all_properties_raw();
        if (property_vector_equal(pv1, pv2))
            return pv1;
        property_vector_swap(pv1, pv2);
    }
}

static int
compat_property_foreach(
    void (*propfn)(const prop_info *pi, void *cookie),
    void *cookie)
{
    find_symbol_in_libc("__system_property_find_nth",
                        &property_find_nth);
    if (property_find_nth == NULL)
        die(EINVAL, "no property-enumeration functions available");

    unsigned propno = 0;
    for (;;) {
        const prop_info* pi = property_find_nth(propno++);
        if (pi == NULL)
            break;
        propfn(pi, cookie);
    }
    return 0;
}