X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibelogind%2Fsd-login%2Fsd-login.c;h=ac22fa3454ce57a5fc757c5726c3309752d67acd;hb=4084669c7f802c9745bcb6bed77bdaf49ffd896d;hp=2041d87ed789bc94644d1e71057523fa84f11f97;hpb=f71a7dceade2ff13131acc352173ad841fe26363;p=elogind.git
diff --git a/src/libelogind/sd-login/sd-login.c b/src/libelogind/sd-login/sd-login.c
index 2041d87ed..ac22fa345 100644
--- a/src/libelogind/sd-login/sd-login.c
+++ b/src/libelogind/sd-login/sd-login.c
@@ -19,21 +19,33 @@
along with systemd; If not, see .
***/
-#include
-#include
#include
-#include
#include
+#include
+#include
+#include
-#include "util.h"
+#include "sd-login.h"
+
+#include "alloc-util.h"
#include "cgroup-util.h"
-#include "macro.h"
-#include "strv.h"
+#include "dirent-util.h"
+#include "escape.h"
+#include "fd-util.h"
#include "fileio.h"
-#include "login-util.h"
#include "formats-util.h"
+#include "fs-util.h"
#include "hostname-util.h"
-#include "sd-login.h"
+#include "io-util.h"
+#include "login-util.h"
+#include "macro.h"
+#include "parse-util.h"
+#include "path-util.h"
+#include "socket-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "user-util.h"
+#include "util.h"
/* Error codes:
*
@@ -58,8 +70,7 @@ _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_unit(pid, unit);
#else
return -ESRCH;
@@ -71,8 +82,7 @@ _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
assert_return(pid >= 0, -EINVAL);
assert_return(unit, -EINVAL);
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_user_unit(pid, unit);
#else
return -ESRCH;
@@ -84,8 +94,7 @@ _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
assert_return(pid >= 0, -EINVAL);
assert_return(name, -EINVAL);
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_machine_name(pid, name);
#else
return -ESRCH;
@@ -97,8 +106,7 @@ _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
assert_return(pid >= 0, -EINVAL);
assert_return(slice, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
+#if 0 /// elogind does not support systemd slices
return cg_pid_get_slice(pid, slice);
#else
return -ESRCH;
@@ -110,8 +118,7 @@ _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
assert_return(pid >= 0, -EINVAL);
assert_return(slice, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
+#if 0 /// elogind does not support systemd slices
return cg_pid_get_user_slice(pid, slice);
#else
return -ESRCH;
@@ -123,8 +130,7 @@ _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
assert_return(pid >= 0, -EINVAL);
assert_return(uid, -EINVAL);
-/// elogind does not support systemd slices
-#if 0
+#if 0 /// elogind does not support systemd slices
return cg_pid_get_owner_uid(pid, uid);
#else
return -ESRCH;
@@ -168,8 +174,7 @@ _public_ int sd_peer_get_session(int fd, char **session) {
if (r < 0)
return r;
-/// elogind does not support systemd scopes
-#if 0
+#if 0 /// elogind does not support systemd scopes
return cg_pid_get_session(ucred.pid, session);
#else
return -ESRCH;
@@ -187,8 +192,7 @@ _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_owner_uid(ucred.pid, uid);
#else
return -ESRCH;
@@ -206,8 +210,7 @@ _public_ int sd_peer_get_unit(int fd, char **unit) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_unit(ucred.pid, unit);
#else
return -ESRCH;
@@ -225,8 +228,7 @@ _public_ int sd_peer_get_user_unit(int fd, char **unit) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_user_unit(ucred.pid, unit);
#else
return -ESRCH;
@@ -244,8 +246,7 @@ _public_ int sd_peer_get_machine_name(int fd, char **machine) {
if (r < 0)
return r;
-/// elogind does not support systemd units
-#if 0
+#if 0 /// elogind does not support systemd units
return cg_pid_get_machine_name(ucred.pid, machine);
#else
return -ESRCH;
@@ -263,8 +264,7 @@ _public_ int sd_peer_get_slice(int fd, char **slice) {
if (r < 0)
return r;
-/// elogind does not support systemd slices
-#if 0
+#if 0 /// elogind does not support systemd slices
return cg_pid_get_slice(ucred.pid, slice);
#else
return -ESRCH;
@@ -282,8 +282,7 @@ _public_ int sd_peer_get_user_slice(int fd, char **slice) {
if (r < 0)
return r;
-/// elogind does not support systemd slices
-#if 0
+#if 0 /// elogind does not support systemd slices
return cg_pid_get_user_slice(ucred.pid, slice);
#else
return -ESRCH;
@@ -305,6 +304,8 @@ _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
}
static int file_of_uid(uid_t uid, char **p) {
+
+ assert_return(uid_is_valid(uid), -EINVAL);
assert(p);
if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
@@ -331,11 +332,15 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
if (!s)
return -ENOMEM;
- } else if (r < 0) {
+ }
+ if (r < 0) {
free(s);
return r;
- } else if (!s)
+ }
+ if (isempty(s)) {
+ free(s);
return -EIO;
+ }
*state = s;
return 0;
@@ -353,12 +358,11 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
if (r == -ENOENT)
- return -ENXIO;
+ return -ENODATA;
if (r < 0)
return r;
-
if (isempty(s))
- return -ENXIO;
+ return -ENODATA;
*session = s;
s = NULL;
@@ -366,35 +370,63 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
return 0;
}
-_public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
- _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
- size_t l;
+static int file_of_seat(const char *seat, char **_p) {
+ char *p;
int r;
- const char *word, *variable, *state;
- assert_return(seat, -EINVAL);
+ assert(_p);
- variable = require_active ? "ACTIVE_UID" : "UIDS";
+ if (seat) {
+ if (!filename_is_valid(seat))
+ return -EINVAL;
p = strappend("/run/systemd/seats/", seat);
+ } else {
+ _cleanup_free_ char *buf = NULL;
+
+ r = sd_session_get_seat(NULL, &buf);
+ if (r < 0)
+ return r;
+
+ p = strappend("/run/systemd/seats/", buf);
+ }
+
if (!p)
return -ENOMEM;
- r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ *_p = p;
+ p = NULL;
+ return 0;
+}
+
+_public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
+ _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
+ size_t l;
+ int r;
+ const char *word, *variable, *state;
+
+ assert_return(uid_is_valid(uid), -EINVAL);
+ r = file_of_seat(seat, &p);
if (r < 0)
return r;
- if (!s)
- return -EIO;
+ variable = require_active ? "ACTIVE_UID" : "UIDS";
+
+ r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ if (r == -ENOENT)
+ return 0;
+ if (r < 0)
+ return r;
+ if (isempty(s))
+ return 0;
if (asprintf(&t, UID_FMT, uid) < 0)
return -ENOMEM;
- FOREACH_WORD(word, l, s, state) {
+ FOREACH_WORD(word, l, s, state)
if (strneq(t, word, l))
return 1;
- }
return 0;
}
@@ -404,31 +436,22 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
char **a;
int r;
+ assert(variable);
+
r = file_of_uid(uid, &p);
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
- variable, &s,
- NULL);
- if (r < 0) {
- if (r == -ENOENT) {
+ r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ if (r == -ENOENT || (r >= 0 && isempty(s))) {
if (array)
*array = NULL;
return 0;
}
-
+ if (r < 0)
return r;
- }
-
- if (!s) {
- if (array)
- *array = NULL;
- return 0;
- }
a = strv_split(s, " ");
-
if (!a)
return -ENOMEM;
@@ -490,37 +513,39 @@ static int file_of_session(const char *session, char **_p) {
}
_public_ int sd_session_is_active(const char *session) {
- int r;
_cleanup_free_ char *p = NULL, *s = NULL;
+ int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
+ if (isempty(s))
return -EIO;
return parse_boolean(s);
}
_public_ int sd_session_is_remote(const char *session) {
- int r;
_cleanup_free_ char *p = NULL, *s = NULL;
+ int r;
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
- return -EIO;
+ if (isempty(s))
+ return -ENODATA;
return parse_boolean(s);
}
@@ -536,9 +561,11 @@ _public_ int sd_session_get_state(const char *session, char **state) {
return r;
r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
- else if (!s)
+ if (isempty(s))
return -EIO;
*state = s;
@@ -558,10 +585,11 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
return r;
r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
- if (!s)
+ if (isempty(s))
return -EIO;
return parse_uid(s, uid);
@@ -572,17 +600,19 @@ static int session_get_string(const char *session, const char *field, char **val
int r;
assert_return(value, -EINVAL);
+ assert(field);
r = file_of_session(session, &p);
if (r < 0)
return r;
r = parse_env_file(p, NEWLINE, field, &s, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
-
if (isempty(s))
- return -ENXIO;
+ return -ENODATA;
*value = s;
s = NULL;
@@ -602,6 +632,8 @@ _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
unsigned u;
int r;
+ assert_return(vtnr, -EINVAL);
+
r = session_get_string(session, "VTNR", &vtnr_string);
if (r < 0)
return r;
@@ -657,32 +689,6 @@ _public_ int sd_session_get_remote_host(const char *session, char **remote_host)
return session_get_string(session, "REMOTE_HOST", remote_host);
}
-static int file_of_seat(const char *seat, char **_p) {
- char *p;
- int r;
-
- assert(_p);
-
- if (seat)
- p = strappend("/run/systemd/seats/", seat);
- else {
- _cleanup_free_ char *buf = NULL;
-
- r = sd_session_get_seat(NULL, &buf);
- if (r < 0)
- return r;
-
- p = strappend("/run/systemd/seats/", buf);
- }
-
- if (!p)
- return -ENOMEM;
-
- *_p = p;
- p = NULL;
- return 0;
-}
-
_public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
_cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
int r;
@@ -697,14 +703,16 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
"ACTIVE", &s,
"ACTIVE_UID", &t,
NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (session && !s)
- return -ENOENT;
+ return -ENODATA;
if (uid && !t)
- return -ENOENT;
+ return -ENODATA;
if (uid && t) {
r = parse_uid(t, uid);
@@ -735,7 +743,8 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
"SESSIONS", &s,
"ACTIVE_SESSIONS", &t,
NULL);
-
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
@@ -767,7 +776,6 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
return -ENOMEM;
r = parse_uid(k, b + i);
-
if (r < 0)
continue;
@@ -798,7 +806,7 @@ static int seat_get_can(const char *seat, const char *variable) {
_cleanup_free_ char *p = NULL, *s = NULL;
int r;
- assert_return(variable, -EINVAL);
+ assert(variable);
r = file_of_seat(seat, &p);
if (r < 0)
@@ -807,10 +815,12 @@ static int seat_get_can(const char *seat, const char *variable) {
r = parse_env_file(p, NEWLINE,
variable, &s,
NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
- if (!s)
- return 0;
+ if (isempty(s))
+ return -ENODATA;
return parse_boolean(s);
}
@@ -934,6 +944,8 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (!c)
@@ -957,6 +969,8 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
+ if (r == -ENOENT)
+ return -ENXIO;
if (r < 0)
return r;
if (!netif) {
@@ -970,9 +984,7 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
*(char*) (mempcpy(buf, word, l)) = 0;
- if (safe_atoi(buf, &ifi) < 0)
- continue;
- if (ifi <= 0)
+ if (parse_ifindex(buf, &ifi) < 0)
continue;
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {