]>
Commit | Line | Data |
---|---|---|
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 | ||
74b91131 LP |
25 | #include "sd-login.h" |
26 | #include "util.h" | |
034a2a52 | 27 | #include "strv.h" |
74b91131 LP |
28 | |
29 | int main(int argc, char* argv[]) { | |
034a2a52 LP |
30 | int r, k; |
31 | uid_t u, u2; | |
74b91131 LP |
32 | char *seat; |
33 | char *session; | |
34 | char *state; | |
35 | char *session2; | |
034a2a52 LP |
36 | char *t; |
37 | char **seats, **sessions; | |
38 | uid_t *uids; | |
39 | unsigned n; | |
40 | struct pollfd pollfd; | |
41 | sd_login_monitor *m; | |
74b91131 LP |
42 | |
43 | assert_se(sd_pid_get_session(0, &session) == 0); | |
44 | printf("session = %s\n", session); | |
45 | ||
034a2a52 LP |
46 | assert_se(sd_pid_get_owner_uid(0, &u2) == 0); |
47 | printf("user = %lu\n", (unsigned long) u2); | |
48 | ||
49 | r = sd_uid_get_sessions(u2, false, &sessions); | |
50 | assert_se(r >= 0); | |
d60ef526 | 51 | assert_se(r == (int) strv_length(sessions)); |
034a2a52 LP |
52 | assert_se(t = strv_join(sessions, ", ")); |
53 | strv_free(sessions); | |
54 | printf("sessions = %s\n", t); | |
55 | free(t); | |
56 | ||
d60ef526 LP |
57 | assert_se(r == sd_uid_get_sessions(u2, false, NULL)); |
58 | ||
034a2a52 LP |
59 | r = sd_uid_get_seats(u2, false, &seats); |
60 | assert_se(r >= 0); | |
d60ef526 | 61 | assert_se(r == (int) strv_length(seats)); |
034a2a52 LP |
62 | assert_se(t = strv_join(seats, ", ")); |
63 | strv_free(seats); | |
64 | printf("seats = %s\n", t); | |
65 | free(t); | |
66 | ||
d60ef526 LP |
67 | assert_se(r == sd_uid_get_seats(u2, false, NULL)); |
68 | ||
74b91131 LP |
69 | r = sd_session_is_active(session); |
70 | assert_se(r >= 0); | |
71 | printf("active = %s\n", yes_no(r)); | |
72 | ||
73 | assert_se(sd_session_get_uid(session, &u) >= 0); | |
74 | printf("uid = %lu\n", (unsigned long) u); | |
034a2a52 | 75 | assert_se(u == u2); |
74b91131 LP |
76 | |
77 | assert_se(sd_session_get_seat(session, &seat) >= 0); | |
78 | printf("seat = %s\n", seat); | |
79 | ||
add30678 LP |
80 | r = sd_seat_can_multi_session(seat); |
81 | assert_se(r >= 0); | |
82 | printf("can do multi session = %s\n", yes_no(r)); | |
83 | ||
74b91131 LP |
84 | assert_se(sd_uid_get_state(u, &state) >= 0); |
85 | printf("state = %s\n", state); | |
86 | ||
034a2a52 | 87 | assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); |
74b91131 | 88 | |
034a2a52 | 89 | k = sd_uid_is_on_seat(u, 1, seat); |
74b91131 LP |
90 | assert_se(k >= 0); |
91 | assert_se(!!r == !!r); | |
92 | ||
93 | assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); | |
94 | printf("session2 = %s\n", session2); | |
95 | printf("uid2 = %lu\n", (unsigned long) u2); | |
96 | ||
d60ef526 LP |
97 | r = sd_seat_get_sessions(seat, &sessions, &uids, &n); |
98 | assert_se(r >= 0); | |
99 | printf("n_sessions = %i\n", r); | |
100 | assert_se(r == (int) strv_length(sessions)); | |
034a2a52 LP |
101 | assert_se(t = strv_join(sessions, ", ")); |
102 | strv_free(sessions); | |
103 | printf("sessions = %s\n", t); | |
104 | free(t); | |
105 | printf("uids ="); | |
106 | for (k = 0; k < (int) n; k++) | |
107 | printf(" %lu", (unsigned long) uids[k]); | |
108 | printf("\n"); | |
109 | free(uids); | |
110 | ||
d60ef526 LP |
111 | assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); |
112 | ||
74b91131 LP |
113 | free(session); |
114 | free(state); | |
115 | free(session2); | |
116 | free(seat); | |
117 | ||
d60ef526 LP |
118 | r = sd_get_seats(&seats); |
119 | assert_se(r >= 0); | |
120 | assert_se(r == (int) strv_length(seats)); | |
034a2a52 LP |
121 | assert_se(t = strv_join(seats, ", ")); |
122 | strv_free(seats); | |
d60ef526 | 123 | printf("n_seats = %i\n", r); |
034a2a52 LP |
124 | printf("seats = %s\n", t); |
125 | free(t); | |
126 | ||
d60ef526 LP |
127 | assert_se(sd_get_seats(NULL) == r); |
128 | ||
129 | r = sd_get_sessions(&sessions); | |
130 | assert_se(r >= 0); | |
131 | assert_se(r == (int) strv_length(sessions)); | |
034a2a52 LP |
132 | assert_se(t = strv_join(sessions, ", ")); |
133 | strv_free(sessions); | |
d60ef526 | 134 | printf("n_sessions = %i\n", r); |
034a2a52 LP |
135 | printf("sessions = %s\n", t); |
136 | free(t); | |
137 | ||
d60ef526 LP |
138 | assert_se(sd_get_sessions(NULL) == r); |
139 | ||
034a2a52 LP |
140 | r = sd_get_uids(&uids); |
141 | assert_se(r >= 0); | |
142 | ||
143 | printf("uids ="); | |
144 | for (k = 0; k < r; k++) | |
145 | printf(" %lu", (unsigned long) uids[k]); | |
146 | printf("\n"); | |
034a2a52 LP |
147 | free(uids); |
148 | ||
d60ef526 LP |
149 | printf("n_uids = %i\n", r); |
150 | assert_se(sd_get_uids(NULL) == r); | |
151 | ||
b9b2b042 LP |
152 | r = sd_login_monitor_new("session", &m); |
153 | assert_se(r >= 0); | |
034a2a52 | 154 | |
b9b2b042 LP |
155 | zero(pollfd); |
156 | pollfd.fd = sd_login_monitor_get_fd(m); | |
157 | pollfd.events = POLLIN; | |
034a2a52 | 158 | |
b9b2b042 LP |
159 | for (n = 0; n < 5; n++) { |
160 | r = poll(&pollfd, 1, -1); | |
161 | assert_se(r >= 0); | |
034a2a52 | 162 | |
b9b2b042 LP |
163 | sd_login_monitor_flush(m); |
164 | printf("Wake!\n"); | |
165 | } | |
034a2a52 | 166 | |
b9b2b042 | 167 | sd_login_monitor_unref(m); |
034a2a52 | 168 | |
74b91131 LP |
169 | return 0; |
170 | } |