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