shared: add PTY helper

This Pty API wraps the ugliness that is POSIX PTY. It takes care of:
  - edge-triggered HUP handling (avoid heavy CPU-usage on vhangup)
  - HUP vs. input-queue draining (handle HUP _after_ draining the whole
    input queue)
  - SIGCHLD vs. HUP (HUP is no reliable way to catch PTY deaths, always
    use SIGCHLD. Otherwise, vhangup() and friends will break.)
  - Output queue buffering (async EPOLLOUT handling)
  - synchronous setup (via Barrier API)

At the same time, the PTY API does not execve(). It simply fork()s and
leaves everything else to the caller. Usually, they execve() but we
support other setups, too.

This will be needed by multiple UI binaries (systemd-console, systemd-er,
...) so it's placed in src/shared/. It's not strictly related to
libsystemd-terminal, so it's not included there.
This commit is contained in:
David Herrmann
2014-07-11 16:29:56 +02:00
parent a2da110b78
commit a47d1dfd08
5 changed files with 870 additions and 0 deletions

1
.gitignore vendored
View File

@@ -204,6 +204,7 @@
/test-path-util
/test-prioq
/test-ratelimit
/test-pty
/test-replace-var
/test-resolve
/test-ring

View File

@@ -843,6 +843,8 @@ libsystemd_shared_la_SOURCES = \
src/shared/ring.h \
src/shared/barrier.c \
src/shared/barrier.h \
src/shared/pty.c \
src/shared/pty.h \
src/shared/async.c \
src/shared/async.h \
src/shared/copy.c \
@@ -1252,6 +1254,7 @@ tests += \
test-util \
test-ring \
test-barrier \
test-pty \
test-tmpfiles \
test-namespace \
test-date \
@@ -1428,6 +1431,12 @@ test_barrier_SOURCES = \
test_barrier_LDADD = \
libsystemd-core.la
test_pty_SOURCES = \
src/test/test-pty.c
test_pty_LDADD = \
libsystemd-core.la
test_tmpfiles_SOURCES = \
src/test/test-tmpfiles.c

640
src/shared/pty.c Normal file

File diff suppressed because it is too large Load Diff

77
src/shared/pty.h Normal file
View File

@@ -0,0 +1,77 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "barrier.h"
#include "macro.h"
#include "sd-event.h"
#include "util.h"
typedef struct Pty Pty;
enum {
PTY_CHILD,
PTY_HUP,
PTY_DATA,
};
typedef int (*pty_event_t) (Pty *pty, void *userdata, unsigned int event, const void *ptr, size_t size);
int pty_new(Pty **out);
Pty *pty_ref(Pty *pty);
Pty *pty_unref(Pty *pty);
#define _pty_unref_ _cleanup_(pty_unrefp)
DEFINE_TRIVIAL_CLEANUP_FUNC(Pty*, pty_unref);
Barrier *pty_get_barrier(Pty *pty);
bool pty_is_unknown(Pty *pty);
bool pty_is_parent(Pty *pty);
bool pty_is_child(Pty *pty);
bool pty_has_child(Pty *pty);
pid_t pty_get_child(Pty *pty);
bool pty_is_open(Pty *pty);
int pty_get_fd(Pty *pty);
int pty_make_child(Pty *pty);
int pty_make_parent(Pty *pty, pid_t child);
int pty_unlock(Pty *pty);
int pty_setup_child(Pty *pty);
void pty_close(Pty *pty);
int pty_attach_event(Pty *pty, sd_event *event, pty_event_t event_fn, void *event_fn_userdata);
void pty_detach_event(Pty *pty);
int pty_write(Pty *pty, const void *buf, size_t size);
int pty_signal(Pty *pty, int sig);
int pty_resize(Pty *pty, unsigned short term_width, unsigned short term_height);
pid_t pty_fork(Pty **out, sd_event *event, pty_event_t event_fn, void *event_fn_userdata, unsigned short initial_term_width, unsigned short initial_term_height);

143
src/test/test-pty.c Normal file
View File

@@ -0,0 +1,143 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "def.h"
#include "pty.h"
#include "util.h"
static const char sndmsg[] = "message\n";
static const char rcvmsg[] = "message\r\n";
static char rcvbuf[128];
static size_t rcvsiz = 0;
static sd_event *event;
static void run_child(Pty *pty) {
int r, l;
char buf[512];
r = read(0, buf, sizeof(buf));
assert_se(r == strlen(sndmsg));
assert_se(!strncmp(buf, sndmsg, r));
l = write(1, buf, r);
assert_se(l == r);
}
static int pty_fn(Pty *pty, void *userdata, unsigned int ev, const void *ptr, size_t size) {
switch (ev) {
case PTY_DATA:
assert_se(rcvsiz < strlen(rcvmsg) * 2);
assert_se(rcvsiz + size < sizeof(rcvbuf));
memcpy(&rcvbuf[rcvsiz], ptr, size);
rcvsiz += size;
if (rcvsiz >= strlen(rcvmsg) * 2) {
assert_se(rcvsiz == strlen(rcvmsg) * 2);
assert_se(!memcmp(rcvbuf, rcvmsg, strlen(rcvmsg)));
assert_se(!memcmp(&rcvbuf[strlen(rcvmsg)], rcvmsg, strlen(rcvmsg)));
}
break;
case PTY_HUP:
/* This is guaranteed to appear _after_ the input queues are
* drained! */
assert_se(rcvsiz == strlen(rcvmsg) * 2);
break;
case PTY_CHILD:
/* this may appear at any time */
break;
default:
assert_se(0);
break;
}
/* if we got HUP _and_ CHILD, exit */
if (pty_get_fd(pty) < 0 && pty_get_child(pty) < 0)
sd_event_exit(event, 0);
return 0;
}
static void run_parent(Pty *pty) {
int r;
/* write message to pty, ECHO mode guarantees that we get it back
* twice: once via ECHO, once from the run_child() fn */
assert_se(pty_write(pty, sndmsg, strlen(sndmsg)) >= 0);
r = sd_event_loop(event);
assert_se(r >= 0);
}
static void test_pty(void) {
pid_t pid;
Pty *pty;
rcvsiz = 0;
memset(rcvbuf, 0, sizeof(rcvbuf));
assert_se(sd_event_default(&event) >= 0);
pid = pty_fork(&pty, event, pty_fn, NULL, 80, 25);
assert_se(pid >= 0);
if (pid == 0) {
/* child */
run_child(pty);
exit(0);
}
/* parent */
run_parent(pty);
/* Make sure the PTY recycled the child; yeah, this is racy if the
* PID was already reused; but that seems fine for a test. */
assert_se(waitpid(pid, NULL, WNOHANG) < 0 && errno == ECHILD);
pty_unref(pty);
sd_event_unref(event);
}
int main(int argc, char *argv[]) {
unsigned int i;
log_parse_environment();
log_open();
assert_se(sigprocmask_many(SIG_BLOCK, SIGCHLD, -1) >= 0);
/* Oh, there're ugly races in the TTY layer regarding HUP vs IN. Turns
* out they appear only 10% of the time. I fixed all of them and
* don't see them, anymore. But lets be safe and run this 1000 times
* so we catch any new ones, in case they appear again. */
for (i = 0; i < 1000; ++i)
test_pty();
return 0;
}