Esempio n. 1
0
/*
 * mnt_context_helper_setopt() backend.
 *
 * This function applies umount.type command line option (for example parsed
 * by getopt() or getopt_long()) to @cxt. All unknown options are ignored and
 * then 1 is returned.
 *
 * Returns: negative number on error, 1 if @c is unknown option, 0 on success.
 */
int mnt_context_umount_setopt(struct libmnt_context *cxt, int c, char *arg)
{
	int rc = -EINVAL;

	assert(cxt);
	assert(cxt->action == MNT_ACT_UMOUNT);

	switch(c) {
	case 'n':
		rc = mnt_context_disable_mtab(cxt, TRUE);
		break;
	case 'l':
		rc = mnt_context_enable_lazy(cxt, TRUE);
		break;
	case 'f':
		rc = mnt_context_enable_force(cxt, TRUE);
		break;
	case 'v':
		rc = mnt_context_enable_verbose(cxt, TRUE);
		break;
	case 'r':
		rc = mnt_context_enable_rdonly_umount(cxt, TRUE);
		break;
	case 't':
		if (arg)
			rc = mnt_context_set_fstype(cxt, arg);
		break;
	default:
		return 1;
		break;
	}

	return rc;
}
Esempio n. 2
0
/*
 * mnt_context_helper_setopt() backend
 *
 * This function applies mount.type command line option (for example parsed
 * by getopt() or getopt_long()) to @cxt. All unknown options are ignored and
 * then 1 is returned.
 *
 * Returns: negative number on error, 1 if @c is unknown option, 0 on success.
 */
int mnt_context_mount_setopt(struct libmnt_context *cxt, int c, char *arg)
{
	int rc = -EINVAL;

	assert(cxt);
	assert(cxt->action == MNT_ACT_MOUNT);

	switch(c) {
	case 'f':
		rc = mnt_context_enable_fake(cxt, TRUE);
		break;
	case 'n':
		rc = mnt_context_disable_mtab(cxt, TRUE);
		break;
	case 'r':
		rc = mnt_context_append_options(cxt, "ro");
		break;
	case 'v':
		rc = mnt_context_enable_verbose(cxt, TRUE);
		break;
	case 'w':
		rc = mnt_context_append_options(cxt, "rw");
		break;
	case 'o':
		if (arg)
			rc = mnt_context_append_options(cxt, arg);
		break;
	case 's':
		rc = mnt_context_enable_sloppy(cxt, TRUE);
		break;
	case 't':
		if (arg)
			rc = mnt_context_set_fstype(cxt, arg);
		break;
	default:
		return 1;
		break;
	}

	return rc;
}
Esempio n. 3
0
static int mount_main(struct libmnt_context *cxt, int argc, char **argv)
{
    int rc, c;
    struct libmnt_fs *fs;
    char *spec = NULL, *mount_point = NULL, *opts = NULL;

    static const struct option longopts[] = {
        { "fake", 0, 0, 'f' },
        { "help", 0, 0, 'h' },
        { "no-mtab", 0, 0, 'n' },
        { "read-only", 0, 0, 'r' },
        { "ro", 0, 0, 'r' },
        { "verbose", 0, 0, 'v' },
        { "version", 0, 0, 'V' },
        { "read-write", 0, 0, 'w' },
        { "rw", 0, 0, 'w' },
        { "options", 1, 0, 'o' },
        { "sloppy", 0, 0, 's' },
        { NULL, 0, 0, 0 }
    };

    mount_config_init(progname);
    mnt_context_init_helper(cxt, MNT_ACT_MOUNT, 0);

    while ((c = getopt_long(argc, argv, "fhnrVvwo:s", longopts, NULL)) != -1) {

        rc = mnt_context_helper_setopt(cxt, c, optarg);
        if (rc == 0)		/* valid option */
            continue;
        if (rc < 0)		/* error (probably ENOMEM) */
            goto err;
        /* rc==1 means unknow option */
        switch (c) {
        case 'V':
            printf("%s: ("PACKAGE_STRING")\n", progname);
            return EX_SUCCESS;
        case 'h':
        default:
            mount_usage();
            return EX_USAGE;
        }
    }

    if (optind < argc)
        spec = argv[optind++];
    if (optind < argc)
        mount_point = argv[optind++];

    if (!mount_point) {
        nfs_error(_("%s: no mount point provided"), progname);
        goto err;
    }
    if (!spec) {
        nfs_error(_("%s: no mount spec provided"), progname);
        goto err;
    }

    if (geteuid() != 0) {
        nfs_error(_("%s: not installed setuid - "
                    "\"user\" NFS mounts not supported."), progname);
        goto err;
    }

    verbose = mnt_context_is_verbose(cxt);
    sloppy = mnt_context_is_sloppy(cxt);
    nomtab = mnt_context_is_nomtab(cxt);

    if (strcmp(progname, "mount.nfs4") == 0)
        mnt_context_set_fstype(cxt, "nfs4");
    else
        mnt_context_set_fstype(cxt, "nfs");	/* default */

    rc = mnt_context_set_source(cxt, spec);
    if (!rc)
        mnt_context_set_target(cxt, mount_point);
    if (rc) {
        nfs_error(_("%s: failed to set spec or mountpoint: %s"),
                  progname, strerror(errno));
        goto err;
    }

    mount_point = mnt_resolve_path(mount_point,
                                   mnt_context_get_cache(cxt));

    if (chk_mountpoint(mount_point))
        goto err;
    /*
     * Concatenate mount options from the configuration file
     */
    fs = mnt_context_get_fs(cxt);
    if (fs) {
        opts = mnt_fs_strdup_options(fs);

        opts = mount_config_opts(spec, mount_point, opts);
        mnt_fs_set_options(fs, opts);
    }

    rc = mnt_context_prepare_mount(cxt);
    if (rc) {
        nfs_error(_("%s: failed to prepare mount: %s\n"),
                  progname, strerror(-rc));
        goto err;
    }

    rc = try_mount(cxt, FOREGROUND);

    if (rc == EX_BG) {
        printf(_("%s: backgrounding \"%s\"\n"),
               progname, mnt_context_get_source(cxt));
        printf(_("%s: mount options: \"%s\"\n"),
               progname, opts);

        fflush(stdout);

        if (daemon(0, 0)) {
            nfs_error(_("%s: failed to start "
                        "background process: %s\n"),
                      progname, strerror(errno));
            exit(EX_FAIL);
        }

        rc = try_mount(cxt, BACKGROUND);

        if (verbose && rc)
            printf(_("%s: giving up \"%s\"\n"),
                   progname, mnt_context_get_source(cxt));
    }

    mnt_context_set_syscall_status(cxt, rc == EX_SUCCESS ? 0 : -1);
    mnt_context_finalize_mount(cxt);	/* mtab update */
    return rc;
err:
    return EX_FAIL;
}