incomplete merge of master into windows (requires upcoming changes to master to be...
[ardour.git] / gtk2_ardour / system_exec.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Copyright 2005-2008 Lennart Poettering
4     Author: Robin Gareus <robin@gareus.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include <assert.h>
28 #include <dirent.h>
29
30 #ifdef __WIN32__
31 #include <windows.h>
32 #else
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 #endif
41
42
43 #include "system_exec.h"
44
45 using namespace std;
46 void * interposer_thread (void *arg);
47
48 static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }
49
50 #ifndef __WIN32__
51 /*
52  * This function was part of libasyncns.
53  * LGPL v2.1
54  * Copyright 2005-2008 Lennart Poettering
55  */
56 static int close_allv(const int except_fds[]) {
57         struct rlimit rl;
58         int fd;
59
60 #ifdef __linux__
61
62         DIR *d;
63
64         assert(except_fds);
65
66         if ((d = opendir("/proc/self/fd"))) {
67                 struct dirent *de;
68
69                 while ((de = readdir(d))) {
70                         int found;
71                         long l;
72                         char *e = NULL;
73                         int i;
74
75                         if (de->d_name[0] == '.')
76                                         continue;
77
78                         errno = 0;
79                         l = strtol(de->d_name, &e, 10);
80                         if (errno != 0 || !e || *e) {
81                                 closedir(d);
82                                 errno = EINVAL;
83                                 return -1;
84                         }
85
86                         fd = (int) l;
87
88                         if ((long) fd != l) {
89                                 closedir(d);
90                                 errno = EINVAL;
91                                 return -1;
92                         }
93
94                         if (fd < 3)
95                                 continue;
96
97                         if (fd == dirfd(d))
98                                 continue;
99
100                         found = 0;
101                         for (i = 0; except_fds[i] >= 0; i++)
102                                 if (except_fds[i] == fd) {
103                                                 found = 1;
104                                                 break;
105                                 }
106
107                         if (found) continue;
108
109                         if (close(fd) < 0) {
110                                 int saved_errno;
111
112                                 saved_errno = errno;
113                                 closedir(d);
114                                 errno = saved_errno;
115
116                                 return -1;
117                         }
118                 }
119
120                 closedir(d);
121                 return 0;
122         }
123
124 #endif
125
126         if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
127                 return -1;
128
129         for (fd = 0; fd < (int) rl.rlim_max; fd++) {
130                 int i;
131
132                 if (fd <= 3)
133                                 continue;
134
135                 for (i = 0; except_fds[i] >= 0; i++)
136                         if (except_fds[i] == fd)
137                                 continue;
138
139                 if (close(fd) < 0 && errno != EBADF)
140                         return -1;
141         }
142
143         return 0;
144 }
145 #endif /* not on windows */
146
147
148 SystemExec::SystemExec (std::string c, std::string a)
149         : cmd(c)
150 {
151         pthread_mutex_init(&write_lock, NULL);
152         thread_active=false;
153         pid = 0;
154         pin[1] = -1;
155         nicelevel = 0;
156         envp = NULL;
157         argp = NULL;
158 #ifdef __WIN32__
159         stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
160         stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
161         stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
162 #endif
163         make_envp();
164         make_argp(a);
165 }
166
167 SystemExec::SystemExec (std::string c, char **a)
168         : cmd(c) , argp(a)
169 {
170         pthread_mutex_init(&write_lock, NULL);
171         thread_active=false;
172         pid = 0;
173         pin[1] = -1;
174         nicelevel = 0;
175         envp = NULL;
176 #ifdef __WIN32__
177         stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
178         stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
179         stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
180         make_wargs(a);
181 #endif
182         make_envp();
183 }
184
185 SystemExec::~SystemExec ()
186 {
187         terminate ();
188         if (envp) {
189                 for (int i=0;envp[i];++i) {
190                   free(envp[i]);
191                 }
192                 free (envp);
193         }
194         if (argp) {
195                 for (int i=0;argp[i];++i) {
196                   free(argp[i]);
197                 }
198                 free (argp);
199         }
200 #ifdef __WIN32__
201         if (w_args) free(w_args);
202 #endif
203         pthread_mutex_destroy(&write_lock);
204 }
205
206 void *
207 interposer_thread (void *arg) {
208         SystemExec *sex = static_cast<SystemExec *>(arg);
209         sex->output_interposer();
210         pthread_exit(0);
211         return 0;
212 }
213
214 #ifdef __WIN32__ /* Windows Process */
215
216 /* HELPER FUNCTIONS */
217
218 static void create_pipe (HANDLE *pipe, bool in) {
219         SECURITY_ATTRIBUTES secAtt = { sizeof( SECURITY_ATTRIBUTES ), NULL, TRUE };
220         HANDLE tmpHandle;
221         if (in) {
222                 if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024)) return;
223                 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
224         } else {
225                 if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024)) return;
226                 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
227         }
228         CloseHandle(tmpHandle);
229 }
230
231 static void destroy_pipe (HANDLE pipe[2]) {
232         if (pipe[0] != INVALID_HANDLE_VALUE) {
233                 CloseHandle(pipe[0]);
234                 pipe[0] = INVALID_HANDLE_VALUE;
235         }
236         if (pipe[1] != INVALID_HANDLE_VALUE) {
237                 CloseHandle(pipe[1]);
238                 pipe[1] = INVALID_HANDLE_VALUE;
239         }
240 }
241
242 static BOOL CALLBACK my_terminateApp(HWND hwnd, LPARAM procId)
243 {
244         DWORD currentProcId = 0;
245         GetWindowThreadProcessId(hwnd, &currentProcId);
246         if (currentProcId == (DWORD)procId)
247                 PostMessage(hwnd, WM_CLOSE, 0, 0);
248         return TRUE;
249 }
250
251 /* PROCESS API */
252
253 void
254 SystemExec::make_envp() {
255         ;/* environemt is copied over with CreateProcess(...,env=0 ,..) */
256 }
257
258 void
259 SystemExec::make_wargs(char **a) {
260         std::string wa = cmd;
261         if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
262         std::replace(cmd.begin(), cmd.end(), '/', '\\' );
263         char **tmp = a;
264         while (tmp && *tmp) {
265                 wa.append(" \"");
266                 wa.append(*tmp);
267                 wa.append("\"");
268                 tmp++;
269         }
270         w_args = strdup(wa.c_str());
271 }
272
273 void
274 SystemExec::make_argp(std::string args) {
275         std::string wa = cmd;
276         if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
277         std::replace(cmd.begin(), cmd.end(), '/', '\\' );
278         wa.append(" ");
279         wa.append(args);
280         w_args = strdup(wa.c_str());
281 }
282
283 void
284 SystemExec::terminate ()
285 {
286         ::pthread_mutex_lock(&write_lock);
287         if (pid) {
288                 /* terminate */
289                 EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
290                 PostThreadMessage(pid->dwThreadId, WM_CLOSE, 0, 0);
291
292                 /* kill ! */
293                 TerminateProcess(pid->hProcess, 0xf291);
294
295                 CloseHandle(pid->hThread);
296                 CloseHandle(pid->hProcess);
297                 destroy_pipe(stdinP);
298                 destroy_pipe(stdoutP);
299                 destroy_pipe(stderrP);
300                 delete pid;
301                 pid=0;
302         }
303         ::pthread_mutex_unlock(&write_lock);
304 }
305
306 int
307 SystemExec::wait (int options)
308 {
309         while (is_running()) {
310                 WaitForSingleObject(pid->hProcess, INFINITE);
311                 Sleep(20);
312         }
313         return 0;
314 }
315
316 bool
317 SystemExec::is_running ()
318 {
319         return pid?true:false;
320 }
321
322 int
323 SystemExec::start (int stderr_mode)
324 {
325         char* working_dir = 0;
326
327         if (pid) { return 0; }
328
329         pid = new PROCESS_INFORMATION;
330         memset(pid, 0, sizeof(PROCESS_INFORMATION));
331
332         create_pipe(stdinP, true);
333         create_pipe(stdoutP, false);
334
335         if (stderr_mode == 2) {
336         /* merge stout & stderr */
337                 DuplicateHandle(GetCurrentProcess(), stdoutP[1], GetCurrentProcess(), &stderrP[1], 0, TRUE, DUPLICATE_SAME_ACCESS);
338         } else if (stderr_mode == 1) {
339                 //TODO read/flush this pipe or close it...
340                 create_pipe(stderrP, false);
341         } else {
342                 //TODO: keep stderr of this process mode.
343         }
344
345         bool success = false;
346         STARTUPINFOA startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0,
347                 (unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
348                 (unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
349                 0, 0, 0,
350                 STARTF_USESTDHANDLES,
351                 0, 0, 0,
352                 stdinP[0], stdoutP[1], stderrP[1]
353         };
354
355         success = CreateProcess(0, w_args,
356                 0, 0, /* bInheritHandles = */ TRUE,
357                 (CREATE_NO_WINDOW&0) | CREATE_UNICODE_ENVIRONMENT | (0&CREATE_NEW_CONSOLE),
358                 /*env = */ 0,
359                 working_dir,
360                 &startupInfo, pid);
361
362         if (stdinP[0] != INVALID_HANDLE_VALUE) {
363                 CloseHandle(stdinP[0]);
364                 stdinP[0] = INVALID_HANDLE_VALUE;
365         }
366         if (stdoutP[1] != INVALID_HANDLE_VALUE) {
367                 CloseHandle(stdoutP[1]);
368                 stdoutP[1] = INVALID_HANDLE_VALUE;
369         }
370         if (stderrP[1] != INVALID_HANDLE_VALUE) {
371                 CloseHandle(stderrP[1]);
372                 stderrP[1] = INVALID_HANDLE_VALUE;
373         }
374
375         if (!success) {
376                 CloseHandle(pid->hThread);
377                 CloseHandle(pid->hProcess);
378                 destroy_pipe(stdinP);
379                 destroy_pipe(stdoutP);
380                 destroy_pipe(stderrP);
381                 delete pid;
382                 pid=0;
383                 return -1;
384         }
385
386         int rv = pthread_create(&thread_id_tt, NULL, interposer_thread, this);
387         thread_active=true;
388         if (rv) {
389                 thread_active=false;
390                 terminate();
391                 return -2;
392         }
393         Sleep(20);
394         return 0;
395 }
396
397 void
398 SystemExec::output_interposer()
399 {
400         DWORD bytesRead = 0;
401         char data[BUFSIZ];
402 #if 0 // untested code to set up nonblocking
403         unsigned long l = 1;
404         ioctlsocket(stdoutP[0], FIONBIO, &l);
405 #endif
406         while(1) {
407 #if 0 // for non-blocking pipes..
408                 DWORD bytesAvail = 0;
409                 PeekNamedPipe(stdoutP[0], 0, 0, 0, &bytesAvail, 0);
410                 if (bytesAvail < 1) {Sleep(500); printf("N/A\n"); continue;}
411 #endif
412                 if (stdoutP[0] == INVALID_HANDLE_VALUE) break;
413                 if (!ReadFile(stdoutP[0], data, BUFSIZ, &bytesRead, 0)) break;
414                 if (bytesRead < 1) continue; /* actually not needed; but this is safe. */
415                 data[bytesRead] = 0;
416                 ReadStdout(data, bytesRead);/* EMIT SIGNAL */
417         }
418         Terminated();/* EMIT SIGNAL */
419 }
420
421 void
422 SystemExec::close_stdin()
423 {
424         if (stdinP[0]!= INVALID_HANDLE_VALUE)  FlushFileBuffers(stdinP[0]);
425         if (stdinP[1]!= INVALID_HANDLE_VALUE)  FlushFileBuffers(stdinP[1]);
426         Sleep(200);
427         destroy_pipe(stdinP);
428 }
429
430 int
431 SystemExec::write_to_stdin(std::string d, size_t len)
432 {
433         const char *data;
434         DWORD r,c;
435
436         ::pthread_mutex_lock(&write_lock);
437
438         data=d.c_str();
439         if (len == 0) {
440                 len=(d.length());
441         }
442         c=0;
443         while (c < len) {
444                 if (!WriteFile(stdinP[1], data+c, len-c, &r, NULL)) {
445                         if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
446                                 Sleep(100);
447                                 continue;
448                         } else {
449                                 fprintf(stderr, "SYSTEM-EXEC: stdin write error.\n");
450                                 break;
451                         }
452                 }
453                 c += r;
454         }
455         ::pthread_mutex_unlock(&write_lock);
456         return c;
457 }
458
459
460 /* end windows process */
461 #else
462 /* UNIX/POSIX process */
463
464 extern char **environ;
465 void
466 SystemExec::make_envp() {
467         int i=0;
468         envp = (char **) calloc(1, sizeof(char*));
469         /* copy current environment */
470         for (i=0;environ[i];++i) {
471           envp[i] = strdup(environ[i]);
472           envp = (char **) realloc(envp, (i+2) * sizeof(char*));
473         }
474         envp[i] = 0;
475 }
476
477 void
478 SystemExec::make_argp(std::string args) {
479         int argn = 1;
480         char *cp1;
481         char *cp2;
482
483         char *carg = strdup(args.c_str());
484
485         argp = (char **) malloc((argn + 1) * sizeof(char *));
486         if (argp == (char **) 0) {
487                 free(carg);
488                 return; // FATAL
489         }
490
491         argp[0] = strdup(cmd.c_str());
492
493         /* TODO: quotations and escapes
494          * http://stackoverflow.com/questions/1511797/convert-string-to-argv-in-c
495          *
496          * It's actually not needed. All relevant invocations specify 'argp' directly.
497          * Only 'xjadeo -L -R' uses this function and that uses neither quotations
498          * nor arguments with white-space.
499          */
500         for (cp1 = cp2 = carg; *cp2 != '\0'; ++cp2) {
501                 if (*cp2 == ' ') {
502                         *cp2 = '\0';
503                         argp[argn++] = strdup(cp1);
504                         cp1 = cp2 + 1;
505             argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
506                 }
507         }
508         if (cp2 != cp1) {
509                 argp[argn++] = strdup(cp1);
510                 argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
511         }
512         argp[argn] = (char *) 0;
513         free(carg);
514 }
515
516
517
518 void
519 SystemExec::terminate ()
520 {
521         ::pthread_mutex_lock(&write_lock);
522
523         /* close stdin in an attempt to get the child to exit cleanly.
524          */
525
526         close_stdin();
527
528         if (pid) {
529                 ::usleep(50000);
530                 sched_yield();
531                 wait(WNOHANG);
532         }
533
534         /* if pid is non-zero, the child task is still executing (i.e. it did
535          * not exit in response to stdin being closed). try to kill it.
536          */
537         
538         if (pid) {
539                 ::kill(pid, SIGTERM);
540                 ::usleep(50000);
541                 sched_yield();
542                 wait(WNOHANG);
543         }
544
545         /* if pid is non-zero, the child task is STILL executing after being
546          * sent SIGTERM. Act tough ... send SIGKILL
547          */
548
549         if (pid) {
550                 ::fprintf(stderr, "Process is still running! trying SIGKILL\n");
551                 ::kill(pid, SIGKILL);
552         }
553
554         wait();
555         if (thread_active) pthread_join(thread_id_tt, NULL);
556         thread_active = false;
557         ::pthread_mutex_unlock(&write_lock);
558 }
559
560 int
561 SystemExec::wait (int options)
562 {
563         int status=0;
564         int ret;
565
566         if (pid==0) return -1;
567
568         ret = waitpid (pid, &status, options);
569
570         if (ret == pid) {
571                 if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
572                         pid=0;
573                 }
574         } else {
575                 if (ret != 0) {
576                         if (errno == ECHILD) {
577                                 /* no currently running children, reset pid */
578                                 pid=0;
579                         }
580                 } /* else the process is still running */
581         }
582         return status;
583 }
584
585 bool
586 SystemExec::is_running ()
587 {
588         int status=0;
589         if (pid==0) return false;
590         if (::waitpid(pid, &status, WNOHANG)==0) return true;
591         return false;
592 }
593
594 int
595 SystemExec::start (int stderr_mode)
596 {
597         if (is_running()) {
598                 return 0; // mmh what to return here?
599         }
600         int r;
601
602         if (::pipe(pin) < 0 || ::pipe(pout) < 0 || ::pipe(pok) < 0) {
603                 /* Something unexpected went wrong creating a pipe. */
604                 return -1;
605         }
606
607         r = ::fork();
608         if (r < 0) {
609                 /* failed to fork */
610                 return -2;
611         }
612
613         if (r > 0) {
614                 /* main */
615                 pid=r;
616
617                 /* check if execve was successful. */
618                 close_fd(pok[1]);
619                 char buf;
620                 for ( ;; ) {
621                         ssize_t n = ::read(pok[0], &buf, 1 );
622                         if ( n==1 ) {
623                                 /* child process returned from execve */
624                                 pid=0;
625                                 close_fd(pok[0]);
626                                 close_fd(pin[1]);
627                                 close_fd(pin[0]);
628                                 close_fd(pout[1]);
629                                 close_fd(pout[0]);
630                                 pin[1] = -1;
631                                 return -3;
632                         } else if ( n==-1 ) {
633                                  if ( errno==EAGAIN || errno==EINTR )
634                                          continue;
635                         }
636                         break;
637                 }
638                 close_fd(pok[0]);
639                 /* child started successfully */
640
641 #if 0
642 /* use fork for output-interposer
643  * it will run in a separated process
644  */
645                 /* catch stdout thread */
646                 r = ::fork();
647                 if (r < 0) {
648                         // failed to fork
649                         terminate();
650                         return -2;
651                 }
652                 if (r == 0) {
653                         /* 2nd child process - catch stdout */
654                         close_fd(pin[1]);
655                         close_fd(pout[1]);
656                         output_interposer();
657                         exit(0);
658                 }
659                 close_fd(pout[1]);
660                 close_fd(pin[0]);
661                 close_fd(pout[0]);
662 #else /* use pthread */
663                 close_fd(pout[1]);
664                 close_fd(pin[0]);
665                 int rv = pthread_create(&thread_id_tt, NULL, interposer_thread, this);
666
667                 thread_active=true;
668                 if (rv) {
669                         thread_active=false;
670                         terminate();
671                         return -2;
672                 }
673 #endif
674                 return 0; /* all systems go - return to main */
675         }
676
677         /* child process - exec external process */
678         close_fd(pok[0]);
679         ::fcntl(pok[1], F_SETFD, FD_CLOEXEC);
680
681         close_fd(pin[1]);
682         if (pin[0] != STDIN_FILENO) {
683           ::dup2(pin[0], STDIN_FILENO);
684         }
685         close_fd(pin[0]);
686         close_fd(pout[0]);
687         if (pout[1] != STDOUT_FILENO) {
688                 ::dup2(pout[1], STDOUT_FILENO);
689         }
690
691         if (stderr_mode == 2) {
692                 /* merge STDERR into output */
693                 if (pout[1] != STDERR_FILENO) {
694                         ::dup2(pout[1], STDERR_FILENO);
695                 }
696         } else if (stderr_mode == 1) {
697                 /* ignore STDERR */
698                 ::close(STDERR_FILENO);
699         } else {
700                 /* keep STDERR */
701         }
702
703         if (pout[1] != STDOUT_FILENO && pout[1] != STDERR_FILENO) {
704                 close_fd(pout[1]);
705         }
706
707         if (nicelevel !=0) {
708                 ::nice(nicelevel);
709         }
710
711 #if 0
712         /* chdir to executable dir */
713         char *directory;
714         directory = strdup(cmd.c_str());
715         if (strrchr(directory, '/') != (char *) 0) {
716                 ::chdir(directory);
717         }
718         free(directory);
719 #endif
720
721 #ifdef HAVE_SIGSET
722         sigset(SIGPIPE, SIG_DFL);
723 #else
724         signal(SIGPIPE, SIG_DFL);
725 #endif
726
727         int good_fds[1] = { -1 };
728         close_allv(good_fds);
729
730         ::execve(argp[0], argp, envp);
731         /* if we reach here something went wrong.. */
732         char buf = 0;
733         (void) ::write(pok[1], &buf, 1 );
734         close_fd(pok[1]);
735         exit(-1);
736         return -1;
737 }
738
739 void
740 SystemExec::output_interposer()
741 {
742         int rfd=pout[0];
743         char buf[BUFSIZ];
744         ssize_t r;
745         unsigned long l = 1;
746
747         ioctl(rfd, FIONBIO, &l); // set non-blocking I/O
748
749         for (;fcntl(rfd, F_GETFL)!=-1;) {
750                 r = read(rfd, buf, sizeof(buf));
751                 if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
752                         ::usleep(1000);
753                         continue;
754                 }
755                 if (r <= 0) {
756                         break;
757                 }
758                 buf[r]=0;
759                 std::string rv = std::string(buf,r); // TODO: check allocation strategy
760                 ReadStdout(rv, r);/* EMIT SIGNAL */
761         }
762         Terminated();/* EMIT SIGNAL */
763 }
764
765 void
766 SystemExec::close_stdin()
767 {
768         if (pin[1]<0) return;
769         close_fd(pin[0]);
770         close_fd(pin[1]);
771         close_fd(pout[0]);
772         close_fd(pout[1]);
773 }
774
775 int
776 SystemExec::write_to_stdin(std::string d, size_t len)
777 {
778         const char *data;
779         ssize_t r;
780         size_t c;
781         ::pthread_mutex_lock(&write_lock);
782
783         data=d.c_str();
784         if (len == 0) {
785                 len=(d.length());
786         }
787         c=0;
788         while (c < len) {
789                 for (;;) {
790                         r=::write(pin[1], data+c, len-c);
791                         if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
792                                 sleep(1);
793                                 continue;
794                         }
795                         if ((size_t) r != (len-c)) {
796                                 ::pthread_mutex_unlock(&write_lock);
797                                 return c;
798                         }
799                         break;
800                 }
801                 c += r;
802         }
803         fsync(pin[1]);
804         ::pthread_mutex_unlock(&write_lock);
805         return c;
806 }
807
808 #endif // end UNIX process