summaryrefslogtreecommitdiff
path: root/gtk2_ardour/system_exec.cc
blob: 1a8399d22ebfc49b25c7164259c235d70310c8fc (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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
    Copyright (C) 2010 Paul Davis
    Copyright 2005-2008 Lennart Poettering
    Author: Robin Gareus <robin@gareus.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <assert.h>

#ifndef COMPILER_MSVC
#include <dirent.h>
#endif

#ifdef PLATFORM_WINDOWS
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif


#include "system_exec.h"

using namespace std;
void * interposer_thread (void *arg);

static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }

#ifndef PLATFORM_WINDOWS
/*
 * This function was part of libasyncns.
 * LGPL v2.1
 * Copyright 2005-2008 Lennart Poettering
 */
static int close_allv(const int except_fds[]) {
	struct rlimit rl;
	int fd;

#ifdef __linux__

	DIR *d;

	assert(except_fds);

	if ((d = opendir("/proc/self/fd"))) {
		struct dirent *de;

		while ((de = readdir(d))) {
			int found;
			long l;
			char *e = NULL;
			int i;

			if (de->d_name[0] == '.')
					continue;

			errno = 0;
			l = strtol(de->d_name, &e, 10);
			if (errno != 0 || !e || *e) {
				closedir(d);
				errno = EINVAL;
				return -1;
			}

			fd = (int) l;

			if ((long) fd != l) {
				closedir(d);
				errno = EINVAL;
				return -1;
			}

			if (fd < 3)
				continue;

			if (fd == dirfd(d))
				continue;

			found = 0;
			for (i = 0; except_fds[i] >= 0; i++)
				if (except_fds[i] == fd) {
						found = 1;
						break;
				}

			if (found) continue;

			if (close(fd) < 0) {
				int saved_errno;

				saved_errno = errno;
				closedir(d);
				errno = saved_errno;

				return -1;
			}
		}

		closedir(d);
		return 0;
	}

#endif

	if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
		return -1;

	for (fd = 0; fd < (int) rl.rlim_max; fd++) {
		int i;

		if (fd <= 3)
				continue;

		for (i = 0; except_fds[i] >= 0; i++)
			if (except_fds[i] == fd)
				continue;

		if (close(fd) < 0 && errno != EBADF)
			return -1;
	}

	return 0;
}
#endif /* not on windows */


SystemExec::SystemExec (std::string c, std::string a)
	: cmd(c)
{
	pthread_mutex_init(&write_lock, NULL);
	thread_active=false;
	pid = 0;
	pin[1] = -1;
	nicelevel = 0;
	envp = NULL;
	argp = NULL;
#ifdef PLATFORM_WINDOWS
	stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
	stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
	stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
#endif
	make_envp();
	make_argp(a);
}

SystemExec::SystemExec (std::string c, char **a)
	: cmd(c) , argp(a)
{
	pthread_mutex_init(&write_lock, NULL);
	thread_active=false;
	pid = 0;
	pin[1] = -1;
	nicelevel = 0;
	envp = NULL;
#ifdef PLATFORM_WINDOWS
	stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
	stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
	stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
	make_wargs(a);
#endif
	make_envp();
}

SystemExec::~SystemExec ()
{
	terminate ();
	if (envp) {
		for (int i=0;envp[i];++i) {
		  free(envp[i]);
		}
		free (envp);
	}
	if (argp) {
		for (int i=0;argp[i];++i) {
		  free(argp[i]);
		}
		free (argp);
	}
#ifdef PLATFORM_WINDOWS
	if (w_args) free(w_args);
#endif
	pthread_mutex_destroy(&write_lock);
}

void *
interposer_thread (void *arg) {
	SystemExec *sex = static_cast<SystemExec *>(arg);
	sex->output_interposer();
	pthread_exit(0);
	return 0;
}

#ifdef PLATFORM_WINDOWS /* Windows Process */

/* HELPER FUNCTIONS */

