2 // compat.cpp -- compatibilty layer to make bacula-fd run
3 // natively under windows
5 // Copyright (C)2004 Raider Solutions, Inc
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of
10 // the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public
18 // License along with this program; if not, write to the Free
19 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 // MA 02111-1307, USA.
22 // Author : Christopher S. Hull
23 // Created On : Sat Jan 31 15:55:00 2004
24 // Last Modified By: Christopher S. Hull
25 // Last Modified On: Sun Feb 22 12:55:40 2004
34 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
35 extern DWORD g_platform_id;
37 // from CYGWIN (should be diff between Jan 1 1601 and Jan 1 1970
38 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
40 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
43 cygwin_conv_to_win32_path(const char *name, char *win32_name)
45 const char *fname = name;
47 /* Check for Unix separator and convert to Win32 */
49 *win32_name++ = '\\'; /* convert char */
50 /* If Win32 separated that is "quoted", remove quote */
51 } else if (*name == '\\' && name[1] == '\\') {
53 name++; /* skip first \ */
55 *win32_name++ = *name; /* copy character */
59 /* Strip any trailing slash, if we stored something */
60 if (*fname != 0 && win32_name[-1] == '\\') {
69 wchar_win32_path(const char *name, WCHAR *win32_name)
71 const char *fname = name;
73 /* Check for Unix separator and convert to Win32 */
75 *win32_name++ = '\\'; /* convert char */
76 /* If Win32 separated that is "quoted", remove quote */
77 } else if (*name == '\\' && name[1] == '\\') {
79 name++; /* skip first \ */
81 *win32_name++ = *name; /* copy character */
85 /* Strip any trailing slash, if we stored something */
86 if (*fname != 0 && win32_name[-1] == '\\') {
95 open(const char *file, int flags, int mode)
102 const char *remap = file;
104 if (flags & O_WRONLY) access = GENERIC_WRITE;
105 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
106 else access = GENERIC_READ;
108 if (flags & O_CREAT) create = CREATE_NEW;
109 else create = OPEN_EXISTING;
111 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
113 if (!(flags & O_EXCL))
114 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
116 if (flags & O_APPEND)
118 printf("open...APPEND not implemented yet.");
122 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
123 if (INVALID_HANDLE_VALUE == foo)
134 if (!CloseHandle((HANDLE)fd))
141 write(int fd, const void *data, int32_t len)
145 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
146 if (status) return bwrite;
152 read(int fd, void *data, int32_t len)
157 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
158 if (status) return bread;
170 chmod(const char *, mode_t)
176 chown(const char *k, uid_t, gid_t)
182 lchown(const char *k, uid_t, gid_t)
189 utime(const char *, struct utimbuf *)
202 srandom(unsigned int seed)
206 // /////////////////////////////////////////////////////////////////
207 // convert from Windows concept of time to Unix concept of time
208 // /////////////////////////////////////////////////////////////////
210 cvt(const time_t &time, FILETIME &wintime)
212 uint64_t mstime = time;
213 mstime *= WIN32_FILETIME_SCALE;
214 mstime += WIN32_FILETIME_ADJUST;
216 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
217 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
221 cvt_ftime_to_utime(const FILETIME &time)
223 uint64_t mstime = time.dwHighDateTime;
225 mstime |= time.dwLowDateTime;
227 mstime -= WIN32_FILETIME_ADJUST;
228 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
230 return (time_t) (mstime & 0xffffffff);
238 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
239 FORMAT_MESSAGE_FROM_SYSTEM |
240 FORMAT_MESSAGE_IGNORE_INSERTS,
243 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
248 return (const char *) lpMsgBuf;
252 statDir(const char *file, struct stat *sb)
254 WIN32_FIND_DATA info; // window's file info
256 if (file[1] == ':' && file[2] == 0) {
257 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
258 sb->st_mode = S_IFDIR;
259 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
265 HANDLE h = FindFirstFile(file, &info);
267 if (h == INVALID_HANDLE_VALUE) {
268 const char *err = errorString();
269 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
270 LocalFree((void *)err);
271 errno = GetLastError();
275 sb->st_mode = 0777; /* start with everything */
276 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
277 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
278 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
279 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
280 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
281 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
282 sb->st_mode |= S_IFDIR;
284 sb->st_size = info.nFileSizeHigh;
286 sb->st_size |= info.nFileSizeLow;
287 sb->st_blksize = 4096;
288 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
290 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
291 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
292 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
299 stat2(const char *file, struct stat *sb)
301 BY_HANDLE_FILE_INFORMATION info;
305 cygwin_conv_to_win32_path(file, tmpbuf);
307 DWORD attr = GetFileAttributes(tmpbuf);
310 const char *err = errorString();
311 d_msg(__FILE__, __LINE__, 99,
312 "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
313 LocalFree((void *)err);
314 errno = GetLastError();
318 if (attr & FILE_ATTRIBUTE_DIRECTORY)
319 return statDir(tmpbuf, sb);
321 h = CreateFile(tmpbuf, GENERIC_READ,
322 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
324 if (h == INVALID_HANDLE_VALUE) {
325 const char *err = errorString();
326 d_msg(__FILE__, __LINE__, 99,
327 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
328 LocalFree((void *)err);
330 errno = GetLastError();
334 if (!GetFileInformationByHandle(h, &info)) {
335 const char *err = errorString();
336 d_msg(__FILE__, __LINE__, 99,
337 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
338 LocalFree((void *)err);
340 errno = GetLastError();
344 sb->st_dev = info.dwVolumeSerialNumber;
345 sb->st_ino = info.nFileIndexHigh;
347 sb->st_ino |= info.nFileIndexLow;
348 sb->st_nlink = (short)info.nNumberOfLinks;
350 sb->st_mode = 0777; /* start with everything */
351 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
352 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
353 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
354 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
355 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
356 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
357 sb->st_mode |= S_IFREG;
359 sb->st_size = info.nFileSizeHigh;
361 sb->st_size |= info.nFileSizeLow;
362 sb->st_blksize = 4096;
363 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
364 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
365 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
366 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
374 stat(const char *file, struct stat *sb)
376 WIN32_FILE_ATTRIBUTE_DATA data;
380 memset(sb, 0, sizeof(*sb));
382 if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
383 return stat2(file, sb);
385 // otherwise we're on NT
393 wchar_win32_path(file, buf+4);
395 if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
396 return stat2(file, sb);
398 if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
399 return stat2(file, sb);
402 sb->st_mode = 0777; /* start with everything */
403 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
404 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
405 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
406 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
407 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
408 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
410 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
411 sb->st_mode |= S_IFDIR;
413 sb->st_mode |= S_IFREG;
416 sb->st_size = data.nFileSizeHigh;
418 sb->st_size |= data.nFileSizeLow;
419 sb->st_blksize = 4096;
420 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
421 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
422 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
423 sb->st_ctime = cvt_ftime_to_utime(data.ftLastWriteTime);
428 lstat(const char *file, struct stat *sb)
430 return stat(file, sb);
446 execvp(const char *, char *[]) {
469 waitpid(int, int*, int)
475 readlink(const char *, char *, int)
481 lseek(int fd, int offset, int whence)
489 method = FILE_CURRENT;
498 return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method);
502 strcasecmp(const char *s1, const char *s2)
504 register int ch1, ch2;
507 return 0; /* strings are equal if same object. */
518 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
524 strncasecmp(const char *s1, const char *s2, int len)
526 register int ch1, ch2;
529 return 0; /* strings are equal if same object. */
540 } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
546 gettimeofday(struct timeval *tv, struct timezone *)
552 if (tv == NULL) return -1;
553 if (!SystemTimeToFileTime(&now, &tmp)) return -1;
555 int64_t _100nsec = tmp.dwHighDateTime;
557 _100nsec |= tmp.dwLowDateTime;
558 _100nsec -= WIN32_FILETIME_ADJUST;
560 tv->tv_sec =(long) (_100nsec / 10000000);
561 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
567 syslog(int, const char *, const char *)
585 // implement opendir/readdir/closedir on top of window's API
588 WIN32_FIND_DATA data; // window's file info
589 const char *spec; // the directory we're traversing
590 HANDLE dirh; // the search handle
591 BOOL valid; // the info in data field is valid
592 UINT32 offset; // pseudo offset for d_off
596 opendir(const char *path)
598 int max_len = strlen(path) + 16;
600 if (path == NULL) return NULL;
602 rval = (_dir *)malloc(sizeof(_dir));
603 if (rval == NULL) return NULL;
604 char *tspec = (char *)malloc(max_len);
605 if (tspec == NULL) goto err1;
607 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
608 // allow path to be 32767 bytes
614 cygwin_conv_to_win32_path(path, tspec+4);
616 cygwin_conv_to_win32_path(path, tspec);
618 strncat(tspec, "\\*", max_len);
621 rval->dirh = FindFirstFile(rval->spec, &rval->data);
622 d_msg(__FILE__, __LINE__,
623 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
624 path, rval->spec, rval->dirh);
627 if (rval->dirh == INVALID_HANDLE_VALUE)
630 d_msg(__FILE__, __LINE__,
631 99, "\tFirstFile=%s\n", rval->data.cFileName);
634 free((void *)rval->spec);
643 _dir *dp = (_dir *)dirp;
645 free((void *)dp->spec);
651 typedef struct _WIN32_FIND_DATA {
652 DWORD dwFileAttributes;
653 FILETIME ftCreationTime;
654 FILETIME ftLastAccessTime;
655 FILETIME ftLastWriteTime;
660 TCHAR cFileName[MAX_PATH];
661 TCHAR cAlternateFileName[14];
662 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
666 copyin(struct dirent &dp, const char *fname)
670 char *cp = dp.d_name;
679 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
682 _dir *dp = (_dir *)dirp;
684 entry->d_off = dp->offset;
685 dp->offset += copyin(*entry, dp->data.cFileName);
686 *result = entry; /* return entry address */
687 d_msg(__FILE__, __LINE__,
688 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
689 dirp, entry->d_name, entry->d_reclen, entry->d_off);
691 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
694 dp->valid = FindNextFile(dp->dirh, &dp->data);
700 inet_aton(const char *a, struct in_addr *inp)
704 uint32_t acc = 0, tmp = 0;
706 if (!isdigit(*a)) return 0;
709 tmp = (tmp * 10) + (*cp -'0');
710 else if (*cp == '.') {
711 if (tmp > 255) return 0;
712 acc = (acc << 8) + tmp;
718 if (dotc != 3) return 0;
724 nanosleep(const struct timespec *req, struct timespec *rem)
727 rem->tv_sec = rem->tv_nsec = 0;
728 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
733 init_signals(void terminate(int sig))
739 init_stack_dump(void)
746 pathconf(const char *path, int name)
750 if (strncmp(path, "\\\\?\\", 4) == 0)
762 WORD wVersionRequested = MAKEWORD( 1, 1);
765 int err = WSAStartup(wVersionRequested, &wsaData);
770 printf("Can not start Windows Sockets\n");
779 win32_chdir(const char *dir)
781 if (0 == SetCurrentDirectory(dir)) return -1;
786 win32_getcwd(char *buf, int maxlen)
788 int n = GetCurrentDirectory(maxlen, buf);
790 if (n == 0 || n > maxlen) return NULL;
792 if (n+1 > maxlen) return NULL;
801 #include "mswinver.h"
803 char WIN_VERSION_LONG[64];
804 char WIN_VERSION[32];
811 static winver INIT; // cause constructor to be called before main()
818 const char *version = "";
819 const char *platform = "";
820 OSVERSIONINFO osvinfo;
821 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
823 // Get the current OS version
824 if (!GetVersionEx(&osvinfo)) {
826 platform = "Unknown";
829 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
831 case MS_WINDOWS_95: (version = "Windows 95"); break;
832 case MS_WINDOWS_98: (version = "Windows 98"); break;
833 case MS_WINDOWS_ME: (version = "Windows ME"); break;
834 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
835 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
836 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
837 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
838 default: version = "Windows ??"; break;
841 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
842 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
843 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
846 BPIPE *b = open_bpipe("ls -l", 10, "r");
848 while (!feof(b->rfd)) {
849 fgets(buf, sizeof(buf), b->rfd);
855 BOOL CreateChildProcess(VOID);
856 VOID WriteToPipe(VOID);
857 VOID ReadFromPipe(VOID);
858 VOID ErrorExit(LPTSTR);
859 VOID ErrMsg(LPTSTR, BOOL);
863 getArgv0(const char *cmdline)
865 const char *cp = cmdline;
867 while (*cp && !isspace(*cp)) cp++;
869 int len = cp - cmdline;
870 char *rval = (char *)malloc(len+1);
882 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
884 PROCESS_INFORMATION piProcInfo;
885 STARTUPINFO siStartInfo;
886 BOOL bFuncRetn = FALSE;
888 // Set up members of the PROCESS_INFORMATION structure.
890 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
892 // Set up members of the STARTUPINFO structure.
894 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
895 siStartInfo.cb = sizeof(STARTUPINFO);
896 // setup new process to use supplied handles for stdin,stdout,stderr
897 // if supplied handles are not used the send a copy of our STD_HANDLE
899 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
901 if (in != INVALID_HANDLE_VALUE)
902 siStartInfo.hStdInput = in;
904 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
906 if (out != INVALID_HANDLE_VALUE)
907 siStartInfo.hStdOutput = out;
909 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
910 if (err != INVALID_HANDLE_VALUE)
911 siStartInfo.hStdError = err;
913 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
914 // Create the child process.
918 // retrive the first compont of the command line which should be the
920 const char *exeName = getArgv0(cmdline);
921 // check to see if absolute path was passed to us already?
922 if (exeName[1] != ':'
923 || (strchr(cmdline, '/') == NULL
924 && strchr(cmdline, '\\') == NULL))
926 // only command name so perform search of PATH to find
928 DWORD rval = SearchPath(NULL,
935 return INVALID_HANDLE_VALUE;
936 if (rval > sizeof(exeFile))
937 return INVALID_HANDLE_VALUE;
941 strcpy(exeFile, exeName);
943 // exeFile now has absolute path to program to execute.
944 free((void *)exeName);
945 // copy original command line to pass to create process
946 strcpy(cmdLine, cmdline);
947 // try to execute program
948 bFuncRetn = CreateProcess(exeFile,
949 cmdLine, // command line
950 NULL, // process security attributes
951 NULL, // primary thread security attributes
952 TRUE, // handles are inherited
954 NULL, // use parent's environment
955 NULL, // use parent's current directory
956 &siStartInfo, // STARTUPINFO pointer
957 &piProcInfo); // receives PROCESS_INFORMATION
959 if (bFuncRetn == 0) {
960 ErrorExit("CreateProcess failed\n");
961 return INVALID_HANDLE_VALUE;
963 // we don't need a handle on the process primary thread so we close
965 CloseHandle(piProcInfo.hThread);
967 return piProcInfo.hProcess;
972 ErrorExit (LPTSTR lpszMessage)
974 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
979 typedef struct s_bpipe {
990 CloseIfValid(HANDLE handle)
992 if (handle != INVALID_HANDLE_VALUE)
995 // include <io.h> causes to many conflicts with some of
996 // the fuctions we define here.
998 int __cdecl _open_osfhandle(long, int);
1002 open_bpipe(char *prog, int wait, const char *mode)
1004 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
1005 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1008 SECURITY_ATTRIBUTES saAttr;
1012 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1013 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1014 hInputFile = INVALID_HANDLE_VALUE;
1016 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1017 memset((void *)bpipe, 0, sizeof(BPIPE));
1019 int mode_read = (mode[0] == 'r');
1020 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1023 // Set the bInheritHandle flag so pipe handles are inherited.
1025 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1026 saAttr.bInheritHandle = TRUE;
1027 saAttr.lpSecurityDescriptor = NULL;
1031 // Create a pipe for the child process's STDOUT.
1032 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1033 ErrorExit("Stdout pipe creation failed\n");
1036 // Create noninheritable read handle and close the inheritable read
1039 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1040 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1042 DUPLICATE_SAME_ACCESS);
1044 ErrorExit("DuplicateHandle failed");
1048 CloseHandle(hChildStdoutRd);
1053 // Create a pipe for the child process's STDIN.
1055 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1056 ErrorExit("Stdin pipe creation failed\n");
1060 // Duplicate the write handle to the pipe so it is not inherited.
1061 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1062 GetCurrentProcess(), &hChildStdinWrDup,
1064 FALSE, // not inherited
1065 DUPLICATE_SAME_ACCESS);
1067 ErrorExit("DuplicateHandle failed");
1071 CloseHandle(hChildStdinWr);
1073 // spawn program with redirected handles as appropriate
1074 bpipe->worker_pid = (pid_t)
1075 CreateChildProcess(prog, // commandline
1076 hChildStdinRd, // stdin HANDLE
1077 hChildStdoutWr, // stdout HANDLE
1078 hChildStdoutWr);// stderr HANDLE
1080 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1084 bpipe->worker_stime = time(NULL);
1087 CloseHandle(hChildStdoutWr); // close our write side so when
1088 // process terminates we can
1090 // ugly but convert WIN32 HANDLE to FILE*
1091 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1092 bpipe->rfd = _fdopen(rfd, "r");
1095 CloseHandle(hChildStdinRd); // close our read side so as not
1096 // to interfre with child's copy
1097 // ugly but convert WIN32 HANDLE to FILE*
1098 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1099 bpipe->wfd = _fdopen(wfd, "w");
1103 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1110 CloseIfValid(hChildStdoutRd);
1111 CloseIfValid(hChildStdoutRdDup);
1112 CloseIfValid(hChildStdinWr);
1113 CloseIfValid(hChildStdinWrDup);
1115 free((void *) bpipe);
1121 kill(int pid, int signal)
1124 if (!TerminateProcess((HANDLE)pid, (UINT) signal))
1126 CloseHandle((HANDLE)pid);
1131 close_bpipe(BPIPE *bpipe)
1134 if (bpipe->rfd) fclose(bpipe->rfd);
1135 if (bpipe->wfd) fclose(bpipe->wfd);
1138 int remaining_wait = bpipe->wait;
1142 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
1144 const char *err = errorString();
1145 rval = GetLastError();
1146 d_msg(__FILE__, __LINE__, 0,
1147 "GetExitCode error %s\n", err);
1148 LocalFree((void *)err);
1152 if (exitCode == STILL_ACTIVE) {
1153 bmicrosleep(1, 0); /* wait one second */
1157 } while(remaining_wait);
1160 if (bpipe->timer_id) {
1161 stop_child_timer(bpipe->timer_id);
1163 free((void *)bpipe);
1168 close_wpipe(BPIPE *bpipe)
1174 if (fclose(bpipe->wfd) != 0) {