-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchildprocess.hpp
67 lines (60 loc) · 1.42 KB
/
childprocess.hpp
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
/*
* Copyright (c) 2012 Vasily i. Redkin <[email protected]>
* License: MIT (See LICENSE.txt or http://www.opensource.org/licenses/MIT)
*/
#ifndef CHILDPROCESS_HPP_INCLUDED
#define CHILDPROCESS_HPP_INCLUDED
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h> // for strerror
#include <stdlib.h> // for exit
#include <string>
#include "eventdispatcher.hpp"
class ReadBuffer
{
private:
std::string m_buf;
int m_fd;
public:
int fd() const { return m_fd; }
bool fd(int fd) {
m_fd = fd;
// Set non-blocking
long arg;
if( (arg = fcntl(m_fd, F_GETFL, NULL)) < 0) {
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
exit(0);
}
arg |= O_NONBLOCK;
if( fcntl(m_fd, F_SETFL, arg) < 0) {
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
exit(0);
}
return true;
}
int do_read();
bool readline(std::string & s);
};
class Family;
class ChildProcess:public AsyncReader, public ChildProc
{
private:
int m_pid;
int m_fdo;
int m_fde;
Family * m_family;
ReadBuffer m_bufo;
ReadBuffer m_bufe;
bool m_setpgid;
public:
ChildProcess(Family * f):m_family(f), m_setpgid(true) { }
virtual ~ChildProcess() { }
int pid() const { return m_pid; }
bool start();
virtual int can_read(int fd);
virtual void dead(int status);
virtual int kill(int signal);
};
#endif /* CHILDPROCESS_HPP_INCLUDED */