tty: add posix open_pt test.

Change-Id: I9a4d927ee693198b8c978f1cb51cf4cc50ca6557
Reviewed-on: https://review.haiku-os.org/c/1417
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Jérôme Duval 2019-04-28 21:31:33 +02:00 committed by waddlesplash
parent e892b55b25
commit 62740a49ce
2 changed files with 102 additions and 0 deletions

View File

@ -3,4 +3,5 @@ SubDir HAIKU_TOP src tests add-ons kernel drivers tty ;
UsePrivateHeaders [ FDirName kernel ] ;
SimpleTest tty-test : tty-test.cpp : libroot.so [ TargetLibstdc++ ] ;
SimpleTest posix_openpt_test : posix_openpt_test.cpp : libroot.so ;

View File

@ -0,0 +1,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <signal.h>
void die(const char *ptr, ...)
{
va_list vlist;
va_start(vlist, ptr);
char ch;
while ((ch = *ptr++) != '\0') {
if (ch == '%' && *ptr != '\0') {
ch = *ptr++;
switch(ch) {
case 'd': printf("%d", va_arg(vlist, int)); break;
case 'e': printf("%e", va_arg(vlist, double)); break;
case 'c': printf("%c", va_arg(vlist, int)); break;
case 's': printf("%s", va_arg(vlist, char *)); break;
default: va_end(vlist);exit(1);
}
} else
printf("%c", ch);
}
va_end(vlist);
exit(1);
}
int main()
{
int pid;
int ptm;
int pts;
char *ptr;
char *program_name[2] = { (char*)"/bin/uname", NULL};
char buf[512];
int n;
if ((ptm = posix_openpt(O_RDWR)) == -1) {
die("posix_openpt error: %s\n", strerror(errno));
}
if (grantpt(ptm) == -1) {
die("grantpt error: %s\n", strerror(errno));
}
if (unlockpt(ptm) == -1) {
die("unlockpt error: %s\n", strerror(errno));
}
pid = fork();
if (pid < 0) {
die("fork error: %s\n", strerror(errno));
} else if (pid == 0) { // child
if (setsid() == (pid_t)-1) {
die("setsid() error: %s\n", strerror(errno));
}
if ((ptr = (char *) ptsname(ptm)) == NULL) {
die("ptsname error: %s\n", strerror(errno));
}
if ((pts = open(ptr, O_RDWR)) < 0) {
die("open of slave failed: %a\n", strerror(errno));
}
close(ptm);
if (dup2(pts, STDIN_FILENO) != STDIN_FILENO
|| dup2(pts, STDOUT_FILENO) != STDOUT_FILENO
|| dup2(pts, STDERR_FILENO) != STDERR_FILENO) {
die("error doing dup's : %s\n", strerror(errno));
}
if (execve(*program_name, program_name , NULL) == -1) {
die("execve error: %s\n", strerror(errno));
}
exit(1);
} else { // parent
if (dup2(ptm, STDIN_FILENO) != STDIN_FILENO) {
die("dup2 of parent failed");
}
while (1) {
n = read(ptm, buf, 511);
if (n <= 0) {
break;
}
write(STDOUT_FILENO, buf, n);
}
}
return 0;
}