| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
|
| 5 |
#include "capability-util.h" |
| 6 |
#include "dev-setup.h" |
| 7 |
#include "mkdir.h" |
| 8 |
#include "path-util.h" |
| 9 |
#include "rm-rf.h" |
| 10 |
#include "tests.h" |
| 11 |
#include "tmpfile-util.h" |
| 12 |
|
| 13 |
int main(int argc, char *argv[]) { |
| 14 |
_cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
| 15 |
_cleanup_free_ char *f = NULL; |
| 16 |
struct stat st; |
| 17 |
|
| 18 |
test_setup_logging(LOG_DEBUG); |
| 19 |
|
| 20 |
if (have_effective_cap(CAP_DAC_OVERRIDE) <= 0) |
| 21 |
return log_tests_skipped("missing capability (CAP_DAC_OVERRIDE)"); |
| 22 |
|
| 23 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-dev-setupXXXXXX", &p)); |
| 24 |
|
| 25 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd")); |
| 26 |
ASSERT_OK(mkdir_p(f, 0755)); |
| 27 |
|
| 28 |
ASSERT_OK(make_inaccessible_nodes(f, 1, 1)); |
| 29 |
ASSERT_OK(make_inaccessible_nodes(f, 1, 1)); /* 2nd call should be a clean NOP */ |
| 30 |
|
| 31 |
free(f); |
| 32 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/reg")); |
| 33 |
ASSERT_OK_ERRNO(stat(f, &st)); |
| 34 |
ASSERT_TRUE(S_ISREG(st.st_mode)); |
| 35 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 36 |
|
| 37 |
free(f); |
| 38 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/dir")); |
| 39 |
ASSERT_OK_ERRNO(stat(f, &st)); |
| 40 |
ASSERT_TRUE(S_ISDIR(st.st_mode)); |
| 41 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 42 |
|
| 43 |
free(f); |
| 44 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/fifo")); |
| 45 |
ASSERT_OK_ERRNO(stat(f, &st)); |
| 46 |
ASSERT_TRUE(S_ISFIFO(st.st_mode)); |
| 47 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 48 |
|
| 49 |
free(f); |
| 50 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/sock")); |
| 51 |
ASSERT_OK_ERRNO(stat(f, &st)); |
| 52 |
ASSERT_TRUE(S_ISSOCK(st.st_mode)); |
| 53 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 54 |
|
| 55 |
free(f); |
| 56 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/chr")); |
| 57 |
if (stat(f, &st) < 0) |
| 58 |
ASSERT_EQ(errno, ENOENT); |
| 59 |
else { |
| 60 |
ASSERT_TRUE(S_ISCHR(st.st_mode)); |
| 61 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 62 |
} |
| 63 |
|
| 64 |
free(f); |
| 65 |
f = ASSERT_NOT_NULL(path_join(p, "/run/systemd/inaccessible/blk")); |
| 66 |
if (stat(f, &st) < 0) |
| 67 |
ASSERT_EQ(errno, ENOENT); |
| 68 |
else { |
| 69 |
ASSERT_TRUE(S_ISBLK(st.st_mode)); |
| 70 |
ASSERT_EQ(st.st_mode & 07777, 0000U); |
| 71 |
} |
| 72 |
|
| 73 |
return EXIT_SUCCESS; |
| 74 |
} |