summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/src/md/windows/ntmisc.c
blob: 1ec6c2a5e5cdb069e83ddba85672a4b30779f782 (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
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * ntmisc.c
 *
 */

#include "primpl.h"
#include <math.h>     /* for fabs() */
#include <windows.h>

char *_PR_MD_GET_ENV(const char *name)
{
    return getenv(name);
}

/*
** _PR_MD_PUT_ENV() -- add or change environment variable
**
**
*/
PRIntn _PR_MD_PUT_ENV(const char *name)
{
    return(putenv(name));
}


/*
 **************************************************************************
 **************************************************************************
 **
 **     Date and time routines
 **
 **************************************************************************
 **************************************************************************
 */

/*
 * The NSPR epoch (00:00:00 1 Jan 1970 UTC) in FILETIME.
 * We store the value in a PRTime variable for convenience.
 */
#ifdef __GNUC__
const PRTime _pr_filetime_offset = 116444736000000000LL;
const PRTime _pr_filetime_divisor = 10LL;
#else
const PRTime _pr_filetime_offset = 116444736000000000i64;
const PRTime _pr_filetime_divisor = 10i64;
#endif

#ifdef WINCE

#define FILETIME_TO_INT64(ft) \
  (((PRInt64)ft.dwHighDateTime) << 32 | (PRInt64)ft.dwLowDateTime)

static void
LowResTime(LPFILETIME lpft)
{
    GetCurrentFT(lpft);
}

typedef struct CalibrationData {
    long double freq;         /* The performance counter frequency */
    long double offset;       /* The low res 'epoch' */
    long double timer_offset; /* The high res 'epoch' */

    /* The last high res time that we returned since recalibrating */
    PRInt64 last;

    PRBool calibrated;

    CRITICAL_SECTION data_lock;
    CRITICAL_SECTION calibration_lock;
    PRInt64 granularity;
} CalibrationData;

static CalibrationData calibration;

typedef void (*GetSystemTimeAsFileTimeFcn)(LPFILETIME);
static GetSystemTimeAsFileTimeFcn ce6_GetSystemTimeAsFileTime = NULL;

static void
NowCalibrate(void)
{
    FILETIME ft, ftStart;
    LARGE_INTEGER liFreq, now;

    if (calibration.freq == 0.0) {
        if(!QueryPerformanceFrequency(&liFreq)) {
            /* High-performance timer is unavailable */
            calibration.freq = -1.0;
        } else {
            calibration.freq = (long double) liFreq.QuadPart;
        }
    }
    if (calibration.freq > 0.0) {
        PRInt64 calibrationDelta = 0;
        /*
         * By wrapping a timeBegin/EndPeriod pair of calls around this loop,
         * the loop seems to take much less time (1 ms vs 15ms) on Vista.
         */
        timeBeginPeriod(1);
        LowResTime(&ftStart);
        do {
            LowResTime(&ft);
        } while (memcmp(&ftStart,&ft, sizeof(ft)) == 0);
        timeEndPeriod(1);

        calibration.granularity =
            (FILETIME_TO_INT64(ft) - FILETIME_TO_INT64(ftStart))/10;

        QueryPerformanceCounter(&now);

        calibration.offset = (long double) FILETIME_TO_INT64(ft);
        calibration.timer_offset = (long double) now.QuadPart;
        /*
         * The windows epoch is around 1600. The unix epoch is around 1970.
         * _pr_filetime_offset is the difference (in windows time units which
         * are 10 times more highres than the JS time unit)
         */
        calibration.offset -= _pr_filetime_offset;
        calibration.offset *= 0.1;
        calibration.last = 0;

        calibration.calibrated = PR_TRUE;
    }
}

#define CALIBRATIONLOCK_SPINCOUNT 0
#define DATALOCK_SPINCOUNT 4096
#define LASTLOCK_SPINCOUNT 4096

void
_MD_InitTime(void)
{
    /* try for CE6 GetSystemTimeAsFileTime first */
    HANDLE h = GetModuleHandleW(L"coredll.dll");
    ce6_GetSystemTimeAsFileTime = (GetSystemTimeAsFileTimeFcn)
                                  GetProcAddressA(h, "GetSystemTimeAsFileTime");

    /* otherwise go the slow route */
    if (ce6_GetSystemTimeAsFileTime == NULL) {
        memset(&calibration, 0, sizeof(calibration));
        NowCalibrate();
        InitializeCriticalSection(&calibration.calibration_lock);
        InitializeCriticalSection(&calibration.data_lock);
    }
}

void
_MD_CleanupTime(void)
{
    if (ce6_GetSystemTimeAsFileTime == NULL) {
        DeleteCriticalSection(&calibration.calibration_lock);
        DeleteCriticalSection(&calibration.data_lock);
    }
}

#define MUTEX_SETSPINCOUNT(m, c)

/*
 *-----------------------------------------------------------------------
 *
 * PR_Now --
 *
 *     Returns the current time in microseconds since the epoch.
 *     The epoch is midnight January 1, 1970 GMT.
 *     The implementation is machine dependent.  This is the
 *     implementation for Windows.
 *     Cf. time_t time(time_t *tp)
 *
 *-----------------------------------------------------------------------
 */

PR_IMPLEMENT(PRTime)
PR_Now(void)
{
    long double lowresTime, highresTimerValue;
    FILETIME ft;
    LARGE_INTEGER now;
    PRBool calibrated = PR_FALSE;
    PRBool needsCalibration = PR_FALSE;
    PRInt64 returnedTime;
    long double cachedOffset = 0.0;

    if (ce6_GetSystemTimeAsFileTime) {
        union {
            FILETIME ft;
            PRTime prt;
        } currentTime;

        PR_ASSERT(sizeof(FILETIME) == sizeof(PRTime));

        ce6_GetSystemTimeAsFileTime(&currentTime.ft);

        /* written this way on purpose, since the second term becomes
         * a constant, and the entire expression is faster to execute.
         */
        return currentTime.prt/_pr_filetime_divisor -
               _pr_filetime_offset/_pr_filetime_divisor;
    }

    do {
        if (!calibration.calibrated || needsCalibration) {
            EnterCriticalSection(&calibration.calibration_lock);
            EnterCriticalSection(&calibration.data_lock);

            /* Recalibrate only if no one else did before us */
            if (calibration.offset == cachedOffset) {
                /*
                 * Since calibration can take a while, make any other
                 * threads immediately wait
                 */
                MUTEX_SETSPINCOUNT(&calibration.data_lock, 0);

                NowCalibrate();

                calibrated = PR_TRUE;

                /* Restore spin count */
                MUTEX_SETSPINCOUNT(&calibration.data_lock, DATALOCK_SPINCOUNT);
            }
            LeaveCriticalSection(&calibration.data_lock);
            LeaveCriticalSection(&calibration.calibration_lock);
        }

        /* Calculate a low resolution time */
        LowResTime(&ft);
        lowresTime =
            ((long double)(FILETIME_TO_INT64(ft) - _pr_filetime_offset)) * 0.1;

        if (calibration.freq > 0.0) {
            long double highresTime, diff;
            DWORD timeAdjustment, timeIncrement;
            BOOL timeAdjustmentDisabled;

            /* Default to 15.625 ms if the syscall fails */
            long double skewThreshold = 15625.25;

            /* Grab high resolution time */
            QueryPerformanceCounter(&now);
            highresTimerValue = (long double)now.QuadPart;

            EnterCriticalSection(&calibration.data_lock);
            highresTime = calibration.offset + 1000000L *
                          (highresTimerValue-calibration.timer_offset)/calibration.freq;
            cachedOffset = calibration.offset;

            /*
             * On some dual processor/core systems, we might get an earlier
             * time so we cache the last time that we returned.
             */
            calibration.last = PR_MAX(calibration.last,(PRInt64)highresTime);
            returnedTime = calibration.last;
            LeaveCriticalSection(&calibration.data_lock);

            /* Get an estimate of clock ticks per second from our own test */
            skewThreshold = calibration.granularity;
            /* Check for clock skew */
            diff = lowresTime - highresTime;

            /*
             * For some reason that I have not determined, the skew can be
             * up to twice a kernel tick. This does not seem to happen by
             * itself, but I have only seen it triggered by another program
             * doing some kind of file I/O. The symptoms are a negative diff
             * followed by an equally large positive diff.
             */
            if (fabs(diff) > 2*skewThreshold) {
                if (calibrated) {
                    /*
                     * If we already calibrated once this instance, and the
                     * clock is still skewed, then either the processor(s) are
                     * wildly changing clockspeed or the system is so busy that
                     * we get switched out for long periods of time. In either
                     * case, it would be infeasible to make use of high
                     * resolution results for anything, so let's resort to old
                     * behavior for this call. It's possible that in the
                     * future, the user will want the high resolution timer, so
                     * we don't disable it entirely.
                     */
                    returnedTime = (PRInt64)lowresTime;
                    needsCalibration = PR_FALSE;
                } else {
                    /*
                     * It is possible that when we recalibrate, we will return
                     * a value less than what we have returned before; this is
                     * unavoidable. We cannot tell the different between a
                     * faulty QueryPerformanceCounter implementation and user
                     * changes to the operating system time. Since we must
                     * respect user changes to the operating system time, we
                     * cannot maintain the invariant that Date.now() never
                     * decreases; the old implementation has this behavior as
                     * well.
                     */
                    needsCalibration = PR_TRUE;
                }
            } else {
                /* No detectable clock skew */
                returnedTime = (PRInt64)highresTime;
                needsCalibration = PR_FALSE;
            }
        } else {
            /* No high resolution timer is available, so fall back */
            returnedTime = (PRInt64)lowresTime;
        }
    } while (needsCalibration);

    return returnedTime;
}

#else

PR_IMPLEMENT(PRTime)
PR_Now(void)
{
    PRTime prt;
    FILETIME ft;
    SYSTEMTIME st;

    GetSystemTime(&st);
    SystemTimeToFileTime(&st, &ft);
    _PR_FileTimeToPRTime(&ft, &prt);
    return prt;
}

#endif

/*
 ***********************************************************************
 ***********************************************************************
 *
 * Process creation routines
 *
 ***********************************************************************
 ***********************************************************************
 */

/*
 * Assemble the command line by concatenating the argv array.
 * On success, this function returns 0 and the resulting command
 * line is returned in *cmdLine.  On failure, it returns -1.
 */
static int assembleCmdLine(char *const *argv, char **cmdLine)
{
    char *const *arg;
    char *p, *q;
    size_t cmdLineSize;
    int numBackslashes;
    int i;
    int argNeedQuotes;

    /*
     * Find out how large the command line buffer should be.
     */
    cmdLineSize = 0;
    for (arg = argv; *arg; arg++) {
        /*
         * \ and " need to be escaped by a \.  In the worst case,
         * every character is a \ or ", so the string of length
         * may double.  If we quote an argument, that needs two ".
         * Finally, we need a space between arguments, and
         * a null byte at the end of command line.
         */
        cmdLineSize += 2 * strlen(*arg)  /* \ and " need to be escaped */
                       + 2                      /* we quote every argument */
                       + 1;                     /* space in between, or final null */
    }
    p = *cmdLine = PR_MALLOC((PRUint32) cmdLineSize);
    if (p == NULL) {
        return -1;
    }

    for (arg = argv; *arg; arg++) {
        /* Add a space to separates the arguments */
        if (arg != argv) {
            *p++ = ' ';
        }
        q = *arg;
        numBackslashes = 0;
        argNeedQuotes = 0;

        /*
         * If the argument is empty or contains white space, it needs to
         * be quoted.
         */
        if (**arg == '\0' || strpbrk(*arg, " \f\n\r\t\v")) {
            argNeedQuotes = 1;
        }

        if (argNeedQuotes) {
            *p++ = '"';
        }
        while (*q) {
            if (*q == '\\') {
                numBackslashes++;
                q++;
            } else if (*q == '"') {
                if (numBackslashes) {
                    /*
                     * Double the backslashes since they are followed
                     * by a quote
                     */
                    for (i = 0; i < 2 * numBackslashes; i++) {
                        *p++ = '\\';
                    }
                    numBackslashes = 0;
                }
                /* To escape the quote */
                *p++ = '\\';
                *p++ = *q++;
            } else {
                if (numBackslashes) {
                    /*
                     * Backslashes are not followed by a quote, so
                     * don't need to double the backslashes.
                     */
                    for (i = 0; i < numBackslashes; i++) {
                        *p++ = '\\';
                    }
                    numBackslashes = 0;
                }
                *p++ = *q++;
            }
        }

        /* Now we are at the end of this argument */
        if (numBackslashes) {
            /*
             * Double the backslashes if we have a quote string
             * delimiter at the end.
             */
            if (argNeedQuotes) {
                numBackslashes *= 2;
            }
            for (i = 0; i < numBackslashes; i++) {
                *p++ = '\\';
            }
        }
        if (argNeedQuotes) {
            *p++ = '"';
        }
    }

    *p = '\0';
    return 0;
}

/*
 * Assemble the environment block by concatenating the envp array
 * (preserving the terminating null byte in each array element)
 * and adding a null byte at the end.
 *
 * Returns 0 on success.  The resulting environment block is returned
 * in *envBlock.  Note that if envp is NULL, a NULL pointer is returned
 * in *envBlock.  Returns -1 on failure.
 */
static int assembleEnvBlock(char **envp, char **envBlock)
{
    char *p;
    char *q;
    char **env;
    char *curEnv;
    char *cwdStart, *cwdEnd;
    size_t envBlockSize;

    if (envp == NULL) {
        *envBlock = NULL;
        return 0;
    }

#ifdef WINCE
    {
        PRUnichar *wideCurEnv = mozce_GetEnvString();
        int len = WideCharToMultiByte(CP_ACP, 0, wideCurEnv, -1,
                                      NULL, 0, NULL, NULL);
        curEnv = (char *) PR_MALLOC(len * sizeof(char));
        WideCharToMultiByte(CP_ACP, 0, wideCurEnv, -1,
                            curEnv, len, NULL, NULL);
        free(wideCurEnv);
    }
#else
    curEnv = GetEnvironmentStrings();
#endif

    cwdStart = curEnv;
    while (*cwdStart) {
        if (cwdStart[0] == '=' && cwdStart[1] != '\0'
            && cwdStart[2] == ':' && cwdStart[3] == '=') {
            break;
        }
        cwdStart += strlen(cwdStart) + 1;
    }
    cwdEnd = cwdStart;
    if (*cwdEnd) {
        cwdEnd += strlen(cwdEnd) + 1;
        while (*cwdEnd) {
            if (cwdEnd[0] != '=' || cwdEnd[1] == '\0'
                || cwdEnd[2] != ':' || cwdEnd[3] != '=') {
                break;
            }
            cwdEnd += strlen(cwdEnd) + 1;
        }
    }
    envBlockSize = cwdEnd - cwdStart;

    for (env = envp; *env; env++) {
        envBlockSize += strlen(*env) + 1;
    }
    envBlockSize++;

    p = *envBlock = PR_MALLOC((PRUint32) envBlockSize);
    if (p == NULL) {
#ifdef WINCE
        PR_Free(curEnv);
#else
        FreeEnvironmentStrings(curEnv);
#endif
        return -1;
    }

    q = cwdStart;
    while (q < cwdEnd) {
        *p++ = *q++;
    }
#ifdef WINCE
    PR_Free(curEnv);
#else
    FreeEnvironmentStrings(curEnv);
#endif

    for (env = envp; *env; env++) {
        q = *env;
        while (*q) {
            *p++ = *q++;
        }
        *p++ = '\0';
    }
    *p = '\0';
    return 0;
}

/*
 * For qsort.  We sort (case-insensitive) the environment strings
 * before generating the environment block.
 */
static int compare(const void *arg1, const void *arg2)
{
    return _stricmp(* (char**)arg1, * (char**)arg2);
}

PRProcess * _PR_CreateWindowsProcess(
    const char *path,
    char *const *argv,
    char *const *envp,
    const PRProcessAttr *attr)
{
#ifdef WINCE
    STARTUPINFOW startupInfo;
    PRUnichar *wideCmdLine;
    PRUnichar *wideCwd;
    int len = 0;
#else
    STARTUPINFO startupInfo;
#endif
    DWORD creationFlags = 0;
    PROCESS_INFORMATION procInfo;
    BOOL retVal;
    char *cmdLine = NULL;
    char *envBlock = NULL;
    char **newEnvp = NULL;
    const char *cwd = NULL; /* current working directory */
    PRProcess *proc = NULL;
    PRBool hasFdInheritBuffer;

    proc = PR_NEW(PRProcess);
    if (!proc) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        goto errorExit;
    }

    if (assembleCmdLine(argv, &cmdLine) == -1) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        goto errorExit;
    }