static void create_pipe (HANDLE *pipe, bool in) {
	SECURITY_ATTRIBUTES secAtt = { sizeof( SECURITY_ATTRIBUTES ), NULL, TRUE };
	HANDLE tmpHandle;
	if (in) {
		if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024)) return;
		if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
	} else {
		if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024)) return;
		if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
	}
	CloseHandle(tmpHandle);
}

static void destroy_pipe (HANDLE pipe[2]) {
	if (pipe[0] != INVALID_HANDLE_VALUE) {
		CloseHandle(pipe[0]);
		pipe[0] = INVALID_HANDLE_VALUE;
	}
	if (pipe[1] != INVALID_HANDLE_VALUE) {
		CloseHandle(pipe[1]);
		pipe[1] = INVALID_HANDLE_VALUE;
	}
}

static BOOL CALLBACK my_terminateApp(HWND hwnd, LPARAM procId)
{
	DWORD currentProcId = 0;
	GetWindowThreadProcessId(hwnd, &currentProcId);
	if (currentProcId == (DWORD)procId)
		PostMessage(hwnd, WM_CLOSE, 0, 0);
	return TRUE;
}

/* PROCESS API */

void
SystemExec::make_envp() {
	;/* environemt is copied over with CreateProcess(...,env=0 ,..) */
}

void
SystemExec::make_wargs(char **a) {
	std::string wa = cmd;
	if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
	std::replace(cmd.begin(), cmd.end(), '/', '\\' );
	char **tmp = a;
	while (tmp && *tmp) {
		wa.append(" \"");
		wa.append(*tmp);
		wa.append("\"");
		tmp++;
	}
	w_args = strdup(wa.c_str());
}

void
SystemExec::make_argp(std::string args) {
	std::string wa = cmd;
	if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
	std::replace(cmd.begin(), cmd.end(), '/', '\\' );
	wa.append(" ");
	wa.append(args);
	w_args = strdup(wa.c_str());
}

void
SystemExec::terminate ()
{
	::pthread_mutex_lock(&write_lock);
	if (pid) {
		/* terminate */
		EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
		PostThreadMessage(pid->dwThreadId, WM_CLOSE, 0, 0);

		/* kill ! */
		TerminateProcess(pid->hProcess, 0xf291);

		CloseHandle(pid->hThread);
		CloseHandle(pid->hProcess);
		destroy_pipe(stdinP);
		destroy_pipe(stdoutP);
		destroy_pipe(stderrP);
		delete pid;
		pid=0;
	}
	::pthread_mutex_unlock(&write_lock);
}

int
SystemExec::wait (int options)
{
	while (is_running()) {
		WaitForSingleObject(pid->hProcess, INFINITE);
		Sleep(20);
	}
	return 0;
}

bool
SystemExec::is_running ()
{
	return pid?true:false;
}

