2
2
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
3
* Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
5
5
* This file is part of LVM2.
94
93
return grouped_arg_count(av, a) ? av[a].i_value : def;
96
int32_t first_grouped_arg_int_value(struct cmd_context *cmd, int a, const int32_t def)
98
struct arg_value_group_list *current_group;
99
struct arg_values *av;
101
dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
102
av = current_group->arg_values;
103
if (grouped_arg_count(av, a))
104
return grouped_arg_int_value(av, a, def);
97
110
int32_t arg_int_value(struct cmd_context *cmd, int a, const int32_t def)
99
return arg_count(cmd, a) ? cmd->arg_values[a].i_value : def;
112
return (_cmdline.arg_props[a].flags & ARG_GROUPABLE) ?
113
first_grouped_arg_int_value(cmd, a, def) : (arg_count(cmd, a) ? cmd->arg_values[a].i_value : def);
102
116
uint32_t arg_uint_value(struct cmd_context *cmd, int a, const uint32_t def)
160
int yes_no_excl_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
174
int activation_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
162
176
av->sign = SIGN_NONE;
163
177
av->percent = PERCENT_NONE;
173
187
av->ui_value = CHANGE_AY;
190
else if (!strcmp(av->value, "a") || !strcmp(av->value, "ay") ||
191
!strcmp(av->value, "ya")) {
192
av->i_value = CHANGE_AAY;
193
av->ui_value = CHANGE_AAY;
176
196
else if (!strcmp(av->value, "n") || !strcmp(av->value, "en") ||
177
197
!strcmp(av->value, "ne")) {
178
198
av->i_value = CHANGE_AN;
218
int discards_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
220
thin_discards_t discards;
222
if (!get_pool_discards(av->value, &discards))
225
av->i_value = discards;
226
av->ui_value = discards;
198
231
int metadatatype_arg(struct cmd_context *cmd, struct arg_values *av)
200
233
return get_format_by_name(cmd, av->value) ? 1 : 0;
376
int minor_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
380
if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
383
if (av->i_value > 255) {
384
log_error("Minor number outside range 0-255");
391
int major_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
395
if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
398
if (av->i_value > 255) {
399
log_error("Major number outside range 0-255");
403
/* FIXME Also Check against /proc/devices */
408
409
int string_arg(struct cmd_context *cmd __attribute__((unused)),
409
410
struct arg_values *av __attribute__((unused)))
506
507
return int_arg(cmd, av);
510
int major_minor_valid(const struct cmd_context *cmd, const struct format_type *fmt,
511
int32_t major, int32_t minor)
513
if (!strncmp(cmd->kernel_vsn, "2.4.", 4) ||
514
(fmt->features & FMT_RESTRICTED_LVIDS)) {
515
if (major < 0 || major > 255) {
516
log_error("Major number outside range 0-255");
519
if (minor < 0 || minor > 255) {
520
log_error("Minor number outside range 0-255");
524
/* 12 bits for major number */
525
if (major < 0 || major > 4095) {
526
log_error("Major number outside range 0-4095");
529
/* 20 bits for minor number */
530
if (minor < 0 || minor > 1048575) {
531
log_error("Minor number outside range 0-1048575");
509
539
static void __alloc(int size)
511
541
if (!(_cmdline.commands = dm_realloc(_cmdline.commands, sizeof(*_cmdline.commands) * size))) {
819
849
cmd->current_settings.verbose = 0;
852
if (arg_count(cmd, quiet_ARG) > 1)
853
cmd->current_settings.silent = 1;
822
855
if (arg_count(cmd, test_ARG))
823
856
cmd->current_settings.test = arg_count(cmd, test_ARG);
836
869
if (arg_count(cmd, partial_ARG)) {
837
870
cmd->partial_activation = 1;
838
log_print("Partial mode. Incomplete logical volumes will be processed.");
871
log_warn("PARTIAL MODE. Incomplete logical volumes will be processed.");
841
874
if (arg_count(cmd, ignorelockingfailure_ARG) || arg_count(cmd, sysinit_ARG))
844
877
init_ignorelockingfailure(0);
879
if (!arg_count(cmd, sysinit_ARG))
846
882
if (arg_count(cmd, nosuffix_ARG))
847
883
cmd->current_settings.suffix = 0;
874
910
if (!_merge_synonym(cmd, resizable_ARG, resizeable_ARG) ||
875
911
!_merge_synonym(cmd, allocation_ARG, allocatable_ARG) ||
876
912
!_merge_synonym(cmd, allocation_ARG, resizeable_ARG) ||
877
!_merge_synonym(cmd, virtualoriginsize_ARG, virtualsize_ARG))
913
!_merge_synonym(cmd, virtualoriginsize_ARG, virtualsize_ARG) ||
914
!_merge_synonym(cmd, available_ARG, activate_ARG))
878
915
return EINVALID_CMD_LINE;
880
917
if ((!strncmp(cmd->command->name, "pv", 2) &&
1193
/* Make sure we have always valid filedescriptors 0,1,2 */
1194
static int _check_standard_fds(void)
1196
int err = is_valid_fd(STDERR_FILENO);
1198
if (!is_valid_fd(STDIN_FILENO) &&
1199
!(stdin = fopen(_PATH_DEVNULL, "r"))) {
1201
perror("stdin stream open");
1203
printf("stdin stream open: %s\n",
1208
if (!is_valid_fd(STDOUT_FILENO) &&
1209
!(stdout = fopen(_PATH_DEVNULL, "w"))) {
1211
perror("stdout stream open");
1212
/* else no stdout */
1216
if (!is_valid_fd(STDERR_FILENO) &&
1217
!(stderr = fopen(_PATH_DEVNULL, "w"))) {
1218
printf("stderr stream open: %s\n",
1155
1226
static const char *_get_cmdline(pid_t pid)
1157
1228
static char _proc_cmdline[32];
1219
1290
fprintf(stderr, " Parent PID %" PRIpid_t ": %s\n", ppid, parent_cmdline);
1222
static void _close_stray_fds(const char *command)
1293
static int _close_stray_fds(const char *command)
1295
#ifndef VALGRIND_POOL
1224
1296
struct rlimit rlim;
1226
1298
unsigned suppress_warnings = 0;
1227
1299
pid_t ppid = getppid();
1228
1300
const char *parent_cmdline = _get_cmdline(ppid);
1230
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
1231
fprintf(stderr, "getrlimit(RLIMIT_NOFILE) failed: %s\n",
1301
static const char _fd_dir[] = DEFAULT_PROC_DIR "/self/fd";
1302
struct dirent *dirent;
1236
1305
if (getenv("LVM_SUPPRESS_FD_WARNINGS"))
1237
1306
suppress_warnings = 1;
1239
for (fd = 3; fd < (int)rlim.rlim_cur; fd++)
1240
_close_descriptor(fd, suppress_warnings, command, ppid,
1308
if (!(d = opendir(_fd_dir))) {
1309
if (errno != ENOENT) {
1310
log_sys_error("opendir", _fd_dir);
1311
return 0; /* broken system */
1314
/* Path does not exist, use the old way */
1315
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
1316
log_sys_error("getrlimit", "RLIMIT_NOFILE");
1320
for (fd = 3; fd < (int)rlim.rlim_cur; fd++)
1321
_close_descriptor(fd, suppress_warnings, command, ppid,
1326
while ((dirent = readdir(d))) {
1327
fd = atoi(dirent->d_name);
1328
if (fd > 2 && fd != dirfd(d))
1329
_close_descriptor(fd, suppress_warnings,
1330
command, ppid, parent_cmdline);
1334
log_sys_error("closedir", _fd_dir);
1244
1340
struct cmd_context *init_lvm(void)
1248
1344
if (!udev_init_library_context())
1251
if (!(cmd = create_toolcontext(0, NULL, 1, 0)))
1347
if (!(cmd = create_toolcontext(0, NULL, 1, 0))) {
1348
udev_fin_library_context();
1254
1352
_cmdline.arg_props = &_arg_props[0];
1256
1354
if (stored_errno()) {
1257
1355
destroy_toolcontext(cmd);
1356
udev_fin_library_context();
1390
1489
strcmp(base, "initrd-lvm"))
1393
_close_stray_fds(base);
1492
if (!_check_standard_fds())
1495
if (!_close_stray_fds(base))
1395
1498
if (is_static() && strcmp(base, "lvm.static") &&
1396
1499
path_exists(LVM_SHARED_PATH) &&