summaryrefslogtreecommitdiff
path: root/src/nspawn
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-02-26 15:29:30 +0100
committerLennart Poettering <lennart@poettering.net>2018-02-28 10:00:50 +0100
commitc5b82d86b5fe1587a6798266baf1a2f8457e6f23 (patch)
treeb07397902e77bd04cc7098c9881b76d2d0a5b754 /src/nspawn
parentbdad9e44e4cf6436985623aaab2cdfb1c5b7d082 (diff)
downloadsystemd-c5b82d86b5fe1587a6798266baf1a2f8457e6f23.tar.gz
nspawn: port some code to use read_line()
This shortens our code a bit. Which is always nice.
Diffstat (limited to 'src/nspawn')
-rw-r--r--src/nspawn/nspawn-setuid.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/src/nspawn/nspawn-setuid.c b/src/nspawn/nspawn-setuid.c
index b08bcd988a..029c4e7954 100644
--- a/src/nspawn/nspawn-setuid.c
+++ b/src/nspawn/nspawn-setuid.c
@@ -23,8 +23,10 @@
#include <unistd.h>
#include "alloc-util.h"
+#include "def.h"
#include "errno.h"
#include "fd-util.h"
+#include "fileio.h"
#include "mkdir.h"
#include "nspawn-setuid.h"
#include "process-util.h"
@@ -87,10 +89,10 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
}
int change_uid_gid(const char *user, char **_home) {
- char line[LINE_MAX], *x, *u, *g, *h;
+ char *x, *u, *g, *h;
const char *word, *state;
_cleanup_free_ uid_t *uids = NULL;
- _cleanup_free_ char *home = NULL;
+ _cleanup_free_ char *home = NULL, *line = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_close_ int fd = -1;
unsigned n_uids = 0;
@@ -118,21 +120,18 @@ int change_uid_gid(const char *user, char **_home) {
if (fd < 0)
return fd;
- f = fdopen(fd, "r");
+ f = fdopen(fd, "re");
if (!f)
return log_oom();
fd = -1;
- if (!fgets(line, sizeof(line), f)) {
- if (!ferror(f)) {
- log_error("Failed to resolve user %s.", user);
- return -ESRCH;
- }
-
- return log_error_errno(errno, "Failed to read from getent: %m");
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r == 0) {
+ log_error("Failed to resolve user %s.", user);
+ return -ESRCH;
}
-
- truncate_nl(line);
+ if (r < 0)
+ return log_error_errno(r, "Failed to read from getent: %m");
(void) wait_for_terminate_and_check("getent passwd", pid, WAIT_LOG);
@@ -195,27 +194,26 @@ int change_uid_gid(const char *user, char **_home) {
if (!home)
return log_oom();
+ f = safe_fclose(f);
+ line = mfree(line);
+
/* Second, get group memberships */
fd = spawn_getent("initgroups", user, &pid);
if (fd < 0)
return fd;
- fclose(f);
- f = fdopen(fd, "r");
+ f = fdopen(fd, "re");
if (!f)
return log_oom();
fd = -1;
- if (!fgets(line, sizeof(line), f)) {
- if (!ferror(f)) {
- log_error("Failed to resolve user %s.", user);
- return -ESRCH;
- }
-
- return log_error_errno(errno, "Failed to read from getent: %m");
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r == 0) {
+ log_error("Failed to resolve user %s.", user);
+ return -ESRCH;
}
-
- truncate_nl(line);
+ if (r < 0)
+ return log_error_errno(r, "Failed to read from getent: %m");
(void) wait_for_terminate_and_check("getent initgroups", pid, WAIT_LOG);