<funcprototype>
<funcdef>int <function>sd_seat_get_sessions</function></funcdef>
<paramdef>const char *<parameter>seat</parameter></paramdef>
- <paramdef>char ***<parameter>sessions</parameter></paramdef>
- <paramdef>uid_t **<parameter>uid</parameter></paramdef>
- <paramdef>unsigned int *<parameter>n_uids</parameter></paramdef>
+ <paramdef>char ***<parameter>ret_sessions</parameter></paramdef>
+ <paramdef>uid_t **<parameter>ret_uids</parameter></paramdef>
+ <paramdef>unsigned int *<parameter>ret_n_uids</parameter></paramdef>
</funcprototype>
<funcprototype>
<citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para>
- <para><function>sd_seat_get_sessions()</function> may be used to
- determine all sessions on the specified seat. Returns two arrays,
- one (<constant>NULL</constant> terminated) with the session
- identifiers of the sessions and one with the user identifiers of
- the Unix users the sessions belong to. An additional parameter may
- be used to return the number of entries in the latter array. This
- value is the same the return value, if the latter is nonnegative.
- The two arrays and the last parameter may be passed as
- <constant>NULL</constant> in case these values need not to be
- determined. The arrays and the strings referenced by them need to
- be freed with the libc
- <citerefentry project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
- call after use. Note that instead of an empty array
- <constant>NULL</constant> may be returned and should be considered
- equivalent to an empty array.</para>
+ <para><function>sd_seat_get_sessions()</function> may be used to determine all sessions on the specified
+ seat. Returns two arrays, one (<constant>NULL</constant> terminated) with the session identifiers of the
+ sessions and one with the user identifiers of the Unix users the sessions belong to. An additional
+ parameter may be used to return the number of entries in the latter array. This value is the same as the
+ return value if the return value is nonnegative. The output parameters may be passed as
+ <constant>NULL</constant> in case these output values are not needed. The arrays and the strings
+ referenced by them need to be freed with the libc <citerefentry
+ project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> call after
+ use. Note that instead of an empty array <constant>NULL</constant> may be returned and should be
+ considered equivalent to an empty array.</para>
<para><function>sd_seat_can_tty()</function> may be used to
determine whether a specific seat provides TTY functionality, i.e.
if (r < 0)
return r;
- a = strv_split(s, " ");
+ a = strv_split(s, NULL);
if (!a)
return -ENOMEM;
return 0;
}
-_public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **uids, unsigned *n_uids) {
- _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
- _cleanup_strv_free_ char **a = NULL;
- _cleanup_free_ uid_t *b = NULL;
- unsigned n = 0;
+_public_ int sd_seat_get_sessions(
+ const char *seat,
+ char ***ret_sessions,
+ uid_t **ret_uids,
+ unsigned *ret_n_uids) {
+
+ _cleanup_free_ char *fname = NULL, *session_line = NULL, *uid_line = NULL;
+ _cleanup_strv_free_ char **sessions = NULL;
+ _cleanup_free_ uid_t *uids = NULL;
+ unsigned n_sessions = 0;
int r;
- r = file_of_seat(seat, &p);
+ r = file_of_seat(seat, &fname);
if (r < 0)
return r;
- r = parse_env_file(NULL, p,
- "SESSIONS", &s,
- "UIDS", &t);
+ r = parse_env_file(NULL, fname,
+ "SESSIONS", &session_line,
+ "UIDS", &uid_line);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
- if (s) {
- a = strv_split(s, " ");
- if (!a)
+ if (session_line) {
+ sessions = strv_split(session_line, NULL);
+ if (!sessions)
return -ENOMEM;
- }
-
- if (uids && t) {
- const char *word, *state;
- size_t l;
-
- FOREACH_WORD(word, l, t, state)
- n++;
-
- if (n > 0) {
- unsigned i = 0;
- b = new(uid_t, n);
- if (!b)
- return -ENOMEM;
+ n_sessions = strv_length(sessions);
+ };
- FOREACH_WORD(word, l, t, state) {
- _cleanup_free_ char *k = NULL;
+ if (ret_uids && uid_line) {
+ uids = new(uid_t, n_sessions);
+ if (!uids)
+ return -ENOMEM;
- k = strndup(word, l);
- if (!k)
- return -ENOMEM;
+ size_t n = 0;
+ for (const char *p = uid_line;;) {
+ _cleanup_free_ char *word = NULL;
- r = parse_uid(k, b + i);
- if (r < 0)
- return r;
+ r = extract_first_word(&p, &word, NULL, 0);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
- i++;
- }
+ r = parse_uid(word, &uids[n++]);
+ if (r < 0)
+ return r;
}
- }
- r = (int) strv_length(a);
-
- if (sessions)
- *sessions = TAKE_PTR(a);
-
- if (uids)
- *uids = TAKE_PTR(b);
+ if (n != n_sessions)
+ return -EUCLEAN;
+ }
- if (n_uids)
- *n_uids = n;
+ if (ret_sessions)
+ *ret_sessions = TAKE_PTR(sessions);
+ if (ret_uids)
+ *ret_uids = TAKE_PTR(uids);
+ if (ret_n_uids)
+ *ret_n_uids = n_sessions;
- return r;
+ return n_sessions;
}
static int seat_get_can(const char *seat, const char *variable) {