Skip to content

Commit

Permalink
Merge pull request #2263 from ShoichiroKitano/fix-pty-master-tiocgpgrp
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt authored Nov 8, 2023
2 parents ac82ebd + 636a35c commit 6dd6c68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
41 changes: 33 additions & 8 deletions fs/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ struct tty *tty_get(struct tty_driver *driver, int type, int num) {
return tty;
}

static struct tty *get_slave_side_tty(struct tty *tty) {
if (tty->type == TTY_PSEUDO_MASTER_MAJOR) {
return tty->pty.other;
} else {
return tty;
}
}

static void tty_poll_wakeup(struct tty *tty, int events) {
unlock(&tty->lock);
struct fd *fd;
Expand Down Expand Up @@ -605,7 +613,7 @@ static ssize_t tty_ioctl_size(int cmd) {
return sizeof(struct termios_);
case TIOCGWINSZ_: case TIOCSWINSZ_:
return sizeof(struct winsize_);
case TIOCGPRGP_: case TIOCSPGRP_:
case TIOCGPGRP_: case TIOCSPGRP_:
case TIOCSPTLCK_: case TIOCGPTN_:
case TIOCPKT_: case TIOCGPKT_:
case FIONREAD_:
Expand Down Expand Up @@ -657,6 +665,26 @@ static int tiocsctty(struct tty *tty, int force) {
return err;
}

static int tiocgpgrp(struct tty *tty, pid_t_ *fg_group) {
int err = 0;
struct tty *slave = get_slave_side_tty(tty);
if (slave != tty) {
lock(&slave->lock);
}

if (tty == slave && !tty_is_current(slave) || slave->fg_group == 0) {
err = _ENOTTY;
goto error_no_ctrl_tty;
}
*fg_group = slave->fg_group;
STRACE("tty group = %d\n", slave->fg_group);

error_no_ctrl_tty:
if (slave != tty)
unlock(&slave->lock);
return err;
}

// These ioctls are separated out because they have to operate on the slave
// side of a pseudoterminal pair even if the master is specified
static int tty_mode_ioctl(struct tty *in_tty, int cmd, void *arg) {
Expand Down Expand Up @@ -730,13 +758,10 @@ static int tty_ioctl(struct fd *fd, int cmd, void *arg) {
err = tiocsctty(tty, (uintptr_t) arg);
break;

case TIOCGPRGP_:
if (!tty_is_current(tty) || tty->fg_group == 0) {
err = _ENOTTY;
break;
}
STRACE("tty group = %d\n", tty->fg_group);
*(dword_t *) arg = tty->fg_group; break;
case TIOCGPGRP_:
err = tiocgpgrp(tty, (pid_t_ *) arg);
break;

case TIOCSPGRP_:
// see "aaaaaaaa" comment above
unlock(&tty->lock);
Expand Down
2 changes: 1 addition & 1 deletion fs/tty.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct termios_ {
#define TCSETSF_ 0x5404
#define TCFLSH_ 0x540b
#define TIOCSCTTY_ 0x540e
#define TIOCGPRGP_ 0x540f
#define TIOCGPGRP_ 0x540f
#define TIOCSPGRP_ 0x5410
#define TIOCGWINSZ_ 0x5413
#define TIOCSWINSZ_ 0x5414
Expand Down

0 comments on commit 6dd6c68

Please sign in to comment.