int open_terminal(const char *name, int mode) {
_cleanup_close_ int fd = -EBADF;
- unsigned c = 0;
/*
* If a TTY is in the process of being closed opening it might cause EIO. This is horribly awful, but
* https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
*/
- if ((mode & (O_CREAT|O_PATH|O_DIRECTORY|O_TMPFILE)) != 0)
- return -EINVAL;
+ assert((mode & (O_CREAT|O_PATH|O_DIRECTORY|O_TMPFILE)) == 0);
- for (;;) {
+ for (unsigned c = 0;; c++) {
fd = open(name, mode, 0);
if (fd >= 0)
break;
/* Max 1s in total */
if (c >= 20)
- return -errno;
+ return -EIO;
(void) usleep_safe(50 * USEC_PER_MSEC);
- c++;
}
if (!isatty_safe(fd))
}
}
-int openpt_allocate(int flags, char **ret_slave) {
+int openpt_allocate(int flags, char **ret_peer_path) {
_cleanup_close_ int fd = -EBADF;
- _cleanup_free_ char *p = NULL;
int r;
fd = posix_openpt(flags|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return -errno;
- if (ret_slave) {
+ _cleanup_free_ char *p = NULL;
+ if (ret_peer_path) {
r = ptsname_malloc(fd, &p);
if (r < 0)
return r;
if (unlockpt(fd) < 0)
return -errno;
- if (ret_slave)
- *ret_slave = TAKE_PTR(p);
+ if (ret_peer_path)
+ *ret_peer_path = TAKE_PTR(p);
return TAKE_FD(fd);
}
static int ptsname_namespace(int pty, char **ret) {
- int no = -1, r;
+ int no = -1;
+
+ assert(pty >= 0);
+ assert(ret);
/* Like ptsname(), but doesn't assume that the path is
* accessible in the local namespace. */
- r = ioctl(pty, TIOCGPTN, &no);
- if (r < 0)
+ if (ioctl(pty, TIOCGPTN, &no) < 0)
return -errno;
if (no < 0)
return 0;
}
-int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave) {
+int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_peer_path) {
_cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, usernsfd = -EBADF, rootfd = -EBADF, fd = -EBADF;
_cleanup_close_pair_ int pair[2] = EBADF_PAIR;
- pid_t child;
int r;
assert(pid > 0);
if (r < 0)
return r;
- if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
+ if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pair) < 0)
return -errno;
- r = namespace_fork("(sd-openptns)", "(sd-openpt)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
- pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child);
+ r = namespace_fork(
+ "(sd-openptns)",
+ "(sd-openpt)",
+ /* except_fds= */ NULL,
+ /* n_except_fds= */ 0,
+ FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT,
+ pidnsfd,
+ mntnsfd,
+ /* netns_fd= */ -EBADF,
+ usernsfd,
+ rootfd,
+ /* ret_pid= */ NULL);
if (r < 0)
return r;
if (r == 0) {
pair[0] = safe_close(pair[0]);
- fd = openpt_allocate(flags, NULL);
+ fd = openpt_allocate(flags, /* ret_peer_path= */ NULL);
if (fd < 0)
_exit(EXIT_FAILURE);
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate_and_check("(sd-openptns)", child, 0);
- if (r < 0)
- return r;
- if (r != EXIT_SUCCESS)
- return -EIO;
-
fd = receive_one_fd(pair[0], 0);
if (fd < 0)
return fd;
- if (ret_slave) {
- r = ptsname_namespace(fd, ret_slave);
+ if (ret_peer_path) {
+ r = ptsname_namespace(fd, ret_peer_path);
if (r < 0)
return r;
}
int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
_cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, usernsfd = -EBADF, rootfd = -EBADF;
_cleanup_close_pair_ int pair[2] = EBADF_PAIR;
- pid_t child;
int r;
- r = namespace_open(pid, &pidnsfd, &mntnsfd, /* ret_netns_fd = */ NULL, &usernsfd, &rootfd);
+ assert(pid > 0);
+ assert(name);
+
+ r = namespace_open(pid, &pidnsfd, &mntnsfd, /* ret_netns_fd= */ NULL, &usernsfd, &rootfd);
if (r < 0)
return r;
- if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
+ if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pair) < 0)
return -errno;
- r = namespace_fork("(sd-terminalns)", "(sd-terminal)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
- pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child);
+ r = namespace_fork(
+ "(sd-terminalns)",
+ "(sd-terminal)",
+ /* except_fds= */ NULL,
+ /* n_except_fds= */ 0,
+ FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT,
+ pidnsfd,
+ mntnsfd,
+ /* netnsd_fd= */ -EBADF,
+ usernsfd,
+ rootfd,
+ /* ret_pid= */ NULL);
if (r < 0)
return r;
if (r == 0) {
- int master;
-
pair[0] = safe_close(pair[0]);
- master = open_terminal(name, mode|O_NOCTTY|O_CLOEXEC);
- if (master < 0)
+ int pty_fd = open_terminal(name, mode|O_NOCTTY|O_CLOEXEC);
+ if (pty_fd < 0)
_exit(EXIT_FAILURE);
- if (send_one_fd(pair[1], master, 0) < 0)
+ if (send_one_fd(pair[1], pty_fd, 0) < 0)
_exit(EXIT_FAILURE);
_exit(EXIT_SUCCESS);
pair[1] = safe_close(pair[1]);
- r = wait_for_terminate_and_check("(sd-terminalns)", child, 0);
- if (r < 0)
- return r;
- if (r != EXIT_SUCCESS)
- return -EIO;
-
return receive_one_fd(pair[0], 0);
}