int
SystemExec::start (int stderr_mode)
{
	char* working_dir = 0;

	if (pid) { return 0; }

	pid = new PROCESS_INFORMATION;
	memset(pid, 0, sizeof(PROCESS_INFORMATION));

	create_pipe(stdinP, true);
	create_pipe(stdoutP, false);

	if (stderr_mode == 2) {
	/* merge stout & stderr */
		DuplicateHandle(GetCurrentProcess(), stdoutP[1], GetCurrentProcess(), &stderrP[1], 0, TRUE, DUPLICATE_SAME_ACCESS);
	} else if (stderr_mode == 1) {
		//TODO read/flush this pipe or close it...
		create_pipe(stderrP, false);
	} else {
		//TODO: keep stderr of this process mode.
	}

	bool success = false;
	STARTUPINFOA startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0,
		(unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
		(unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
		0, 0, 0,
		STARTF_USESTDHANDLES,
		0, 0, 0,
		stdinP[0], stdoutP[1], stderrP[1]
	};

	success = CreateProcess(0, w_args,
		0, 0, /* bInheritHandles = */ TRUE,
		(CREATE_NO_WINDOW&0) | CREATE_UNICODE_ENVIRONMENT | (0&CREATE_NEW_CONSOLE),
		/*env = */ 0,
		working_dir,
		&startupInfo, pid);

	if (stdinP[0] != INVALID_HANDLE_VALUE) {
		CloseHandle(stdinP[0]);
		stdinP[0] = INVALID_HANDLE_VALUE;
	}
	if (stdoutP[1] != INVALID_HANDLE_VALUE) {
		CloseHandle(stdoutP[1]);
		stdoutP[1] = INVALID_HANDLE_VALUE;
	}
	if (stderrP[1] != INVALID_HANDLE_VALUE) {
		CloseHandle(stderrP[1]);
		stderrP[1] = INVALID_HANDLE_VALUE;
	}

	if (!success) {
		CloseHandle(pid->hThread);
		CloseHandle(pid->hProcess);
		destroy_pipe(stdinP);
		destroy_pipe(stdoutP);
		destroy_pipe(stderrP);
		delete pid;
		pid=0;
		return -1;
	}

	int rv = pthread_create(&thread_id_tt, NULL, interposer_thread, this);
	thread_active=true;
	if (rv) {
		thread_active=false;
		terminate();
		return -2;
	}
	Sleep(20);
	return 0;
}

void
SystemExec::output_interposer()
{
	DWORD bytesRead = 0;
	char data[BUFSIZ];
#if 0 // untested code to set up nonblocking
	unsigned long l = 1;
	ioctlsocket(stdoutP[0], FIONBIO, &l);
#endif
	while(1) {
#if 0 // for non-blocking pipes..
		DWORD bytesAvail = 0;
		PeekNamedPipe(stdoutP[0], 0, 0, 0, &bytesAvail, 0);
		if (bytesAvail < 1) {Sleep(500); printf("N/A\n"); continue;}
#endif
		if (stdoutP[0] == INVALID_HANDLE_VALUE) break;
		if (!ReadFile(stdoutP[0], data, BUFSIZ, &bytesRead, 0)) break;
		if (bytesRead < 1) continue; /* actually not needed; but this is safe. */
		data[bytesRead] = 0;
		ReadStdout(data, bytesRead);/* EMIT SIGNAL */
	}
	Terminated();/* EMIT SIGNAL */
}

void
SystemExec::close_stdin()
{
	if (stdinP[0]!= INVALID_HANDLE_VALUE)  FlushFileBuffers(stdinP[0]);
	if (stdinP[1]!= INVALID_HANDLE_VALUE)  FlushFileBuffers(stdinP[1]);
	Sleep(200);
	destroy_pipe(stdinP);
}

int
SystemExec::write_to_stdin(std::string d, size_t len)
{
	const char *data;
	DWORD r,c;

	::pthread_mutex_lock(&write_lock);

	data=d.c_str();
	if (len == 0) {
		len=(d.length());
	}
	c=0;
	while (c < len) {
		if (!WriteFile(stdinP[1], data+c, len-c, &r, NULL)) {
			if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
				Sleep(100);
				continue;
			} else {
				fprintf(stderr, "SYSTEM-EXEC: stdin write error.\n");
				break;
			}
		}
		c += r;
	}
	::pthread_mutex_unlock(&write_lock);
	return c;
}


/* end windows process */
#else
/* UNIX/POSIX process */

extern char **environ;
void
SystemExec::make_envp() {
	int i=0;
	envp = (char **) calloc(1, sizeof(char*));
	/* copy current environment */
	for (i=0;environ[i];++i) {
	  envp[i] = strdup(environ[i]);
	  envp = (char **) realloc(envp, (i+2) * sizeof(char*));
	}
	envp[i] = 0;
}

