static void _test_parse_bool(void *fixture) { static struct pb_test _tests[] = { {"", false, false}, {"fish", false, false}, {"true", false, false}, {"false", false, false}, {"1", true, true}, {" \t 1\t\t", true, true}, {"0", true, false}, {" \t0 ", true, false} }; unsigned i; for (i = 0; i < DM_ARRAY_SIZE(_tests); i++) { bool result; struct pb_test *t = _tests + i; if (t->parsed) { if (!_parse_bool(t->input, &result)) test_fail("_parse_bool('%s') unexpectedly failed", t->input); if (result != t->result) test_fail("_parse_bool('%s') -> %s", t->input, _bool(result)); } else { if (_parse_bool(t->input, &result)) test_fail("_parse_bool('%s') unexpectedly succeeded", t->input); } } }
static void _test_begins_with(void *fixture) { static struct bw_test _tests[] = { {"", "foo", NULL}, {"lskdj", "foo", NULL}, {"foo", "foobar", NULL}, {"fish", "fish", ""}, {"foo=bar ", "foo=", "bar "}, }; unsigned i; for (i = 0; i < DM_ARRAY_SIZE(_tests); i++) { const char *val; struct bw_test *t = _tests + i; if (t->val) { if (!_begins_with(t->input, t->prefix, &val)) test_fail("_begins_with('%s', '%s') failed", t->input, t->prefix); if (strcmp(val, t->val)) test_fail("_begins_with('%s', '%s') -> '%s', expected '%s'", t->input, t->prefix, val, t->val); } else { if (_begins_with(t->input, t->prefix, &val)) test_fail("_begins_with('%s', '%s') unexpectedly succeeded", t->input, t->prefix); } } }
static void test_kabi_query(void *fixture) { // Remember, matches regexes from last to first. static const char *_patterns[] = { ".*", ".*/dev/md.*", "loop" }; static struct { const char *input; int r; } _cases[] = { {"foo", 0}, {"/dev/mapper/vg-lvol1", 0}, {"/dev/mapper/vglvol1", 0}, {"/dev/md1", 1}, {"loop", 2}, }; int r; unsigned i; struct dm_pool *mem = fixture; struct dm_regex *scanner; scanner = dm_regex_create(mem, _patterns, DM_ARRAY_SIZE(_patterns)); T_ASSERT(scanner != NULL); for (i = 0; i < DM_ARRAY_SIZE(_cases); i++) { r = dm_regex_match(scanner, _cases[i].input); if (r != _cases[i].r) { test_fail("'%s' expected to match '%s', but matched %s", _cases[i].input, _cases[i].r >= DM_ARRAY_SIZE(_patterns) ? "<nothing>" : _patterns[_cases[i].r], r >= DM_ARRAY_SIZE(_patterns) ? "<nothing>" : _patterns[r]); } } }
static const char *_lvname_has_reserved_prefix(const char *lvname) { static const char _prefixes[][12] = { "pvmove", "snapshot" }; unsigned i; for (i = 0; i < DM_ARRAY_SIZE(_prefixes); ++i) if (!strncmp(lvname, _prefixes[i], strlen(_prefixes[i]))) return _prefixes[i]; return NULL; }
static const char *_lvname_has_reserved_string(const char *lvname) { static const char _strings[][12] = { /* Additional suffixes for non-compoment LVs */ "_pmspare", "_vorigin" }; unsigned i; const char *cs; if ((cs = _lvname_has_reserved_component_string(lvname))) return cs; for (i = 0; i < DM_ARRAY_SIZE(_strings); ++i) if (strstr(lvname, _strings[i])) return _strings[i]; return NULL; }
static const char *_lvname_has_reserved_string(const char *lvname) { static const char _strings[][12] = { "_cdata", "_cmeta", "_mimage", "_mlog", "_pmspare", "_rimage", "_rmeta", "_tdata", "_tmeta", "_vorigin" }; unsigned i; for (i = 0; i < DM_ARRAY_SIZE(_strings); ++i) if (strstr(lvname, _strings[i])) return _strings[i]; return NULL; }
static const char *_lvname_has_reserved_component_string(const char *lvname) { static const char _strings[][12] = { /* Suffixes for compoment LVs */ "_cdata", "_cmeta", "_corig", "_mimage", "_mlog", "_rimage", "_rmeta", "_tdata", "_tmeta" }; unsigned i; for (i = 0; i < DM_ARRAY_SIZE(_strings); ++i) if (strstr(lvname, _strings[i])) return _strings[i]; return NULL; }
/* * mlock/munlock memory areas from /proc/self/maps * format described in kernel/Documentation/filesystem/proc.txt */ static int _maps_line(const struct dm_config_node *cn, lvmlock_t lock, const char *line, size_t *mstats) { const struct dm_config_value *cv; long from, to; int pos; unsigned i; char fr, fw, fx, fp; size_t sz; const char *lock_str = (lock == LVM_MLOCK) ? "mlock" : "munlock"; if (sscanf(line, "%lx-%lx %c%c%c%c%n", &from, &to, &fr, &fw, &fx, &fp, &pos) != 6) { log_error("Failed to parse maps line: %s", line); return 0; } /* Select readable maps */ if (fr != 'r') { log_debug_mem("%s area unreadable %s : Skipping.", lock_str, line); return 1; } /* always ignored areas */ for (i = 0; i < DM_ARRAY_SIZE(_ignore_maps); ++i) if (strstr(line + pos, _ignore_maps[i])) { log_debug_mem("%s ignore filter '%s' matches '%s': Skipping.", lock_str, _ignore_maps[i], line); return 1; } sz = to - from; if (!cn) { /* If no blacklist configured, use an internal set */ for (i = 0; i < DM_ARRAY_SIZE(_blacklist_maps); ++i) if (strstr(line + pos, _blacklist_maps[i])) { log_debug_mem("%s default filter '%s' matches '%s': Skipping.", lock_str, _blacklist_maps[i], line); return 1; } } else { for (cv = cn->v; cv; cv = cv->next) { if ((cv->type != DM_CFG_STRING) || !cv->v.str[0]) continue; if (strstr(line + pos, cv->v.str)) { log_debug_mem("%s_filter '%s' matches '%s': Skipping.", lock_str, cv->v.str, line); return 1; } } } #ifdef HAVE_VALGRIND /* * Valgrind is continually eating memory while executing code * so we need to deactivate check of locked memory size */ #ifndef VALGRIND_POOL if (RUNNING_ON_VALGRIND) #endif sz -= sz; /* = 0, but avoids getting warning about dead assigment */ #endif *mstats += sz; log_debug_mem("%s %10ldKiB %12lx - %12lx %c%c%c%c%s", lock_str, ((long)sz + 1023) / 1024, from, to, fr, fw, fx, fp, line + pos); if (lock == LVM_MLOCK) { if (mlock((const void*)from, sz) < 0) { log_sys_error("mlock", line); return 0; } } else { if (munlock((const void*)from, sz) < 0) { log_sys_error("munlock", line); return 0; } } return 1; }