tree-wide: port various places over to use chmod_and_chown()

Doing this properly is hard, hence let's unify the code.
This commit is contained in:
Lennart Poettering
2019-05-24 15:07:55 +02:00
parent f8a8579e84
commit 4b3b5bc71b
6 changed files with 32 additions and 58 deletions
+11 -12
View File
@@ -728,25 +728,24 @@ static int setup_output(
}
static int chown_terminal(int fd, uid_t uid) {
struct stat st;
int r;
assert(fd >= 0);
/* Before we chown/chmod the TTY, let's ensure this is actually a tty */
if (isatty(fd) < 1)
return 0;
if (isatty(fd) < 1) {
if (IN_SET(errno, EINVAL, ENOTTY))
return 0; /* not a tty */
return -errno;
}
/* This might fail. What matters are the results. */
(void) fchown(fd, uid, -1);
(void) fchmod(fd, TTY_MODE);
r = fchmod_and_chown(fd, TTY_MODE, uid, -1);
if (r < 0)
return r;
if (fstat(fd, &st) < 0)
return -errno;
if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
return -EPERM;
return 0;
return 1;
}
static int setup_confirm_stdio(const char *vc, int *_saved_stdin, int *_saved_stdout) {