#ifndef WINCE
    /*
     * If attr->fdInheritBuffer is not NULL, we need to insert
     * it into the envp array, so envp cannot be NULL.
     */
    hasFdInheritBuffer = (attr && attr->fdInheritBuffer);
    if ((envp == NULL) && hasFdInheritBuffer) {
        envp = environ;
    }

    if (envp != NULL) {
        int idx;
        int numEnv;
        PRBool found = PR_FALSE;

        numEnv = 0;
        while (envp[numEnv]) {
            numEnv++;
        }
        newEnvp = (char **) PR_MALLOC((numEnv + 2) * sizeof(char *));
        for (idx = 0; idx < numEnv; idx++) {
            newEnvp[idx] = envp[idx];
            if (hasFdInheritBuffer && !found
                && !strncmp(newEnvp[idx], "NSPR_INHERIT_FDS=", 17)) {
                newEnvp[idx] = attr->fdInheritBuffer;
                found = PR_TRUE;
            }
        }
        if (hasFdInheritBuffer && !found) {
            newEnvp[idx++] = attr->fdInheritBuffer;
        }
        newEnvp[idx] = NULL;
        qsort((void *) newEnvp, (size_t) idx, sizeof(char *), compare);
    }
    if (assembleEnvBlock(newEnvp, &envBlock) == -1) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        goto errorExit;
    }

    ZeroMemory(&startupInfo, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);

    if (attr) {
        PRBool redirected = PR_FALSE;

        /*
         * XXX the default value for stdin, stdout, and stderr
         * should probably be the console input and output, not
         * those of the parent process.
         */
        startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
        startupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        startupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
        if (attr->stdinFd) {
            startupInfo.hStdInput = (HANDLE) attr->stdinFd->secret->md.osfd;
            redirected = PR_TRUE;
        }
        if (attr->stdoutFd) {
            startupInfo.hStdOutput = (HANDLE) attr->stdoutFd->secret->md.osfd;
            redirected = PR_TRUE;
            /*
             * If stdout is redirected, we can assume that the process will
             * not write anything useful to the console windows, and therefore
             * automatically set the CREATE_NO_WINDOW flag.
             */
            creationFlags |= CREATE_NO_WINDOW;
        }
        if (attr->stderrFd) {
            startupInfo.hStdError = (HANDLE) attr->stderrFd->secret->md.osfd;
            redirected = PR_TRUE;
        }
        if (redirected) {
            startupInfo.dwFlags |= STARTF_USESTDHANDLES;
        }
        cwd = attr->currentDirectory;
    }
#endif

#ifdef WINCE
    len = MultiByteToWideChar(CP_ACP, 0, cmdLine, -1, NULL, 0);
    wideCmdLine = (PRUnichar *)PR_MALLOC(len * sizeof(PRUnichar));
    MultiByteToWideChar(CP_ACP, 0, cmdLine, -1, wideCmdLine, len);
    len = MultiByteToWideChar(CP_ACP, 0, cwd, -1, NULL, 0);
    wideCwd = PR_MALLOC(len * sizeof(PRUnichar));
    MultiByteToWideChar(CP_ACP, 0, cwd, -1, wideCwd, len);
    retVal = CreateProcessW(NULL,
                            wideCmdLine,
                            NULL,  /* security attributes for the new
                                    * process */
                            NULL,  /* security attributes for the primary
                                    * thread in the new process */
                            TRUE,  /* inherit handles */
                            creationFlags,
                            envBlock,  /* an environment block, consisting
                                        * of a null-terminated block of
                                        * null-terminated strings.  Each
                                        * string is in the form:
                                        *     name=value
                                        * XXX: usually NULL */
                            wideCwd,  /* current drive and directory */
                            &startupInfo,
                            &procInfo
                           );
    PR_Free(wideCmdLine);
    PR_Free(wideCwd);
#else
    retVal = CreateProcess(NULL,
                           cmdLine,
                           NULL,  /* security attributes for the new
                                   * process */
                           NULL,  /* security attributes for the primary
                                   * thread in the new process */
                           TRUE,  /* inherit handles */
                           creationFlags,
                           envBlock,  /* an environment block, consisting
                                       * of a null-terminated block of
                                       * null-terminated strings.  Each
                                       * string is in the form:
                                       *     name=value
                                       * XXX: usually NULL */
                           cwd,  /* current drive and directory */
                           &startupInfo,
                           &procInfo
                          );
