www.pudn.com > cryptfs030905.rar > fist_ioctl.c


/*
 * Copyright (c) 1997-2003 Erez Zadok
 * Copyright (c) 2001-2003 Stony Brook University
 *
 * For specific licensing information, see the COPYING file distributed with
 * this package, or get one from ftp://ftp.filesystems.org/pub/fist/COPYING.
 *
 * This Copyright notice must be kept intact and distributed with all
 * fistgen sources INCLUDING sources generated by fistgen.
 */
/*
 *  $Id: fist_ioctl.c,v 1.10 2003/07/29 18:17:20 ezk Exp $
 */

#ifdef HAVE_CONFIG_H
# include 
#endif /* HAVE_CONFIG_H */
#ifdef FISTGEN
# include "fist_cryptfs.h"
#endif /* FISTGEN */
#include "fist.h"
#include "cryptfs.h"

#include 
#include 
#include 
#include 
#include 
#include 

static int opt_d = 0;
static int opt_attach = 0, opt_detach = 0;
static struct fist_ioctl_attach_data attach_data;
static struct fist_ioctl_detach_data detach_data;
static int optcount;
static const char *progname;


void
usage(void)
{
    fprintf(stderr, "Usage: %s -d file [val]\n\tto set/get debugging values\n", progname);
    fprintf(stderr, "Usage: %s +a root node target\n\tto attach node to target, inside directory root\n", progname);
    fprintf(stderr, "Usage: %s -a path\n\tto detach path (root/node)\n", progname);
    exit(1);
}


int
main(int argc, char *argv[])
{
    int fd, ret, val = 0;

    progname = argv[0];

    /* check that minimum number of args were specified */
    if (argc < 3)
	usage();

    if (strcmp(argv[1], "+a") == 0) {
	if (argc != 5)
	    usage();
	opt_attach++;
	optcount++;
	attach_data.from = argv[3];
	attach_data.to = argv[4];
	attach_data.uid=getuid();

	//printf("fist_ioctl attach_data.uid:%d\n",attach_data.uid);
		
    } else if (strcmp(argv[1], "-a") == 0) {
	if (argc != 4)
	    usage();
	opt_detach++;
	optcount++;
	detach_data.from = argv[3];
    }
    if (strcmp(argv[1], "-d") == 0) {
	if (argc > 4)
	    usage();
	opt_d++;
	optcount++;
    }

    /* check that at least one option was used */
    if (!optcount)
	usage();

    /* open file on which ioctl will operate */
    fd = open(argv[2], O_RDONLY);
    if (fd < 0) {
	perror(argv[2]);
	exit(1);
    }

    /* if specified 3rd arg, want to set debug level */
    if (opt_d) {
	if (argc == 4) {
	    val = atoi(argv[3]);
	    ret = ioctl(fd, FIST_IOCTL_SET_DEBUG_VALUE, &val);
	    if (ret < 0) {
		perror("ioctl set");
		exit(1);
	    }
	} else {
	    ret = ioctl(fd, FIST_IOCTL_GET_DEBUG_VALUE, &val);
	    if (ret < 0) {
		perror("ioctl get");
		exit(1);
	    }
	    printf("debug ioctl returned value %d\n", val);
	}
	goto out;
    }


    if (opt_detach) {
	ret = ioctl(fd, FIST_IOCTL_DETACH, &detach_data);
	if (ret < 0) {
	    perror("ioctl detach");
	    exit(1);
	}
    }
    if (opt_attach) {
	printf("ioctl:FIST_IOCTL_ATTACH OK\n");

	ret = ioctl(fd, FIST_IOCTL_ATTACH, &attach_data);
	if (ret < 0) {
	    perror("ioctl attach");
	    exit(1);
	}
    }

 out:
    close(fd);
    exit(0);
}

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */