aboutsummaryrefslogtreecommitdiff
path: root/lib/base/console.cpp
blob: 0a01094be3c153390623cd413591ede55696e159 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <lib/base/console.h>
#include <lib/base/eerror.h>
#include <sys/vfs.h> // for statfs
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char *cwd )
{
	int pfdin[2];  /* from child to parent */
	int pfdout[2]; /* from parent to child */
	int pfderr[2]; /* stderr from child to parent */
	int pid;       /* child's pid */

	if ( pipe(pfdin) == -1 || pipe(pfdout) == -1 || pipe(pfderr) == -1)
		return(-1);

	if ( ( pid = vfork() ) == -1 )
		return(-1);
	else if (pid == 0) /* child process */
	{
		setsid();
		if ( close(0) == -1 || close(1) == -1 || close(2) == -1 )
			_exit(0);

		if (dup(pfdout[0]) != 0 || dup(pfdin[1]) != 1 || dup(pfderr[1]) != 2 )
			_exit(0);

		if (close(pfdout[0]) == -1 || close(pfdout[1]) == -1 ||
				close(pfdin[0]) == -1 || close(pfdin[1]) == -1 ||
				close(pfderr[0]) == -1 || close(pfderr[1]) == -1 )
			_exit(0);

		for (unsigned int i=3; i < 90; ++i )
			close(i);

		if (cwd)
			chdir(cwd);

		execvp(cmd, (char * const *)argv); 
				/* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
		_exit(0);
	}
	if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
			return(-1);

	pfd[0] = pfdin[0];
	pfd[1] = pfdout[1];
	pfd[2] = pfderr[0];

	return(pid);
}

eConsoleAppContainer::eConsoleAppContainer()
:pid(-1), killstate(0), in(0), out(0), err(0)
{
	for (int i=0; i < 3; ++i)
		fd[i]=-1;
}

static char brakets[][2] = {
	{ '\'','\'' },
	{'"','"'},
	{'`','`'},
	{'(',')'},
	{'{','}'},
	{'[',']'},
	{'<','>'}
};

static char *find_bracket(char ch)
{
	size_t idx=0;
	while (idx < sizeof(brakets)/2) {
		if (brakets[idx][0] == ch)
			return &brakets[idx][0];
		++idx;
	}
	return NULL;
}

int eConsoleAppContainer::setCWD( const char *path )
{
	struct stat dir_stat;

	if (stat(path, &dir_stat) == -1)
		return -1;

	if (!S_ISDIR(dir_stat.st_mode))
		return -2;

	m_cwd = path;
	return 0;
}

int eConsoleAppContainer::execute( const char *cmd )
{
	int cnt=0, slen=strlen(cmd);
	char buf[slen+1];
	char *tmp=0, *argv[64], *path=buf, *cmds = buf;
	memcpy(buf, cmd, slen+1);

//	printf("cmd = %s, len %d\n", cmd, slen);

	// kill spaces at beginning
	while(path[0] == ' ') {
		++path;
		++cmds;
		--slen;
	}

	// kill spaces at the end
	while(slen && path[slen-1] == ' ') {
		path[slen-1] = 0;
		--slen;
	}

	if (!slen)
		return -2;

	tmp = strchr(path, ' ');
	if (tmp) {
		*tmp = 0;
		cmds = tmp+1;
		while(*cmds && *cmds == ' ')
			++cmds;
	}
	else
		cmds = path+slen;

	memset(argv, 0, sizeof(argv));
	argv[cnt++] = path;

	if (*cmds) {
		char *argb=NULL, *it=NULL;
		while ( (tmp = strchr(cmds, ' ')) ) {
			if (!it && *cmds && (it = find_bracket(*cmds)) )
				*cmds = 'X'; // replace open braket...
			if (!argb) // not arg begin
				argb = cmds;
			if (it && *(tmp-1) == it[1]) {
				*argb = it[0]; // set old char for open braket
				it = 0;
			}
			if (!it) { // end of arg
				*tmp = 0;
				argv[cnt++] = argb;
				argb=0; // reset arg begin
			}
			cmds = tmp+1;
			while (*cmds && *cmds == ' ')
				++cmds;
		}
		argv[cnt++] = argb ? argb : cmds;
		if (it)
		    *argv[cnt-1] = it[0]; // set old char for open braket
	}

//	int tmp=0;
//	while(argv[tmp])
//		eDebug("%d is %s", tmp, argv[tmp++]);
	return execute(argv[0], argv);
}

int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
{
	if (running())
		return -1;

	pid=-1;
	killstate=0;

	// get one read ,one write and the err pipe to the prog..
	pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);

	if ( pid == -1 )
		return -3;

//	eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);

	::fcntl(fd[1], F_SETFL, O_NONBLOCK);
	::fcntl(fd[2], F_SETFL, O_NONBLOCK);
	in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
	out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);  
	err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
	CONNECT(in->activated, eConsoleAppContainer::readyRead);
	CONNECT(out->activated, eConsoleAppContainer::readyWrite);
	CONNECT(err->activated, eConsoleAppContainer::readyErrRead);

	return 0;
}

int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
{
	if (!PyString_Check(cmdline))
		return -1;
	if (!PyList_Check(args))
		return -1;
	const char *argv[PyList_Size(args) + 1];
	int i;
	for (i = 0; i < PyList_Size(args); ++i)
	{
		PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
		if (!arg)
			return -1;
		if (!PyString_Check(arg))
			return -1;
		argv[i] = PyString_AsString(arg); /* borrowed pointer */
	}
	argv[i] = 0;

	return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
}

