~ubuntu-branches/debian/sid/lvm2/sid

« back to all changes in this revision

Viewing changes to tools/lvmcmdline.c

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2013-03-03 12:33:47 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20130303123347-smfwei6dodkdth55
Tags: 2.02.98-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
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.
4
4
 *
5
5
 * This file is part of LVM2.
6
6
 *
19
19
#include "lvm-version.h"
20
20
 
21
21
#include "stub.h"
22
 
#include "lvm2cmd.h"
23
22
#include "last-path-component.h"
24
23
 
25
24
#include <signal.h>
26
 
#include <syslog.h>
27
 
#include <libgen.h>
28
25
#include <sys/stat.h>
29
26
#include <time.h>
30
27
#include <sys/resource.h>
 
28
#include <dirent.h>
 
29
#include <paths.h>
31
30
 
32
31
#ifdef HAVE_GETOPTLONG
33
32
#  include <getopt.h>
94
93
        return grouped_arg_count(av, a) ? av[a].i_value : def;
95
94
}
96
95
 
 
96
int32_t first_grouped_arg_int_value(struct cmd_context *cmd, int a, const int32_t def)
 
97
{
 
98
        struct arg_value_group_list *current_group;
 
99
        struct arg_values *av;
 
100
 
 
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);
 
105
        }
 
106
 
 
107
        return def;
 
108
}
 
109
 
97
110
int32_t arg_int_value(struct cmd_context *cmd, int a, const int32_t def)
98
111
{
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);
100
114
}
101
115
 
102
116
uint32_t arg_uint_value(struct cmd_context *cmd, int a, const uint32_t def)
157
171
        return 1;
158
172
}
159
173
 
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)
161
175
{
162
176
        av->sign = SIGN_NONE;
163
177
        av->percent = PERCENT_NONE;
173
187
                av->ui_value = CHANGE_AY;
174
188
        }
175
189
 
 
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;
 
194
        }
 
195
 
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;
195
215
        return 1;
196
216
}
197
217
 
 
218
int discards_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
 
219
{
 
220
        thin_discards_t discards;
 
221
 
 
222
        if (!get_pool_discards(av->value, &discards))
 
223
                return_0;
 
224
 
 
225
        av->i_value = discards;
 
226
        av->ui_value = discards;
 
227
 
 
228
        return 1;
 
229
}
 
230
 
198
231
int metadatatype_arg(struct cmd_context *cmd, struct arg_values *av)
199
232
{
200
233
        return get_format_by_name(cmd, av->value) ? 1 : 0;
373
406
        return 1;
374
407
}
375
408
 
376
 
int minor_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
377
 
{
378
 
        char *ptr;
379
 
 
380
 
        if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
381
 
                return 0;
382
 
 
383
 
        if (av->i_value > 255) {
384
 
                log_error("Minor number outside range 0-255");
385
 
                return 0;
386
 
        }
387
 
 
388
 
        return 1;
389
 
}
390
 
 
391
 
int major_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
392
 
{
393
 
        char *ptr;
394
 
 
395
 
        if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
396
 
                return 0;
397
 
 
398
 
        if (av->i_value > 255) {
399
 
                log_error("Major number outside range 0-255");
400
 
                return 0;
401
 
        }
402
 
 
403
 
        /* FIXME Also Check against /proc/devices */
404
 
 
405
 
        return 1;
406
 
}
407
 
 
408
409
int string_arg(struct cmd_context *cmd __attribute__((unused)),
409
410
               struct arg_values *av __attribute__((unused)))
410
411
{
506
507
        return int_arg(cmd, av);
507
508
}
508
509
 
 
510
int major_minor_valid(const struct cmd_context *cmd, const struct format_type *fmt,
 
511
                      int32_t major, int32_t minor)
 
512
{
 
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");
 
517
                        return 0;
 
518
                }
 
519
                if (minor < 0 || minor > 255) {
 
520
                        log_error("Minor number outside range 0-255");
 
521
                        return 0;
 
522
                }
 
