-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpty_ipc_clt.h
71 lines (60 loc) · 1.66 KB
/
pty_ipc_clt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __PTY_IPC_CLT_H__
#define __PTY_IPC_CLT_H__
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <util.h> // openpty
#include "stringutils.h"
#include "err/posix.h"
#include "readwriter.h"
#include "forkchild.h"
class pty_ipc_client : public readwriter, public fork_child {
int _pty;
int _tty;
char _ttydev[PATH_MAX];
public:
pty_ipc_client(const std::string& svrname, const StringList& args)
{
int rc = openpty(&_pty, &_tty, _ttydev, NULL, NULL);
if (rc < 0) {
throw posixerror("openpty");
}
fprintf(stderr, "openpty -> %d, %d : %s\n", _pty, _tty, _ttydev);
// signal(SIGUSR1, do_nothing); /* don't die */
// signal(SIGCHLD, do_nothing); /* don't ignore SIGCHLD */
StringList ptyargs;
#ifndef PTY_TO_STDIO
ptyargs.push_back(_ttydev);
#endif
ptyargs.insert(ptyargs.end(), args.begin(), args.end());
run_child(svrname, ptyargs);
close(_pty);
fprintf(stderr, "parent is using %d\n", _tty);
}
virtual void initialize_child()
{
close(_tty);
fprintf(stderr, "child is using %d\n", _pty);
#ifdef PTY_TO_STDIO
if (-1==dup2(_pty, STDIN_FILENO))
throw posixerror("dup2(stdin)");
if (-1==dup2(_pty, STDOUT_FILENO))
throw posixerror("dup2(stdout)");
close(_pty);
#endif
// signal(SIGUSR1, SIG_DFL);
}
~pty_ipc_client()
{
close(_tty);
}
virtual size_t readsome(void*p, size_t n)
{
return ::read(_tty, p, n);
}
virtual size_t writesome(const void*p, size_t n)
{
return ::write(_tty, p, n);
}
};
#endif