]>
git.proxmox.com Git - systemd.git/blob - src/test/test-pty.c
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/>.
33 static const char sndmsg
[] = "message\n";
34 static const char rcvmsg
[] = "message\r\n";
35 static char rcvbuf
[128];
36 static size_t rcvsiz
= 0;
37 static sd_event
*event
;
39 static void run_child(Pty
*pty
) {
43 r
= read(0, buf
, sizeof(buf
));
44 assert_se((size_t)r
== strlen(sndmsg
));
45 assert_se(!strncmp(buf
, sndmsg
, r
));
51 static int pty_fn(Pty
*pty
, void *userdata
, unsigned int ev
, const void *ptr
, size_t size
) {
54 assert_se(rcvsiz
< strlen(rcvmsg
) * 2);
55 assert_se(rcvsiz
+ size
< sizeof(rcvbuf
));
57 memcpy(&rcvbuf
[rcvsiz
], ptr
, size
);
60 if (rcvsiz
>= strlen(rcvmsg
) * 2) {
61 assert_se(rcvsiz
== strlen(rcvmsg
) * 2);
62 assert_se(!memcmp(rcvbuf
, rcvmsg
, strlen(rcvmsg
)));
63 assert_se(!memcmp(&rcvbuf
[strlen(rcvmsg
)], rcvmsg
, strlen(rcvmsg
)));
68 /* This is guaranteed to appear _after_ the input queues are
70 assert_se(rcvsiz
== strlen(rcvmsg
) * 2);
73 /* this may appear at any time */
80 /* if we got HUP _and_ CHILD, exit */
81 if (pty_get_fd(pty
) < 0 && pty_get_child(pty
) < 0)
82 sd_event_exit(event
, 0);
87 static void run_parent(Pty
*pty
) {
90 /* write message to pty, ECHO mode guarantees that we get it back
91 * twice: once via ECHO, once from the run_child() fn */
92 assert_se(pty_write(pty
, sndmsg
, strlen(sndmsg
)) >= 0);
94 r
= sd_event_loop(event
);
98 static void test_pty(void) {
105 assert_se(sd_event_default(&event
) >= 0);
107 pid
= pty_fork(&pty
, event
, pty_fn
, NULL
, 80, 25);
119 /* Make sure the PTY recycled the child; yeah, this is racy if the
120 * PID was already reused; but that seems fine for a test. */
121 assert_se(waitpid(pid
, NULL
, WNOHANG
) < 0 && errno
== ECHILD
);
124 sd_event_unref(event
);
127 int main(int argc
, char *argv
[]) {
130 log_parse_environment();
133 assert_se(sigprocmask_many(SIG_BLOCK
, SIGCHLD
, -1) >= 0);
135 /* Oh, there're ugly races in the TTY layer regarding HUP vs IN. Turns
136 * out they appear only 10% of the time. I fixed all of them and
137 * don't see them, anymore. But lets be safe and run this 1000 times
138 * so we catch any new ones, in case they appear again. */
139 for (i
= 0; i
< 1000; ++i
)