523
        } else {
 
524
                /* 12 bits for major number */
 
525
                if (major < 0 || major > 4095) {
 
526
                        log_error("Major number outside range 0-4095");
 
527
                        return 0;
 
528
                }
 
529
                /* 20 bits for minor number */
 
530
                if (minor < 0 || minor > 1048575) {
 
531
                        log_error("Minor number outside range 0-1048575");
 
532
                        return 0;
 
533
                }
 
534
        }
 
535
 
 
536
        return 1;
 
537
}
 
538
 
509
539
static void __alloc(int size)
510
540
{
511
541
        if (!(_cmdline.commands = dm_realloc(_cmdline.commands, sizeof(*_cmdline.commands) * size))) {
819
849
                cmd->current_settings.verbose = 0;
820
850
        }
821
851
 
 
852
        if (arg_count(cmd, quiet_ARG) > 1)
 
853
                cmd->current_settings.silent = 1;
 
854
 
822
855
        if (arg_count(cmd, test_ARG))
823
856
                cmd->current_settings.test = arg_count(cmd, test_ARG);
824
857
 
835
868
 
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.");
839
872
        }
840
873
 
841
874
        if (arg_count(cmd, ignorelockingfailure_ARG) || arg_count(cmd, sysinit_ARG))
843
876
        else
844
877
                init_ignorelockingfailure(0);
845
878
 
 
879
        if (!arg_count(cmd, sysinit_ARG))
 
880
                lvmetad_warning();
 
881
 
846
882
        if (arg_count(cmd, nosuffix_ARG))
847
883
                cmd->current_settings.suffix = 0;
848
884
 
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;
879
916
 
