Debian Codegraph @

src/libsystemd/sd-journal/test-journal-init.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-journal.h"
7
8 #include "chattr-util.h"
9 #include "journal-internal.h"
10 #include "log.h"
11 #include "parse-util.h"
12 #include "process-util.h"
13 #include "rm-rf.h"
14 #include "tests.h"
15
16 int main(int argc, char *argv[]) {
17 sd_journal *j;
18 int r, i, I = 100;
19 char t[] = "/var/tmp/journal-stream-XXXXXX";
20
21 test_setup_logging(LOG_DEBUG);
22
23 if (argc >= 2) {
24 r = safe_atoi(argv[1], &I);
25 if (r < 0)
26 log_info("Could not parse loop count argument. Using default.");
27 }
28
29 log_info("Running %d loops", I);
30
31 ASSERT_NOT_NULL(mkdtemp(t));
32 (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL);
33
34 for (i = 0; i < I; i++) {
35 ASSERT_OK_ZERO(sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE));
36
37 sd_journal_close(j);
38
39 ASSERT_OK_ZERO(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
40
41 ASSERT_OK_ZERO(sd_journal_seek_head(j));
42 ASSERT_EQ(j->current_location.type, (LocationType) LOCATION_HEAD);
43
44 r = pidref_safe_fork("(journal-fork-test)", FORK_WAIT|FORK_LOG, NULL);
45 if (r == 0) {
46 ASSERT_NOT_NULL(j);
47 ASSERT_RETURN_EXPECTED_SE(sd_journal_get_realtime_usec(j, NULL) == -ECHILD);
48 ASSERT_RETURN_EXPECTED_SE(sd_journal_seek_tail(j) == -ECHILD);
49 ASSERT_EQ(j->current_location.type, (LocationType) LOCATION_HEAD);
50 sd_journal_close(j);
51 _exit(EXIT_SUCCESS);
52 }
53
54 ASSERT_OK(r);
55
56 sd_journal_close(j);
57
58 j = NULL;
59 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_open_directory(&j, t, SD_JOURNAL_LOCAL_ONLY), EINVAL));
60 ASSERT_NULL(j);
61 }
62
63 ASSERT_OK(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL));
64
65 return 0;
66 }