1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 static const char sndmsg[] = "message\n";
32 static const char rcvmsg[] = "message\r\n";
33 static char rcvbuf[128];
34 static size_t rcvsiz = 0;
35 static sd_event *event;
37 static void run_child(Pty *pty) {
41 r = read(0, buf, sizeof(buf));
42 assert_se((size_t)r == strlen(sndmsg));
43 assert_se(!strncmp(buf, sndmsg, r));
49 static int pty_fn(Pty *pty, void *userdata, unsigned int ev, const void *ptr, size_t size) {
52 assert_se(rcvsiz < strlen(rcvmsg) * 2);
53 assert_se(rcvsiz + size < sizeof(rcvbuf));
55 memcpy(&rcvbuf[rcvsiz], ptr, size);
58 if (rcvsiz >= strlen(rcvmsg) * 2) {
59 assert_se(rcvsiz == strlen(rcvmsg) * 2);
60 assert_se(!memcmp(rcvbuf, rcvmsg, strlen(rcvmsg)));
61 assert_se(!memcmp(&rcvbuf[strlen(rcvmsg)], rcvmsg, strlen(rcvmsg)));
66 /* This is guaranteed to appear _after_ the input queues are
68 assert_se(rcvsiz == strlen(rcvmsg) * 2);
71 /* this may appear at any time */
78 /* if we got HUP _and_ CHILD, exit */
79 if (pty_get_fd(pty) < 0 && pty_get_child(pty) < 0)
80 sd_event_exit(event, 0);
85 static void run_parent(Pty *pty) {
88 /* write message to pty, ECHO mode guarantees that we get it back
89 * twice: once via ECHO, once from the run_child() fn */
90 assert_se(pty_write(pty, sndmsg, strlen(sndmsg)) >= 0);
92 r = sd_event_loop(event);
96 static void test_pty(void) {
103 assert_se(sd_event_default(&event) >= 0);
105 pid = pty_fork(&pty, event, pty_fn, NULL, 80, 25);
117 /* Make sure the PTY recycled the child; yeah, this is racy if the
118 * PID was already reused; but that seems fine for a test. */
119 assert_se(waitpid(pid, NULL, WNOHANG) < 0 && errno == ECHILD);
122 sd_event_unref(event);
125 int main(int argc, char *argv[]) {
128 log_parse_environment();
131 assert_se(sigprocmask_many(SIG_BLOCK, SIGCHLD, -1) >= 0);
133 /* Oh, there're ugly races in the TTY layer regarding HUP vs IN. Turns
134 * out they appear only 10% of the time. I fixed all of them and
135 * don't see them, anymore. But lets be safe and run this 1000 times
136 * so we catch any new ones, in case they appear again. */
137 for (i = 0; i < 1000; ++i)