xref: /dragonfly/contrib/lvm2/dist/tools/lvmcmdlib.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1 /*        $NetBSD: lvmcmdlib.c,v 1.1.1.3 2009/12/02 00:25:51 haad Exp $         */
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2009 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 "lvm-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 
cmdlib_lvm2_init(unsigned static_compile)33 void *cmdlib_lvm2_init(unsigned static_compile)
34 {
35           struct cmd_context *cmd;
36 
37           lvm_register_commands();
38 
39           init_is_static(static_compile);
40           if (!(cmd = init_lvm()))
41                     return NULL;
42 
43           return (void *) cmd;
44 }
45 
lvm2_run(void * handle,const char * cmdline)46 int lvm2_run(void *handle, const char *cmdline)
47 {
48           int argc, ret, oneoff = 0;
49           char *args[MAX_ARGS], **argv, *cmdcopy = NULL;
50           struct cmd_context *cmd;
51 
52           argv = args;
53 
54           if (!handle) {
55                     oneoff = 1;
56                     if (!(handle = lvm2_init())) {
57                               log_error("Handle initialisation failed.");
58                               return ECMD_FAILED;
59                     }
60           }
61 
62           cmd = (struct cmd_context *) handle;
63 
64           cmd->argv = argv;
65 
66           if (!(cmdcopy = dm_strdup(cmdline))) {
67                     log_error("Cmdline copy failed.");
68                     ret = ECMD_FAILED;
69                     goto out;
70           }
71 
72           if (lvm_split(cmdcopy, &argc, argv, MAX_ARGS) == MAX_ARGS) {
73                     log_error("Too many arguments.  Limit is %d.", MAX_ARGS);
74                     ret = EINVALID_CMD_LINE;
75                     goto out;
76           }
77 
78           if (!argc) {
79                     log_error("No command supplied");
80                     ret = EINVALID_CMD_LINE;
81                     goto out;
82           }
83 
84           /* FIXME Temporary - move to libdevmapper */
85           ret = ECMD_PROCESSED;
86           if (!strcmp(cmdline, "_memlock_inc"))
87                     memlock_inc_daemon();
88           else if (!strcmp(cmdline, "_memlock_dec"))
89                     memlock_dec_daemon();
90           else
91                     ret = lvm_run_command(cmd, argc, argv);
92 
93       out:
94           dm_free(cmdcopy);
95 
96           if (oneoff)
97                     lvm2_exit(handle);
98 
99           return ret;
100 }
101 
lvm2_log_level(void * handle,int level)102 void lvm2_log_level(void *handle, int level)
103 {
104           struct cmd_context *cmd = (struct cmd_context *) handle;
105 
106           cmd->default_settings.verbose = level - VERBOSE_BASE_LEVEL;
107 
108           return;
109 }
110 
lvm2_log_fn(lvm2_log_fn_t log_fn)111 void lvm2_log_fn(lvm2_log_fn_t log_fn)
112 {
113           init_log_fn(log_fn);
114 }
115 
lvm2_exit(void * handle)116 void lvm2_exit(void *handle)
117 {
118           struct cmd_context *cmd = (struct cmd_context *) handle;
119 
120           lvm_fin(cmd);
121 }
122