]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-login/test-login.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
12 #include "format-util.h"
14 #include "string-util.h"
18 static char* format_uids(char **buf
, uid_t
* uids
, int count
) {
20 size_t size
= (DECIMAL_STR_MAX(uid_t
) + 1) * count
+ 1;
22 assert_se(*buf
= malloc(size
));
24 for (k
= 0; k
< count
; k
++) {
25 sprintf(*buf
+ pos
, "%s"UID_FMT
"%n", k
> 0 ? " " : "", uids
[k
], &inc
);
29 assert_se(pos
< (ssize_t
)size
);
35 static void test_login(void) {
36 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
37 _cleanup_free_
char *pp
= NULL
, *qq
= NULL
,
38 *display_session
= NULL
, *cgroup
= NULL
,
39 *display
= NULL
, *remote_user
= NULL
, *remote_host
= NULL
,
40 *type
= NULL
, *class = NULL
, *state
= NULL
, *state2
= NULL
,
41 *seat
= NULL
, *session
= NULL
,
42 *unit
= NULL
, *user_unit
= NULL
, *slice
= NULL
;
45 char *t
, **seats
, **sessions
;
47 r
= sd_pid_get_unit(0, &unit
);
48 assert_se(r
>= 0 || r
== -ENODATA
);
49 log_info("sd_pid_get_unit(0, …) → \"%s\"", strna(unit
));
51 r
= sd_pid_get_user_unit(0, &user_unit
);
52 assert_se(r
>= 0 || r
== -ENODATA
);
53 log_info("sd_pid_get_user_unit(0, …) → \"%s\"", strna(user_unit
));
55 r
= sd_pid_get_slice(0, &slice
);
56 assert_se(r
>= 0 || r
== -ENODATA
);
57 log_info("sd_pid_get_slice(0, …) → \"%s\"", strna(slice
));
59 r
= sd_pid_get_session(0, &session
);
61 log_warning_errno(r
, "sd_pid_get_session(0, …): %m");
63 log_info("Seems we are not running in a session, skipping some tests.");
65 log_info("sd_pid_get_session(0, …) → \"%s\"", session
);
67 assert_se(sd_pid_get_owner_uid(0, &u2
) == 0);
68 log_info("sd_pid_get_owner_uid(0, …) → "UID_FMT
, u2
);
70 assert_se(sd_pid_get_cgroup(0, &cgroup
) == 0);
71 log_info("sd_pid_get_cgroup(0, …) → \"%s\"", cgroup
);
73 r
= sd_uid_get_display(u2
, &display_session
);
74 assert_se(r
>= 0 || r
== -ENODATA
);
75 log_info("sd_uid_get_display("UID_FMT
", …) → \"%s\"",
76 u2
, strnull(display_session
));
78 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == 0);
79 sd_peer_get_session(pair
[0], &pp
);
80 sd_peer_get_session(pair
[1], &qq
);
81 assert_se(streq_ptr(pp
, qq
));
83 r
= sd_uid_get_sessions(u2
, false, &sessions
);
85 assert_se(r
== (int) strv_length(sessions
));
86 assert_se(t
= strv_join(sessions
, " "));
88 log_info("sd_uid_get_sessions("UID_FMT
", …) → [%i] \"%s\"", u2
, r
, t
);
91 assert_se(r
== sd_uid_get_sessions(u2
, false, NULL
));
93 r
= sd_uid_get_seats(u2
, false, &seats
);
95 assert_se(r
== (int) strv_length(seats
));
96 assert_se(t
= strv_join(seats
, " "));
98 log_info("sd_uid_get_seats("UID_FMT
", …) → [%i] \"%s\"", u2
, r
, t
);
101 assert_se(r
== sd_uid_get_seats(u2
, false, NULL
));
105 r
= sd_session_is_active(session
);
107 log_info("sd_session_is_active(\"%s\") → %s", session
, yes_no(r
));
109 r
= sd_session_is_remote(session
);
111 log_info("sd_session_is_remote(\"%s\") → %s", session
, yes_no(r
));
113 r
= sd_session_get_state(session
, &state
);
115 log_info("sd_session_get_state(\"%s\") → \"%s\"", session
, state
);
117 assert_se(sd_session_get_uid(session
, &u
) >= 0);
118 log_info("sd_session_get_uid(\"%s\") → "UID_FMT
, session
, u
);
121 assert_se(sd_session_get_type(session
, &type
) >= 0);
122 log_info("sd_session_get_type(\"%s\") → \"%s\"", session
, type
);
124 assert_se(sd_session_get_class(session
, &class) >= 0);
125 log_info("sd_session_get_class(\"%s\") → \"%s\"", session
, class);
127 r
= sd_session_get_display(session
, &display
);
128 assert_se(r
>= 0 || r
== -ENODATA
);
129 log_info("sd_session_get_display(\"%s\") → \"%s\"", session
, strna(display
));
131 r
= sd_session_get_remote_user(session
, &remote_user
);
132 assert_se(r
>= 0 || r
== -ENODATA
);
133 log_info("sd_session_get_remote_user(\"%s\") → \"%s\"",
134 session
, strna(remote_user
));
136 r
= sd_session_get_remote_host(session
, &remote_host
);
137 assert_se(r
>= 0 || r
== -ENODATA
);
138 log_info("sd_session_get_remote_host(\"%s\") → \"%s\"",
139 session
, strna(remote_host
));
141 r
= sd_session_get_seat(session
, &seat
);
145 log_info("sd_session_get_seat(\"%s\") → \"%s\"", session
, seat
);
147 r
= sd_seat_can_multi_session(seat
);
149 log_info("sd_session_can_multi_seat(\"%s\") → %s", seat
, yes_no(r
));
151 r
= sd_seat_can_tty(seat
);
153 log_info("sd_session_can_tty(\"%s\") → %s", seat
, yes_no(r
));
155 r
= sd_seat_can_graphical(seat
);
157 log_info("sd_session_can_graphical(\"%s\") → %s", seat
, yes_no(r
));
159 log_info_errno(r
, "sd_session_get_seat(\"%s\"): %m", session
);
160 assert_se(r
== -ENODATA
);
163 assert_se(sd_uid_get_state(u
, &state2
) >= 0);
164 log_info("sd_uid_get_state("UID_FMT
", …) → %s", u
, state2
);
168 _cleanup_free_
char *session2
= NULL
, *buf
= NULL
;
169 _cleanup_free_ uid_t
*uids
= NULL
;
172 assert_se(sd_uid_is_on_seat(u
, 0, seat
) > 0);
174 r
= sd_seat_get_active(seat
, &session2
, &u2
);
176 log_info("sd_seat_get_active(\"%s\", …) → \"%s\", "UID_FMT
, seat
, session2
, u2
);
178 r
= sd_uid_is_on_seat(u
, 1, seat
);
180 assert_se(!!r
== streq(session
, session2
));
182 r
= sd_seat_get_sessions(seat
, &sessions
, &uids
, &n
);
184 assert_se(r
== (int) strv_length(sessions
));
185 assert_se(t
= strv_join(sessions
, " "));
187 log_info("sd_seat_get_sessions(\"%s\", …) → %i, \"%s\", [%i] {%s}",
188 seat
, r
, t
, n
, format_uids(&buf
, uids
, n
));
191 assert_se(sd_seat_get_sessions(seat
, NULL
, NULL
, NULL
) == r
);
194 r
= sd_get_seats(&seats
);
196 assert_se(r
== (int) strv_length(seats
));
197 assert_se(t
= strv_join(seats
, ", "));
199 log_info("sd_get_seats(…) → [%i] \"%s\"", r
, t
);
202 assert_se(sd_get_seats(NULL
) == r
);
204 r
= sd_seat_get_active(NULL
, &t
, NULL
);
205 assert_se(IN_SET(r
, 0, -ENODATA
));
206 log_info("sd_seat_get_active(NULL, …) (active session on current seat) → %s", strnull(t
));
209 r
= sd_get_sessions(&sessions
);
211 assert_se(r
== (int) strv_length(sessions
));
212 assert_se(t
= strv_join(sessions
, ", "));
214 log_info("sd_get_sessions(…) → [%i] \"%s\"", r
, t
);
217 assert_se(sd_get_sessions(NULL
) == r
);
220 _cleanup_free_ uid_t
*uids
= NULL
;
221 _cleanup_free_
char *buf
= NULL
;
223 r
= sd_get_uids(&uids
);
225 log_info("sd_get_uids(…) → [%i] {%s}", r
, format_uids(&buf
, uids
, r
));
227 assert_se(sd_get_uids(NULL
) == r
);
231 _cleanup_strv_free_
char **machines
= NULL
;
232 _cleanup_free_
char *buf
= NULL
;
234 r
= sd_get_machine_names(&machines
);
236 assert_se(r
== (int) strv_length(machines
));
237 assert_se(buf
= strv_join(machines
, " "));
238 log_info("sd_get_machines(…) → [%i] \"%s\"", r
, buf
);
240 assert_se(sd_get_machine_names(NULL
) == r
);
244 static void test_monitor(void) {
245 sd_login_monitor
*m
= NULL
;
249 r
= sd_login_monitor_new("session", &m
);
252 for (n
= 0; n
< 5; n
++) {
253 struct pollfd pollfd
= {};
256 assert_se((pollfd
.fd
= sd_login_monitor_get_fd(m
)) >= 0);
257 assert_se((pollfd
.events
= sd_login_monitor_get_events(m
)) >= 0);
259 assert_se(sd_login_monitor_get_timeout(m
, &timeout
) >= 0);
261 nw
= now(CLOCK_MONOTONIC
);
264 timeout
== (uint64_t) -1 ? -1 :
265 timeout
> nw
? (int) ((timeout
- nw
) / 1000) :
270 sd_login_monitor_flush(m
);
274 sd_login_monitor_unref(m
);
277 int main(int argc
, char* argv
[]) {
278 log_parse_environment();
281 log_info("/* Information printed is from the live system */");
285 if (streq_ptr(argv
[1], "-m"))