Exemple #1
0
/*
 * Fill a structure with the request packet of the client.
 */
int opt_parse_request(char *data, int data_size, struct tftp_opt *options)
{
     char *entry = NULL;
     char *tmp;
     struct tftphdr *tftp_data = (struct tftphdr *)data;
     size_t size = data_size - sizeof(tftp_data->th_opcode);

     /* read filename */
     entry = argz_next(tftp_data->th_stuff, size, entry);
     if (!entry)
          return ERR;
     else
          opt_set_options(options, "filename", entry);
     /* read mode */
     entry = argz_next(tftp_data->th_stuff, size, entry);
     if (!entry)
          return ERR;
     else
          opt_set_options(options, "mode", entry);
     /* scan for options */
     // FIXME: we should use opt_parse_options() here
     while ((entry = argz_next(tftp_data->th_stuff, size, entry)))
     {
          tmp = entry;
          entry = argz_next(tftp_data->th_stuff, size, entry);
          if (!entry)
               return ERR;
          else
               opt_set_options(options, tmp, entry);
     }
     return OK;
}
void runSuccess2() {
    size_t argz_len = anysize();
    char* argz = anyargz(argz_len, anysize());
    char* elem = argz_next(argz, argz_len, argz);
    if (elem) {
        elem = argz_next(argz, argz_len, elem);
        if (elem) {
            argz_delete(&argz, &argz_len, elem);
        }
    }
}
Exemple #3
0
TEST(database, list_packages){
  char* argz=0;
  size_t argz_len=0;
  TEST_ASSERT_EQUAL(SQLITE_ROW,list_packages(&argz,&argz_len,"Gcc"));
  char* entry=0;
  entry=argz_next(argz,argz_len,entry);
  TEST_ASSERT_EQUAL_STRING(package.name, entry);
  entry=argz_next(argz,argz_len,entry);
  TEST_ASSERT_EQUAL_STRING(package.description, entry);
  entry=argz_next(argz,argz_len,entry);
  TEST_ASSERT_EQUAL(package.version, parse_version(entry));

}
Exemple #4
0
static void add_transforms(char *ts, size_t tslen) {
    char *command;
    int r;
    char *t = NULL;
    bool added_transform = false;

    while ((t = argz_next(ts, tslen, t))) {
        r = xasprintf(&command, "transform %s", t);
        if (r < 0)
            fprintf(stderr, "error: Failed to add transform %s: could not allocate memory\n", t);

        r = aug_srun(aug, stdout, command);
        if (r < 0)
            fprintf(stderr, "error: Failed to add transform %s: %s\n", t, aug_error_message(aug));

        free(command);
        added_transform = true;
    }

    if (added_transform) {
        r = aug_load(aug);
        if (r < 0)
            fprintf(stderr, "error: Failed to load with new transforms: %s\n", aug_error_message(aug));
    }
}
Exemple #5
0
/* Note: return value of this function is ignored by libsubprocess.
 * Also: zio_json_decode() returns -1 on error, 0 on eof, strlen(s) on
 * success; caller must free 's'.
 */
static int subprocess_io_cb (struct subprocess *p, const char *json_str)
{
    runlevel_t *r;
    json_object *o = NULL;
    const char *name;
    int len;
    bool eof;
    char *s = NULL, *argz = NULL, *line = NULL;
    size_t argz_len;

    r = subprocess_get_context (p, "runlevel_t");
    assert (r != NULL);

    if (!r->io_cb)
        goto done;
    if (!(o = Jfromstr (json_str)) || !Jget_str (o, "name", &name))
        goto done;
    len = zio_json_decode (json_str, (void **)&s, &eof);
    if (len <= 0 || !s || !*s || s[len] != '\0')
        goto done;
    if (argz_create_sep (s, '\n', &argz, &argz_len) != 0)
        goto done;
    while ((line = argz_next (argz, argz_len, line)) && *line)
        r->io_cb (r, name, line, r->io_cb_arg);
done:
    if (s)
        free (s);
    if (argz)
        free (argz);
    Jput (o);
    return 0;
}
Exemple #6
0
/*
 * Fill a structure looking only at TFTP options.
 */
