summaryrefslogtreecommitdiff
path: root/ijs_exec_win.c
blob: 984c1ed42d5528d6f813691f427c257a3666c17d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * Copyright (c) 2001-2002 artofcode LLC.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

#define STRICT
#include <windows.h>
#include <io.h>
#include <process.h>
#include "ijs.h"
#include "ijs_client.h"

/* Start child program with redirected standard input and output */
int 
ijs_exec_server(const char *server_cmd, int *pfd_to, int *pfd_from, 
    int *pchild_pid)
{
    SECURITY_ATTRIBUTES saAttr;
    STARTUPINFO siStartInfo;
    LPVOID env;
    HANDLE hPipeTemp;
    HANDLE hChildStdinRd = INVALID_HANDLE_VALUE;
    HANDLE hChildStdinWr = INVALID_HANDLE_VALUE;
    HANDLE hChildStdoutRd = INVALID_HANDLE_VALUE;
    HANDLE hChildStdoutWr = INVALID_HANDLE_VALUE;
    PROCESS_INFORMATION piProcInfo;
    BOOL flag;
    int fd_stdin_wr = -1;
    int fd_stdout_rd = -1;

    /* Set the bInheritHandle flag so pipe handles are inherited. */
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    /* Create anonymous inheritable pipes for STDIN and STDOUT
     * for child. For each pipe, create a noninheritable duplicate handle
     * of our end of the pipe, then close the inheritable handle.
     * Do not redirect STDERR.
     */
    flag = CreatePipe(&hChildStdinRd, &hPipeTemp, &saAttr, 0);
    if (flag) {
        flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
            GetCurrentProcess(), &hChildStdinWr, 0,
            FALSE,       /* not inherited */
            DUPLICATE_SAME_ACCESS);
	CloseHandle(hPipeTemp);
    }
    if (flag)
	flag = CreatePipe(&hPipeTemp, &hChildStdoutWr, &saAttr, 0);
    if (flag) {
        flag = DuplicateHandle(GetCurrentProcess(), hPipeTemp,
            GetCurrentProcess(), &hChildStdoutRd, 0,
            FALSE,       /* not inherited */
            DUPLICATE_SAME_ACCESS);
        CloseHandle(hPipeTemp);
    }
    if (flag)
	flag = (fd_stdin_wr = _open_osfhandle((LONG)hChildStdinWr, 0)) != -1;
    if (flag)
	flag = (fd_stdout_rd = _open_osfhandle((LONG)hChildStdoutRd, 0)) != -1;

    /* Now create the child process. */
    if (flag) {
	/* Set up members of STARTUPINFO structure. */
	siStartInfo.cb = sizeof(STARTUPINFO);
	siStartInfo.lpReserved = NULL;
	siStartInfo.lpDesktop = NULL;
	siStartInfo.lpTitle = NULL;  /* use executable name as title */
	siStartInfo.dwX = siStartInfo.dwY = CW_USEDEFAULT;	/* ignored */
	siStartInfo.dwXSize = siStartInfo.dwYSize = CW_USEDEFAULT;/* ignored */
	siStartInfo.dwXCountChars = 80;
	siStartInfo.dwYCountChars = 25;
	siStartInfo.dwFillAttribute = 0;			/* ignored */
	siStartInfo.dwFlags = STARTF_USESTDHANDLES;
#ifdef VERBOSE
	siStartInfo.wShowWindow = SW_SHOWNORMAL;
	siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
#else
	siStartInfo.wShowWindow = SW_HIDE;
#endif
	siStartInfo.cbReserved2 = 0;
	siStartInfo.lpReserved2 = NULL;
	siStartInfo.hStdInput  = hChildStdinRd;
	siStartInfo.hStdOutput = hChildStdoutWr;
	siStartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);

	env = NULL;

	/* Create the child process. */

	flag = CreateProcess(server_cmd,
	    NULL,  	   /* command line                       */
	    NULL,          /* process security attributes        */
	    NULL,          /* primary thread security attributes */
	    TRUE,          /* handles are inherited              */
	    0,             /* creation flags                     */
	    env,           /* environment                        */
	    NULL,          /* use parent's current directory     */
	    &siStartInfo,  /* STARTUPINFO pointer                */
	    &piProcInfo);  /* receives PROCESS_INFORMATION  */
	if (flag) {
	    CloseHandle(piProcInfo.hProcess);
	    CloseHandle(piProcInfo.hThread);
	}
    }

    if (hChildStdinRd != INVALID_HANDLE_VALUE)
        CloseHandle(hChildStdinRd);
    if (hChildStdoutWr != INVALID_HANDLE_VALUE)
	CloseHandle(hChildStdoutWr);

    if (flag) {
	*pfd_to = fd_stdin_wr;
	*pfd_from = fd_stdout_rd;
	*pchild_pid = (int)piProcInfo.dwProcessId;
    }
    else {
	if (fd_stdin_wr != -1)
	    close(fd_stdin_wr);
	else if (hChildStdinWr != INVALID_HANDLE_VALUE)
	    CloseHandle(hChildStdinWr);
	if (fd_stdout_rd != -1)
	    close(fd_stdout_rd);
	else if (hChildStdoutRd != INVALID_HANDLE_VALUE)
	    CloseHandle(hChildStdoutRd);
	return -1;
    }
    return flag ? 0 : -1;
}