Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add library with function to spawn a process connected to a pseudo-terminal #742

Merged
merged 8 commits into from
Mar 25, 2024
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ add_subdirectory(ffiTestLibrary ${CMAKE_CURRENT_BINARY_DIR}/build/ffiTestLibrary
# Handling Third party dependencies
add_third_party_dependencies_per_platform()

if (UNIX)
addIndependentLibraryWithRPATH(tty ${CMAKE_CURRENT_SOURCE_DIR}/tty/tty.c)
endif()

# Packaging Setup
include(cmake/packaging.cmake)

Expand Down
30 changes: 30 additions & 0 deletions tty/tty.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>

#define CHECK_ERROR(exp) if ((exp) == -1) { printf("Error in %s at %s: %s\n", __func__, #exp, strerror(errno)); exit(1); }

pid_t tty_spawn(int fdm, const char *path, char *const argv[], char *const envp[])
{
pid_t pid = fork();
if (pid == 0)
{
int fds = open(ptsname(fdm), O_RDWR);
close(fdm);
close(0);
close(1);
close(2);
dup(fds);
dup(fds);
dup(fds);
close(fds);
CHECK_ERROR(setsid());
CHECK_ERROR(ioctl(0, TIOCSCTTY, 0));
CHECK_ERROR(execve(path, argv, envp));
}
return pid;
}