comparison cinst/nssstore_win.c @ 330:1e6d1eab8395

Fix NSS unit test for Windows and change how instructions are written This was supposed to fix the block on error. But it did not.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 08 Apr 2014 15:08:57 +0000
parents b1059360a0c7
children c0eac5c8c245
comparison
equal deleted inserted replaced
329:b1059360a0c7 330:1e6d1eab8395
30 /**@brief Write strv of instructions to a handle 30 /**@brief Write strv of instructions to a handle
31 * 31 *
32 * Writes the null terminated list of instructions to 32 * Writes the null terminated list of instructions to
33 * the handle. 33 * the handle.
34 * 34 *
35 * @param [in] instructions instructions to write 35 * @param [in] base64 encoded der certificates to write
36 * @param [in] write_handle to write to 36 * @param [in] write_handle to write to
37 * @param [in] remove weather the certificate should be installed or removed
37 * 38 *
38 * @returns true on success, false on failure 39 * @returns true on success, false on failure
39 */ 40 */
40 static bool 41 static bool
41 write_instructions(char **instructions, HANDLE write_handle) 42 write_instructions(char **certificates, HANDLE write_handle,
43 bool remove)
42 { 44 {
43 bool retval = false;
44 int i = 0; 45 int i = 0;
45 const char *line_end = "\n\0"; 46 int cHandle = -1;
46 47 FILE *write_stream = NULL;
47 if (!instructions) 48
49 if (!certificates)
48 { 50 {
49 return true; 51 return true;
50 } 52 }
51 53
52 for (i = 0; instructions[i]; i++) 54 cHandle = _open_osfhandle ((intptr_t)write_handle, 0);
53 { 55
54 DWORD written = 0; 56 if (cHandle == -1)
55 DWORD inst_len = strlen (instructions[i]); 57 {
56 retval = WriteFile (write_handle, (LPCVOID) instructions[i], inst_len, &written, NULL); 58 ERRORPRINTF ("Failed to open write handle.\n");
57 if (!retval) 59 }
60
61 write_stream = _fdopen(cHandle, "w");
62 for (i = 0; certificates[i]; i++)
63 {
64 int ret = 0;
65 DEBUGPRINTF("Writing \n");
66 if (remove)
67 ret = fprintf (write_stream, "R:%s\n", certificates[i]);
68 else
69 ret = fprintf (write_stream, "I:%s\n", certificates[i]);
70
71 if (ret <= 0)
58 { 72 {
59 PRINTLASTERROR ("Failed to write\n"); 73 DEBUGPRINTF ("Failed to write everything.\n");
60 return false; 74 break;
61 } 75 }
62 if (inst_len != written) 76 }
63 { 77
64 ERRORPRINTF ("Failed to write everything\n"); 78 DEBUGPRINTF("Write done\n");
65 retval = false;
66 return false;
67 }
68 written = 0;
69 retval = WriteFile (write_handle, (LPCVOID) line_end, 2, &written, NULL);
70 if (!retval)
71 {
72 PRINTLASTERROR ("Failed to write line end\n");
73 return false;
74 }
75 if (inst_len != written)
76 {
77 ERRORPRINTF ("Failed to write full line end\n");
78 retval = false;
79 return false;
80 }
81 }
82 return true; 79 return true;
83 } 80 }
84 81
85 /**@brief Start the process to install / remove 82 /**@brief Start the process to install / remove
86 * 83 *
149 146
150 /* set up handles. stdin and stdout go to the same stdout*/ 147 /* set up handles. stdin and stdout go to the same stdout*/
151 siStartInfo.cb = sizeof (STARTUPINFO); 148 siStartInfo.cb = sizeof (STARTUPINFO);
152 siStartInfo.hStdError = h_stdout_child_w; 149 siStartInfo.hStdError = h_stdout_child_w;
153 siStartInfo.hStdOutput = h_stdout_child_w; 150 siStartInfo.hStdOutput = h_stdout_child_w;
154 siStartInfo.hStdInput = h_stdin_child_w; 151 siStartInfo.hStdInput = h_stdin_child_r;
155 siStartInfo.dwFlags = STARTF_USESTDHANDLES; 152 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
156 153
157 success = CreateProcessAsUserW (hToken, 154 success = CreateProcessAsUserW (hToken,
158 lpApplicationName, 155 lpApplicationName,
159 NULL, /* Commandline */ 156 NULL, /* Commandline */
160 NULL, /* Process attributes. Take hToken */ 157 NULL, /* Process attributes. Take hToken */
161 NULL, /* Thread attribues. Take hToken */ 158 NULL, /* Thread attribues. Take hToken */
162 TRUE, /* Inherit Handles */ 159 TRUE, /* Inherit Handles */
163 0, /* Creation flags. Use default */ 160 CREATE_UNICODE_ENVIRONMENT, /* Creation flags. */
164 lpEnvironment, 161 lpEnvironment,
165 NULL, /* Current working directory */ 162 NULL, /* Current working directory */
166 &siStartInfo, 163 &siStartInfo,
167 &piProcInfo); 164 &piProcInfo);
168 if (!success) 165 if (!success)
169 { 166 {
170 PRINTLASTERROR ("Failed to create process.\n"); 167 PRINTLASTERROR ("Failed to create process.\n");
171 goto closepipes; 168 goto closepipes;
172 } 169 }
173 170
174 if (!write_instructions (to_install, h_stdin_child_w)) 171 if (!write_instructions (to_install, h_stdin_child_w, false))
175 { 172 {
176 ERRORPRINTF ("Failed to write install instructions.\n"); 173 ERRORPRINTF ("Failed to write install instructions.\n");
177 goto closepipes; 174 goto closepipes;
178 } 175 }
179 if (!write_instructions (to_remove, h_stdin_child_w)) 176 if (!write_instructions (to_remove, h_stdin_child_w, true))
180 { 177 {
181 ERRORPRINTF ("Failed to write remove instructions.\n"); 178 ERRORPRINTF ("Failed to write remove instructions.\n");
182 goto closepipes; 179 goto closepipes;
183 } 180 }
184 181
223 ERRORPRINTF ("Failed to wait for process.\n"); 220 ERRORPRINTF ("Failed to wait for process.\n");
224 } 221 }
225 222
226 closeprocess: 223 closeprocess:
227 if (piProcInfo.hProcess) 224 if (piProcInfo.hProcess)
228 CloseHandle(piProcInfo.hProcess); 225 CloseHandle (piProcInfo.hProcess);
229 if (piProcInfo.hThread) 226 if (piProcInfo.hThread)
230 CloseHandle(piProcInfo.hThread); 227 CloseHandle (piProcInfo.hThread);
231 228
232 closepipes: 229 closepipes:
230 if (lpEnvironment)
231 DestroyEnvironmentBlock (lpEnvironment);
233 if (h_stdin_child_w) 232 if (h_stdin_child_w)
234 CloseHandle(h_stdin_child_w); 233 CloseHandle (h_stdin_child_w);
235 if (h_stdin_child_r) 234 if (h_stdin_child_r)
236 CloseHandle(h_stdin_child_r); 235 CloseHandle (h_stdin_child_r);
237 if (h_stdout_child_w) 236 if (h_stdout_child_w)
238 CloseHandle(h_stdout_child_w); 237 CloseHandle (h_stdout_child_w);
239 if (h_stdout_child_r) 238 if (h_stdout_child_r)
240 CloseHandle(h_stdout_child_r); 239 CloseHandle (h_stdout_child_r);
241 240
242 return retval; 241 return retval;
243 } 242 }
244 243
245 int 244 int

http://wald.intevation.org/projects/trustbridge/