880
917
        if ((!strncmp(cmd->command->name, "pv", 2) &&
937
974
{
938
975
        init_debug(cmd->current_settings.debug);
939
976
        init_verbose(cmd->current_settings.verbose + VERBOSE_BASE_LEVEL);
 
977
        init_silent(cmd->current_settings.silent);
940
978
        init_test(cmd->current_settings.test);
941
979
        init_full_scan_done(0);
942
980
        init_mirror_in_sync(0);
1152
1190
        return *argc;
1153
1191
}
1154
1192
 
 
1193
/* Make sure we have always valid filedescriptors 0,1,2 */
 
1194
static int _check_standard_fds(void)
 
1195
{
 
1196
        int err = is_valid_fd(STDERR_FILENO);
 
1197
 
 
1198
        if (!is_valid_fd(STDIN_FILENO) &&
 
1199
            !(stdin = fopen(_PATH_DEVNULL, "r"))) {
 
1200
                if (err)
 
1201
                        perror("stdin stream open");
 
1202
                else
 
1203
                        printf("stdin stream open: %s\n",
 
1204
                               strerror(errno));
 
1205
                return 0;
 
1206
        }
 
1207
 
 
1208
        if (!is_valid_fd(STDOUT_FILENO) &&
 
1209
            !(stdout = fopen(_PATH_DEVNULL, "w"))) {
 
1210
                if (err)
 
1211
                        perror("stdout stream open");
 
1212
                /* else no stdout */
 
1213
                return 0;
 
1214
        }
 
1215
 
 
1216
        if (!is_valid_fd(STDERR_FILENO) &&
 
1217
            !(stderr = fopen(_PATH_DEVNULL, "w"))) {
 
1218
                printf("stderr stream open: %s\n",
 
1219
                       strerror(errno));
 
1220
                return 0;
 
1221
        }
 
1222
 
 
1223
        return 1;
 
1224
}
 
1225
 
1155
1226
static const char *_get_cmdline(pid_t pid)
1156
1227
{
1157
1228
        static char _proc_cmdline[32];
1197
1268
        const char *filename;
1198
1269
 
1199
1270
        /* Ignore bad file descriptors */
1200
 
        if (fcntl(fd, F_GETFD) == -1 && errno == EBADF)
 
1271
        if (!is_valid_fd(fd))
1201
1272
                return;
1202
1273
 
1203
1274
        if (!suppress_warnings)
1219
1290
        fprintf(stderr, " Parent PID %" PRIpid_t ": %s\n", ppid, parent_cmdline);
1220
1291
}
1221
1292
 
1222
 
static void _close_stray_fds(const char *command)
 
1293
static int _close_stray_fds(const char *command)
1223
1294
{
 
1295
#ifndef VALGRIND_POOL
1224
1296
        struct rlimit rlim;
1225
1297
        int fd;
1226
1298
        unsigned suppress_warnings = 0;
1227
1299
        pid_t ppid = getppid();
1228
1300
        const char *parent_cmdline = _get_cmdline(ppid);
1229
 
 
1230
 
        if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
1231
 
                fprintf(stderr, "getrlimit(RLIMIT_NOFILE) failed: %s\n",
1232
 
                        strerror(errno));
1233
 
                return;
1234
 
        }
 
1301
        static const char _fd_dir[] = DEFAULT_PROC_DIR "/self/fd";
 
1302
        struct dirent *dirent;
 
1303
        DIR *d;
1235
1304
 
1236
1305
        if (getenv("LVM_SUPPRESS_FD_WARNINGS"))
1237
1306
                suppress_warnings = 1;
1238
1307
 
1239
 
        for (fd = 3; fd < (int)rlim.rlim_cur; fd++)
1240
 
                _close_descriptor(fd, suppress_warnings, command, ppid,
1241
 
                                  parent_cmdline);
 
1308
        if (!(d = opendir(_fd_dir))) {
 
1309
                if (errno != ENOENT) {
 
1310
                        log_sys_error("opendir", _fd_dir);
 
1311
                        return 0; /* broken system */
 
1312
                }
 
1313
 
 
1314
                /* Path does not exist, use the old way */
 
1315
                if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
 
1316
                        log_sys_error("getrlimit", "RLIMIT_NOFILE");
 
1317
                        return 1;
 
1318
                }
 
1319
 
 
1320
                for (fd = 3; fd < (int)rlim.rlim_cur; fd++)
 
1321
                        _close_descriptor(fd, suppress_warnings, command, ppid,
 
1322
                                          parent_cmdline);
 
1323
                return 1;
 
1324
        }
 
1325
 
 
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);
 
1331
        }
 
1332
 
 
1333
        if (closedir(d))
 
1334
                log_sys_error("closedir", _fd_dir);
 
1335
#endif
 
1336
 
 
1337
        return 1;
1242
1338
}
1243
1339
 
1244
1340
struct cmd_context *init_lvm(void)
1248
1344
        if (!udev_init_library_context())
1249
1345
                stack;
1250
1346
 
1251
 
        if (!(cmd = create_toolcontext(0, NULL, 1, 0)))
 
1347
        if (!(cmd = create_toolcontext(0, NULL, 1, 0))) {
 
1348
                udev_fin_library_context();
1252
1349
                return_NULL;
 
1350
        }
1253
1351
 
1254
1352
        _cmdline.arg_props = &_arg_props[0];
1255
1353
 
1256
1354
        if (stored_errno()) {
1257
1355
                destroy_toolcontext(cmd);
 
1356
                udev_fin_library_context();
1258
1357
                return_NULL;
1259
1358
        }
1260
1359
 
1390
1489
            strcmp(base, "initrd-lvm"))
1391
1490
                alias = 1;
1392
1491
 
1393
 
        _close_stray_fds(base);
 
1492
        if (!_check_standard_fds())
 
1493
                return -1;
 
1494
 
 
1495
        if (!_close_stray_fds(base))
 
1496
                return -1;
1394
1497
 
1395
1498
        if (is_static() && strcmp(base, "lvm.static") &&
1396
1499
            path_exists(LVM_SHARED_PATH) &&