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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
/* spawn-w32.c - Fork and exec helpers for W32.
* Copyright (C) 2004, 2007-2009, 2010 Free Software Foundation, Inc.
* Copyright (C) 2004, 2006-2012, 2014-2017 g10 Code GmbH
*
* This file is part of Libgpg-error.
*
* Libgpg-error is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgpg-error is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+
*
* This file was originally a part of GnuPG.
*/
#include <config.h>
#if !defined(HAVE_W32_SYSTEM)
#error This code is only used on W32.
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_STAT
# include <sys/stat.h>
#endif
#define WIN32_LEAN_AND_MEAN /* We only need the OS core stuff. */
#include <windows.h>
#define NEED_STRUCT_SPAWN_CB_ARG
#include "gpgrt-int.h"
/* Define to 1 do enable debugging. */
#define DEBUG_W32_SPAWN 0
/* It seems Vista doesn't grok X_OK and so fails access() tests.
* Previous versions interpreted X_OK as F_OK anyway, so we'll just
* use F_OK directly. */
#undef X_OK
#define X_OK F_OK
/* For HANDLE and the internal file descriptor (fd) of this module:
* HANDLE can be represented by an intptr_t which should be true for
* all systems (HANDLE is defined as void *). Further, we assume that
* -1 denotes an invalid handle.
*
* Note that a C run-time file descriptor (the exposed one to API) is
* always represented by an int.
*/
#define fd_to_handle(a) ((HANDLE)(a))
#define handle_to_fd(a) ((intptr_t)(a))
/* Definition for the gpgrt_spawn_actions_t. Note that there is a
* different one for Unices. */
struct gpgrt_spawn_actions {
void *hd[3];
void **inherit_hds;
char *env;
const char *const *envchange;
};
/* Definition for the gpgrt_process_t. Note that there is a different
* one for Unices. */
struct gpgrt_process {
const char *pgmname;
unsigned int terminated:1; /* or detached */
unsigned int flags;
HANDLE hProcess;
HANDLE hd_in;
HANDLE hd_out;
HANDLE hd_err;
int exitcode;
};
/* Helper function to build_w32_commandline. */
static char *
build_w32_commandline_copy (char *buffer, const char *string)
{
char *p = buffer;
const char *s;
if (!*string) /* Empty string. */
p = stpcpy (p, "\"\"");
else if (strpbrk (string, " \t\n\v\f\""))
{
/* Need to do some kind of quoting. */
p = stpcpy (p, "\"");
for (s=string; *s; s++)
{
*p++ = *s;
if (*s == '\"')
*p++ = *s;
}
*p++ = '\"';
*p = 0;
}
else
p = stpcpy (p, string);
return p;
}
/* Build a command line for use with W32's CreateProcess. On success
* CMDLINE gets the address of a newly allocated string. */
static gpg_err_code_t
build_w32_commandline (const char *pgmname, const char * const *argv,
char **cmdline)
{
int i, n;
const char *s;
char *buf, *p;
*cmdline = NULL;
n = 0;
s = pgmname;
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
for (; *s; s++)
if (*s == '\"')
n++; /* Need to double inner quotes. */
for (i=0; (s=argv[i]); i++)
{
n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
for (; *s; s++)
if (*s == '\"')
n++; /* Need to double inner quotes. */
}
n++;
buf = p = xtrymalloc (n);
if (!buf)
return _gpg_err_code_from_syserror ();
p = build_w32_commandline_copy (p, pgmname);
for (i=0; argv[i]; i++)
{
*p++ = ' ';
p = build_w32_commandline_copy (p, argv[i]);
}
*cmdline= buf;
return 0;
}
#define INHERIT_READ 1
#define INHERIT_WRITE 2
#define INHERIT_BOTH (INHERIT_READ|INHERIT_WRITE)
/* Create pipe. FLAGS indicates which ends are inheritable. */
static int
create_inheritable_pipe (HANDLE filedes[2], int flags)
{
HANDLE r, w;
SECURITY_ATTRIBUTES sec_attr;
memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = TRUE;
_gpgrt_pre_syscall ();
if (!CreatePipe (&r, &w, &sec_attr, 0))
{
_gpgrt_post_syscall ();
return -1;
}
_gpgrt_post_syscall ();
if ((flags & INHERIT_READ) == 0)
if (! SetHandleInformation (r, HANDLE_FLAG_INHERIT, 0))
goto fail;
if ((flags & INHERIT_WRITE) == 0)
if (! SetHandleInformation (w, HANDLE_FLAG_INHERIT, 0))
goto fail;
filedes[0] = r;
filedes[1] = w;
return 0;
fail:
_gpgrt_log_info ("SetHandleInformation failed: ec=%d\n",
(int)GetLastError ());
CloseHandle (r);
CloseHandle (w);
return -1;
}
static HANDLE
w32_open_null (int for_write)
{
HANDLE hfile;
hfile = CreateFileW (L"nul",
for_write? GENERIC_WRITE : GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hfile == INVALID_HANDLE_VALUE)
_gpgrt_log_debug ("can't open 'nul': ec=%d\n", (int)GetLastError ());
return hfile;
}
static gpg_err_code_t
do_create_pipe_and_estream (int filedes[2],
estream_t *r_fp, int direction, int nonblock)
{
gpg_err_code_t err = 0;
int flags;
HANDLE fds[2];
gpgrt_syshd_t syshd;
if (direction < 0)
flags = INHERIT_WRITE;
else if (direction > 0)
flags = INHERIT_READ;
else
flags = INHERIT_BOTH;
filedes[0] = filedes[1] = -1;
err = GPG_ERR_GENERAL;
if (!create_inheritable_pipe (fds, flags))
{
filedes[0] = _open_osfhandle (handle_to_fd (fds[0]), O_RDONLY);
if (filedes[0] == -1)
{
_gpgrt_log_info ("failed to translate osfhandle %p\n", fds[0]);
CloseHandle (fds[1]);
}
else
{
filedes[1] = _open_osfhandle (handle_to_fd (fds[1]), O_APPEND);
if (filedes[1] == -1)
{
_gpgrt_log_info ("failed to translate osfhandle %p\n", fds[1]);
close (filedes[0]);
filedes[0] = -1;
CloseHandle (fds[1]);
}
else
err = 0;
}
}
if (! err && r_fp)
{
syshd.type = ES_SYSHD_HANDLE;
if (direction < 0)
{
syshd.u.handle = fds[0];
*r_fp = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
}
else
{
syshd.u.handle = fds[1];
*r_fp = _gpgrt_sysopen (&syshd, nonblock? "w,nonblock" : "w");
}
if (!*r_fp)
{
err = _gpg_err_code_from_syserror ();
_gpgrt_log_info (_("error creating a stream for a pipe: %s\n"),
_gpg_strerror (err));
close (filedes[0]);
close (filedes[1]);
filedes[0] = filedes[1] = -1;
return err;
}
}
return err;
}
/* Create a pipe. The DIRECTION parameter gives the type of the created pipe:
* DIRECTION < 0 := Inbound pipe: On Windows the write end is inheritable.
* DIRECTION > 0 := Outbound pipe: On Windows the read end is inheritable.
* If R_FP is NULL a standard pipe and no stream is created, DIRECTION
* should then be 0. */
gpg_err_code_t
_gpgrt_make_pipe (int filedes[2], estream_t *r_fp, int direction, int nonblock)
{
if (r_fp && direction)
return do_create_pipe_and_estream (filedes, r_fp, direction, nonblock);
else
return do_create_pipe_and_estream (filedes, NULL, 0, 0);
}
/*
* Check if STARTUPINFOEXW supports PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
*/
static int
check_windows_version (void)
{
static int is_vista_or_later = -1;
OSVERSIONINFO osvi;
if (is_vista_or_later == -1)
{
memset (&osvi,0,sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
GetVersionEx (&osvi);
/* The feature is available on Vista or later. */
is_vista_or_later = (osvi.dwMajorVersion >= 6);
}
return is_vista_or_later;
}
static gpg_err_code_t
prepare_env_block (char **r_env, const char *const *envchange)
{
gpg_err_code_t ec;
wchar_t *orig_env_block;
wchar_t *wp;
int i;
size_t envlen[256];
wchar_t *env_block;
size_t env_block_len;
const char *const *envp;
const char *e;
int env_num;
wp = orig_env_block = GetEnvironmentStringsW ();
for (i = 0; *wp != L'\0'; i++)
if (i >= DIM (envlen))
{
FreeEnvironmentStringsW (orig_env_block);
return GPG_ERR_TOO_LARGE;
}
else
wp += ((envlen[i] = wcslen (wp)) + 1);
wp++;
env_num = i;
env_block_len = (char *)wp - (char *)orig_env_block;
env_block = xtrymalloc (env_block_len);
if (!env_block)
{
FreeEnvironmentStringsW (orig_env_block);
return _gpg_err_code_from_syserror ();
}
memcpy (env_block, orig_env_block, env_block_len);
FreeEnvironmentStringsW (orig_env_block);
for (envp = envchange; (e = *envp); envp++)
{
wchar_t *we;
int off = 0;
we = _gpgrt_utf8_to_wchar (e);
if (!we)
{
ec = _gpg_err_code_from_syserror ();
goto leave;
}
wp = wcschr (we, L'=');
if (!wp)
{
/* Remove WE entry in the environment block. */
for (i = 0; i < env_num; i++)
if (!wcsncmp (&env_block[off], we, wcslen (we))
&& env_block[off+wcslen (we)] == L'=')
break;
else
off += envlen[i] + 1;
if (i == env_num)
/* not found */;
else
{
env_block_len -= (envlen[i] + 1) * sizeof (wchar_t);
env_num--;
for (; i < env_num; i++)
{
int off0 = off;
off += envlen[i] + 1;
memmove (&env_block[off0], &env_block[off],
((envlen[i] = envlen[i+1]) + 1) * sizeof (wchar_t));
}
env_block[(env_block_len / sizeof (wchar_t)) - 1] = L'\0';
}
}
else
{
size_t old_env_block_len;
for (i = 0; i < env_num; i++)
if (!wcsncmp (&env_block[off], we, wp - we + 1))
break;
else
off += envlen[i] + 1;
if (i < env_num)
{
int off0 = off;
off += envlen[i] + 1;
/* If an existing entry, remove it. */
env_block_len -= (envlen[i] + 1) * sizeof (wchar_t);
env_num--;
for (; i < env_num; i++)
{
size_t len = (envlen[i] = envlen[i+1]) + 1;
memmove (&env_block[off0], &env_block[off],
len * sizeof (wchar_t));
off0 += len;
off += len;
}
env_block[(env_block_len / sizeof (wchar_t)) - 1] = L'\0';
}
if (i >= DIM (envlen) - 1)
{
ec = GPG_ERR_TOO_LARGE;
_gpgrt_free_wchar (we);
goto leave;
}
old_env_block_len = env_block_len;
env_block_len += ((envlen[i++] = wcslen (we)) + 1) * sizeof (wchar_t);
env_num = i;
env_block = xtryrealloc (env_block, env_block_len);
if (!env_block)
{
ec = _gpg_err_code_from_syserror ();
_gpgrt_free_wchar (we);
goto leave;
}
memmove ((char *)env_block + old_env_block_len - sizeof (wchar_t),
we, (envlen[env_num - 1] + 1) * sizeof (wchar_t));
env_block[(env_block_len / sizeof (wchar_t)) - 1] = L'\0';
}
_gpgrt_free_wchar (we);
}
ec = 0;
leave:
if (ec)
xfree (env_block);
else
*r_env = (char *)env_block;
return ec;
}
static gpg_err_code_t
spawn_detached (const char *pgmname, char *cmdline, gpgrt_spawn_actions_t act)
{
SECURITY_ATTRIBUTES sec_attr;
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
STARTUPINFOEXW si;
int cr_flags;
wchar_t *wcmdline = NULL;
wchar_t *wpgmname = NULL;
gpg_err_code_t ec;
int ret;
BOOL ask_inherit = FALSE;
int i;
char *env = NULL;
ec = _gpgrt_access (pgmname, X_OK);
if (ec)
{
xfree (cmdline);
return ec;
}
memset (&si, 0, sizeof si);
i = 0;
if (act->hd[0] != INVALID_HANDLE_VALUE)
i++;
if (act->hd[1] != INVALID_HANDLE_VALUE)
i++;
if (act->hd[2] != INVALID_HANDLE_VALUE)
i++;
if (i != 0 || act->inherit_hds)
{
SIZE_T attr_list_size = 0;
HANDLE hd[32];
HANDLE *hd_p = act->inherit_hds;
int j = 0;
if (act->hd[0] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[0];
if (act->hd[1] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[1];
if (act->hd[2] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[2];
if (hd_p)
{
while (*hd_p != INVALID_HANDLE_VALUE)
if (j < DIM (hd))
hd[j++] = *hd_p++;
else
{
_gpgrt_log_info ("gpgrt_spawn_detached: too many handles\n");
break;
}
}
if (j)
{
if (check_windows_version ())
{
InitializeProcThreadAttributeList (NULL, 1, 0, &attr_list_size);
si.lpAttributeList = xtrymalloc (attr_list_size);
if (si.lpAttributeList == NULL)
{
xfree (cmdline);
return _gpg_err_code_from_syserror ();
}
InitializeProcThreadAttributeList (si.lpAttributeList, 1, 0,
&attr_list_size);
UpdateProcThreadAttribute (si.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
hd, sizeof (HANDLE) * j, NULL, NULL);
}
ask_inherit = TRUE;
}
}
/* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = FALSE;
/* Start the process. */
si.StartupInfo.cb = sizeof (si);
si.StartupInfo.dwFlags = ((i > 0 ? STARTF_USESTDHANDLES : 0)
| STARTF_USESHOWWINDOW);
si.StartupInfo.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_MINIMIZE;
si.StartupInfo.hStdInput = act->hd[0];
si.StartupInfo.hStdOutput = act->hd[1];
si.StartupInfo.hStdError = act->hd[2];
cr_flags = (CREATE_DEFAULT_ERROR_MODE
| GetPriorityClass (GetCurrentProcess ())
| CREATE_NEW_PROCESS_GROUP
| DETACHED_PROCESS);
if (act->env)
{
/* Either ENV or ENVCHANGE can be specified, not both. */
if (act->envchange)
{
xfree (cmdline);
return GPG_ERR_INV_ARG;
}
env = act->env;
}
else if (act->envchange)
{
ec = prepare_env_block (&env, act->envchange);
if (ec)
{
xfree (cmdline);
return ec;
}
cr_flags |= CREATE_UNICODE_ENVIRONMENT;
}
/* Take care: CreateProcessW may modify wpgmname */
if (!(wpgmname = _gpgrt_utf8_to_wchar (pgmname)))
ret = 0;
else if (!(wcmdline = _gpgrt_utf8_to_wchar (cmdline)))
ret = 0;
else
ret = CreateProcessW (wpgmname, /* Program to start. */
wcmdline, /* Command line arguments. */
&sec_attr, /* Process security attributes. */
&sec_attr, /* Thread security attributes. */
ask_inherit, /* Inherit handles. */
cr_flags, /* Creation flags. */
env, /* Environment. */
NULL, /* Use current drive/directory. */
(STARTUPINFOW *)&si, /* Startup information. */
&pi /* Returns process information. */
);
if (act->envchange)
xfree (env);
env = NULL;
if (si.lpAttributeList)
DeleteProcThreadAttributeList (si.lpAttributeList);
if (!ret)
{
if (!wpgmname || !wcmdline)
_gpgrt_log_info ("gpgrt_spawn_detached: "
"CreateProcess failed (utf8_to_wchar): %s\n",
strerror (errno));
else
_gpgrt_log_info ("gpgrt_spawn_detached: "
"CreateProcess(detached) failed: %d\n",
(int)GetLastError ());
_gpgrt_free_wchar (wpgmname);
_gpgrt_free_wchar (wcmdline);
xfree (cmdline);
return GPG_ERR_GENERAL;
}
_gpgrt_free_wchar (wpgmname);
_gpgrt_free_wchar (wcmdline);
xfree (cmdline);
/* log_debug ("CreateProcess(detached) ready: hProcess=%p hThread=%p" */
/* " dwProcessID=%d dwThreadId=%d\n", */
/* pi.hProcess, pi.hThread, */
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
/* Note: AllowSetForegroundWindow doesn't make sense for background
process. */
CloseHandle (pi.hThread);
CloseHandle (pi.hProcess);
return 0;
}
gpg_err_code_t
_gpgrt_spawn_actions_new (gpgrt_spawn_actions_t *r_act)
{
gpgrt_spawn_actions_t act;
int i;
*r_act = NULL;
act = xtrycalloc (1, sizeof (struct gpgrt_spawn_actions));
if (act == NULL)
return _gpg_err_code_from_syserror ();
for (i = 0; i <= 2; i++)
act->hd[i] = INVALID_HANDLE_VALUE;
*r_act = act;
return 0;
}
void
_gpgrt_spawn_actions_release (gpgrt_spawn_actions_t act)
{
if (!act)
return;
xfree (act);
}
void
_gpgrt_spawn_actions_set_env_rev (gpgrt_spawn_actions_t act,
const char *const *envchange)
{
act->envchange = envchange;
}
/* Set the environment block for child process.
* ENV is an ASCII encoded string, terminated by two zero bytes.
*/
void
_gpgrt_spawn_actions_set_envvars (gpgrt_spawn_actions_t act, char *env)
{
act->env = env;
}
void
_gpgrt_spawn_actions_set_redirect (gpgrt_spawn_actions_t act,
void *in, void *out, void *err)
{
act->hd[0] = in;
act->hd[1] = out;
act->hd[2] = err;
}
void
_gpgrt_spawn_actions_set_inherit_handles (gpgrt_spawn_actions_t act,
void **handles)
{
act->inherit_hds = handles;
}
gpg_err_code_t
_gpgrt_process_spawn (const char *pgmname, const char *argv[],
unsigned int flags, gpgrt_spawn_actions_t act,
gpgrt_process_t *r_process)
{
gpg_err_code_t ec;
gpgrt_process_t process;
SECURITY_ATTRIBUTES sec_attr;
PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
STARTUPINFOEXW si;
int cr_flags;
char *cmdline;
wchar_t *wcmdline = NULL;
wchar_t *wpgmname = NULL;
int ret;
HANDLE hd_in[2];
HANDLE hd_out[2];
HANDLE hd_err[2];
int i;
BOOL ask_inherit = FALSE;
struct gpgrt_spawn_actions act_default;
char *env = NULL;
if (!act)
{
memset (&act_default, 0, sizeof (act_default));
for (i = 0; i <= 2; i++)
act_default.hd[i] = INVALID_HANDLE_VALUE;
act = &act_default;
}
/* Build the command line. */
ec = build_w32_commandline (pgmname, argv, &cmdline);
if (ec)
return ec;
if ((flags & GPGRT_PROCESS_DETACHED))
{
if ((flags & GPGRT_PROCESS_STDFDS_SETTING))
{
xfree (cmdline);
return GPG_ERR_INV_FLAG;
}
/* In detached case, it must be no R_PROCESS. */
if (r_process || pgmname == NULL)
{
xfree (cmdline);
return GPG_ERR_INV_ARG;
}
return spawn_detached (pgmname, cmdline, act);
}
if (r_process)
*r_process = NULL;
if (pgmname == NULL)
{
xfree (cmdline);
return GPG_ERR_INV_ARG;
}
process = xtrymalloc (sizeof (struct gpgrt_process));
if (process == NULL)
{
xfree (cmdline);
return _gpg_err_code_from_syserror ();
}
process->pgmname = pgmname;
process->flags = flags;
if ((flags & GPGRT_PROCESS_STDINOUT_SOCKETPAIR))
{
xfree (process);
xfree (cmdline);
return GPG_ERR_NOT_SUPPORTED;
}
if ((flags & GPGRT_PROCESS_STDIN_PIPE))
{
ec = create_inheritable_pipe (hd_in, INHERIT_READ);
if (ec)
{
xfree (process);
xfree (cmdline);
return ec;
}
}
else if ((flags & GPGRT_PROCESS_STDIN_KEEP))
{
hd_in[0] = GetStdHandle (STD_INPUT_HANDLE);
hd_in[1] = INVALID_HANDLE_VALUE;
}
else
{
hd_in[0] = w32_open_null (0);
hd_in[1] = INVALID_HANDLE_VALUE;
}
if ((flags & GPGRT_PROCESS_STDOUT_PIPE))
{
ec = create_inheritable_pipe (hd_out, INHERIT_WRITE);
if (ec)
{
if (hd_in[0] != INVALID_HANDLE_VALUE)
CloseHandle (hd_in[0]);
if (hd_in[1] != INVALID_HANDLE_VALUE)
CloseHandle (hd_in[1]);
xfree (process);
xfree (cmdline);
return ec;
}
}
else if ((flags & GPGRT_PROCESS_STDOUT_KEEP))
{
hd_out[0] = INVALID_HANDLE_VALUE;
hd_out[1] = GetStdHandle (STD_OUTPUT_HANDLE);
}
else
{
hd_out[0] = INVALID_HANDLE_VALUE;
hd_out[1] = w32_open_null (1);
}
if ((flags & GPGRT_PROCESS_STDERR_PIPE))
{
ec = create_inheritable_pipe (hd_err, INHERIT_WRITE);
if (ec)
{
if (hd_in[0] != INVALID_HANDLE_VALUE)
CloseHandle (hd_in[0]);
if (hd_in[1] != INVALID_HANDLE_VALUE)
CloseHandle (hd_in[1]);
if (hd_out[0] != INVALID_HANDLE_VALUE)
CloseHandle (hd_out[0]);
if (hd_out[1] != INVALID_HANDLE_VALUE)
CloseHandle (hd_out[1]);
xfree (process);
xfree (cmdline);
return ec;
}
}
else if ((flags & GPGRT_PROCESS_STDERR_KEEP))
{
hd_err[0] = INVALID_HANDLE_VALUE;
hd_err[1] = GetStdHandle (STD_ERROR_HANDLE);
}
else
{
hd_err[0] = INVALID_HANDLE_VALUE;
hd_err[1] = w32_open_null (1);
}
memset (&si, 0, sizeof si);
if (act->hd[0] == INVALID_HANDLE_VALUE)
act->hd[0] = hd_in[0];
if (act->hd[1] == INVALID_HANDLE_VALUE)
act->hd[1] = hd_out[1];
if (act->hd[2] == INVALID_HANDLE_VALUE)
act->hd[2] = hd_err[1];
i = 0;
if (act->hd[0] != INVALID_HANDLE_VALUE)
i++;
if (act->hd[1] != INVALID_HANDLE_VALUE)
i++;
if (act->hd[2] != INVALID_HANDLE_VALUE)
i++;
if (i != 0 || act->inherit_hds)
{
SIZE_T attr_list_size = 0;
HANDLE hd[32];
HANDLE *hd_p = act->inherit_hds;
int j = 0;
if (act->hd[0] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[0];
if (act->hd[1] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[1];
if (act->hd[2] != INVALID_HANDLE_VALUE)
hd[j++] = act->hd[2];
if (hd_p)
{
while (*hd_p != INVALID_HANDLE_VALUE)
if (j < DIM (hd))
hd[j++] = *hd_p++;
else
{
_gpgrt_log_info ("gpgrt_process_spawn: too many handles\n");
break;
}
}
if (j)
{
if (check_windows_version ())
{
InitializeProcThreadAttributeList (NULL, 1, 0, &attr_list_size);
si.lpAttributeList = xtrymalloc (attr_list_size);
if (si.lpAttributeList == NULL)
{
if ((flags & GPGRT_PROCESS_STDIN_PIPE)
|| !(flags & GPGRT_PROCESS_STDIN_KEEP))
CloseHandle (hd_in[0]);
if ((flags & GPGRT_PROCESS_STDIN_PIPE))
CloseHandle (hd_in[1]);
if ((flags & GPGRT_PROCESS_STDOUT_PIPE))
CloseHandle (hd_out[0]);
if ((flags & GPGRT_PROCESS_STDOUT_PIPE)
|| !(flags & GPGRT_PROCESS_STDOUT_KEEP))
CloseHandle (hd_out[1]);
if ((flags & GPGRT_PROCESS_STDERR_PIPE))
CloseHandle (hd_err[0]);
if ((flags & GPGRT_PROCESS_STDERR_PIPE)
|| !(flags & GPGRT_PROCESS_STDERR_KEEP))
CloseHandle (hd_err[1]);
xfree (process);
xfree (cmdline);
return _gpg_err_code_from_syserror ();
}
InitializeProcThreadAttributeList (si.lpAttributeList, 1, 0,
&attr_list_size);
UpdateProcThreadAttribute (si.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
hd, sizeof (HANDLE) * j, NULL, NULL);
}
ask_inherit = TRUE;
}
}
/* Prepare security attributes. */
memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = FALSE;
/* Start the process. */
si.StartupInfo.cb = sizeof (si);
si.StartupInfo.dwFlags = ((i > 0 ? STARTF_USESTDHANDLES : 0)
| STARTF_USESHOWWINDOW);
si.StartupInfo.wShowWindow = DEBUG_W32_SPAWN? SW_SHOW : SW_HIDE;
si.StartupInfo.hStdInput = act->hd[0];
si.StartupInfo.hStdOutput = act->hd[1];
si.StartupInfo.hStdError = act->hd[2];
/* log_debug ("CreateProcess, path='%s' cmdline='%s'\n", pgmname, cmdline); */
cr_flags = (CREATE_DEFAULT_ERROR_MODE
| ((flags & GPGRT_PROCESS_NO_CONSOLE) ? DETACHED_PROCESS : 0)
| GetPriorityClass (GetCurrentProcess ())
| CREATE_SUSPENDED);
if (act->env)
{
/* Either ENV or ENVCHANGE can be specified, not both. */
if (act->envchange)
{
xfree (process);
xfree (cmdline);
return GPG_ERR_INV_ARG;
}
env = act->env;
}
else if (act->envchange)
{
ec = prepare_env_block (&env, act->envchange);
if (ec)
{
xfree (process);
xfree (cmdline);
return ec;
}
cr_flags |= CREATE_UNICODE_ENVIRONMENT;
}
if (!(wpgmname = _gpgrt_utf8_to_wchar (pgmname)))
ret = 0;
else if (!(wcmdline = _gpgrt_utf8_to_wchar (cmdline)))
ret = 0;
else
ret = CreateProcessW (wpgmname, /* Program to start. */
wcmdline, /* Command line arguments. */
&sec_attr, /* Process security attributes. */
&sec_attr, /* Thread security attributes. */
ask_inherit, /* Inherit handles. */
cr_flags, /* Creation flags. */
env, /* Environment. */
NULL, /* Use current drive/directory. */
(STARTUPINFOW *)&si, /* Startup information. */
&pi /* Returns process information. */
);
if (act->envchange)
xfree (env);
env = NULL;
if (si.lpAttributeList)
DeleteProcThreadAttributeList (si.lpAttributeList);
if (!ret)
{
if (!wpgmname || !wcmdline)
_gpgrt_log_info ("CreateProcess failed (utf8_to_wchar): %s\n",
strerror (errno));
else
_gpgrt_log_info ("CreateProcess failed: ec=%d\n",
(int)GetLastError ());
if ((flags & GPGRT_PROCESS_STDIN_PIPE)
|| !(flags & GPGRT_PROCESS_STDIN_KEEP))
CloseHandle (hd_in[0]);
if ((flags & GPGRT_PROCESS_STDIN_PIPE))
CloseHandle (hd_in[1]);
if ((flags & GPGRT_PROCESS_STDOUT_PIPE))
CloseHandle (hd_out[0]);
if ((flags & GPGRT_PROCESS_STDOUT_PIPE)
|| !(flags & GPGRT_PROCESS_STDOUT_KEEP))
CloseHandle (hd_out[1]);
if ((flags & GPGRT_PROCESS_STDERR_PIPE))
CloseHandle (hd_err[0]);
if ((flags & GPGRT_PROCESS_STDERR_PIPE)
|| !(flags & GPGRT_PROCESS_STDERR_KEEP))
CloseHandle (hd_err[1]);
_gpgrt_free_wchar (wpgmname);
_gpgrt_free_wchar (wcmdline);
xfree (process);
xfree (cmdline);
return GPG_ERR_GENERAL;
}
_gpgrt_free_wchar (wpgmname);
_gpgrt_free_wchar (wcmdline);
xfree (cmdline);
if ((flags & GPGRT_PROCESS_STDIN_PIPE)
|| !(flags & GPGRT_PROCESS_STDIN_KEEP))
CloseHandle (hd_in[0]);
if ((flags & GPGRT_PROCESS_STDOUT_PIPE)
|| !(flags & GPGRT_PROCESS_STDOUT_KEEP))
CloseHandle (hd_out[1]);
if ((flags & GPGRT_PROCESS_STDERR_PIPE)
|| !(flags & GPGRT_PROCESS_STDERR_KEEP))
CloseHandle (hd_err[1]);
/* log_debug ("CreateProcess ready: hProcess=%p hThread=%p" */
/* " dwProcessID=%d dwThreadId=%d\n", */
/* pi.hProcess, pi.hThread, */
/* (int) pi.dwProcessId, (int) pi.dwThreadId); */
if ((flags & GPGRT_PROCESS_ALLOW_SET_FG))
{
/* Fixme: For unknown reasons AllowSetForegroundWindow returns
* an invalid argument error if we pass it the correct
* processID. As a workaround we use -1 (ASFW_ANY). */
if (!AllowSetForegroundWindow (ASFW_ANY /*pi.dwProcessId*/))
_gpgrt_log_info ("AllowSetForegroundWindow() failed: ec=%d\n",
(int)GetLastError ());
}
/* Process has been created suspended; resume it now. */
_gpgrt_pre_syscall ();
ResumeThread (pi.hThread);
CloseHandle (pi.hThread);
_gpgrt_post_syscall ();
process->hProcess = pi.hProcess;
process->hd_in = hd_in[1];
process->hd_out = hd_out[0];
process->hd_err = hd_err[0];
process->exitcode = -1;
process->terminated = 0;
if (r_process == NULL)
{
ec = _gpgrt_process_wait (process, 1);
_gpgrt_process_release (process);
return ec;
}
*r_process = process;
return 0;
}
gpg_err_code_t
_gpgrt_process_get_fds (gpgrt_process_t process, unsigned int flags,
int *r_fd_in, int *r_fd_out, int *r_fd_err)
{
(void)flags;
if (r_fd_in)
{
*r_fd_in = _open_osfhandle ((intptr_t)process->hd_in, O_APPEND);
process->hd_in = INVALID_HANDLE_VALUE;
}
if (r_fd_out)
{
*r_fd_out = _open_osfhandle ((intptr_t)process->hd_out, O_RDONLY);
process->hd_out = INVALID_HANDLE_VALUE;
}
if (r_fd_err)
{
*r_fd_err = _open_osfhandle ((intptr_t)process->hd_err, O_RDONLY);
process->hd_err = INVALID_HANDLE_VALUE;
}
return 0;
}
gpg_err_code_t
_gpgrt_process_get_streams (gpgrt_process_t process, unsigned int flags,
estream_t *r_fp_in, estream_t *r_fp_out,
estream_t *r_fp_err)
{
int nonblock = (flags & GPGRT_PROCESS_STREAM_NONBLOCK)? 1: 0;
es_syshd_t syshd;
syshd.type = ES_SYSHD_HANDLE;
if (r_fp_in)
{
syshd.u.handle = process->hd_in;
*r_fp_in = _gpgrt_sysopen (&syshd, nonblock? "w,nonblock" : "w");
process->hd_in = INVALID_HANDLE_VALUE;
}
if (r_fp_out)
{
syshd.u.handle = process->hd_out;
*r_fp_out = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
process->hd_out = INVALID_HANDLE_VALUE;
}
if (r_fp_err)
{
syshd.u.handle = process->hd_err;
*r_fp_err = _gpgrt_sysopen (&syshd, nonblock? "r,nonblock" : "r");
process->hd_err = INVALID_HANDLE_VALUE;
}
return 0;
}
static gpg_err_code_t
process_kill (gpgrt_process_t process, unsigned int exitcode)
{
gpg_err_code_t ec = 0;
_gpgrt_pre_syscall ();
if (TerminateProcess (process->hProcess, exitcode))
ec = _gpg_err_code_from_syserror ();
_gpgrt_post_syscall ();
return ec;
}
gpg_err_code_t
_gpgrt_process_vctl (gpgrt_process_t process, unsigned int request,
va_list arg_ptr)
{
switch (request)
{
case GPGRT_PROCESS_NOP:
return 0;
case GPGRT_PROCESS_GET_PROC_ID:
{
int *r_id = va_arg (arg_ptr, int *);
if (r_id == NULL)
return GPG_ERR_INV_VALUE;
*r_id = (int)GetProcessId (process->hProcess);
return 0;
}
case GPGRT_PROCESS_GET_EXIT_ID:
{
int *r_exit_status = va_arg (arg_ptr, int *);
unsigned long exit_code;
*r_exit_status = -1;
if (!process->terminated)
return GPG_ERR_UNFINISHED;
if (process->hProcess == INVALID_HANDLE_VALUE)
return 0;
if (GetExitCodeProcess (process->hProcess, &exit_code) == 0)
return _gpg_err_code_from_syserror ();
*r_exit_status = (int)exit_code;
return 0;
}
case GPGRT_PROCESS_GET_P_HANDLE:
{
HANDLE *r_hProcess = va_arg (arg_ptr, HANDLE *);
if (r_hProcess == NULL)
return GPG_ERR_INV_VALUE;
*r_hProcess = process->hProcess;
process->hProcess = INVALID_HANDLE_VALUE;
return 0;
}
case GPGRT_PROCESS_GET_HANDLES:
{
HANDLE *r_hd_in = va_arg (arg_ptr, HANDLE *);
HANDLE *r_hd_out = va_arg (arg_ptr, HANDLE *);
HANDLE *r_hd_err = va_arg (arg_ptr, HANDLE *);
if (r_hd_in)
{
*r_hd_in = process->hd_in;
process->hd_in = INVALID_HANDLE_VALUE;
}
if (r_hd_out)
{
*r_hd_out = process->hd_out;
process->hd_out = INVALID_HANDLE_VALUE;
}
if (r_hd_err)
{
*r_hd_err = process->hd_err;
process->hd_err = INVALID_HANDLE_VALUE;
}
return 0;
}
case GPGRT_PROCESS_GET_EXIT_CODE:
{
unsigned long *r_exitcode = va_arg (arg_ptr, unsigned long *);
if (!process->terminated)
return GPG_ERR_UNFINISHED;
if (process->hProcess == INVALID_HANDLE_VALUE)
{
*r_exitcode = (unsigned long)-1;
return 0;
}
if (GetExitCodeProcess (process->hProcess, r_exitcode) == 0)
return _gpg_err_code_from_syserror ();
return 0;
}
case GPGRT_PROCESS_KILL_WITH_EC:
{
unsigned int exitcode = va_arg (arg_ptr, unsigned int);
if (process->terminated)
return 0;
if (process->hProcess == INVALID_HANDLE_VALUE)
return 0;
return process_kill (process, exitcode);
}
default:
break;
}
return GPG_ERR_UNKNOWN_COMMAND;
}
gpg_err_code_t
_gpgrt_process_wait (gpgrt_process_t process, int hang)
{
gpg_err_code_t ec;
int code;
if (process->hProcess == INVALID_HANDLE_VALUE)
return 0;
_gpgrt_pre_syscall ();
code = WaitForSingleObject (process->hProcess, hang? INFINITE : 0);
_gpgrt_post_syscall ();
switch (code)
{
case WAIT_TIMEOUT:
ec = GPG_ERR_TIMEOUT; /* Still running. */
break;
case WAIT_FAILED:
_gpgrt_log_info (_("waiting for process failed: ec=%d\n"),
(int)GetLastError ());
ec = GPG_ERR_GENERAL;
break;
case WAIT_OBJECT_0:
process->terminated = 1;
ec = 0;
break;
default:
_gpgrt_log_debug ("WaitForSingleObject returned unexpected code %d\n",
code);
ec = GPG_ERR_GENERAL;
break;
}
return ec;
}
gpg_err_code_t
_gpgrt_process_terminate (gpgrt_process_t process)
{
return process_kill (process, 1);
}
void
_gpgrt_process_release (gpgrt_process_t process)
{
if (!process)
return;
if (!process->terminated)
{
_gpgrt_process_terminate (process);
_gpgrt_process_wait (process, 1);
}
CloseHandle (process->hProcess);
xfree (process);
}
gpg_err_code_t
_gpgrt_process_wait_list (gpgrt_process_t *process_list, int count, int hang)
{
gpg_err_code_t ec = 0;
int i;
for (i = 0; i < count; i++)
{
if (process_list[i]->terminated)
continue;
ec = _gpgrt_process_wait (process_list[i], hang);
if (ec)
break;
}
return ec;
}
|