Skip to content

Commit ae1d13d

Browse files
committed
terminal-util: introduce openpt_allocate()
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().
1 parent 3acc84e commit ae1d13d

File tree

5 files changed

+55
-45
lines changed

5 files changed

+55
-45
lines changed

src/basic/terminal-util.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,34 @@ int ptsname_malloc(int fd, char **ret) {
10491049
}
10501050
}
10511051

1052-
int ptsname_namespace(int pty, char **ret) {
1052+
int openpt_allocate(int flags, char **ret_slave) {
1053+
_cleanup_close_ int fd = -1;
1054+
_cleanup_free_ char *p = NULL;
1055+
int r;
1056+
1057+
fd = posix_openpt(flags|O_NOCTTY|O_CLOEXEC);
1058+
if (fd < 0)
1059+
return -errno;
1060+
1061+
if (ret_slave) {
1062+
r = ptsname_malloc(fd, &p);
1063+
if (r < 0)
1064+
return r;
1065+
1066+
if (!path_startswith(p, "/dev/pts/"))
1067+
return -EINVAL;
1068+
}
1069+
1070+
if (unlockpt(fd) < 0)
1071+
return -errno;
1072+
1073+
if (ret_slave)
1074+
*ret_slave = TAKE_PTR(p);
1075+
1076+
return TAKE_FD(fd);
1077+
}
1078+
1079+
static int ptsname_namespace(int pty, char **ret) {
10531080
int no = -1, r;
10541081

10551082
/* Like ptsname(), but doesn't assume that the path is
@@ -1068,8 +1095,8 @@ int ptsname_namespace(int pty, char **ret) {
10681095
return 0;
10691096
}
10701097

1071-
int openpt_in_namespace(pid_t pid, int flags) {
1072-
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1;
1098+
int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave) {
1099+
_cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1, fd = -1;
10731100
_cleanup_close_pair_ int pair[2] = { -1, -1 };
10741101
pid_t child;
10751102
int r;
@@ -1088,18 +1115,13 @@ int openpt_in_namespace(pid_t pid, int flags) {
10881115
if (r < 0)
10891116
return r;
10901117
if (r == 0) {
1091-
int master;
1092-
10931118
pair[0] = safe_close(pair[0]);
10941119

1095-
master = posix_openpt(flags|O_NOCTTY|O_CLOEXEC);
1096-
if (master < 0)
1097-
_exit(EXIT_FAILURE);
1098-
1099-
if (unlockpt(master) < 0)
1120+
fd = openpt_allocate(flags, NULL);
1121+
if (fd < 0)
11001122
_exit(EXIT_FAILURE);
11011123

1102-
if (send_one_fd(pair[1], master, 0) < 0)
1124+
if (send_one_fd(pair[1], fd, 0) < 0)
11031125
_exit(EXIT_FAILURE);
11041126

11051127
_exit(EXIT_SUCCESS);
@@ -1113,7 +1135,17 @@ int openpt_in_namespace(pid_t pid, int flags) {
11131135
if (r != EXIT_SUCCESS)
11141136
return -EIO;
11151137

1116-
return receive_one_fd(pair[0], 0);
1138+
fd = receive_one_fd(pair[0], 0);
1139+
if (fd < 0)
1140+
return fd;
1141+
1142+
if (ret_slave) {
1143+
r = ptsname_namespace(fd, ret_slave);
1144+
if (r < 0)
1145+
return r;
1146+
}
1147+
1148+
return TAKE_FD(fd);
11171149
}
11181150

11191151
int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {

src/basic/terminal-util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ int getttyname_malloc(int fd, char **r);
151151
int getttyname_harder(int fd, char **r);
152152

153153
int ptsname_malloc(int fd, char **ret);
154-
int ptsname_namespace(int pty, char **ret);
155154

156-
int openpt_in_namespace(pid_t pid, int flags);
155+
int openpt_allocate(int flags, char **ret_slave);
156+
int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave);
157157
int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
158158

159159
int vt_default_utf8(void);

src/machine/machine-dbus.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,10 @@ int bus_machine_method_open_pty(sd_bus_message *message, void *userdata, sd_bus_
423423
if (r == 0)
424424
return 1; /* Will call us back */
425425

426-
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC);
426+
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name);
427427
if (master < 0)
428428
return master;
429429

430-
r = ptsname_namespace(master, &pty_name);
431-
if (r < 0)
432-
return r;
433-
434430
r = sd_bus_message_new_method_return(message, &reply);
435431
if (r < 0)
436432
return r;
@@ -514,17 +510,12 @@ int bus_machine_method_open_login(sd_bus_message *message, void *userdata, sd_bu
514510
if (r == 0)
515511
return 1; /* Will call us back */
516512

517-
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC);
513+
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name);
518514
if (master < 0)
519515
return master;
520516

521-
r = ptsname_namespace(master, &pty_name);
522-
if (r < 0)
523-
return r;
524-
525517
p = path_startswith(pty_name, "/dev/pts/");
526-
if (!p)
527-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "PTS name %s is invalid", pty_name);
518+
assert(p);
528519

529520
r = container_bus_new(m, error, &allocated_bus);
530521
if (r < 0)
@@ -630,14 +621,10 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu
630621
if (r == 0)
631622
return 1; /* Will call us back */
632623

633-
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC);
624+
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name);
634625
if (master < 0)
635626
return master;
636627

637-
r = ptsname_namespace(master, &pty_name);
638-
if (r < 0)
639-
return r;
640-
641628
p = path_startswith(pty_name, "/dev/pts/");
642629
assert(p);
643630

src/machine/machine.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,29 +530,20 @@ int machine_kill(Machine *m, KillWho who, int signo) {
530530
return manager_kill_unit(m->manager, m->unit, signo, NULL);
531531
}
532532

533-
int machine_openpt(Machine *m, int flags) {
533+
int machine_openpt(Machine *m, int flags, char **ret_slave) {
534534
assert(m);
535535

536536
switch (m->class) {
537537

538-
case MACHINE_HOST: {
539-
int fd;
540-
541-
fd = posix_openpt(flags);
542-
if (fd < 0)
543-
return -errno;
544-
545-
if (unlockpt(fd) < 0)
546-
return -errno;
538+
case MACHINE_HOST:
547539

548-
return fd;
549-
}
540+
return openpt_allocate(flags, ret_slave);
550541

551542
case MACHINE_CONTAINER:
552543
if (m->leader <= 0)
553544
return -EINVAL;
554545

555-
return openpt_in_namespace(m->leader, flags);
546+
return openpt_allocate_in_namespace(m->leader, flags, ret_slave);
556547

557548
default:
558549
return -EOPNOTSUPP;

src/machine/machine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ MachineState machine_state_from_string(const char *s) _pure_;
8989
const char *kill_who_to_string(KillWho k) _const_;
9090
KillWho kill_who_from_string(const char *s) _pure_;
9191

92-
int machine_openpt(Machine *m, int flags);
92+
int machine_openpt(Machine *m, int flags, char **ret_slave);
9393
int machine_open_terminal(Machine *m, const char *path, int mode);
9494

9595
int machine_get_uid_shift(Machine *m, uid_t *ret);

0 commit comments

Comments
 (0)