#endif

    if (retVal == FALSE) {
        /* XXX what error code? */
        PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
        goto errorExit;
    }

    CloseHandle(procInfo.hThread);
    proc->md.handle = procInfo.hProcess;
    proc->md.id = procInfo.dwProcessId;

    PR_DELETE(cmdLine);
    if (newEnvp) {
        PR_DELETE(newEnvp);
    }
    if (envBlock) {
        PR_DELETE(envBlock);
    }
    return proc;

errorExit:
    if (cmdLine) {
        PR_DELETE(cmdLine);
    }
    if (newEnvp) {
        PR_DELETE(newEnvp);
    }
    if (envBlock) {
        PR_DELETE(envBlock);
    }
    if (proc) {
        PR_DELETE(proc);
    }
    return NULL;
}  /* _PR_CreateWindowsProcess */

PRStatus _PR_DetachWindowsProcess(PRProcess *process)
{
    CloseHandle(process->md.handle);
    PR_DELETE(process);
    return PR_SUCCESS;
}

/*
 * XXX: This implementation is a temporary quick solution.
 * It can be called by native threads only (not by fibers).
 */
PRStatus _PR_WaitWindowsProcess(PRProcess *process,
                                PRInt32 *exitCode)
{
    DWORD dwRetVal;

    dwRetVal = WaitForSingleObject(process->md.handle, INFINITE);
    if (dwRetVal == WAIT_FAILED) {
        PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
        return PR_FAILURE;
    }
    PR_ASSERT(dwRetVal == WAIT_OBJECT_0);
    if (exitCode != NULL &&
        GetExitCodeProcess(process->md.handle, exitCode) == FALSE) {
        PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
        return PR_FAILURE;
    }
    CloseHandle(process->md.handle);
    PR_DELETE(process);
    return PR_SUCCESS;
}

