1 /**
2  * Copyright (C) 2001-2002 Artifex Software, Inc.
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 <fcntl.h>
30 #include "ijs.h"
31 #include "ijs_client.h"
32 
33 /* Start child program with redirected standard input and output */
34 int
ijs_exec_server(const char * server_cmd,int * pfd_to,int * pfd_from,int * pchild_pid)35 ijs_exec_server(const char *server_cmd, int *pfd_to, int *pfd_from,
36     int *pchild_pid)
37 {
38     SECURITY_ATTRIBUTES saAttr;
39     STARTUPINFO siStartInfo;
40     LPVOID env;
41     HANDLE hPipeTemp;
42     HANDLE hChildStdinRd = INVALID_HANDLE_VALUE;
43     HANDLE hChildStdinWr = INVALID_HANDLE_VALUE;
44     HANDLE hChildStdoutRd = INVALID_HANDLE_VALUE;
45     HANDLE hChildStdoutWr = INVALID_HANDLE_VALUE;
46     PROCESS_INFORMATION piProcInfo;
47     BOOL flag;
48     int fd_stdin_wr = -1;
49     int fd_stdout_rd = -1;
50 
51     /* Set the bInheritHandle flag so pipe handles are inherited. */
52     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
53     saAttr.bInheritHandle = TRUE;
54     saAttr.lpSecurityDescriptor = NULL;
55 
56     /* Create anonymous inheritable pipes for STDIN and STDOUT
57      * for child. For each pipe, create a noninheritable duplicate handle
58      * of our end of the pipe, then close the inheritable handle.
59      * Do not redirect STDERR.
60      */
61     flag = CreatePipe(&hChildStdinRd, &hPipeTemp, &saAttr, 0);
62     if (flag) {
63         flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
64             GetCurrentProcess(), &hChildStdinWr, 0,
65             FALSE,       /* not inherited */
66             DUPLICATE_SAME_ACCESS);
67 	CloseHandle(hPipeTemp);
68     }
69     if (flag)
70 	flag = CreatePipe(&hPipeTemp, &hChildStdoutWr, &saAttr, 0);
71     if (flag) {
72         flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
73             GetCurrentProcess(), &hChildStdoutRd, 0,
74             FALSE,       /* not inherited */
75             DUPLICATE_SAME_ACCESS);
76         CloseHandle(hPipeTemp);
77     }
78     if (flag)
79 	flag = (fd_stdin_wr = _open_osfhandle((LONG)hChildStdinWr, _O_BINARY)) != -1;
80     if (flag)
81 	flag = (fd_stdout_rd = _open_osfhandle((LONG)hChildStdoutRd, _O_BINARY)) != -1;
82 
83     /* Now create the child process. */
84     if (flag) {
85 	/* Set up members of STARTUPINFO structure. */
86 	siStartInfo.cb = sizeof(STARTUPINFO);
87 	siStartInfo.lpReserved = NULL;
88 	siStartInfo.lpDesktop = NULL;
89 	siStartInfo.lpTitle = NULL;  /* use executable name as title */
90 	siStartInfo.dwX = siStartInfo.dwY = CW_USEDEFAULT;	/* ignored */
91 	siStartInfo.dwXSize = siStartInfo.dwYSize = CW_USEDEFAULT;/* ignored */
92 	siStartInfo.dwXCountChars = 80;
93 	siStartInfo.dwYCountChars = 25;
94 	siStartInfo.dwFillAttribute = 0;			/* ignored */
95 	siStartInfo.dwFlags = STARTF_USESTDHANDLES;
96 #ifdef VERBOSE
97 	siStartInfo.wShowWindow = SW_SHOWNORMAL;
98 	siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
99 #else
100 	siStartInfo.wShowWindow = SW_HIDE;
101 #endif
102 	siStartInfo.cbReserved2 = 0;
103 	siStartInfo.lpReserved2 = NULL;
104 	siStartInfo.hStdInput  = hChildStdinRd;
105 	siStartInfo.hStdOutput = hChildStdoutWr;
106 	siStartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);
107 
108 	env = NULL;
109 
110 	/* Create the child process. */
111 
112 	flag = CreateProcess(server_cmd,
113 	    NULL,  	   /* command line                       */
114 	    NULL,          /* process security attributes        */
115 	    NULL,          /* primary thread security attributes */
116 	    TRUE,          /* handles are inherited              */
117 	    0,             /* creation flags                     */
118 	    env,           /* environment                        */
119 	    NULL,          /* use parent's current directory     */
120 	    &siStartInfo,  /* STARTUPINFO pointer                */
121 	    &piProcInfo);  /* receives PROCESS_INFORMATION  */
122 	if (flag) {
123 	    CloseHandle(piProcInfo.hProcess);
124 	    CloseHandle(piProcInfo.hThread);
125 	}
126     }
127 
128     if (hChildStdinRd != INVALID_HANDLE_VALUE)
129         CloseHandle(hChildStdinRd);
130     if (hChildStdoutWr != INVALID_HANDLE_VALUE)
131 	CloseHandle(hChildStdoutWr);
132 
133     if (flag) {
134 	*pfd_to = fd_stdin_wr;
135 	*pfd_from = fd_stdout_rd;
136 	*pchild_pid = (int)piProcInfo.dwProcessId;
137     }
138     else {
139 	if (fd_stdin_wr != -1)
140 	    close(fd_stdin_wr);
141 	else if (hChildStdinWr != INVALID_HANDLE_VALUE)
142 	    CloseHandle(hChildStdinWr);
143 	if (fd_stdout_rd != -1)
144 	    close(fd_stdout_rd);
145 	else if (hChildStdoutRd != INVALID_HANDLE_VALUE)
146 	    CloseHandle(hChildStdoutRd);
147 	return -1;
148     }
149     return flag ? 0 : -1;
150 }
151 
152 
153