Skip to content

Create nspawn console tty in the child#12758

Merged
poettering merged 6 commits intosystemd:masterfrom
fbuihuu:nspawn-console-tty
Jun 18, 2019
Merged

Create nspawn console tty in the child#12758
poettering merged 6 commits intosystemd:masterfrom
fbuihuu:nspawn-console-tty

Conversation

@fbuihuu
Copy link
Contributor

@fbuihuu fbuihuu commented Jun 7, 2019

No description provided.

@fbuihuu fbuihuu added the nspawn label Jun 7, 2019
@lgtm-com

This comment has been minimized.

@mrc0mmand

This comment has been minimized.

@fbuihuu fbuihuu force-pushed the nspawn-console-tty branch 2 times, most recently from 73fc5ad to 65f5747 Compare June 11, 2019 07:08
Copy link
Member

@keszybz keszybz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very nice. I'll leave it open for a bit though to let other people review if they wish to.


if (do_chmod)
if (chmod(fd_path, mode & 07777) < 0)
if (fchmod_opath(fd, mode & 07777) < 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing the xsprintf once, now we'll do it twice (sometimes). The compiler might be able to optimize it away, because chmod takes a const*. OK, seems reasonable to do this like this.

@fbuihuu fbuihuu force-pushed the nspawn-console-tty branch from 65f5747 to a8b088c Compare June 13, 2019 15:25
@fbuihuu
Copy link
Contributor Author

fbuihuu commented Jun 13, 2019

Thanks for the review @keszybz, I addressed your comment and force pushed a new version.

@keszybz
Copy link
Member

keszybz commented Jun 13, 2019

Hmm, https://www.freedesktop.org/wiki/Software/systemd/ContainerInterface/#executionenvironment says

bind mount some suitable TTY to /dev/console

So those docs would need adjusting.

@keszybz keszybz requested a review from poettering June 13, 2019 15:41
@fbuihuu
Copy link
Contributor Author

fbuihuu commented Jun 14, 2019

Indeed although bind mounting a tty is still a possible solution for setting up /dev/console. Maybe a more generic wording would be better, something like "make some suitable TTY available through /dev/console" ?

@keszybz keszybz changed the title Nspawn console tty Create nspawn console tty in the child Jun 15, 2019
Copy link
Member

@poettering poettering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good, some comments though

if (unlockpt(fd) < 0)
return -errno;

*master = TAKE_FD(fd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return this directly? i.e. do return TAKE_FD(fd); here? no need for the additional return param

}

int ptsname_namespace(int pty, char **ret) {
int openpt_allocate(int flags, int *master, char **console) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why call the return param console here? that name mostly makes sense in the nspawn usecase, but i figure this one should be generic, hence maybe ret_slave?


int openpt_in_namespace(pid_t pid, int flags) {
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
int openpt_allocate_in_namespace(pid_t pid, int flags, int *master, char **console) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return the fd as parameter instead of as return value like before?

also, as above, console → ret_slave?

}

int machine_openpt(Machine *m, int flags) {
int machine_openpt(Machine *m, int flags, int *master, char **console) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

static int setup_dev_console(const char *dest, const char *console) {
_cleanup_umask_ mode_t u;
const char *to;
static int setup_stdio(const char *console) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parameter is always "/dev/console", no? hence should be good to just drop it and use the literal string in this function

@poettering poettering added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Jun 17, 2019
fbuihuu added 4 commits June 18, 2019 07:54
fstat(2) is fine with O_PATH fds.

For changing owership of a file opened with O_PATH, there's fchownat(2).

Only changing permissions is problematic but we introduced fchmod_opath() for
that purpose.
The console tty is now allocated from within the container so it's not
necessary anymore to allocate it from the host and bind mount the pty slave
into the container. The pty master is sent to the host.

/dev/console is now a symlink pointing to the pty slave.

This might also be less confusing for applications running inside the container
and the overall result looks cleaner (we don't need to apply manually the
passed selinux context, if any, to the allocated pty for instance).
@fbuihuu fbuihuu force-pushed the nspawn-console-tty branch from a8b088c to 31532b5 Compare June 18, 2019 06:18
@fbuihuu
Copy link
Contributor Author

fbuihuu commented Jun 18, 2019

Thanks for the feedback @poettering , I force pushed a new version addressing your comments.

@fbuihuu fbuihuu removed the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Jun 18, 2019
@lgtm-com
Copy link

lgtm-com bot commented Jun 18, 2019

This pull request introduces 2 alerts when merging 31532b5 into b337d89 - view on LGTM.com

new alerts:

  • 2 for Comparison result is always the same

@fbuihuu fbuihuu force-pushed the nspawn-console-tty branch from 31532b5 to 7f10988 Compare June 18, 2019 07:24
fbuihuu added 2 commits June 18, 2019 09:26
Allocating a pty is done in a couple of places so let's introduce a new helper
which does the job.

Also the new function, as well as openpt_in_namespace(), returns both pty
master and slave so the callers don't need to know about the pty slave
allocation details.

For the same reasons machine_openpt() prototype has also been changed to return
both pty master and slave so callers don't need to allocate a pty slave which
might be in a different namespace.

Finally openpt_in_namespace() has been renamed into
openpt_allocate_in_namespace().
@fbuihuu fbuihuu force-pushed the nspawn-console-tty branch from 7f10988 to dc98cae Compare June 18, 2019 07:27
@poettering poettering added the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Jun 18, 2019
@poettering
Copy link
Member

lgtm

@poettering poettering merged commit 59da647 into systemd:master Jun 18, 2019
@fbuihuu fbuihuu deleted the nspawn-console-tty branch June 18, 2019 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed nspawn

Development

Successfully merging this pull request may close these issues.

4 participants