PRStatus _PR_KillWindowsProcess(PRProcess *process)
{
    /*
     * On Unix, if a process terminates normally, its exit code is
     * between 0 and 255.  So here on Windows, we use the exit code
     * 256 to indicate that the process is killed.
     */
    if (TerminateProcess(process->md.handle, 256)) {
        return PR_SUCCESS;
    }
    PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
    return PR_FAILURE;
}

PRStatus _MD_WindowsGetHostName(char *name, PRUint32 namelen)
{
    PRIntn rv;
    PRInt32 syserror;

    rv = gethostname(name, (PRInt32) namelen);
    if (0 == rv) {
        return PR_SUCCESS;
    }
    syserror = WSAGetLastError();
    PR_ASSERT(WSANOTINITIALISED != syserror);
    _PR_MD_MAP_GETHOSTNAME_ERROR(syserror);
    return PR_FAILURE;
}

PRStatus _MD_WindowsGetSysInfo(PRSysInfo cmd, char *name, PRUint32 namelen)
{
    OSVERSIONINFO osvi;

    PR_ASSERT((cmd == PR_SI_SYSNAME) || (cmd == PR_SI_RELEASE));

    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    if (! GetVersionEx (&osvi) ) {
        _PR_MD_MAP_DEFAULT_ERROR(GetLastError());
        return PR_FAILURE;
    }

    switch (osvi.dwPlatformId) {
        case VER_PLATFORM_WIN32_NT:
            if (PR_SI_SYSNAME == cmd) {
                (void)PR_snprintf(name, namelen, "Windows_NT");
            }
            else if (PR_SI_RELEASE == cmd)
                (void)PR_snprintf(name, namelen, "%d.%d",osvi.dwMajorVersion,
                                  osvi.dwMinorVersion);
            break;
        case VER_PLATFORM_WIN32_WINDOWS:
            if (PR_SI_SYSNAME == cmd) {
                if ((osvi.dwMajorVersion > 4) ||
                    ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0))) {
                    (void)PR_snprintf(name, namelen, "Windows_98");
                }
                else {
                    (void)PR_snprintf(name, namelen, "Windows_95");
                }
            } else if (PR_SI_RELEASE == cmd) {
                (void)PR_snprintf(name, namelen, "%d.%d",osvi.dwMajorVersion,
                                  osvi.dwMinorVersion);
            }
            break;
