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
|
/* dcrt0.cc -- essentially the main() for the Cygwin dll
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010
Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "miscfuncs.h"
#include <unistd.h>
#include <stdlib.h>
#include "glob.h"
#include <ctype.h>
#include <locale.h>
#include "environ.h"
#include "sigproc.h"
#include "pinfo.h"
#include "cygerrno.h"
#define NEED_VFORK
#include "perprocess.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "child_info_magic.h"
#include "cygtls.h"
#include "shared_info.h"
#include "cygwin_version.h"
#include "dll_init.h"
#include "heap.h"
#include "tls_pbuf.h"
#include "exception.h"
#include "cygxdr.h"
#include "ntdll.h"
#define MAX_AT_FILE_LEVEL 10
#define PREMAIN_LEN (sizeof (user_data->premain) / sizeof (user_data->premain[0]))
extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
extern "C" void __sinit (_reent *);
static int NO_COPY envc;
static char NO_COPY **envp;
static char title_buf[TITLESIZE + 1];
bool NO_COPY jit_debug;
static void
do_global_dtors ()
{
void (**pfunc) () = user_data->dtors;
if (pfunc)
{
user_data->dtors = NULL;
while (*++pfunc)
(*pfunc) ();
}
}
static void __stdcall
do_global_ctors (void (**in_pfunc)(), int force)
{
if (!force && in_forkee)
return; // inherit constructed stuff from parent pid
/* Run ctors backwards, so skip the first entry and find how many
there are, then run them. */
void (**pfunc) () = in_pfunc;
while (*++pfunc)
;
while (--pfunc > in_pfunc)
(*pfunc) ();
}
/*
* Replaces @file in the command line with the contents of the file.
* There may be multiple @file's in a single command line
* A \@file is replaced with @file so that echo \@foo would print
* @foo and not the contents of foo.
*/
static bool __stdcall
insert_file (char *name, char *&cmd)
{
HANDLE f;
DWORD size;
tmp_pathbuf tp;
PWCHAR wname = tp.w_get ();
sys_mbstowcs (wname, NT_MAX_PATH, name + 1);
f = CreateFileW (wname,
GENERIC_READ, /* open for reading */
FILE_SHARE_READ, /* share for reading */
&sec_none_nih, /* default security */
OPEN_EXISTING, /* existing file only */
FILE_ATTRIBUTE_NORMAL,/* normal file */
NULL); /* no attr. template */
if (f == INVALID_HANDLE_VALUE)
{
debug_printf ("couldn't open file '%s', %E", name);
return false;
}
/* This only supports files up to about 4 billion bytes in
size. I am making the bold assumption that this is big
enough for this feature */
size = GetFileSize (f, NULL);
if (size == 0xFFFFFFFF)
{
debug_printf ("couldn't get file size for '%s', %E", name);
return false;
}
int new_size = strlen (cmd) + size + 2;
char *tmp = (char *) malloc (new_size);
if (!tmp)
{
debug_printf ("malloc failed, %E");
return false;
}
/* realloc passed as it should */
DWORD rf_read;
BOOL rf_result;
rf_result = ReadFile (f, tmp, size, &rf_read, NULL);
CloseHandle (f);
if (!rf_result || (rf_read != size))
{
debug_printf ("ReadFile failed, %E");
return false;
}
tmp[size++] = ' ';
strcpy (tmp + size, cmd);
cmd = tmp;
return true;
}
static inline int
isquote (char c)
{
char ch = c;
return ch == '"' || ch == '\'';
}
/* Step over a run of characters delimited by quotes */
static /*__inline*/ char *
quoted (char *cmd, int winshell)
{
char *p;
char quote = *cmd;
if (!winshell)
{
char *p;
strcpy (cmd, cmd + 1);
if (*(p = strechr (cmd, quote)))
strcpy (p, p + 1);
return p;
}
const char *s = quote == '\'' ? "'" : "\\\"";
/* This must have been run from a Windows shell, so preserve
quotes for globify to play with later. */
while (*cmd && *++cmd)
if ((p = strpbrk (cmd, s)) == NULL)
{
cmd = strchr (cmd, '\0'); // no closing quote
break;
}
else if (*p == '\\')
cmd = ++p;
else if (quote == '"' && p[1] == '"')
{
*p = '\\';
cmd = ++p; // a quoted quote
}
else
{
cmd = p + 1; // point to after end
break;
}
return cmd;
}
/* Perform a glob on word if it contains wildcard characters.
Also quote every character between quotes to force glob to
treat the characters literally. */
static int __stdcall
globify (char *word, char **&argv, int &argc, int &argvlen)
{
if (*word != '~' && strpbrk (word, "?*[\"\'(){}") == NULL)
return 0;
int n = 0;
char *p, *s;
int dos_spec = isdrive (word);
if (!dos_spec && isquote (*word) && word[1] && word[2])
dos_spec = isdrive (word + 1);
/* We'll need more space if there are quoting characters in
word. If that is the case, doubling the size of the
string should provide more than enough space. */
if (strpbrk (word, "'\""))
n = strlen (word);
char pattern[strlen (word) + ((dos_spec + 1) * n) + 1];
/* Fill pattern with characters from word, quoting any
characters found within quotes. */
for (p = pattern, s = word; *s != '\000'; s++, p++)
if (!isquote (*s))
{
if (dos_spec && *s == '\\')
*p++ = '\\';
*p = *s;
}
else
{
char quote = *s;
while (*++s && *s != quote)
{
if (dos_spec || *s != '\\')
/* nothing */;
else if (s[1] == quote || s[1] == '\\')
s++;
*p++ = '\\';
size_t cnt = isascii (*s) ? 1 : mbtowc (NULL, s, MB_CUR_MAX);
if (cnt <= 1 || cnt == (size_t)-1)
*p++ = *s;
else
{
--s;
while (cnt-- > 0)
*p++ = *++s;
}
}
if (*s == quote)
p--;
if (*s == '\0')
break;
}
*p = '\0';
glob_t gl;
gl.gl_offs = 0;
/* Attempt to match the argument. Return just word (minus quoting) if no match. */
if (glob (pattern, GLOB_TILDE | GLOB_NOCHECK | GLOB_BRACE | GLOB_QUOTE, NULL, &gl) || !gl.gl_pathc)
return 0;
/* Allocate enough space in argv for the matched filenames. */
n = argc;
if ((argc += gl.gl_pathc) > argvlen)
{
argvlen = argc + 10;
argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
}
/* Copy the matched filenames to argv. */
char **gv = gl.gl_pathv;
char **av = argv + n;
while (*gv)
{
debug_printf ("argv[%d] = '%s'", n++, *gv);
*av++ = *gv++;
}
/* Clean up after glob. */
free (gl.gl_pathv);
return 1;
}
/* Build argv, argc from string passed from Windows. */
static void __stdcall
build_argv (char *cmd, char **&argv, int &argc, int winshell)
{
int argvlen = 0;
int nesting = 0; // monitor "nesting" from insert_file
argc = 0;
argvlen = 0;
argv = NULL;
/* Scan command line until there is nothing left. */
while (*cmd)
{
/* Ignore spaces */
if (issep (*cmd))
{
cmd++;
continue;
}
/* Found the beginning of an argument. */
char *word = cmd;
char *sawquote = NULL;
while (*cmd)
{
if (*cmd != '"' && (!winshell || *cmd != '\''))
cmd++; // Skip over this character
else
/* Skip over characters until the closing quote */
{
sawquote = cmd;
cmd = quoted (cmd, winshell && argc > 0);
}
if (issep (*cmd)) // End of argument if space
break;
}
if (*cmd)
*cmd++ = '\0'; // Terminate `word'
/* Possibly look for @file construction assuming that this isn't
the very first argument and the @ wasn't quoted */
if (argc && sawquote != word && *word == '@')
{
if (++nesting > MAX_AT_FILE_LEVEL)
api_fatal ("Too many levels of nesting for %s", word);
if (insert_file (word, cmd))
continue; // There's new stuff in cmd now
}
/* See if we need to allocate more space for argv */
if (argc >= argvlen)
{
argvlen = argc + 10;
argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
}
/* Add word to argv file after (optional) wildcard expansion. */
if (!winshell || !argc || !globify (word, argv, argc, argvlen))
{
debug_printf ("argv[%d] = '%s'", argc, word);
argv[argc++] = word;
}
}
argv[argc] = NULL;
debug_printf ("argc %d", argc);
}
/* sanity and sync check */
void __stdcall
check_sanity_and_sync (per_process *p)
{
/* Sanity check to make sure developers didn't change the per_process */
/* struct without updating SIZEOF_PER_PROCESS [it makes them think twice */
/* about changing it]. */
if (sizeof (per_process) != SIZEOF_PER_PROCESS)
api_fatal ("per_process sanity check failed");
/* Make sure that the app and the dll are in sync. */
/* Complain if older than last incompatible change */
if (p->dll_major < CYGWIN_VERSION_DLL_EPOCH)
api_fatal ("cygwin DLL and APP are out of sync -- DLL version mismatch %d < %d",
p->dll_major, CYGWIN_VERSION_DLL_EPOCH);
/* magic_biscuit != 0 if using the old style version numbering scheme. */
if (p->magic_biscuit != SIZEOF_PER_PROCESS)
api_fatal ("Incompatible cygwin .dll -- incompatible per_process info %d != %d",
p->magic_biscuit, SIZEOF_PER_PROCESS);
/* Complain if incompatible API changes made */
if (p->api_major > cygwin_version.api_major)
api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %d > %d",
p->api_major, cygwin_version.api_major);
/* This is a kludge to work around a version of _cygwin_common_crt0
which overwrote the cxx_malloc field with the local DLL copy.
Hilarity ensues if the DLL is not loaded while the process
is forking. */
__cygwin_user_data.cxx_malloc = &default_cygwin_cxx_malloc;
}
child_info NO_COPY *child_proc_info = NULL;
#define CYGWIN_GUARD (PAGE_EXECUTE_READWRITE | PAGE_GUARD)
void
child_info_fork::alloc_stack_hard_way (volatile char *b)
{
void *new_stack_pointer;
MEMORY_BASIC_INFORMATION m;
void *newbase;
int newlen;
bool guard;
if (!VirtualQuery ((LPCVOID) &b, &m, sizeof m))
api_fatal ("fork: couldn't get stack info, %E");
LPBYTE curbot = (LPBYTE) m.BaseAddress + m.RegionSize;
if (stacktop > (LPBYTE) m.AllocationBase && stacktop < curbot)
{
newbase = curbot;
newlen = (LPBYTE) stackbottom - (LPBYTE) curbot;
guard = false;
}
else
{
newbase = (LPBYTE) stacktop - (128 * 1024);
newlen = (LPBYTE) stackbottom - (LPBYTE) newbase;
guard = true;
}
if (!VirtualAlloc (newbase, newlen, MEM_RESERVE, PAGE_NOACCESS))
api_fatal ("fork: can't reserve memory for stack %p - %p, %E",
stacktop, stackbottom);
new_stack_pointer = (void *) ((LPBYTE) stackbottom - (stacksize += 8192));
if (!VirtualAlloc (new_stack_pointer, stacksize, MEM_COMMIT,
PAGE_EXECUTE_READWRITE))
api_fatal ("fork: can't commit memory for stack %p(%d), %E",
new_stack_pointer, stacksize);
if (!VirtualQuery ((LPCVOID) new_stack_pointer, &m, sizeof m))
api_fatal ("fork: couldn't get new stack info, %E");
if (guard)
{
m.BaseAddress = (LPBYTE) m.BaseAddress - 1;
if (!VirtualAlloc ((LPVOID) m.BaseAddress, 1, MEM_COMMIT,
CYGWIN_GUARD))
api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
m.BaseAddress);
}
if (!VirtualQuery ((LPCVOID) m.BaseAddress, &m, sizeof m))
api_fatal ("fork: couldn't get new stack info, %E");
stacktop = m.BaseAddress;
b[0] = '\0';
}
void *getstack (void *) __attribute__ ((noinline));
volatile char *
getstack (volatile char * volatile p)
{
*p ^= 1;
*p ^= 1;
return p - 4096;
}
/* extend the stack prior to fork longjmp */
void
child_info_fork::alloc_stack ()
{
volatile char * volatile esp;
__asm__ volatile ("movl %%esp,%0": "=r" (esp));
if (_tlsbase != stackbottom)
alloc_stack_hard_way (esp);
else
{
char *st = (char *) stacktop - 4096;
while (_tlstop >= st)
esp = getstack (esp);
stacksize = 0;
}
}
extern "C" void
break_here ()
{
static int NO_COPY sent_break;
if (!sent_break++)
DebugBreak ();
debug_printf ("break here");
}
static void
initial_env ()
{
if (GetEnvironmentVariableA ("CYGWIN_TESTING", NULL, 0))
_cygwin_testing = 1;
#ifdef DEBUGGING
char buf[NT_MAX_PATH];
DWORD len;
if (GetEnvironmentVariableA ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
{
DWORD ms = atoi (buf);
console_printf ("Sleeping %d, pid %u %P\n", ms, GetCurrentProcessId ());
Sleep (ms);
if (!strace.active () && !dynamically_loaded)
strace.hello ();
}
if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
{
char buf1[NT_MAX_PATH];
len = GetModuleFileName (NULL, buf1, NT_MAX_PATH);
strlwr (buf1);
strlwr (buf);
char *p = strpbrk (buf, ":=");
if (!p)
p = (char *) "gdb.exe -nw";
else
*p++ = '\0';
if (strstr (buf1, buf))
{
error_start_init (p);
jit_debug = true;
try_to_debug ();
console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n");
break_here ();
}
}
#endif
}
child_info *
get_cygwin_startup_info ()
{
STARTUPINFO si;
GetStartupInfo (&si);
child_info *res = (child_info *) si.lpReserved2;
if (si.cbReserved2 < EXEC_MAGIC_SIZE || !res
|| res->intro != PROC_MAGIC_GENERIC || res->magic != CHILD_INFO_MAGIC)
res = NULL;
else
{
if ((res->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
multiple_cygwin_problem ("proc intro", res->intro, 0);
else if (res->cygheap != (void *) &_cygheap_start)
multiple_cygwin_problem ("cygheap base", (DWORD) res->cygheap,
(DWORD) &_cygheap_start);
unsigned should_be_cb = 0;
switch (res->type)
{
case _PROC_FORK:
in_forkee = true;
should_be_cb = sizeof (child_info_fork);
/* fall through */;
case _PROC_SPAWN:
case _PROC_EXEC:
if (!should_be_cb)
should_be_cb = sizeof (child_info_spawn);
if (should_be_cb != res->cb)
multiple_cygwin_problem ("proc size", res->cb, should_be_cb);
else if (sizeof (fhandler_union) != res->fhandler_union_cb)
multiple_cygwin_problem ("fhandler size", res->fhandler_union_cb, sizeof (fhandler_union));
if (res->isstraced ())
{
res->ready (false);
for (unsigned i = 0; !being_debugged () && i < 10000; i++)
yield ();
strace.hello ();
}
break;
default:
system_printf ("unknown exec type %d", res->type);
/* intentionally fall through */
case _PROC_WHOOPS:
res = NULL;
break;
}
}
return res;
}
#define dll_data_start &_data_start__
#define dll_data_end &_data_end__
#define dll_bss_start &_bss_start__
#define dll_bss_end &_bss_end__
void
child_info_fork::handle_fork ()
{
cygheap_fixup_in_child (false);
memory_init (false);
myself.thisproc (NULL);
myself->uid = cygheap->user.real_uid;
myself->gid = cygheap->user.real_gid;
child_copy (parent, false,
"dll data", dll_data_start, dll_data_end,
"dll bss", dll_bss_start, dll_bss_end,
"user heap", cygheap->user_heap.base, cygheap->user_heap.ptr,
NULL);
/* Do the relocations here. These will actually likely be overwritten by the
below child_copy but we do them here in case there is a read-only section
which does not get copied by fork. */
_pei386_runtime_relocator (user_data);
/* step 2 now that the dll has its heap filled in, we can fill in the
user's data and bss since user_data is now filled out. */
child_copy (parent, false,
"data", user_data->data_start, user_data->data_end,
"bss", user_data->bss_start, user_data->bss_end,
NULL);
if (fixup_mmaps_after_fork (parent))
api_fatal ("recreate_mmaps_after_fork_failed");
}
void
child_info_spawn::handle_spawn ()
{
extern void fixup_lockf_after_exec ();
HANDLE h;
cygheap_fixup_in_child (true);
memory_init (false);
if (!moreinfo->myself_pinfo ||
!DuplicateHandle (GetCurrentProcess (), moreinfo->myself_pinfo,
GetCurrentProcess (), &h, 0,
FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
h = NULL;
myself.thisproc (h);
__argc = moreinfo->argc;
__argv = moreinfo->argv;
envp = moreinfo->envp;
envc = moreinfo->envc;
if (!dynamically_loaded)
cygheap->fdtab.fixup_after_exec ();
if (__stdin >= 0)
cygheap->fdtab.move_fd (__stdin, 0);
if (__stdout >= 0)
cygheap->fdtab.move_fd (__stdout, 1);
cygheap->user.groups.clear_supp ();
ready (true);
/* Need to do this after debug_fixup_after_fork_exec or DEBUGGING handling of
handles might get confused. */
CloseHandle (child_proc_info->parent);
child_proc_info->parent = NULL;
signal_fixup_after_exec ();
if (moreinfo->old_title)
{
old_title = strcpy (title_buf, moreinfo->old_title);
cfree (moreinfo->old_title);
}
fixup_lockf_after_exec ();
}
#if 0
/* Setting the TS-aware flag in the application's PE header is sufficient.
Just keep this in as a reminder. */
static DEP_SYSTEM_POLICY_TYPE dep_system_policy = (DEP_SYSTEM_POLICY_TYPE) -1;
static void
disable_dep ()
{
DWORD ppolicy;
BOOL perm;
if (dep_system_policy < 0)
{
dep_system_policy = GetSystemDEPPolicy ();
debug_printf ("DEP System Policy: %d", (int) dep_system_policy);
}
if (dep_system_policy < OptIn)
return;
if (!GetProcessDEPPolicy (GetCurrentProcess (), &ppolicy, &perm))
{
debug_printf ("GetProcessDEPPolicy: %E");
return;
}
debug_printf ("DEP Process Policy: %d (permanent = %d)", ppolicy, perm);
if (ppolicy > 0 && !perm && !SetProcessDEPPolicy (0))
debug_printf ("SetProcessDEPPolicy: %E");
}
#endif
/* Retrieve and store system directory for later use. Note that the
directory is stored with a trailing backslash! */
static void
init_windows_system_directory ()
{
windows_system_directory_length =
GetSystemDirectoryW (windows_system_directory, MAX_PATH);
if (windows_system_directory_length == 0)
api_fatal ("can't find windows system directory");
windows_system_directory[windows_system_directory_length++] = L'\\';
windows_system_directory[windows_system_directory_length] = L'\0';
}
void __stdcall
dll_crt0_0 ()
{
init_windows_system_directory ();
init_global_security ();
initial_env ();
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
lock_process::init ();
_impure_ptr = _GLOBAL_REENT;
_impure_ptr->_stdin = &_impure_ptr->__sf[0];
_impure_ptr->_stdout = &_impure_ptr->__sf[1];
_impure_ptr->_stderr = &_impure_ptr->__sf[2];
_impure_ptr->_current_locale = "C";
user_data->impure_ptr = _impure_ptr;
user_data->impure_ptr_ptr = &_impure_ptr;
DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
GetCurrentProcess (), &hMainThread,
0, false, DUPLICATE_SAME_ACCESS);
OpenProcessToken (GetCurrentProcess (), MAXIMUM_ALLOWED, &hProcToken);
set_cygwin_privileges (hProcToken);
device::init ();
do_global_ctors (&__CTOR_LIST__, 1);
cygthread::init ();
child_proc_info = get_cygwin_startup_info ();
if (!child_proc_info)
memory_init (true);
else
{
cygwin_user_h = child_proc_info->user_h;
switch (child_proc_info->type)
{
case _PROC_FORK:
fork_info->handle_fork ();
break;
case _PROC_SPAWN:
case _PROC_EXEC:
spawn_info->handle_spawn ();
break;
}
}
user_data->threadinterface->Init ();
_cygtls::init ();
/* Initialize events */
events_init ();
tty_list::init_session ();
#if 0
/* Setting the TS-aware flag in the application's PE header is sufficient.
Just keep this in as a reminder. */
/* The disable_dep function disables DEP for all Cygwin processes if
the process runs on a Windows Server 2008 with Terminal Services
installed. This combination (TS+DEP) breaks *some* Cygwin
applications. The Terminal Service specific DLL tsappcmp.dll
changes the page protection of some pages in the application's text
segment from PAGE_EXECUTE_WRITECOPY to PAGE_WRITECOPY for no
apparent reason. This occurs before any Cygwin or applicaton code
had a chance to run. MS has no explanation for this so far, but is
rather busy trying to avoid giving support for this problem (as of
2008-11-11).
Unfortunately disabling DEP seems to have a not negligible
performance hit. In the long run, either MS has to fix their
problem, or we have to find a better workaround, if any exists.
Idle idea: Adding EXECUTE protection to all text segment pages? */
if (wincap.ts_has_dep_problem ())
disable_dep ();
#endif
_main_tls = &_my_tls;
/* Initialize signal processing here, early, in the hopes that the creation
of a thread early in the process will cause more predictability in memory
layout for the main thread. */
if (!wincap.has_buggy_thread_startup () && !dynamically_loaded)
sigproc_init ();
debug_printf ("finished dll_crt0_0 initialization");
}
/* Take over from libc's crt0.o and start the application. Note the
various special cases when Cygwin DLL is being runtime loaded (as
opposed to being link-time loaded by Cygwin apps) from a non
cygwin app via LoadLibrary. */
void
dll_crt0_1 (void *)
{
extern void initial_setlocale ();
if (wincap.has_buggy_thread_startup () || dynamically_loaded)
sigproc_init ();
check_sanity_and_sync (user_data);
/* Initialize malloc and then call user_shared_initialize since it relies
on a functioning malloc and it's possible that the user's program may
have overridden malloc. We only know about that at this stage,
unfortunately. */
malloc_init ();
user_shared->initialize ();
#ifdef CGF
int i = 0;
const int n = 2 * 1024 * 1024;
while (i--)
small_printf ("cmalloc returns %p\n", cmalloc (HEAP_STR, n));
#endif
ProtectHandle (hMainThread);
cygheap->cwd.init ();
/* Initialize pthread mainthread when not forked and it is safe to call new,
otherwise it is reinitalized in fixup_after_fork */
if (!in_forkee)
{
pthread::init_mainthread ();
_pei386_runtime_relocator (user_data);
}
#ifdef DEBUGGING
strace.microseconds ();
#endif
create_signal_arrived (); /* FIXME: move into wait_sig? */
/* Initialize debug muto, if DLL is built with --enable-debugging.
Need to do this before any helper threads start. */
debug_init ();
#ifdef NEWVFORK
cygheap->fdtab.vfork_child_fixup ();
main_vfork = vfork_storage.create ();
#endif
cygbench ("pre-forkee");
if (in_forkee)
{
/* If we've played with the stack, stacksize != 0. That means that
fork() was invoked from other than the main thread. Make sure that
frame pointer is referencing the new stack so that the OS knows what
to do when it needs to increase the size of the stack.
NOTE: Don't do anything that involves the stack until you've completed
this step. */
if (fork_info->stacksize)
{
_tlsbase = (char *) fork_info->stackbottom;
_tlstop = (char *) fork_info->stacktop;
}
longjmp (fork_info->jmp, true);
}
__sinit (_impure_ptr);
#ifdef DEBUGGING
{
extern void fork_init ();
fork_init ();
}
#endif
pinfo_init (envp, envc);
if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
old_title = title_buf;
/* Allocate cygheap->fdtab */
dtable_init ();
uinfo_init (); /* initialize user info */
/* Connect to tty. */
tty::init_session ();
/* Set internal locale to the environment settings. */
initial_setlocale ();
if (!__argc)
{
PWCHAR wline = GetCommandLineW ();
size_t size = sys_wcstombs (NULL, 0, wline);
char *line = (char *) alloca (size);
sys_wcstombs (line, size, wline);
/* Scan the command line and build argv. Expand wildcards if not
called from another cygwin process. */
build_argv (line, __argv, __argc,
NOTSTATE (myself, PID_CYGPARENT) && allow_glob);
/* Convert argv[0] to posix rules if it's currently blatantly
win32 style. */
if ((strchr (__argv[0], ':')) || (strchr (__argv[0], '\\')))
{
char *new_argv0 = (char *) malloc (NT_MAX_PATH);
cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, __argv[0],
new_argv0, NT_MAX_PATH);
__argv[0] = (char *) realloc (new_argv0, strlen (new_argv0) + 1);
}
}
__argc_safe = __argc;
if (user_data->premain[0])
for (unsigned int i = 0; i < PREMAIN_LEN / 2; i++)
user_data->premain[i] (__argc, __argv, user_data);
/* Set up standard fds in file descriptor table. */
cygheap->fdtab.stdio_init ();
/* Set up __progname for getopt error call. */
if (__argv[0] && (__progname = strrchr (__argv[0], '/')))
++__progname;
else
__progname = __argv[0];
program_invocation_name = __argv[0];
program_invocation_short_name = __progname;
if (__progname)
{
char *cp = strchr (__progname, '\0') - 4;
if (cp > __progname && ascii_strcasematch (cp, ".exe"))
*cp = '\0';
}
/* Set new console title if appropriate. */
if (display_title && !dynamically_loaded)
{
char *cp = __progname;
if (strip_title_path)
for (char *ptr = cp; *ptr && *ptr != ' '; ptr++)
if (isdirsep (*ptr))
cp = ptr + 1;
set_console_title (cp);
}
(void) xdr_set_vprintf (&cygxdr_vwarnx);
cygwin_finished_initializing = true;
/* Call init of loaded dlls. */
dlls.init ();
/* Execute any specified "premain" functions */
if (user_data->premain[PREMAIN_LEN / 2])
for (unsigned int i = PREMAIN_LEN / 2; i < PREMAIN_LEN; i++)
user_data->premain[i] (__argc, __argv, user_data);
set_errno (0);
if (dynamically_loaded)
return;
/* Disable case-insensitive globbing */
ignore_case_with_glob = false;
MALLOC_CHECK;
cygbench (__progname);
ld_preload ();
/* Per POSIX set the default application locale back to "C". */
_setlocale_r (_REENT, LC_CTYPE, "C");
if (user_data->main)
cygwin_exit (user_data->main (__argc, __argv, *user_data->envptr));
__asm__ (" \n\
.global __cygwin_exit_return \n\
__cygwin_exit_return: \n\
");
}
extern "C" void __stdcall
_dll_crt0 ()
{
main_environ = user_data->envptr;
if (in_forkee)
{
fork_info->alloc_stack ();
_main_tls = &_my_tls;
}
_main_tls->call ((DWORD (*) (void *, void *)) dll_crt0_1, NULL);
}
void
dll_crt0 (per_process *uptr)
{
/* Set the local copy of the pointer into the user space. */
if (!in_forkee && uptr && uptr != user_data)
{
memcpy (user_data, uptr, per_process_overwrite);
*(user_data->impure_ptr_ptr) = _GLOBAL_REENT;
}
_dll_crt0 ();
}
/* This must be called by anyone who uses LoadLibrary to load cygwin1.dll.
You must have CYGTLS_PADSIZE bytes reserved at the bottom of the stack
calling this function, and that storage must not be overwritten until you
unload cygwin1.dll, as it is used for _my_tls. It is best to load
cygwin1.dll before spawning any additional threads in your process.
See winsup/testsuite/cygload for an example of how to use cygwin1.dll
from MSVC and non-cygwin MinGW applications. */
extern "C" void
cygwin_dll_init ()
{
static char **envp;
static int _fmode;
user_data->magic_biscuit = sizeof (per_process);
user_data->envptr = &envp;
user_data->fmode_ptr = &_fmode;
_dll_crt0 ();
}
extern "C" void
__main (void)
{
/* Ordering is critical here. DLL ctors have already been
run as they were being loaded, so we should stack the
queued call to DLL dtors now. */
atexit (dll_global_dtors);
do_global_ctors (user_data->ctors, false);
/* Now we have run global ctors, register their dtors.
At exit, global dtors will run first, so the app can still
use shared library functions while terminating; then the
DLLs will be destroyed; finally newlib will shut down stdio
and terminate itself. */
atexit (do_global_dtors);
sig_dispatch_pending (true);
}
void __stdcall
do_exit (int status)
{
syscall_printf ("do_exit (%d), exit_state %d", status, exit_state);
#ifdef NEWVFORK
vfork_save *vf = vfork_storage.val ();
if (vf != NULL && vf->pid < 0)
{
exit_state = ES_NOT_EXITING;
vf->restore_exit (status);
}
#endif
lock_process until_exit (true);
if (exit_state < ES_EVENTS_TERMINATE)
{
exit_state = ES_EVENTS_TERMINATE;
events_terminate ();
}
if (exit_state < ES_SIGNAL)
{
exit_state = ES_SIGNAL;
signal (SIGCHLD, SIG_IGN);
signal (SIGHUP, SIG_IGN);
signal (SIGINT, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
}
if (exit_state < ES_CLOSEALL)
{
exit_state = ES_CLOSEALL;
close_all_files ();
}
UINT n = (UINT) status;
if (exit_state < ES_THREADTERM)
{
exit_state = ES_THREADTERM;
cygthread::terminate ();
}
myself->stopsig = 0;
if (exit_state < ES_HUP_PGRP)
{
exit_state = ES_HUP_PGRP;
/* Kill orphaned children on group leader exit */
if (myself->has_pgid_children && myself->pid == myself->pgid)
{
siginfo_t si = {0};
si.si_signo = -SIGHUP;
si.si_code = SI_KERNEL;
sigproc_printf ("%d == pgrp %d, send SIG{HUP,CONT} to stopped children",
myself->pid, myself->pgid);
kill_pgrp (myself->pgid, si);
}
}
if (exit_state < ES_HUP_SID)
{
exit_state = ES_HUP_SID;
/* Kill the foreground process group on session leader exit */
if (getpgrp () > 0 && myself->pid == myself->sid && real_tty_attached (myself))
{
tty *tp = cygwin_shared->tty[myself->ctty];
sigproc_printf ("%d == sid %d, send SIGHUP to children",
myself->pid, myself->sid);
/* CGF FIXME: This can't be right. */
if (tp->getsid () == myself->sid)
tp->kill_pgrp (SIGHUP);
}
}
if (exit_state < ES_TITLE)
{
exit_state = ES_TITLE;
/* restore console title */
if (old_title && display_title)
set_console_title (old_title);
}
if (exit_state < ES_TTY_TERMINATE)
{
exit_state = ES_TTY_TERMINATE;
cygwin_shared->tty.terminate ();
}
myself.exit (n);
}
extern "C" int
cygwin_atexit (void (*fn) (void))
{
int res;
dll *d = dlls.find ((void *) _my_tls.retaddr ());
res = d ? __cxa_atexit ((void (*) (void *)) fn, NULL, d) : atexit (fn);
return res;
}
extern "C" void
cygwin_exit (int n)
{
exit_state = ES_EXIT_STARTING;
exit (n);
}
extern "C" void
_exit (int n)
{
do_exit (((DWORD) n & 0xff) << 8);
}
extern "C" void cygwin_stackdump ();
extern "C" void
__api_fatal (const char *fmt, ...)
{
char buf[4096];
va_list ap;
va_start (ap, fmt);
int n = __small_sprintf (buf, "%P: *** fatal error - ");
__small_vsprintf (buf + n, fmt, ap);
va_end (ap);
strace.prntf (_STRACE_SYSTEM, NULL, "%s", buf);
#ifdef DEBUGGING
try_to_debug ();
#endif
cygwin_stackdump ();
myself.exit (__api_fatal_exit_val);
}
void
multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned version)
{
if (_cygwin_testing && (strstr (what, "proc") || strstr (what, "cygheap")))
{
child_proc_info->type = _PROC_WHOOPS;
return;
}
if (GetEnvironmentVariableA ("CYGWIN_MISMATCH_OK", NULL, 0))
return;
if (CYGWIN_VERSION_MAGIC_VERSION (magic_version) == version)
system_printf ("%s magic number mismatch detected - %p/%p", what, magic_version, version);
else
api_fatal ("%s mismatch detected - %p/%p.\n\
This problem is probably due to using incompatible versions of the cygwin DLL.\n\
Search for cygwin1.dll using the Windows Start->Find/Search facility\n\
and delete all but the most recent version. The most recent version *should*\n\
reside in x:\\cygwin\\bin, where 'x' is the drive on which you have\n\
installed the cygwin distribution. Rebooting is also suggested if you\n\
are unable to find another cygwin DLL.",
what, magic_version, version);
}
#ifdef DEBUGGING
void __stdcall
cygbench (const char *s)
{
if (GetEnvironmentVariableA ("CYGWIN_BENCH", NULL, 0))
small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
}
#endif
|