diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2019-06-13 13:19:10 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2019-06-13 13:19:10 +0000 |
| commit | acda4d2d315b16d0cf37755a3f9add4e49eb7514 (patch) | |
| tree | ef15135c85f07793f2937f4071fb4fe4165424b4 /slave.c | |
| parent | main: handle window resize (diff) | |
Set up pseudo terminal and fork slave
Diffstat (limited to 'slave.c')
| -rw-r--r-- | slave.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/slave.c b/slave.c new file mode 100644 index 00000000..77f9649b --- /dev/null +++ b/slave.c @@ -0,0 +1,67 @@ +#define _XOPEN_SOURCE 500 +#include "slave.h" +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +#include <sys/stat.h> +#include <fcntl.h> + +#define LOG_MODULE "slave" +#define LOG_ENABLE_DBG 1 +#include "log.h" + +void +slave_spawn(int ptmx) +{ + int pts = -1; + const char *pts_name = ptsname(ptmx); + + if (grantpt(ptmx) == -1) { + LOG_ERRNO("failed to grantpt()"); + goto err; + } + if (unlockpt(ptmx) == -1) { + LOG_ERRNO("failed to unlockpt()"); + goto err; + } + + close(ptmx); + ptmx = -1; + + if (setsid() == -1) { + LOG_ERRNO("failed to setsid()"); + goto err; + } + + pts = open(pts_name, O_RDWR); + if (pts == -1) { + LOG_ERRNO("failed to open pseudo terminal slave device"); + goto err; + } + + if (dup2(pts, STDIN_FILENO) == -1 || + dup2(pts, STDOUT_FILENO) == -1 || + dup2(pts, STDERR_FILENO) == -1) + { + LOG_ERRNO("failed to dup stdin/stdout/stderr"); + goto err; + } + + close(pts); + pts = -1; + + /* TODO: exec() */ + const char *s = "hello world\n"; + write(STDOUT_FILENO, s, strlen(s)); + + sleep(1000); + +err: + if (pts != -1) + close(pts); + if (ptmx != -1) + close(ptmx); + _exit(errno); +} |
