Systemd/src/test/test-umount.c

75 lines
2.5 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "errno-util.h"
#include "log.h"
#include "path-util.h"
#include "string-util.h"
#include "tests.h"
#include "umount.h"
#include "util.h"
static void test_mount_points_list(const char *fname) {
2018-03-14 11:32:13 +01:00
_cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
_cleanup_free_ char *testdata_fname = NULL;
MountPoint *m;
log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo");
if (fname) {
assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
fname = testdata_fname;
}
LIST_HEAD_INIT(mp_list_head);
assert_se(mount_points_list_get(fname, &mp_list_head) >= 0);
LIST_FOREACH(mount_point, m, mp_list_head)
log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
m->path,
strempty(m->remount_options),
m->remount_flags,
yes_no(m->try_remount_ro),
major(m->devnum), minor(m->devnum));
}
static void test_swap_list(const char *fname) {
_cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
_cleanup_free_ char *testdata_fname = NULL;
MountPoint *m;
int r;
core/umount: use libmount to enumerate /proc/swaps example.swaps with "(deleted)" does not cause bogus entries in the list now, but a memleak in libmount instead. The memleaks is not very important since this code is run just once. Reported as https://github.com/karelzak/util-linux/issues/596. $ build/test-umount ... /* test_swap_list("/proc/swaps") */ path=/var/tmp/swap o= f=0x0 try-ro=no dev=0:0 path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0 /* test_swap_list("/home/zbyszek/src/systemd/test/test-umount/example.swaps") */ path=/some/swapfile o= f=0x0 try-ro=no dev=0:0 path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0 ==26912== ==26912== HEAP SUMMARY: ==26912== in use at exit: 16 bytes in 1 blocks ==26912== total heap usage: 1,546 allocs, 1,545 frees, 149,008 bytes allocated ==26912== ==26912== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==26912== at 0x4C31C15: realloc (vg_replace_malloc.c:785) ==26912== by 0x55C5D8C: _IO_vfscanf (in /usr/lib64/libc-2.26.so) ==26912== by 0x55D8AEC: vsscanf (in /usr/lib64/libc-2.26.so) ==26912== by 0x55D25C3: sscanf (in /usr/lib64/libc-2.26.so) ==26912== by 0x53236D0: mnt_table_parse_stream (in /usr/lib64/libmount.so.1.1.0) ==26912== by 0x53249B6: mnt_table_parse_file (in /usr/lib64/libmount.so.1.1.0) ==26912== by 0x10D157: swap_list_get (umount.c:194) ==26912== by 0x10B06E: test_swap_list (test-umount.c:34) ==26912== by 0x10B24B: main (test-umount.c:56) ==26912== ==26912== LEAK SUMMARY: ==26912== definitely lost: 16 bytes in 1 blocks ==26912== indirectly lost: 0 bytes in 0 blocks ==26912== possibly lost: 0 bytes in 0 blocks ==26912== still reachable: 0 bytes in 0 blocks ==26912== suppressed: 0 bytes in 0 blocks
2018-03-14 12:22:27 +01:00
log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/swaps");
if (fname) {
assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
fname = testdata_fname;
}
LIST_HEAD_INIT(mp_list_head);
r = swap_list_get(fname, &mp_list_head);
if (ERRNO_IS_PRIVILEGE(r))
return;
assert_se(r >= 0);
LIST_FOREACH(mount_point, m, mp_list_head)
log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
m->path,
strempty(m->remount_options),
m->remount_flags,
yes_no(m->try_remount_ro),
major(m->devnum), minor(m->devnum));
}
int main(int argc, char **argv) {
test_setup_logging(LOG_DEBUG);
test_mount_points_list(NULL);
test_mount_points_list("/test-umount/empty.mountinfo");
test_mount_points_list("/test-umount/garbled.mountinfo");
test_mount_points_list("/test-umount/rhbug-1554943.mountinfo");
test_swap_list(NULL);
test_swap_list("/test-umount/example.swaps");
}