test-pty.c revision 2eec67acbb00593e414549a7e5b35eb7dd776b1b
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
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 <locale.h>
#include <string.h>
#include <unistd.h>
#include "pty.h"
#include "util.h"
static const char sndmsg[] = "message\n";
static const char rcvmsg[] = "message\r\n";
static char rcvbuf[128];
ssize_t r, l;
char buf[512];
assert_se(l == r);
}
switch (ev) {
case PTY_DATA:
}
break;
case PTY_HUP:
/* This is guaranteed to appear _after_ the input queues are
* drained! */
break;
case PTY_CHILD:
/* this may appear at any time */
break;
default:
assert_se(0);
break;
}
/* if we got HUP _and_ CHILD, exit */
sd_event_exit(event, 0);
return 0;
}
int r;
/* write message to pty, ECHO mode guarantees that we get it back
* twice: once via ECHO, once from the run_child() fn */
r = sd_event_loop(event);
assert_se(r >= 0);
}
static void test_pty(void) {
rcvsiz = 0;
if (pid == 0) {
/* child */
exit(0);
}
/* parent */
/* Make sure the PTY recycled the child; yeah, this is racy if the
* PID was already reused; but that seems fine for a test. */
}
unsigned int i;
log_open();
/* 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;
}