libc: Add login_tty() and forkpty()
This commit is contained in:
parent
3a5924be64
commit
5bb4321134
@ -26,6 +26,7 @@ set(SOURCES
|
||||
src/strtod.cpp
|
||||
src/math.cpp
|
||||
src/pty.cpp
|
||||
src/utmp.cpp
|
||||
src/sys/stat.cpp
|
||||
src/sys/mman.cpp
|
||||
src/sys/wait.cpp
|
||||
|
@ -14,6 +14,9 @@ extern "C"
|
||||
/* Find an available pseudoterminal pair and initialize it, returning file descriptors for both sides. */
|
||||
int openpty(int* master, int* slave, char* name, const struct termios* term, const struct winsize* win);
|
||||
|
||||
/* Create a new process operating in a pseudoterminal. */
|
||||
pid_t forkpty(int* master, char* name, const struct termios* term, const struct winsize* win);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
18
libc/include/utmp.h
Normal file
18
libc/include/utmp.h
Normal file
@ -0,0 +1,18 @@
|
||||
/* utmp.h: Write entries to utmp files. */
|
||||
|
||||
#ifndef _UTMP_H
|
||||
#define _UTMP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Prepare a new terminal session on a tty. */
|
||||
int login_tty(int fd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -5,6 +5,7 @@
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <utmp.h>
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
@ -44,4 +45,27 @@ extern "C"
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
pid_t forkpty(int* master, char* name, const struct termios* term, const struct winsize* win)
|
||||
{
|
||||
int slave;
|
||||
if (openpty(master, &slave, name, term, win) < 0) return -1;
|
||||
|
||||
pid_t child = fork();
|
||||
if (child < 0)
|
||||
{
|
||||
close(*master);
|
||||
close(slave);
|
||||
return -1;
|
||||
}
|
||||
if (!child)
|
||||
{
|
||||
close(*master);
|
||||
login_tty(slave);
|
||||
}
|
||||
else
|
||||
close(slave);
|
||||
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
22
libc/src/utmp.cpp
Normal file
22
libc/src/utmp.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <utmp.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int login_tty(int fd)
|
||||
{
|
||||
setsid();
|
||||
|
||||
if (ioctl(fd, TIOCSCTTY) < 0) return -1;
|
||||
|
||||
dup2(fd, STDIN_FILENO);
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user