eConsoleAppContainer::~eConsoleAppContainer()
{
	kill();
}

void eConsoleAppContainer::kill()
{
	if ( killstate != -1 && pid != -1 )
	{
		eDebug("user kill(SIGKILL) console App");
		killstate=-1;
		/*
		 * Use a negative pid value, to signal the whole process group
		 * ('pid' might not even be running anymore at this point)
		 */
		::kill(-pid, SIGKILL);
		closePipes();
	}
	while( outbuf.size() ) // cleanup out buffer
	{
		queue_data d = outbuf.front();
		outbuf.pop();
		delete [] d.data;
	}
	delete in;
	delete out;
	delete err;
	in=out=err=0;
}

void eConsoleAppContainer::sendCtrlC()
{
	if ( killstate != -1 && pid != -1 )
	{
		eDebug("user send SIGINT(Ctrl-C) to console App");
		/*
		 * Use a negative pid value, to signal the whole process group
		 * ('pid' might not even be running anymore at this point)
		 */
		::kill(-pid, SIGINT);
	}
}

void eConsoleAppContainer::closePipes()
{
	if (in)
		in->stop();
	if (out)
		out->stop();
	if (err)
		err->stop();
	if (fd[0] != -1)
	{
		::close(fd[0]);
		fd[0]=-1;
	}
	if (fd[1] != -1)
	{
		::close(fd[1]);
		fd[1]=-1;
	}
	if (fd[2] != -1)
	{
		::close(fd[2]);
		fd[2]=-1;
	}
	eDebug("pipes closed");
	while( outbuf.size() ) // cleanup out buffer
	{
		queue_data d = outbuf.front();
		outbuf.pop();
		delete [] d.data;
	}
	pid = -1;
}

void eConsoleAppContainer::readyRead(int what)
{
	bool hungup = what & eSocketNotifier::Hungup;
	if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
	{
//		eDebug("what = %d");
		char buf[2049];
		int rd;
		while((rd = read(fd[0], buf, 2048)) > 0)
		{
			buf[rd]=0;
			/*emit*/ dataAvail(buf);
			if (!hungup)
				break;
		}
	}
	readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
	if (hungup)
	{
		eDebug("child has terminated");
		closePipes();
		int childstatus;
		int retval = killstate;
		/*
		 * We have to call 'wait' on the child process, in order to avoid zombies.
		 * Also, this gives us the chance to provide better exit status info to appClosed.
		 */
		if (::waitpid(pid, &childstatus, 0) > 0)
		{
			if (WIFEXITED(childstatus))
			{
				retval = WEXITSTATUS(childstatus);
			}
		}
		/*emit*/ appClosed(retval);
	}
}

void eConsoleAppContainer::readyErrRead(int what)
{
	if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
	{
//		eDebug("what = %d");
		char buf[2049];
		int rd;
		while((rd = read(fd[2], buf, 2048)) > 0)
		{
/*			for ( int i = 0; i < rd; i++ )
				eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
			buf[rd]=0;
			/*emit*/ dataAvail(buf);
		}
	}
}

void eConsoleAppContainer::write( const char *data, int len )
{
	char *tmp = new char[len];
	memcpy(tmp, data, len);
	outbuf.push(queue_data(tmp,len));
	if (out)
		out->start();
}

void eConsoleAppContainer::write( PyObject *data )
{
	char *buffer;
	int length;
	if (PyString_AsStringAndSize(data, &buffer, &length))
		return;
	if (buffer && length)
		write(buffer, length);
}

void eConsoleAppContainer::readyWrite(int what)
{
	if (what&eSocketNotifier::Write && outbuf.size() )
	{
		queue_data d = outbuf.front();
		int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
		if (wr < 0)
			eDebug("eConsoleAppContainer write failed (%m)");
		else
			d.dataSent += wr;
		if (d.dataSent == d.len)
		{
			outbuf.pop();
			delete [] d.data;
			/* emit */ dataSent(0);
		}
	}
	if ( !outbuf.size() )
		out->stop();
}