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