void
SystemExec::make_argp(std::string args) {
	int argn = 1;
	char *cp1;
	char *cp2;

	char *carg = strdup(args.c_str());

	argp = (char **) malloc((argn + 1) * sizeof(char *));
	if (argp == (char **) 0) {
		free(carg);
		return; // FATAL
	}

	argp[0] = strdup(cmd.c_str());

	/* TODO: quotations and escapes
	 * http://stackoverflow.com/questions/1511797/convert-string-to-argv-in-c
	 *
	 * It's actually not needed. All relevant invocations specify 'argp' directly.
	 * Only 'xjadeo -L -R' uses this function and that uses neither quotations
	 * nor arguments with white-space.
	 */
	for (cp1 = cp2 = carg; *cp2 != '\0'; ++cp2) {
		if (*cp2 == ' ') {
			*cp2 = '\0';
			argp[argn++] = strdup(cp1);
			cp1 = cp2 + 1;
	    argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
		}
	}
	if (cp2 != cp1) {
		argp[argn++] = strdup(cp1);
		argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
	}
	argp[argn] = (char *) 0;
	free(carg);
}



void
SystemExec::terminate ()
{
	::pthread_mutex_lock(&write_lock);

	/* close stdin in an attempt to get the child to exit cleanly.
	 */

	close_stdin();

	if (pid) {
		::usleep(50000);
		sched_yield();
		wait(WNOHANG);
	}

	/* if pid is non-zero, the child task is still executing (i.e. it did
	 * not exit in response to stdin being closed). try to kill it.
	 */
	
	if (pid) {
		::kill(pid, SIGTERM);
		::usleep(50000);
		sched_yield();
		wait(WNOHANG);
	}

	/* if pid is non-zero, the child task is STILL executing after being
	 * sent SIGTERM. Act tough ... send SIGKILL
	 */

	if (pid) {
		::fprintf(stderr, "Process is still running! trying SIGKILL\n");
		::kill(pid, SIGKILL);
	}

	wait();
	if (thread_active) pthread_join(thread_id_tt, NULL);
	thread_active = false;
	::pthread_mutex_unlock(&write_lock);
}

int
SystemExec::wait (int options)
{
	int status=0;
	int ret;

	if (pid==0) return -1;

	ret = waitpid (pid, &status, options);

	if (ret == pid) {
		if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
			pid=0;
		}
	} else {
		if (ret != 0) {
			if (errno == ECHILD) {
				/* no currently running children, reset pid */
				pid=0;
			}
		} /* else the process is still running */
	}
	return status;
}

bool
SystemExec::is_running ()
{
	int status=0;
	if (pid==0) return false;
	if (::waitpid(pid, &status, WNOHANG)==0) return true;
	return false;
}