int opt_parse_options(char *data, int data_size, struct tftp_opt *options)
{
     char *entry = NULL;
     char *tmp;
     struct tftphdr *tftp_data = (struct tftphdr *)data;
     size_t size = data_size - sizeof(tftp_data->th_opcode);

     while ((entry = argz_next(tftp_data->th_stuff, size, entry)))
     {
          tmp = entry;
          entry = argz_next(tftp_data->th_stuff, size, entry);
          if (!entry)
               return ERR;
          else
               opt_set_options(options, tmp, entry);
     }
     return OK;
}
Exemple #7
0
const char * subprocess_get_arg (struct subprocess *p, int n)
{
    int i;
    char *entry = NULL;

    if (n > subprocess_get_argc (p))
        return (NULL);

    for (i = 0; i <= n; i++)
        entry = argz_next (p->argz, p->argz_len, entry);

    return (entry);
}
void testValues() {
    f = 2;

    size_t argz_len = anysize();
    char* argz = anyargz(argz_len, anysize());
    char* elem = argz_next(argz, argz_len, argz);
    if (elem) {
        argz_delete(&argz, &argz_len, elem);
    }

    //@ assert f == 2;
    //@ assert vacuous: \false;
}
Exemple #9
0
int
main (int argc, char **argv)
{
  error_t err;

  err = argp_parse (&argp, argc, argv, 0, 0, &fstab_params);
  if (err)
    error (3, err, "parsing arguments");

  /* Read the mtab file by default.  */
  if (! fstab_params.fstab_path)
    fstab_params.fstab_path = _PATH_MOUNTED;

  struct fstab *fstab = fstab_argp_create (&fstab_params, NULL, 0);
  if (! fstab)
    error (3, ENOMEM, "fstab creation");

  if (targets)
    for (char *t = targets; t; t = argz_next (targets, targets_len, t))
      {
	/* Figure out if t is the device or the mountpoint.  */
	struct fs *fs = fstab_find_mount (fstab, t);
	if (! fs)
	  {
	    fs = fstab_find_device (fstab, t);
	    if (! fs)
	      {
		/* As last resort, just assume it is the mountpoint.  */
		struct mntent m =
		  {
		    mnt_fsname: "",
		    mnt_dir: t,
		    mnt_type: "",
		    mnt_opts: 0,
		    mnt_freq: 0,
		    mnt_passno: 0,
		  };

		err = fstab_add_mntent (fstab, &m, &fs);
		if (err)
		  error (2, err, "could not find entry for: %s", t);
	      }
	  }

	if (fs)
	  err |= do_umount (fs);
      }
Exemple #10
0
int process_args (flux_t h, char *argz, size_t argz_len, const sched_params_t *sp)
{
    int rc = 0;
    char *reserve_depth_str = NULL;
    char *entry = NULL;

    for (entry = argz;
         entry;
         entry = argz_next (argz, argz_len, entry)) {

        if (!strncmp ("reserve-depth=", entry, sizeof ("reserve-depth"))) {
            reserve_depth_str = strstr (entry, "=") + 1;
        } else {
            rc = -1;
            errno = EINVAL;
            goto done;
        }
    }

    if (reserve_depth_str) {
        // If atoi fails, it defaults to 0, which is fine for us
        reservation_depth = atoi (reserve_depth_str);
    } else {
        reservation_depth = 0;
    }

    if (!sp) {
        flux_log (h, LOG_ERR, "scheduling parameters unavailable");
        rc = -1;
        errno = EINVAL;
    } else if (reservation_depth > sp->queue_depth) {
        /* Conservative backfill (-1) will still be limited by the queue-depth
         * but we just treat queue-depth as the limit for it
         */
        flux_log (h, LOG_ERR,
                  "reserve-depth value (%d) - greater than queue-depth (%ld)",
                  reservation_depth, sp->queue_depth);
        rc = -1;
        errno = EINVAL;
    }

 done:
    return rc;
}
Exemple #11
0
/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
   ARGZ as necessary.  If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
   incremented by number of replacements performed.  */
error_t
argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
                unsigned *replace_count)
{
  error_t err = 0;

  if (str && *str)
    {
      char *arg = 0;
      char *src = *argz;
      size_t src_len = *argz_len;
      char *dst = 0;
      size_t dst_len = 0;
      int delayed_copy = 1;     /* True while we've avoided copying anything.  */
      size_t str_len = strlen (str), with_len = strlen (with);

      while (!err && (arg = argz_next (src, src_len, arg)))
        {
          char *match = strstr (arg, str);
          if (match)
            {
              char *from = match + str_len;
              size_t to_len = match - arg;
              char *to = strndup (arg, to_len);

              while (to && from)
                {
                  str_append (&to, &to_len, with, with_len);
                  if (to)
                    {
                      match = strstr (from, str);
                      if (match)
                        {
                          str_append (&to, &to_len, from, match - from);
                          from = match + str_len;
                        }
                      else
                        {
                          str_append (&to, &to_len, from, strlen (from));
                          from = 0;
                        }
                    }
                }

              if (to)
                {
                  if (delayed_copy)
                    /* We avoided copying SRC to DST until we found a match;
                       now that we've done so, copy everything from the start
                       of SRC.  */
                    {
                      if (arg > src)
                        err = argz_append (&dst, &dst_len, src, (arg - src));
                      delayed_copy = 0;
                    }
                  if (! err)
                    err = argz_add (&dst, &dst_len, to);
                  free (to);
                }
              else
                err = ENOMEM;

              if (replace_count)
                (*replace_count)++;
            }
          else if (! delayed_copy)
            err = argz_add (&dst, &dst_len, arg);
        }

      if (! err)
        {
          if (! delayed_copy)
            /* We never found any instances of str.  */
            {
              free (src);
              *argz = dst;
              *argz_len = dst_len;
            }
        }
      else if (dst_len > 0)
        free (dst);
    }

  return err;
}
Exemple #12
0
/* Mount one filesystem.  */
static error_t
do_mount (struct fs *fs, int remount)
{
    error_t err;
    char *fsopts, *o;
    size_t fsopts_len;
    char *mntopts;
    size_t mntopts_len;
    fsys_t mounted;

    inline void explain (const char *command)
    {
        if (verbose)
        {
            const char *o;
            printf ("%s %s", command, fs->mntent.mnt_dir);
            for (o = fsopts; o; o = argz_next (fsopts, fsopts_len, o))
                printf (" %s", o);
            putchar ('\n');
        }
    }

    err = fs_fsys (fs, &mounted);
    if (err)
    {
        error (0, err, "cannot determine if %s is already mounted",
               fs->mntent.mnt_fsname);
        return err;
    }


    /* Produce an argz of translator option arguments from the
       given FS's options and the command-line options.  */

#define ARGZ(call)							      \
  err = argz_##call;							      \
  if (err)								      \
    error (3, ENOMEM, "collecting mount options");			      \

    if (fs->mntent.mnt_opts)
    {
        /* Append the fstab options to any specified on the command line.  */
        ARGZ (create_sep (fs->mntent.mnt_opts, ',', &mntopts, &mntopts_len));

        /* Remove the `noauto' and `bind' options, since they're for us not the
           filesystem.  */
        for (o = mntopts; o; o = argz_next (mntopts, mntopts_len, o))
        {
            if (strcmp (o, MNTOPT_NOAUTO) == 0)
                argz_delete (&mntopts, &mntopts_len, o);
            if (strcmp (o, "bind") == 0)
            {
                fs->mntent.mnt_type = strdup ("firmlink");
                if (!fs->mntent.mnt_type)
                    error (3, ENOMEM, "failed to allocate memory");
                argz_delete (&mntopts, &mntopts_len, o);
            }
        }

        ARGZ (append (&mntopts, &mntopts_len, options, options_len));
    }
    else
    {
        mntopts = options;
        mntopts_len = options_len;
    }

    /* Convert the list of options into a list of switch arguments.  */
    fsopts = 0;
    fsopts_len = 0;
    for (o = mntopts; o; o = argz_next (mntopts, mntopts_len, o))
        if (*o == '-')		/* Allow letter opts `-o -r,-E', BSD style.  */
        {
            ARGZ (add (&fsopts, &fsopts_len, o));
        }
        else if ((strcmp (o, "defaults") != 0) && (strlen (o) != 0))
        {
            /* Prepend `--' to the option to make a long option switch,
               e.g. `--ro' or `--rsize=1024'.  */
            char arg[2 + strlen (o) + 1];
            arg[0] = arg[1] = '-';
            memcpy (&arg[2], o, sizeof arg - 2);
            ARGZ (add (&fsopts, &fsopts_len, arg));
        }

    if (mntopts != options)
        free (mntopts);
#undef ARGZ

    if (remount)
    {
        if (mounted == MACH_PORT_NULL)
        {
            error (0, 0, "%s not already mounted", fs->mntent.mnt_fsname);
            return EBUSY;
        }

        /* Send an RPC to request the new options, including --update.  */
        explain ("fsysopts");
        err = fsys_set_options (mounted, fsopts, fsopts_len, 0);
        if (err)
            error (0, err, "cannot remount %s", fs->mntent.mnt_fsname);
        return err;
    }
    else
    {
        /* Error during file lookup; we use this to avoid duplicating error
        messages.  */
        error_t open_err = 0;
        /* The control port for any active translator we start up.  */
        fsys_t active_control;
        file_t node;		/* Port to the underlying node.  */
        struct fstype *type;

        /* The callback to start_translator opens NODE as a side effect.  */
        error_t open_node (int flags,
                           mach_port_t *underlying,
                           mach_msg_type_name_t *underlying_type,
                           task_t task, void *cookie)
        {
            node = file_name_lookup (fs->mntent.mnt_dir,
                                     flags | O_NOTRANS, 0666);
            if (node == MACH_PORT_NULL)
            {
                open_err = errno;
                return open_err;
            }

            *underlying = node;
            *underlying_type = MACH_MSG_TYPE_COPY_SEND;

            return 0;
        }

        /* Do not fail if there is an active translator if --fake is
           given. This mimics Linux mount utility more closely which
           just looks into the mtab file. */
        if (mounted != MACH_PORT_NULL && !fake)
        {
            error (0, 0, "%s already mounted", fs->mntent.mnt_fsname);
            return EBUSY;
        }

        if (strcmp (fs->mntent.mnt_type, "auto") == 0)
        {
#if HAVE_BLKID
            char *type =
                blkid_get_tag_value (NULL, "TYPE", fs->mntent.mnt_fsname);
            if (! type)
            {
                error (0, 0, "failed to detect file system type");
                return EFTYPE;
            }
            else
            {
                if (strcmp (type, "vfat") == 0)
                    fs->mntent.mnt_type = strdup ("fat");
                else
                    fs->mntent.mnt_type = strdup (type);
                if (! fs->mntent.mnt_type)
                    error (3, ENOMEM, "failed to allocate memory");
            }
#else
            fs->mntent.mnt_type = strdup ("ext2");
            if (! fs->mntent.mnt_type)
                error (3, ENOMEM, "failed to allocate memory");
#endif
        }

        err = fs_type (fs, &type);
        if (err)
        {
            error (0, err, "%s: cannot determine filesystem type",
                   fs->mntent.mnt_fsname);
            return err;
        }
        if (type->program == 0)
        {
            error (0, 0, "%s: filesystem type `%s' unknown",
                   fs->mntent.mnt_fsname, type->name);
            return EFTYPE;
        }

        /* Stick the translator program name in front of the option switches.  */
        err = argz_insert (&fsopts, &fsopts_len, fsopts, type->program);
        /* Now stick the device name on the end as the last argument.  */
        if (!err)
            err = argz_add (&fsopts, &fsopts_len, fs->mntent.mnt_fsname);
        if (err)
            error (3, ENOMEM, "collecting mount options");

        /* Now we have a translator command line argz in FSOPTS.  */

        if (fake) {
            /* Fake the translator startup. */
            mach_port_t underlying;
            mach_msg_type_name_t underlying_type;
            err = open_node (O_READ, &underlying, &underlying_type, 0, NULL);
            if (err)
                error (1, errno, "cannot mount on %s", fs->mntent.mnt_dir);

            mach_port_deallocate (mach_task_self (), underlying);

            /* See if the translator is at least executable. */
            if (access(type->program, X_OK) == -1)
                error (1, errno, "can not execute %s", type->program);

            return 0;
        }

        explain ("settrans -a");
        err = fshelp_start_translator (open_node, NULL, fsopts,
                                       fsopts, fsopts_len, timeout,
                                       &active_control);
        /* If ERR is due to a problem opening the translated node, we print
        that name, otherwise, the name of the translator.  */
        if (open_err)
            error (0, open_err, "cannot mount on %s", fs->mntent.mnt_dir);
        else if (err)
            error (0, err, "cannot start translator %s", fsopts);
        else
        {
            err = file_set_translator (node, 0, FS_TRANS_SET|FS_TRANS_EXCL, 0,
                                       0, 0,
                                       active_control, MACH_MSG_TYPE_COPY_SEND);
            if (err == EBUSY)
                error (0, 0, "%s already mounted on", fs->mntent.mnt_dir);
            else if (err)
                error (0, err, "cannot set translator on %s", fs->mntent.mnt_dir);
            if (err)
                fsys_goaway (active_control, FSYS_GOAWAY_FORCE);
            mach_port_deallocate (mach_task_self (), active_control);
        }

        return err;
    }