Debian Codegraph @

src/test/test-notify-recv.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-daemon.h"
7
8 #include "event-util.h"
9 #include "fd-util.h"
10 #include "fdset.h"
11 #include "notify-recv.h"
12 #include "pidref.h"
13 #include "process-util.h"
14 #include "strv.h"
15 #include "tests.h"
16
17 typedef struct Context {
18 unsigned data;
19 PidRef pidref;
20 } Context;
21
22 static void context_done(Context *c) {
23 assert(c);
24
25 pidref_done(&c->pidref);
26 }
27
28 static int on_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
29 Context *c = ASSERT_PTR(userdata);
30
31 _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
32 _cleanup_(pidref_done) PidRef sender = PIDREF_NULL;
33 _cleanup_strv_free_ char **l = NULL;
34 struct ucred ucred;
35 ASSERT_OK(notify_recv_with_fds_strv(fd, &l, &ucred, &sender, &fds));
36
37 ASSERT_TRUE(pidref_equal(&c->pidref, &sender));
38
39 ASSERT_EQ(ucred.gid, getgid());
40 ASSERT_EQ(ucred.pid, c->pidref.pid);
41 ASSERT_EQ(ucred.uid, getuid());
42
43 _cleanup_free_ char *joined = strv_join(l, ", ");
44 ASSERT_NOT_NULL(joined);
45 log_info("Received message: %s", joined);
46
47 if (strv_contains(l, "FIRST_MESSAGE=1")) {
48 ASSERT_STREQ(l[0], "FIRST_MESSAGE=1");
49 ASSERT_NULL(l[1]);
50 ASSERT_EQ(++c->data, 1u);
51 ASSERT_NULL(fds);
52 } else if (strv_contains(l, "SECOND_MESSAGE=1")) {
53 ASSERT_STREQ(l[0], "SECOND_MESSAGE=1");
54 ASSERT_STREQ(l[1], "ADDITIONAL_DATA=hoge");
55 ASSERT_EQ(++c->data, 2u);
56 ASSERT_NOT_NULL(fds);
57 ASSERT_EQ(fdset_size(fds), 2u);
58
59 int i;
60 FDSET_FOREACH(i, fds) {
61 _cleanup_free_ char *path = NULL;
62 ASSERT_OK(fd_get_path(i, &path));
63 ASSERT_TRUE(STR_IN_SET(path, "/tmp", "/dev/null"));
64 }
65 }
66
67 return 0;
68 }
69
70 static int on_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) {
71 Context *c = ASSERT_PTR(userdata);
72
73 ASSERT_EQ(si->si_code, CLD_EXITED);
74 ASSERT_EQ(si->si_status, EXIT_SUCCESS);
75
76 ASSERT_EQ(c->data, 2u);
77
78 return sd_event_exit(sd_event_source_get_event(s), 0);
79 }
80
81 TEST(notify_socket_prepare) {
82 int r;
83
84 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
85 ASSERT_OK(sd_event_new(&e));
86
87 _cleanup_(context_done) Context c = {
88 .pidref = PIDREF_NULL,
89 };
90 _cleanup_free_ char *path = NULL;
91 ASSERT_OK(notify_socket_prepare_full(e, SD_EVENT_PRIORITY_NORMAL - 10, on_recv, &c, true, &path, NULL));
92
93 ASSERT_OK(r = pidref_safe_fork("(test-notify-recv-child)", FORK_DEATHSIG_SIGTERM|FORK_LOG, &c.pidref));
94 if (r == 0) {
95 ASSERT_OK_ERRNO(setenv("NOTIFY_SOCKET", path, /* overwrite= */ true));
96 ASSERT_OK_POSITIVE(sd_notify(/* unset_environment= */ false, "FIRST_MESSAGE=1"));
97
98 _cleanup_close_ int fd1 = open("/tmp", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
99 ASSERT_OK_ERRNO(fd1);
100 _cleanup_close_ int fd2 = open("/dev/null", O_RDONLY|O_CLOEXEC);
101 ASSERT_OK_ERRNO(fd2);
102
103 ASSERT_OK_POSITIVE(
104 sd_pid_notify_with_fds(
105 0, /* unset_environment= */ false,
106 "SECOND_MESSAGE=1\nADDITIONAL_DATA=hoge", (int[]) { fd1, fd2 }, 2));
107 _exit(EXIT_SUCCESS);
108 }
109
110 ASSERT_OK(event_add_child_pidref(e, NULL, &c.pidref, WEXITED, on_sigchld, &c));
111 ASSERT_OK(sd_event_loop(e));
112 }
113
114 DEFINE_TEST_MAIN(LOG_DEBUG);