aboutsummaryrefslogtreecommitdiff
path: root/lib/base/console.cpp
blob: 830855fc9433df0f3604becebea4e1a1f5661493 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#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;
		filefd[i]=-1;
	}
}

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 argc = 3;
	const char *argv[argc + 1];
	argv[0] = "/bin/sh";
	argv[1] = "-c";
	argv[2] = cmd;
	argv[argc] = NULL;

	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;
}

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;

	for (int i=0; i < 3; ++i)
	{
		if ( filefd[i] > 0 )
			close(filefd[i]);
	}
}

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::sendEOF()
{
	if (out)
		out->stop();
	if (fd[1] != -1)
	{
		::close(fd[1]);
		fd[1]=-1;
	}
}

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);
			stdoutAvail(buf);
			if ( filefd[1] > 0 )
				::write(filefd[1], buf, rd);
			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);
			stderrAvail(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::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;
			if ( filefd[0] == -1 )
			/* emit */ dataSent(0);
		}
	}
	if ( !outbuf.size() )
	{
		if ( filefd[0] > 0 )
		{
			char readbuf[32*1024];
			int rsize = read(filefd[0], readbuf, 32*1024);
			if ( rsize > 0 )
				write(readbuf, rsize);
			else
			{
				close(filefd[0]);
				filefd[0] = -1;
				::close(fd[1]);
				eDebug("readFromFile done - closing eConsoleAppContainer stdin pipe");
				fd[1]=-1;
				dataSent(0);
				out->stop();
			}
		}
		else
			out->stop();
	}
}

#include "structmember.h"

extern "C" {

struct eConsolePy
{
	PyObject_HEAD
	eConsoleAppContainer *cont;
	PyObject *in_weakreflist; /* List of weak references */
};

static PyObject *
eConsolePy_dataAvail(eConsolePy *self, void *closure)
{
	return self->cont->dataAvail.get();
}

static PyObject *
eConsolePy_stdoutAvail(eConsolePy *self, void *closure)
{
	return self->cont->stdoutAvail.get();
}

static PyObject *
eConsolePy_stderrAvail(eConsolePy *self, void *closure)
{
	return self->cont->stderrAvail.get();
}

static PyObject *
eConsolePy_dataSent(eConsolePy *self, void *closure)
{
	return self->cont->dataSent.get();
}

static PyObject *
eConsolePy_appClosed(eConsolePy *self, void *closure)
{
	return self->cont->appClosed.get();
}

static PyGetSetDef eConsolePy_getseters[] = {
	{"dataAvail",
	 (getter)eConsolePy_dataAvail, (setter)0,
	 "dataAvail callback list",
	 NULL},
	{"stdoutAvail",
	 (getter)eConsolePy_stdoutAvail, (setter)0,
	 "stdoutAvail callback list",
	 NULL},
	{"stderrAvail",
	 (getter)eConsolePy_stderrAvail, (setter)0,
	 "stderrAvail callback list",
	 NULL},
	{"dataSent",
	 (getter)eConsolePy_dataSent, (setter)0,
	 "dataSent callback list",
	 NULL},
	{"appClosed",
	 (getter)eConsolePy_appClosed, (setter)0,
	 "appClosed callback list",
	 NULL},
	{NULL} /* Sentinel */
};

static int
eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg)
{
	PyObject *obj = self->cont->dataAvail.get(true);
	if (obj) {
		Py_VISIT(obj);
	}
	obj = self->cont->stdoutAvail.get(true);
	if (obj) {
		Py_VISIT(obj);
	}
	obj = self->cont->stderrAvail.get(true);
	if (obj) {
		Py_VISIT(obj);
	}
	obj = self->cont->dataSent.get(true);
	if (obj) {
		Py_VISIT(obj);
	}
	obj = self->cont->appClosed.get(true);
	if (obj) {
		Py_VISIT(obj);
	}
	return 0;
}