#ifdef VER_PLATFORM_WIN32_CE
        case VER_PLATFORM_WIN32_CE:
            if (PR_SI_SYSNAME == cmd) {
                (void)PR_snprintf(name, namelen, "Windows_CE");
            }
            else if (PR_SI_RELEASE == cmd)
                (void)PR_snprintf(name, namelen, "%d.%d",osvi.dwMajorVersion,
                                  osvi.dwMinorVersion);
            break;
#endif
        default:
            if (PR_SI_SYSNAME == cmd) {
                (void)PR_snprintf(name, namelen, "Windows_Unknown");
            }
            else if (PR_SI_RELEASE == cmd) {
                (void)PR_snprintf(name, namelen, "%d.%d",0,0);
            }
            break;
    }
    return PR_SUCCESS;
}

PRStatus _MD_WindowsGetReleaseName(char *name, PRUint32 namelen)
{
    OSVERSIONINFO osvi;

    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    if (! GetVersionEx (&osvi) ) {
        _PR_MD_MAP_DEFAULT_ERROR(GetLastError());
        return PR_FAILURE;
    }

    switch (osvi.dwPlatformId) {
        case VER_PLATFORM_WIN32_NT:
        case VER_PLATFORM_WIN32_WINDOWS:
            (void)PR_snprintf(name, namelen, "%d.%d",osvi.dwMajorVersion,
                              osvi.dwMinorVersion);
            break;
        default:
            (void)PR_snprintf(name, namelen, "%d.%d",0,0);
            break;
    }
    return PR_SUCCESS;
}

