]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-login/test-login.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "formats-util.h"
29 #include "string-util.h"
33 static void test_login(void) {
34 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
35 _cleanup_free_
char *pp
= NULL
, *qq
= NULL
;
38 char *seat
, *type
, *class, *display
, *remote_user
, *remote_host
, *display_session
, *cgroup
;
43 char **seats
, **sessions
, **machines
;
47 sd_login_monitor
*m
= NULL
;
49 assert_se(sd_pid_get_session(0, &session
) == 0);
50 printf("session = %s\n", session
);
52 assert_se(sd_pid_get_owner_uid(0, &u2
) == 0);
53 printf("user = "UID_FMT
"\n", u2
);
55 assert_se(sd_pid_get_cgroup(0, &cgroup
) == 0);
56 printf("cgroup = %s\n", cgroup
);
59 display_session
= NULL
;
60 r
= sd_uid_get_display(u2
, &display_session
);
61 assert_se(r
>= 0 || r
== -ENODATA
);
62 printf("user's display session = %s\n", strna(display_session
));
63 free(display_session
);
65 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == 0);
66 sd_peer_get_session(pair
[0], &pp
);
67 sd_peer_get_session(pair
[1], &qq
);
68 assert_se(streq_ptr(pp
, qq
));
70 r
= sd_uid_get_sessions(u2
, false, &sessions
);
72 assert_se(r
== (int) strv_length(sessions
));
73 assert_se(t
= strv_join(sessions
, ", "));
75 printf("sessions = %s\n", t
);
78 assert_se(r
== sd_uid_get_sessions(u2
, false, NULL
));
80 r
= sd_uid_get_seats(u2
, false, &seats
);
82 assert_se(r
== (int) strv_length(seats
));
83 assert_se(t
= strv_join(seats
, ", "));
85 printf("seats = %s\n", t
);
88 assert_se(r
== sd_uid_get_seats(u2
, false, NULL
));
90 r
= sd_session_is_active(session
);
92 printf("active = %s\n", yes_no(r
));
94 r
= sd_session_is_remote(session
);
96 printf("remote = %s\n", yes_no(r
));
98 r
= sd_session_get_state(session
, &state
);
100 printf("state = %s\n", state
);
103 assert_se(sd_session_get_uid(session
, &u
) >= 0);
104 printf("uid = "UID_FMT
"\n", u
);
107 assert_se(sd_session_get_type(session
, &type
) >= 0);
108 printf("type = %s\n", type
);
111 assert_se(sd_session_get_class(session
, &class) >= 0);
112 printf("class = %s\n", class);
116 r
= sd_session_get_display(session
, &display
);
117 assert_se(r
>= 0 || r
== -ENODATA
);
118 printf("display = %s\n", strna(display
));
122 r
= sd_session_get_remote_user(session
, &remote_user
);
123 assert_se(r
>= 0 || r
== -ENODATA
);
124 printf("remote_user = %s\n", strna(remote_user
));
128 r
= sd_session_get_remote_host(session
, &remote_host
);
129 assert_se(r
>= 0 || r
== -ENODATA
);
130 printf("remote_host = %s\n", strna(remote_host
));
133 assert_se(sd_session_get_seat(session
, &seat
) >= 0);
134 printf("seat = %s\n", seat
);
136 r
= sd_seat_can_multi_session(seat
);
138 printf("can do multi session = %s\n", yes_no(r
));
140 r
= sd_seat_can_tty(seat
);
142 printf("can do tty = %s\n", yes_no(r
));
144 r
= sd_seat_can_graphical(seat
);
146 printf("can do graphical = %s\n", yes_no(r
));
148 assert_se(sd_uid_get_state(u
, &state
) >= 0);
149 printf("state = %s\n", state
);
151 assert_se(sd_uid_is_on_seat(u
, 0, seat
) > 0);
153 k
= sd_uid_is_on_seat(u
, 1, seat
);
155 assert_se(!!r
== !!r
);
157 assert_se(sd_seat_get_active(seat
, &session2
, &u2
) >= 0);
158 printf("session2 = %s\n", session2
);
159 printf("uid2 = "UID_FMT
"\n", u2
);
161 r
= sd_seat_get_sessions(seat
, &sessions
, &uids
, &n
);
163 printf("n_sessions = %i\n", r
);
164 assert_se(r
== (int) strv_length(sessions
));
165 assert_se(t
= strv_join(sessions
, ", "));
167 printf("sessions = %s\n", t
);
170 for (k
= 0; k
< (int) n
; k
++)
171 printf(" "UID_FMT
, uids
[k
]);
175 assert_se(sd_seat_get_sessions(seat
, NULL
, NULL
, NULL
) == r
);
182 r
= sd_get_seats(&seats
);
184 assert_se(r
== (int) strv_length(seats
));
185 assert_se(t
= strv_join(seats
, ", "));
187 printf("n_seats = %i\n", r
);
188 printf("seats = %s\n", t
);
191 assert_se(sd_get_seats(NULL
) == r
);
193 r
= sd_seat_get_active(NULL
, &t
, NULL
);
195 printf("active session on current seat = %s\n", t
);
198 r
= sd_get_sessions(&sessions
);
200 assert_se(r
== (int) strv_length(sessions
));
201 assert_se(t
= strv_join(sessions
, ", "));
203 printf("n_sessions = %i\n", r
);
204 printf("sessions = %s\n", t
);
207 assert_se(sd_get_sessions(NULL
) == r
);
209 r
= sd_get_uids(&uids
);
213 for (k
= 0; k
< r
; k
++)
214 printf(" "UID_FMT
, uids
[k
]);
218 printf("n_uids = %i\n", r
);
219 assert_se(sd_get_uids(NULL
) == r
);
221 r
= sd_get_machine_names(&machines
);
223 assert_se(r
== (int) strv_length(machines
));
224 assert_se(t
= strv_join(machines
, ", "));
226 printf("n_machines = %i\n", r
);
227 printf("machines = %s\n", t
);
230 r
= sd_login_monitor_new("session", &m
);
233 for (n
= 0; n
< 5; n
++) {
237 assert_se((pollfd
.fd
= sd_login_monitor_get_fd(m
)) >= 0);
238 assert_se((pollfd
.events
= sd_login_monitor_get_events(m
)) >= 0);
240 assert_se(sd_login_monitor_get_timeout(m
, &timeout
) >= 0);
242 nw
= now(CLOCK_MONOTONIC
);
245 timeout
== (uint64_t) -1 ? -1 :
246 timeout
> nw
? (int) ((timeout
- nw
) / 1000) :
251 sd_login_monitor_flush(m
);
255 sd_login_monitor_unref(m
);
258 int main(int argc
, char* argv
[]) {
259 log_parse_environment();