static int
eConsolePy_clear(eConsolePy *self)
{
	PyObject *obj = self->cont->dataAvail.get(true);
	if (obj) {
		Py_CLEAR(obj);
	}
	obj = self->cont->stdoutAvail.get(true);
	if (obj) {
		Py_CLEAR(obj);
	}
	obj = self->cont->stderrAvail.get(true);
	if (obj) {
		Py_CLEAR(obj);
	}
	obj = self->cont->dataSent.get(true);
	if (obj) {
		Py_CLEAR(obj);
	}
	obj = self->cont->appClosed.get(true);
	if (obj) {
		Py_CLEAR(obj);
	}
	return 0;
}

static void
eConsolePy_dealloc(eConsolePy* self)
{
	if (self->in_weakreflist != NULL)
		PyObject_ClearWeakRefs((PyObject *) self);
	eConsolePy_clear(self);
	delete self->cont;
	self->ob_type->tp_free((PyObject*)self);
}

static PyObject *
eConsolePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	eConsolePy *self = (eConsolePy *)type->tp_alloc(type, 0);
	self->cont = new eConsoleAppContainer();
	self->in_weakreflist = NULL;
	return (PyObject *)self;
}

static PyObject *
eConsolePy_running(eConsolePy* self)
{
	PyObject *ret = NULL;
	ret = self->cont->running() ? Py_True : Py_False;
	Org_Py_INCREF(ret);
	return ret;
}

static PyObject *
eConsolePy_execute(eConsolePy* self, PyObject *argt)
{
	const char *str;
	if (PyArg_ParseTuple(argt, "s", &str))
		return PyInt_FromLong(self->cont->execute(str));
	PyErr_SetString(PyExc_TypeError,
		"argument is not a string");
	return NULL;
}

