]>
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/>.
27 #include "formats-util.h"
28 #include "string-util.h"
32 static void test_login(void) {
33 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
34 _cleanup_free_
char *pp
= NULL
, *qq
= NULL
;
37 char *seat
, *type
, *class, *display
, *remote_user
, *remote_host
, *display_session
, *cgroup
;
42 char **seats
, **sessions
, **machines
;
46 sd_login_monitor
*m
= NULL
;
48 assert_se(sd_pid_get_session(0, &session
) == 0);
49 printf("session = %s\n", session
);
51 assert_se(sd_pid_get_owner_uid(0, &u2
) == 0);
52 printf("user = "UID_FMT
"\n", u2
);
54 assert_se(sd_pid_get_cgroup(0, &cgroup
) == 0);
55 printf("cgroup = %s\n", cgroup
);
58 display_session
= NULL
;
59 r
= sd_uid_get_display(u2
, &display_session
);
60 assert_se(r
>= 0 || r
== -ENODATA
);
61 printf("user's display session = %s\n", strna(display_session
));
62 free(display_session
);
64 assert_se(socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == 0);
65 sd_peer_get_session(pair
[0], &pp
);
66 sd_peer_get_session(pair
[1], &qq
);
67 assert_se(streq_ptr(pp
, qq
));
69 r
= sd_uid_get_sessions(u2
, false, &sessions
);
71 assert_se(r
== (int) strv_length(sessions
));
72 assert_se(t
= strv_join(sessions
, ", "));
74 printf("sessions = %s\n", t
);
77 assert_se(r
== sd_uid_get_sessions(u2
, false, NULL
));
79 r
= sd_uid_get_seats(u2
, false, &seats
);
81 assert_se(r
== (int) strv_length(seats
));
82 assert_se(t
= strv_join(seats
, ", "));
84 printf("seats = %s\n", t
);
87 assert_se(r
== sd_uid_get_seats(u2
, false, NULL
));
89 r
= sd_session_is_active(session
);
91 printf("active = %s\n", yes_no(r
));
93 r
= sd_session_is_remote(session
);
95 printf("remote = %s\n", yes_no(r
));
97 r
= sd_session_get_state(session
, &state
);
99 printf("state = %s\n", state
);
102 assert_se(sd_session_get_uid(session
, &u
) >= 0);
103 printf("uid = "UID_FMT
"\n", u
);
106 assert_se(sd_session_get_type(session
, &type
) >= 0);
107 printf("type = %s\n", type
);
110 assert_se(sd_session_get_class(session
, &class) >= 0);
111 printf("class = %s\n", class);
115 r
= sd_session_get_display(session
, &display
);
116 assert_se(r
>= 0 || r
== -ENODATA
);
117 printf("display = %s\n", strna(display
));
121 r
= sd_session_get_remote_user(session
, &remote_user
);
122 assert_se(r
>= 0 || r
== -ENODATA
);
123 printf("remote_user = %s\n", strna(remote_user
));
127 r
= sd_session_get_remote_host(session
, &remote_host
);
128 assert_se(r
>= 0 || r
== -ENODATA
);
129 printf("remote_host = %s\n", strna(remote_host
));
132 assert_se(sd_session_get_seat(session
, &seat
) >= 0);
133 printf("seat = %s\n", seat
);
135 r
= sd_seat_can_multi_session(seat
);
137 printf("can do multi session = %s\n", yes_no(r
));
139 r
= sd_seat_can_tty(seat
);
141 printf("can do tty = %s\n", yes_no(r
));
143 r
= sd_seat_can_graphical(seat
);
145 printf("can do graphical = %s\n", yes_no(r
));
147 assert_se(sd_uid_get_state(u
, &state
) >= 0);
148 printf("state = %s\n", state
);
150 assert_se(sd_uid_is_on_seat(u
, 0, seat
) > 0);
152 k
= sd_uid_is_on_seat(u
, 1, seat
);
154 assert_se(!!r
== !!r
);
156 assert_se(sd_seat_get_active(seat
, &session2
, &u2
) >= 0);
157 printf("session2 = %s\n", session2
);
158 printf("uid2 = "UID_FMT
"\n", u2
);
160 r
= sd_seat_get_sessions(seat
, &sessions
, &uids
, &n
);
162 printf("n_sessions = %i\n", r
);
163 assert_se(r
== (int) strv_length(sessions
));
164 assert_se(t
= strv_join(sessions
, ", "));
166 printf("sessions = %s\n", t
);
169 for (k
= 0; k
< (int) n
; k
++)
170 printf(" "UID_FMT
, uids
[k
]);
174 assert_se(sd_seat_get_sessions(seat
, NULL
, NULL
, NULL
) == r
);
181 r
= sd_get_seats(&seats
);
183 assert_se(r
== (int) strv_length(seats
));
184 assert_se(t
= strv_join(seats
, ", "));
186 printf("n_seats = %i\n", r
);
187 printf("seats = %s\n", t
);
190 assert_se(sd_get_seats(NULL
) == r
);
192 r
= sd_seat_get_active(NULL
, &t
, NULL
);
194 printf("active session on current seat = %s\n", t
);
197 r
= sd_get_sessions(&sessions
);
199 assert_se(r
== (int) strv_length(sessions
));
200 assert_se(t
= strv_join(sessions
, ", "));
202 printf("n_sessions = %i\n", r
);
203 printf("sessions = %s\n", t
);
206 assert_se(sd_get_sessions(NULL
) == r
);
208 r
= sd_get_uids(&uids
);
212 for (k
= 0; k
< r
; k
++)
213 printf(" "UID_FMT
, uids
[k
]);
217 printf("n_uids = %i\n", r
);
218 assert_se(sd_get_uids(NULL
) == r
);
220 r
= sd_get_machine_names(&machines
);
222 assert_se(r
== (int) strv_length(machines
));
223 assert_se(t
= strv_join(machines
, ", "));
225 printf("n_machines = %i\n", r
);
226 printf("machines = %s\n", t
);
229 r
= sd_login_monitor_new("session", &m
);
232 for (n
= 0; n
< 5; n
++) {
236 assert_se((pollfd
.fd
= sd_login_monitor_get_fd(m
)) >= 0);
237 assert_se((pollfd
.events
= sd_login_monitor_get_events(m
)) >= 0);
239 assert_se(sd_login_monitor_get_timeout(m
, &timeout
) >= 0);
241 nw
= now(CLOCK_MONOTONIC
);
244 timeout
== (uint64_t) -1 ? -1 :
245 timeout
> nw
? (int) ((timeout
- nw
) / 1000) :
250 sd_login_monitor_flush(m
);
254 sd_login_monitor_unref(m
);
257 int main(int argc
, char* argv
[]) {
258 log_parse_environment();