Home | History | Annotate | Line # | Download | only in tools
lvmcmdlib.c revision 1.1.1.1
      1 /*	$NetBSD: lvmcmdlib.c,v 1.1.1.1 2008/12/22 00:19:04 haad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
      5  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
      6  *
      7  * This file is part of LVM2.
      8  *
      9  * This copyrighted material is made available to anyone wishing to use,
     10  * modify, copy, or redistribute it subject to the terms and conditions
     11  * of the GNU Lesser General Public License v.2.1.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program; if not, write to the Free Software Foundation,
     15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     16  */
     17 
     18 #include "tools.h"
     19 #include "lvm2cmdline.h"
     20 #include "label.h"
     21 #include "memlock.h"
     22 #include "version.h"
     23 
     24 #include "lvm2cmd.h"
     25 
     26 #include <signal.h>
     27 #include <syslog.h>
     28 #include <libgen.h>
     29 #include <sys/stat.h>
     30 #include <time.h>
     31 #include <sys/resource.h>
     32 
     33 void *cmdlib_lvm2_init(unsigned is_static)
     34 {
     35 	struct cmd_context *cmd;
     36 
     37 	lvm_register_commands();
     38 
     39 	if (!(cmd = init_lvm(is_static)))
     40 		return NULL;
     41 
     42 	return (void *) cmd;
     43 }
     44 
     45 int lvm2_run(void *handle, const char *cmdline)
     46 {
     47 	int argc, ret, oneoff = 0;
     48 	char *args[MAX_ARGS], **argv, *cmdcopy = NULL;
     49 	struct cmd_context *cmd;
     50 
     51 	argv = args;
     52 
     53 	if (!handle) {
     54 		oneoff = 1;
     55 		if (!(handle = lvm2_init())) {
     56 			log_error("Handle initialisation failed.");
     57 			return ECMD_FAILED;
     58 		}
     59 	}
     60 
     61 	cmd = (struct cmd_context *) handle;
     62 
     63 	cmd->argv = argv;
     64 
     65 	if (!(cmdcopy = dm_strdup(cmdline))) {
     66 		log_error("Cmdline copy failed.");
     67 		ret = ECMD_FAILED;
     68 		goto out;
     69 	}
     70 
     71 	if (lvm_split(cmdcopy, &argc, argv, MAX_ARGS) == MAX_ARGS) {
     72 		log_error("Too many arguments.  Limit is %d.", MAX_ARGS);
     73 		ret = EINVALID_CMD_LINE;
     74 		goto out;
     75 	}
     76 
     77 	if (!argc) {
     78 		log_error("No command supplied");
     79 		ret = EINVALID_CMD_LINE;
     80 		goto out;
     81 	}
     82 
     83 	/* FIXME Temporary - move to libdevmapper */
     84 	ret = ECMD_PROCESSED;
     85 	if (!strcmp(cmdline, "_memlock_inc"))
     86 		memlock_inc();
     87 	else if (!strcmp(cmdline, "_memlock_dec"))
     88 		memlock_dec();
     89 	else
     90 		ret = lvm_run_command(cmd, argc, argv);
     91 
     92       out:
     93 	dm_free(cmdcopy);
     94 
     95 	if (oneoff)
     96 		lvm2_exit(handle);
     97 
     98 	return ret;
     99 }
    100 
    101 void lvm2_log_level(void *handle, int level)
    102 {
    103 	struct cmd_context *cmd = (struct cmd_context *) handle;
    104 
    105 	cmd->default_settings.verbose = level - VERBOSE_BASE_LEVEL;
    106 
    107 	return;
    108 }
    109 
    110 void lvm2_log_fn(lvm2_log_fn_t log_fn)
    111 {
    112 	init_log_fn(log_fn);
    113 }
    114 
    115 void lvm2_exit(void *handle)
    116 {
    117 	struct cmd_context *cmd = (struct cmd_context *) handle;
    118 
    119 	lvm_fin(cmd);
    120 }
    121