Code sync
[external/libijs.git] / ijs_exec_win.c
1 /**
2  * Copyright (c) 2001-2002 artofcode LLC.
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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
22  * SOFTWARE.
23 **/
24
25 #define STRICT
26 #include <windows.h>
27 #include <io.h>
28 #include <process.h>
29 #include "ijs.h"
30 #include "ijs_client.h"
31
32 /* Start child program with redirected standard input and output */
33 int 
34 ijs_exec_server(const char *server_cmd, int *pfd_to, int *pfd_from, 
35     int *pchild_pid)
36 {
37     SECURITY_ATTRIBUTES saAttr;
38     STARTUPINFO siStartInfo;
39     LPVOID env;
40     HANDLE hPipeTemp;
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;
46     BOOL flag;
47     int fd_stdin_wr = -1;
48     int fd_stdout_rd = -1;
49
50     /* Set the bInheritHandle flag so pipe handles are inherited. */
51     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
52     saAttr.bInheritHandle = TRUE;
53     saAttr.lpSecurityDescriptor = NULL;
54
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.
59      */
60     flag = CreatePipe(&hChildStdinRd, &hPipeTemp, &saAttr, 0);
61     if (flag) {
62         flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
63             GetCurrentProcess(), &hChildStdinWr, 0,
64             FALSE,       /* not inherited */
65             DUPLICATE_SAME_ACCESS);
66         CloseHandle(hPipeTemp);
67     }
68     if (flag)
69         flag = CreatePipe(&hPipeTemp, &hChildStdoutWr, &saAttr, 0);
70     if (flag) {
71         flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
72             GetCurrentProcess(), &hChildStdoutRd, 0,
73             FALSE,       /* not inherited */
74             DUPLICATE_SAME_ACCESS);
75         CloseHandle(hPipeTemp);
76     }
77     if (flag)
78         flag = (fd_stdin_wr = _open_osfhandle((LONG)hChildStdinWr, 0)) != -1;
79     if (flag)
80         flag = (fd_stdout_rd = _open_osfhandle((LONG)hChildStdoutRd, 0)) != -1;
81
82     /* Now create the child process. */
83     if (flag) {
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;
95 #ifdef VERBOSE
96         siStartInfo.wShowWindow = SW_SHOWNORMAL;
97         siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
98 #else
99         siStartInfo.wShowWindow = SW_HIDE;
100 #endif
101         siStartInfo.cbReserved2 = 0;
102         siStartInfo.lpReserved2 = NULL;
103         siStartInfo.hStdInput  = hChildStdinRd;
104         siStartInfo.hStdOutput = hChildStdoutWr;
105         siStartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);
106
107         env = NULL;
108
109         /* Create the child process. */
110
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  */
121         if (flag) {
122             CloseHandle(piProcInfo.hProcess);
123             CloseHandle(piProcInfo.hThread);
124         }
125     }
126
127     if (hChildStdinRd != INVALID_HANDLE_VALUE)
128         CloseHandle(hChildStdinRd);
129     if (hChildStdoutWr != INVALID_HANDLE_VALUE)
130         CloseHandle(hChildStdoutWr);
131
132     if (flag) {
133         *pfd_to = fd_stdin_wr;
134         *pfd_from = fd_stdout_rd;
135         *pchild_pid = (int)piProcInfo.dwProcessId;
136     }
137     else {
138         if (fd_stdin_wr != -1)
139             close(fd_stdin_wr);
140         else if (hChildStdinWr != INVALID_HANDLE_VALUE)
141             CloseHandle(hChildStdinWr);
142         if (fd_stdout_rd != -1)
143             close(fd_stdout_rd);
144         else if (hChildStdoutRd != INVALID_HANDLE_VALUE)
145             CloseHandle(hChildStdoutRd);
146         return -1;
147     }
148     return flag ? 0 : -1;
149 }
150
151