int
SystemExec::start (int stderr_mode)
{
	if (is_running()) {
		return 0; // mmh what to return here?
	}
	int r;

	if (::pipe(pin) < 0 || ::pipe(pout) < 0 || ::pipe(pok) < 0) {
		/* Something unexpected went wrong creating a pipe. */
		return -1;
	}

	r = ::fork();
	if (r < 0) {
		/* failed to fork */
		return -2;
	}

	if (r > 0) {
		/* main */
		pid=r;

		/* check if execve was successful. */
		close_fd(pok[1]);
		char buf;
		for ( ;; ) {
			ssize_t n = ::read(pok[0], &buf, 1 );
			if ( n==1 ) {
				/* child process returned from execve */
				pid=0;
				close_fd(pok[0]);
				close_fd(pin[1]);
				close_fd(pin[0]);
				close_fd(pout[1]);
				close_fd(pout[0]);
				pin[1] = -1;
				return -3;
			} else if ( n==-1 ) {
				 if ( errno==EAGAIN || errno==EINTR )
					 continue;
			}
			break;
		}
		close_fd(pok[0]);
		/* child started successfully */

#if 0
/* use fork for output-interposer
 * it will run in a separated process
 */
		/* catch stdout thread */
		r = ::fork();
		if (r < 0) {
			// failed to fork
			terminate();
			return -2;
		}
		if (r == 0) {
			/* 2nd child process - catch stdout */
			close_fd(pin[1]);
			close_fd(pout[1]);
			output_interposer();
			exit(0);
		}
		close_fd(pout[1]);
		close_fd(pin[0]);
		close_fd(pout[0]);
#else /* use pthread */
		close_fd(pout[1]);
		close_fd(pin[0]);
		int rv = pthread_create(&thread_id_tt, NULL, interposer_thread, this);

		thread_active=true;
		if (rv) {
			thread_active=false;
			terminate();
			return -2;
		}
#endif
		return 0; /* all systems go - return to main */
	}

	/* child process - exec external process */
	close_fd(pok[0]);
	::fcntl(pok[1], F_SETFD, FD_CLOEXEC);

	close_fd(pin[1]);
	if (pin[0] != STDIN_FILENO) {
	  ::dup2(pin[0], STDIN_FILENO);
	}
	close_fd(pin[0]);
	close_fd(pout[0]);
	if (pout[1] != STDOUT_FILENO) {
		::dup2(pout[1], STDOUT_FILENO);
	}

	if (stderr_mode == 2) {
		/* merge STDERR into output */
		if (pout[1] != STDERR_FILENO) {
			::dup2(pout[1], STDERR_FILENO);
		}
	} else if (stderr_mode == 1) {
		/* ignore STDERR */
		::close(STDERR_FILENO);
	} else {
		/* keep STDERR */
	}

	if (pout[1] != STDOUT_FILENO && pout[1] != STDERR_FILENO) {
		close_fd(pout[1]);
	}

	if (nicelevel !=0) {
		::nice(nicelevel);
	}

#if 0
	/* chdir to executable dir */
	char *directory;
	directory = strdup(cmd.c_str());
	if (strrchr(directory, '/') != (char *) 0) {
		::chdir(directory);
	}
	free(directory);
#endif

#ifdef HAVE_SIGSET
	sigset(SIGPIPE, SIG_DFL);
#else
	signal(SIGPIPE, SIG_DFL);
#endif

	int good_fds[1] = { -1 };
	close_allv(good_fds);

	::execve(argp[0], argp, envp);
	/* if we reach here something went wrong.. */
	char buf = 0;
	(void) ::write(pok[1], &buf, 1 );
	close_fd(pok[1]);
	exit(-1);
	return -1;
}

void
SystemExec::output_interposer()
{
	int rfd=pout[0];
	char buf[BUFSIZ];
	ssize_t r;
	unsigned long l = 1;

	ioctl(rfd, FIONBIO, &l); // set non-blocking I/O

	for (;fcntl(rfd, F_GETFL)!=-1;) {
		r = read(rfd, buf, sizeof(buf));
		if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
			::usleep(1000);
			continue;
		}
		if (r <= 0) {
			break;
		}
		buf[r]=0;
		std::string rv = std::string(buf,r); // TODO: check allocation strategy
		ReadStdout(rv, r);/* EMIT SIGNAL */
	}
	Terminated();/* EMIT SIGNAL */
}

void
SystemExec::close_stdin()
{
	if (pin[1]<0) return;
	close_fd(pin[0]);
	close_fd(pin[1]);
	close_fd(pout[0]);
	close_fd(pout[1]);
}

int
SystemExec::write_to_stdin(std::string d, size_t len)
{
	const char *data;
	ssize_t r;
	size_t c;
	::pthread_mutex_lock(&write_lock);

	data=d.c_str();
	if (len == 0) {
		len=(d.length());
	}
	c=0;
	while (c < len) {
		for (;;) {
			r=::write(pin[1], data+c, len-c);
			if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
				sleep(1);
				continue;
			}
			if ((size_t) r != (len-c)) {
				::pthread_mutex_unlock(&write_lock);
				return c;
			}
			break;
		}
		c += r;
	}
	fsync(pin[1]);
	::pthread_mutex_unlock(&write_lock);
	return c;
}

#endif // end UNIX process