/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
#include <errno.h>
#include <poll.h>
#include "alloc-util.h"
#include "cgroup-util.h"
#include "dirent-util.h"
+#include "env-file.h"
#include "escape.h"
#include "fd-util.h"
-#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "hostname-util.h"
}
_public_ int sd_uid_get_state(uid_t uid, char**state) {
- _cleanup_free_ char *p = NULL;
- char *s = NULL;
+ _cleanup_free_ char *p = NULL, *s = NULL;
int r;
assert_return(state, -EINVAL);
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ r = parse_env_file(NULL, p, "STATE", &s);
if (r == -ENOENT) {
- free(s);
- s = strdup("offline");
- if (!s)
- return -ENOMEM;
-
- }
- else if (r < 0) {
- free(s);
+ r = free_and_strdup(&s, "offline");
+ if (r < 0)
+ return r;
+ } else if (r < 0)
return r;
- }
- if (isempty(s)) {
- free(s);
+ else if (isempty(s))
return -EIO;
- }
- *state = s;
+ *state = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+ r = parse_env_file(NULL, p, "DISPLAY", &s);
if (r == -ENOENT)
return -ENODATA;
if (r < 0)
if (isempty(s))
return -ENODATA;
- *session = s;
- s = NULL;
+ *session = TAKE_PTR(s);
return 0;
}
if (!p)
return -ENOMEM;
- *_p = p;
- p = NULL;
+ *_p = TAKE_PTR(p);
return 0;
}
variable = require_active ? "ACTIVE_UID" : "UIDS";
- r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ r = parse_env_file(NULL, p, variable, &s);
if (r == -ENOENT)
return 0;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, variable, &s, NULL);
+ r = parse_env_file(NULL, p, variable, &s);
if (r == -ENOENT || (r >= 0 && isempty(s))) {
if (array)
*array = NULL;
return -ENOMEM;
strv_uniq(a);
- r = strv_length(a);
+ r = (int) strv_length(a);
if (array)
*array = a;
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
+ r = parse_env_file(NULL, p, "ACTIVE", &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
+ r = parse_env_file(NULL, p, "REMOTE", &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+ r = parse_env_file(NULL, p, "STATE", &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (isempty(s))
return -EIO;
- *state = s;
- s = NULL;
+ *state = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+ r = parse_env_file(NULL, p, "UID", &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE, field, &s, NULL);
+ r = parse_env_file(NULL, p, field, &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
if (isempty(s))
return -ENODATA;
- *value = s;
- s = NULL;
+ *value = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
+ r = parse_env_file(NULL, p,
"ACTIVE", &s,
- "ACTIVE_UID", &t,
- NULL);
+ "ACTIVE_UID", &t);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
return r;
}
- if (session && s) {
- *session = s;
- s = NULL;
- }
+ if (session && s)
+ *session = TAKE_PTR(s);
return 0;
}
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
+ r = parse_env_file(NULL, p,
"SESSIONS", &s,
- "UIDS", &t,
- NULL);
+ "UIDS", &t);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
}
}
- r = strv_length(a);
+ r = (int) strv_length(a);
- if (sessions) {
- *sessions = a;
- a = NULL;
- }
+ if (sessions)
+ *sessions = TAKE_PTR(a);
- if (uids) {
- *uids = b;
- b = NULL;
- }
+ if (uids)
+ *uids = TAKE_PTR(b);
if (n_uids)
*n_uids = n;
if (r < 0)
return r;
- r = parse_env_file(p, NEWLINE,
- variable, &s,
- NULL);
+ r = parse_env_file(NULL, p,
+ variable, &s);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)
r++;
}
- if (users) {
- *users = l;
- l = NULL;
- }
+ if (users)
+ *users = TAKE_PTR(l);
return r;
}
*b = NULL;
}
- if (machines) {
- *machines = l;
- l = NULL;
- }
+ if (machines)
+ *machines = TAKE_PTR(l);
+
return r;
}
const char *p;
int r;
- assert_return(machine_name_is_valid(machine), -EINVAL);
assert_return(class, -EINVAL);
- 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)
- return -EIO;
+ if (streq(machine, ".host")) {
+ c = strdup("host");
+ if (!c)
+ return -ENOMEM;
+ } else {
+ if (!machine_name_is_valid(machine))
+ return -EINVAL;
- *class = c;
- c = NULL;
+ p = strjoina("/run/systemd/machines/", machine);
+ r = parse_env_file(NULL, p, "CLASS", &c);
+ if (r == -ENOENT)
+ return -ENXIO;
+ if (r < 0)
+ return r;
+ if (!c)
+ return -EIO;
+ }
+ *class = TAKE_PTR(c);
return 0;
}
assert_return(ifindices, -EINVAL);
p = strjoina("/run/systemd/machines/", machine);
- r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
+ r = parse_env_file(NULL, p, "NETIF", &netif);
if (r == -ENOENT)
return -ENXIO;
if (r < 0)