2 * Copyright (c) 2001-2002 artofcode LLC.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "ijs_client.h"
32 /* Start child program with redirected standard input and output */
34 ijs_exec_server(const char *server_cmd, int *pfd_to, int *pfd_from,
37 SECURITY_ATTRIBUTES saAttr;
38 STARTUPINFO siStartInfo;
41 HANDLE hChildStdinRd = INVALID_HANDLE_VALUE;
42 HANDLE hChildStdinWr = INVALID_HANDLE_VALUE;
43 HANDLE hChildStdoutRd = INVALID_HANDLE_VALUE;
44 HANDLE hChildStdoutWr = INVALID_HANDLE_VALUE;
45 PROCESS_INFORMATION piProcInfo;
48 int fd_stdout_rd = -1;
50 /* Set the bInheritHandle flag so pipe handles are inherited. */
51 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
52 saAttr.bInheritHandle = TRUE;
53 saAttr.lpSecurityDescriptor = NULL;
55 /* Create anonymous inheritable pipes for STDIN and STDOUT
56 * for child. For each pipe, create a noninheritable duplicate handle
57 * of our end of the pipe, then close the inheritable handle.
58 * Do not redirect STDERR.
60 flag = CreatePipe(&hChildStdinRd, &hPipeTemp, &saAttr, 0);
62 flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
63 GetCurrentProcess(), &hChildStdinWr, 0,
64 FALSE, /* not inherited */
65 DUPLICATE_SAME_ACCESS);
66 CloseHandle(hPipeTemp);
69 flag = CreatePipe(&hPipeTemp, &hChildStdoutWr, &saAttr, 0);
71 flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
72 GetCurrentProcess(), &hChildStdoutRd, 0,
73 FALSE, /* not inherited */
74 DUPLICATE_SAME_ACCESS);
75 CloseHandle(hPipeTemp);
78 flag = (fd_stdin_wr = _open_osfhandle((LONG)hChildStdinWr, 0)) != -1;
80 flag = (fd_stdout_rd = _open_osfhandle((LONG)hChildStdoutRd, 0)) != -1;
82 /* Now create the child process. */
84 /* Set up members of STARTUPINFO structure. */
85 siStartInfo.cb = sizeof(STARTUPINFO);
86 siStartInfo.lpReserved = NULL;
87 siStartInfo.lpDesktop = NULL;
88 siStartInfo.lpTitle = NULL; /* use executable name as title */
89 siStartInfo.dwX = siStartInfo.dwY = CW_USEDEFAULT; /* ignored */
90 siStartInfo.dwXSize = siStartInfo.dwYSize = CW_USEDEFAULT;/* ignored */
91 siStartInfo.dwXCountChars = 80;
92 siStartInfo.dwYCountChars = 25;
93 siStartInfo.dwFillAttribute = 0; /* ignored */
94 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
96 siStartInfo.wShowWindow = SW_SHOWNORMAL;
97 siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
99 siStartInfo.wShowWindow = SW_HIDE;
101 siStartInfo.cbReserved2 = 0;
102 siStartInfo.lpReserved2 = NULL;
103 siStartInfo.hStdInput = hChildStdinRd;
104 siStartInfo.hStdOutput = hChildStdoutWr;
105 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
109 /* Create the child process. */
111 flag = CreateProcess(server_cmd,
112 NULL, /* command line */
113 NULL, /* process security attributes */
114 NULL, /* primary thread security attributes */
115 TRUE, /* handles are inherited */
116 0, /* creation flags */
117 env, /* environment */
118 NULL, /* use parent's current directory */
119 &siStartInfo, /* STARTUPINFO pointer */
120 &piProcInfo); /* receives PROCESS_INFORMATION */
122 CloseHandle(piProcInfo.hProcess);
123 CloseHandle(piProcInfo.hThread);
127 if (hChildStdinRd != INVALID_HANDLE_VALUE)
128 CloseHandle(hChildStdinRd);
129 if (hChildStdoutWr != INVALID_HANDLE_VALUE)
130 CloseHandle(hChildStdoutWr);
133 *pfd_to = fd_stdin_wr;
134 *pfd_from = fd_stdout_rd;
135 *pchild_pid = (int)piProcInfo.dwProcessId;
138 if (fd_stdin_wr != -1)
140 else if (hChildStdinWr != INVALID_HANDLE_VALUE)
141 CloseHandle(hChildStdinWr);
142 if (fd_stdout_rd != -1)
144 else if (hChildStdoutRd != INVALID_HANDLE_VALUE)
145 CloseHandle(hChildStdoutRd);
148 return flag ? 0 : -1;