static PyObject *
eConsolePy_write(eConsolePy* self, PyObject *args)
{
	int len;
	char *data;
	if (PyArg_ParseTuple(args, "si", &data, &len))
		;
	else
	{
		PyObject *ob;
		if (!PyArg_ParseTuple(args, "O", &ob) || !PyString_Check(ob))
		{
			PyErr_SetString(PyExc_TypeError,
				"1st arg must be a string, optionaly 2nd arg can be the string length");
			return NULL;
		}
		else
		{
			Py_ssize_t length;
			if (!PyString_AsStringAndSize(ob, &data, &length))
				len = length;
			else
				len = 0;
		}
	}
	self->cont->write(data, len);
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_getPID(eConsolePy* self)
{
	return PyInt_FromLong(self->cont->getPID());
}

static PyObject *
eConsolePy_setCWD(eConsolePy* self, PyObject *args)
{
	const char *path=0;
	if (!PyArg_ParseTuple(args, "s", &path))
		return NULL;
	self->cont->setCWD(path);
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_kill(eConsolePy* self)
{
	self->cont->kill();
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_sendCtrlC(eConsolePy* self)
{
	self->cont->sendCtrlC();
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_sendEOF(eConsolePy* self)
{
	self->cont->sendEOF();
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_dumpToFile(eConsolePy* self, PyObject *args)
{
	char *filename;
	if (!PyArg_ParseTuple(args, "s", &filename))
	{
		PyErr_SetString(PyExc_TypeError,
			"arg must be a string (filename)");
		return NULL;
	}
	else
	{
		int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
		self->cont->setFileFD(1, fd);
		eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%d", filename, fd);
	}
	Py_RETURN_NONE;
}

static PyObject *
eConsolePy_readFromFile(eConsolePy* self, PyObject *args)
{
	char *filename;
	if (!PyArg_ParseTuple(args, "s", &filename))
	{
		PyErr_SetString(PyExc_TypeError,
			"arg must be a string (filename)");
		return NULL;
	}
	else
	{
		int fd = open(filename, O_RDONLY);
		if (fd >= 0)
		{
			char readbuf[32*1024];
			int rsize = read(fd, readbuf, 32*1024);
			self->cont->setFileFD(0, fd);
			eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%d, read: %d", filename, fd, rsize);
			self->cont->write(readbuf, rsize);
		}
		else
		{
			eDebug("eConsoleAppContainer::readFromFile %s not exist!", filename);
			self->cont->setFileFD(0, -1);
		}
	}
	Py_RETURN_NONE;
}

static PyMethodDef eConsolePy_methods[] = {
	{"setCWD", (PyCFunction)eConsolePy_setCWD, METH_VARARGS,
	 "set working dir"
	},
	{"execute", (PyCFunction)eConsolePy_execute, METH_VARARGS,
	 "execute command"
	},
	{"dumpToFile", (PyCFunction)eConsolePy_dumpToFile, METH_VARARGS,
	 "set output file"
	},
	{"readFromFile", (PyCFunction)eConsolePy_readFromFile, METH_VARARGS,
	 "set input file"
	},
	{"getPID", (PyCFunction)eConsolePy_getPID, METH_NOARGS,
	 "execute command"
	},
	{"kill", (PyCFunction)eConsolePy_kill, METH_NOARGS,
	 "kill application"
	},
	{"sendCtrlC", (PyCFunction)eConsolePy_sendCtrlC, METH_NOARGS,
	 "send Ctrl-C to application"
	},
	{"sendEOF", (PyCFunction)eConsolePy_sendEOF, METH_NOARGS,
	 "send EOF to application"
	},
	{"write", (PyCFunction)eConsolePy_write, METH_VARARGS,
	 "write data to application"
	},
	{"running", (PyCFunction)eConsolePy_running, METH_NOARGS,
	 "returns the running state"
	},
	{NULL}  /* Sentinel */
};

static PyTypeObject eConsolePyType = {
	PyObject_HEAD_INIT(NULL)
	0, /*ob_size*/
	"eConsoleImpl.eConsoleAppContainer", /*tp_name*/
	sizeof(eConsolePy), /*tp_basicsize*/
	0, /*tp_itemsize*/
	(destructor)eConsolePy_dealloc, /*tp_dealloc*/
	0, /*tp_print*/
	0, /*tp_getattr*/
	0, /*tp_setattr*/
	0, /*tp_compare*/
	0, /*tp_repr*/
	0, /*tp_as_number*/
	0, /*tp_as_sequence*/
	0, /*tp_as_mapping*/
	0, /*tp_hash */
	0, /*tp_call*/
	0, /*tp_str*/
	0, /*tp_getattro*/
	0, /*tp_setattro*/
	0, /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
	"eConsoleAppContainer objects", /* tp_doc */
	(traverseproc)eConsolePy_traverse, /* tp_traverse */
	(inquiry)eConsolePy_clear, /* tp_clear */
	0, /* tp_richcompare */
	offsetof(eConsolePy, in_weakreflist), /* tp_weaklistoffset */
	0, /* tp_iter */
	0, /* tp_iternext */
	eConsolePy_methods, /* tp_methods */
	0, /* tp_members */
	eConsolePy_getseters, /* tp_getset */
	0, /* tp_base */
	0, /* tp_dict */
	0, /* tp_descr_get */
	0, /* tp_descr_set */
	0, /* tp_dictoffset */
	0, /* tp_init */
	0, /* tp_alloc */
	eConsolePy_new, /* tp_new */
};

static PyMethodDef module_methods[] = {
	{NULL}  /* Sentinel */
};

void eConsoleInit(void)
{
	PyObject* m = Py_InitModule3("eConsoleImpl", module_methods,
		"Module that implements eConsoleAppContainer with working cyclic garbage collection.");

	if (m == NULL)
		return;

	if (!PyType_Ready(&eConsolePyType))
	{
		Org_Py_INCREF((PyObject*)&eConsolePyType);
		PyModule_AddObject(m, "eConsoleAppContainer", (PyObject*)&eConsolePyType);
	}
}
}