chiark / gitweb /
2aaa31f22f1c3ab064018104465496fc3162dfa6
[elogind.git] / src / login / test-login.c
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
22 #include <sys/poll.h>
23 #include <string.h>
24
25 #include <systemd/sd-login.h>
26
27 #include "util.h"
28 #include "strv.h"
29
30 int main(int argc, char* argv[]) {
31         int r, k;
32         uid_t u, u2;
33         char *seat, *type, *class;
34         char *session;
35         char *state;
36         char *session2;
37         char *t;
38         char **seats, **sessions;
39         uid_t *uids;
40         unsigned n;
41         struct pollfd pollfd;
42         sd_login_monitor *m;
43
44         assert_se(sd_pid_get_session(0, &session) == 0);
45         printf("session = %s\n", session);
46
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);
52         assert_se(r == (int) strv_length(sessions));
53         assert_se(t = strv_join(sessions, ", "));
54         strv_free(sessions);
55         printf("sessions = %s\n", t);
56         free(t);
57
58         assert_se(r == sd_uid_get_sessions(u2, false, NULL));
59
60         r = sd_uid_get_seats(u2, false, &seats);
61         assert_se(r >= 0);
62         assert_se(r == (int) strv_length(seats));
63         assert_se(t = strv_join(seats, ", "));
64         strv_free(seats);
65         printf("seats = %s\n", t);
66         free(t);
67
68         assert_se(r == sd_uid_get_seats(u2, false, NULL));
69
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);
76         assert_se(u == u2);
77
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
86         assert_se(sd_session_get_seat(session, &seat) >= 0);
87         printf("seat = %s\n", seat);
88
89         r = sd_seat_can_multi_session(seat);
90         assert_se(r >= 0);
91         printf("can do multi session = %s\n", yes_no(r));
92
93         assert_se(sd_uid_get_state(u, &state) >= 0);
94         printf("state = %s\n", state);
95
96         assert_se(sd_uid_is_on_seat(u, 0, seat) > 0);
97
98         k = sd_uid_is_on_seat(u, 1, seat);
99         assert_se(k >= 0);
100         assert_se(!!r == !!r);
101
102         assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0);
103         printf("session2 = %s\n", session2);
104         printf("uid2 = %lu\n", (unsigned long) u2);
105
106         r = sd_seat_get_sessions(seat, &sessions, &uids, &n);
107         assert_se(r >= 0);
108         printf("n_sessions = %i\n", r);
109         assert_se(r == (int) strv_length(sessions));
110         assert_se(t = strv_join(sessions, ", "));
111         strv_free(sessions);
112         printf("sessions = %s\n", t);
113         free(t);
114         printf("uids =");
115         for (k = 0; k < (int) n; k++)
116                 printf(" %lu", (unsigned long) uids[k]);
117         printf("\n");
118         free(uids);
119
120         assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r);
121
122         free(session);
123         free(state);
124         free(session2);
125         free(seat);
126
127         r = sd_get_seats(&seats);
128         assert_se(r >= 0);
129         assert_se(r == (int) strv_length(seats));
130         assert_se(t = strv_join(seats, ", "));
131         strv_free(seats);
132         printf("n_seats = %i\n", r);
133         printf("seats = %s\n", t);
134         free(t);
135
136         assert_se(sd_get_seats(NULL) == r);
137
138         r = sd_seat_get_active(NULL, &t, NULL);
139         assert_se(r >= 0);
140         printf("active session on current seat = %s\n", t);
141         free(t);
142
143         r = sd_get_sessions(&sessions);
144         assert_se(r >= 0);
145         assert_se(r == (int) strv_length(sessions));
146         assert_se(t = strv_join(sessions, ", "));
147         strv_free(sessions);
148         printf("n_sessions = %i\n", r);
149         printf("sessions = %s\n", t);
150         free(t);
151
152         assert_se(sd_get_sessions(NULL) == r);
153
154         r = sd_get_uids(&uids);
155         assert_se(r >= 0);
156
157         printf("uids =");
158         for (k = 0; k < r; k++)
159                 printf(" %lu", (unsigned long) uids[k]);
160         printf("\n");
161         free(uids);
162
163         printf("n_uids = %i\n", r);
164         assert_se(sd_get_uids(NULL) == r);
165
166         r = sd_login_monitor_new("session", &m);
167         assert_se(r >= 0);
168
169         zero(pollfd);
170         pollfd.fd = sd_login_monitor_get_fd(m);
171         pollfd.events = POLLIN;
172
173         for (n = 0; n < 5; n++) {
174                 r = poll(&pollfd, 1, -1);
175                 assert_se(r >= 0);
176
177                 sd_login_monitor_flush(m);
178                 printf("Wake!\n");
179         }
180
181         sd_login_monitor_unref(m);
182
183         return 0;
184 }