/*
 **********************************************************************
 *
 * Memory-mapped files
 *
 **********************************************************************
 */

PRStatus _MD_CreateFileMap(PRFileMap *fmap, PRInt64 size)
{
    DWORD dwHi, dwLo;
    DWORD flProtect;
    PROsfd osfd;

    osfd = ( fmap->fd == (PRFileDesc*)-1 )?  -1 : fmap->fd->secret->md.osfd;

    dwLo = (DWORD) (size & 0xffffffff);
    dwHi = (DWORD) (((PRUint64) size >> 32) & 0xffffffff);

    if (fmap->prot == PR_PROT_READONLY) {
        flProtect = PAGE_READONLY;
        fmap->md.dwAccess = FILE_MAP_READ;
    } else if (fmap->prot == PR_PROT_READWRITE) {
        flProtect = PAGE_READWRITE;
        fmap->md.dwAccess = FILE_MAP_WRITE;
    } else {
        PR_ASSERT(fmap->prot == PR_PROT_WRITECOPY);
#ifdef WINCE
        /* WINCE does not have FILE_MAP_COPY. */
        PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
        return PR_FAILURE;
#else
        flProtect = PAGE_WRITECOPY;
        fmap->md.dwAccess = FILE_MAP_COPY;
#endif
    }

    fmap->md.hFileMap = CreateFileMapping(
                            (HANDLE) osfd,
                            NULL,
                            flProtect,
                            dwHi,
                            dwLo,
                            NULL);

    if (fmap->md.hFileMap == NULL) {
        PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

PRInt32 _MD_GetMemMapAlignment(void)
{
    SYSTEM_INFO info;
    GetSystemInfo(&info);
    return info.dwAllocationGranularity;
}

extern PRLogModuleInfo *_pr_shma_lm;

void * _MD_MemMap(
    PRFileMap *fmap,
    PROffset64 offset,
    PRUint32 len)
{
    DWORD dwHi, dwLo;
    void *addr;

    dwLo = (DWORD) (offset & 0xffffffff);
    dwHi = (DWORD) (((PRUint64) offset >> 32) & 0xffffffff);
    if ((addr = MapViewOfFile(fmap->md.hFileMap, fmap->md.dwAccess,
                              dwHi, dwLo, len)) == NULL) {
        {
            LPVOID lpMsgBuf;

            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                NULL,
                GetLastError(),
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                (LPTSTR) &lpMsgBuf,
                0,
                NULL
            );
            PR_LOG( _pr_shma_lm, PR_LOG_DEBUG, ("md_memmap(): %s", lpMsgBuf ));
        }
        PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
    }
    return addr;
}

PRStatus _MD_MemUnmap(void *addr, PRUint32 len)
{
    if (UnmapViewOfFile(addr)) {
        return PR_SUCCESS;
    }
    _PR_MD_MAP_DEFAULT_ERROR(GetLastError());
    return PR_FAILURE;
}

PRStatus _MD_CloseFileMap(PRFileMap *fmap)
{
    CloseHandle(fmap->md.hFileMap);
    PR_DELETE(fmap);
    return PR_SUCCESS;
}

PRStatus _MD_SyncMemMap(
    PRFileDesc *fd,
    void *addr,
    PRUint32 len)
{
    PROsfd osfd = fd->secret->md.osfd;

    /* The FlushViewOfFile page on MSDN says:
     *  To flush all the dirty pages plus the metadata for the file and
     *  ensure that they are physically written to disk, call
     *  FlushViewOfFile and then call the FlushFileBuffers function.
     */
    if (FlushViewOfFile(addr, len) && FlushFileBuffers((HANDLE) osfd)) {
        return PR_SUCCESS;
    }
    _PR_MD_MAP_DEFAULT_ERROR(GetLastError());
    return PR_FAILURE;
}

/*
 ***********************************************************************
 *
 * Atomic increment and decrement operations for x86 processors
 *
 * We don't use InterlockedIncrement and InterlockedDecrement
 * because on NT 3.51 and Win95, they return a number with
 * the same sign as the incremented/decremented result, rather
 * than the result itself.  On NT 4.0 these functions do return
 * the incremented/decremented result.
 *
 * The result is returned in the eax register by the inline
 * assembly code.  We disable the harmless "no return value"
 * warning (4035) for these two functions.
 *
 ***********************************************************************
 */

#if defined(_M_IX86) || defined(_X86_)

#pragma warning(disable: 4035)
PRInt32 _PR_MD_ATOMIC_INCREMENT(PRInt32 *val)
{
#if defined(__GNUC__)
    PRInt32 result;
    asm volatile ("lock ; xadd %0, %1"
                  : "=r"(result), "=m"(*val)
                  : "0"(1), "m"(*val));
    return result + 1;
#else
    __asm
    {
        mov ecx, val
        mov eax, 1
        lock xadd dword ptr [ecx], eax
        inc eax
    }
#endif /* __GNUC__ */
}
#pragma warning(default: 4035)

#pragma warning(disable: 4035)
PRInt32 _PR_MD_ATOMIC_DECREMENT(PRInt32 *val)
{
#if defined(__GNUC__)
    PRInt32 result;
    asm volatile ("lock ; xadd %0, %1"
                  : "=r"(result), "=m"(*val)
                  : "0"(-1), "m"(*val));
    //asm volatile("lock ; xadd %0, %1" : "=m" (val), "=a" (result) : "-1" (1));
    return result - 1;
#else
    __asm
    {
        mov ecx, val
        mov eax, 0ffffffffh
        lock xadd dword ptr [ecx], eax
        dec eax
    }
#endif /* __GNUC__ */
}
#pragma warning(default: 4035)

#pragma warning(disable: 4035)
PRInt32 _PR_MD_ATOMIC_ADD(PRInt32 *intp, PRInt32 val)
{
#if defined(__GNUC__)
    PRInt32 result;
    //asm volatile("lock ; xadd %1, %0" : "=m" (intp), "=a" (result) : "1" (val));
    asm volatile ("lock ; xadd %0, %1"
                  : "=r"(result), "=m"(*intp)
                  : "0"(val), "m"(*intp));
    return result + val;
#else
    __asm
    {
        mov ecx, intp
        mov eax, val
        mov edx, eax
        lock xadd dword ptr [ecx], eax
        add eax, edx
    }
#endif /* __GNUC__ */
}
#pragma warning(default: 4035)

#ifdef _PR_HAVE_ATOMIC_CAS

#pragma warning(disable: 4035)
void
PR_StackPush(PRStack *stack, PRStackElem *stack_elem)
{
#if defined(__GNUC__)
    void **tos = (void **) stack;
    void *tmp;

retry:
    if (*tos == (void *) -1) {
        goto retry;
    }

    __asm__("xchg %0,%1"
            : "=r" (tmp), "=m"(*tos)
            : "0" (-1), "m"(*tos));

    if (tmp == (void *) -1) {
        goto retry;
    }

    *(void **)stack_elem = tmp;
    __asm__("" : : : "memory");
    *tos = stack_elem;
#else
    __asm
    {
        mov ebx, stack
        mov ecx, stack_elem
        retry:  mov eax,[ebx]
        cmp eax,-1
        je retry
        mov eax,-1
        xchg dword ptr [ebx], eax
        cmp eax,-1
        je  retry
        mov [ecx],eax
        mov [ebx],ecx
    }
#endif /* __GNUC__ */
}
#pragma warning(default: 4035)

#pragma warning(disable: 4035)
PRStackElem *
PR_StackPop(PRStack *stack)
{
#if defined(__GNUC__)
    void **tos = (void **) stack;
    void *tmp;

retry:
    if (*tos == (void *) -1) {
        goto retry;
    }

    __asm__("xchg %0,%1"
            : "=r" (tmp), "=m"(*tos)
            : "0" (-1), "m"(*tos));

    if (tmp == (void *) -1) {
        goto retry;
    }

    if (tmp != (void *) 0)
    {
        void *next = *(void **)tmp;
        *tos = next;
        *(void **)tmp = 0;
    }
    else {
        *tos = tmp;
    }

    return tmp;
#else
    __asm
    {
        mov ebx, stack
        retry:  mov eax,[ebx]
        cmp eax,-1
        je retry
        mov eax,-1
        xchg dword ptr [ebx], eax
        cmp eax,-1
        je  retry
        cmp eax,0
        je  empty
        mov ecx,[eax]
        mov [ebx],ecx
        mov [eax],0
        jmp done
        empty:
        mov [ebx],eax
        done:
    }
#endif /* __GNUC__ */
}
#pragma warning(default: 4035)

#endif /* _PR_HAVE_ATOMIC_CAS */

#endif /* x86 processors */