]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/test-login.c
udev: check-keymaps.sh - fix source directory
[thirdparty/systemd.git] / src / login / test-login.c
CommitLineData
74b91131
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
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 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
034a2a52
LP
22#include <sys/poll.h>
23#include <string.h>
24
81527be1
LP
25#include <systemd/sd-login.h>
26
74b91131 27#include "util.h"
034a2a52 28#include "strv.h"
74b91131
LP
29
30int main(int argc, char* argv[]) {
034a2a52
LP
31 int r, k;
32 uid_t u, u2;
fc8af9ff 33 char *seat, *type, *class, *display;
74b91131
LP
34 char *session;
35 char *state;
36 char *session2;
034a2a52
LP
37 char *t;
38 char **seats, **sessions;
39 uid_t *uids;
40 unsigned n;
41 struct pollfd pollfd;
42 sd_login_monitor *m;
74b91131
LP
43
44 assert_se(sd_pid_get_session(0, &session) == 0);
45 printf("session = %s\n", session);
46
034a2a52
LP
47 assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
48 printf("user = %lu\n", (unsigned long) u2);
49
50 r = sd_uid_get_sessions(u2, false, &sessions);
51 assert_se(r >= 0);
d60ef526 52 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
53 assert_se(t = strv_join(sessions, ", "));
54 strv_free(sessions);
55 printf("sessions = %s\n", t);
56 free(t);
57
d60ef526
LP
58 assert_se(r == sd_uid_get_sessions(u2, false, NULL));
59
034a2a52
LP
60 r = sd_uid_get_seats(u2, false, &seats);
61 assert_se(r >= 0);
d60ef526 62 assert_se(r == (int) strv_length(seats));
034a2a52
LP
63 assert_se(t = strv_join(seats, ", "));
64 strv_free(seats);
65 printf("seats = %s\n", t);
66 free(t);
67
d60ef526
LP
68 assert_se(r == sd_uid_get_seats(u2, false, NULL));
69
74b91131
LP
70 r = sd_session_is_active(session);
71 assert_se(r >= 0);
72 printf("active = %s\n", yes_no(r));
73
74 assert_se(sd_session_get_uid(session, &u) >= 0);
75 printf("uid = %lu\n", (unsigned long) u);
034a2a52 76 assert_se(u == u2);
74b91131 77
51f58f08
LP
78 assert_se(sd_session_get_type(session, &type) >= 0);
79 printf("type = %s\n", type);
80 free(type);
81
82 assert_se(sd_session_get_class(session, &class) >= 0);
83 printf("class = %s\n", class);
84 free(class);
85
fc8af9ff
LP
86 assert_se(sd_session_get_display(session, &display) >= 0);
87 printf("display = %s\n", display);
88 free(display);
89
74b91131
LP
90 assert_se(sd_session_get_seat(session, &seat) >= 0);
91 printf("seat = %s\n", seat);
92
add30678
LP
93 r = sd_seat_can_multi_session(seat);
94 assert_se(r >= 0);
95 printf("can do multi session = %s\n", yes_no(r));
96
74b91131
LP
97 assert_se(sd_uid_get_state(u, &state) >= 0);
98 printf("state = %s\n", state);
99
034a2a52 100 assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
74b91131 101
034a2a52 102 k = sd_uid_is_on_seat(u, 1, seat);
74b91131
LP
103 assert_se(k >= 0);
104 assert_se(!!r == !!r);
105
106 assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
107 printf("session2 = %s\n", session2);
108 printf("uid2 = %lu\n", (unsigned long) u2);
109
d60ef526
LP
110 r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
111 assert_se(r >= 0);
112 printf("n_sessions = %i\n", r);
113 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
114 assert_se(t = strv_join(sessions, ", "));
115 strv_free(sessions);
116 printf("sessions = %s\n", t);
117 free(t);
118 printf("uids =");
119 for (k = 0; k < (int) n; k++)
120 printf(" %lu", (unsigned long) uids[k]);
121 printf("\n");
122 free(uids);
123
d60ef526
LP
124 assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
125
74b91131
LP
126 free(session);
127 free(state);
128 free(session2);
129 free(seat);
130
d60ef526
LP
131 r = sd_get_seats(&seats);
132 assert_se(r >= 0);
133 assert_se(r == (int) strv_length(seats));
034a2a52
LP
134 assert_se(t = strv_join(seats, ", "));
135 strv_free(seats);
d60ef526 136 printf("n_seats = %i\n", r);
034a2a52
LP
137 printf("seats = %s\n", t);
138 free(t);
139
51f58f08
LP
140 assert_se(sd_get_seats(NULL) == r);
141
50b1678a
LP
142 r = sd_seat_get_active(NULL, &t, NULL);
143 assert_se(r >= 0);
144 printf("active session on current seat = %s\n", t);
145 free(t);
146
d60ef526
LP
147 r = sd_get_sessions(&sessions);
148 assert_se(r >= 0);
149 assert_se(r == (int) strv_length(sessions));
034a2a52
LP
150 assert_se(t = strv_join(sessions, ", "));
151 strv_free(sessions);
d60ef526 152 printf("n_sessions = %i\n", r);
034a2a52
LP
153 printf("sessions = %s\n", t);
154 free(t);
155
d60ef526
LP
156 assert_se(sd_get_sessions(NULL) == r);
157
034a2a52
LP
158 r = sd_get_uids(&uids);
159 assert_se(r >= 0);
160
161 printf("uids =");
162 for (k = 0; k < r; k++)
163 printf(" %lu", (unsigned long) uids[k]);
164 printf("\n");
034a2a52
LP
165 free(uids);
166
d60ef526
LP
167 printf("n_uids = %i\n", r);
168 assert_se(sd_get_uids(NULL) == r);
169
b9b2b042
LP
170 r = sd_login_monitor_new("session", &m);
171 assert_se(r >= 0);
034a2a52 172
b9b2b042
LP
173 zero(pollfd);
174 pollfd.fd = sd_login_monitor_get_fd(m);
175 pollfd.events = POLLIN;
034a2a52 176
b9b2b042
LP
177 for (n = 0; n < 5; n++) {
178 r = poll(&pollfd, 1, -1);
179 assert_se(r >= 0);
034a2a52 180
b9b2b042
LP
181 sd_login_monitor_flush(m);
182 printf("Wake!\n");
183 }
034a2a52 184
b9b2b042 185 sd_login_monitor_unref(m);
034a2a52 186
74b91131
LP
187 return 0;
188}