summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/testdriver/base.py
blob: 9bc5ebbe6e5ac126ac2f3f1ed92599c931293f45 (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
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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
# -*- coding: utf-8 -*-
# $Id: base.py $
# pylint: disable=C0302

"""
Base testdriver module.
"""

__copyright__ = \
"""
Copyright (C) 2010-2019 Oracle Corporation

This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
you can redistribute it and/or modify it under the terms of the GNU
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.

The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.

You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
__version__ = "$Revision: 127855 $"


# Standard Python imports.
import os
import os.path
import signal
import socket
import stat
import subprocess
import sys
import time
if sys.version_info[0] < 3: import thread;            # pylint: disable=import-error
else:                       import _thread as thread; # pylint: disable=import-error
import threading
import traceback
import tempfile;
import unittest;

# Validation Kit imports.
from common                 import utils;
from common.constants       import rtexitcode;
from testdriver             import reporter;
if sys.platform == 'win32':
    from testdriver         import winbase;

# Figure where we are.
try:    __file__
except: __file__ = sys.argv[0];
g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)));

# Python 3 hacks:
if sys.version_info[0] >= 3:
    long = int;     # pylint: disable=redefined-builtin,invalid-name


#
# Some utility functions.
#

def exeSuff():
    """
    Returns the executable suffix.
    """
    if os.name == 'nt' or os.name == 'os2':
        return '.exe';
    return '';

def searchPath(sExecName):
    """
    Searches the PATH for the specified executable name, returning the first
    existing file/directory/whatever.  The return is abspath'ed.
    """
    sSuff = exeSuff();

    sPath = os.getenv('PATH', os.getenv('Path', os.path.defpath));
    aPaths = sPath.split(os.path.pathsep)
    for sDir in aPaths:
        sFullExecName = os.path.join(sDir, sExecName);
        if os.path.exists(sFullExecName):
            return os.path.abspath(sFullExecName);
        sFullExecName += sSuff;
        if os.path.exists(sFullExecName):
            return os.path.abspath(sFullExecName);
    return sExecName;

def getEnv(sVar, sLocalAlternative = None):
    """
    Tries to get an environment variable, optionally with a local run alternative.
    Will raise an exception if sLocalAlternative is None and the variable is
    empty or missing.
    """
    try:
        sVal = os.environ.get(sVar, None);
        if sVal is None:
            raise GenError('environment variable "%s" is missing' % (sVar));
        if sVal == "":
            raise GenError('environment variable "%s" is empty' % (sVar));
    except:
        if sLocalAlternative is None  or  not reporter.isLocal():
            raise
        sVal = sLocalAlternative;
    return sVal;

def getDirEnv(sVar, sAlternative = None, fLocalReq = False, fTryCreate = False):
    """
    Tries to get an environment variable specifying a directory path.

    Resolves it into an absolute path and verifies its existance before
    returning it.

    If the environment variable is empty or isn't set, or if the directory
    doesn't exist or isn't a directory, sAlternative is returned instead.
    If sAlternative is None, then we'll raise a GenError.  For local runs we'll
    only do this if fLocalReq is True.
    """
    assert sAlternative is None or fTryCreate is False;
    try:
        sVal = os.environ.get(sVar, None);
        if sVal is None:
            raise GenError('environment variable "%s" is missing' % (sVar));
        if sVal == "":
            raise GenError('environment variable "%s" is empty' % (sVar));

        sVal = os.path.abspath(sVal);
        if not os.path.isdir(sVal):
            if not fTryCreate  or  os.path.exists(sVal):
                reporter.error('the value of env.var. "%s" is not a dir: "%s"' % (sVar, sVal));
                raise GenError('the value of env.var. "%s" is not a dir: "%s"' % (sVar, sVal));
            try:
                os.makedirs(sVal, 0o700);
            except:
                reporter.error('makedirs failed on the value of env.var. "%s": "%s"' % (sVar, sVal));
                raise GenError('makedirs failed on the value of env.var. "%s": "%s"' % (sVar, sVal));
    except:
        if sAlternative is None:
            if reporter.isLocal() and fLocalReq:
                raise;
            sVal = None;
        else:
            sVal = os.path.abspath(sAlternative);
    return sVal;

def timestampMilli():
    """
    Gets a millisecond timestamp.
    """
    if sys.platform == 'win32':
        return long(time.clock() * 1000);
    return long(time.time() * 1000);

def timestampNano():
    """
    Gets a nanosecond timestamp.
    """
    if sys.platform == 'win32':
        return long(time.clock() * 1000000000);
    return long(time.time() * 1000000000);

def tryGetHostByName(sName):
    """
    Wrapper around gethostbyname.
    """
    if sName is not None:
        try:
            sIpAddr = socket.gethostbyname(sName);
        except:
            reporter.errorXcpt('gethostbyname(%s)' % (sName));
        else:
            if sIpAddr != '0.0.0.0':
                sName = sIpAddr;
            else:
                reporter.error('gethostbyname(%s) -> %s' % (sName, sIpAddr));
    return sName;

def __processSudoKill(uPid, iSignal, fSudo):
    """
    Does the sudo kill -signal pid thing if fSudo is true, else uses os.kill.
    """
    try:
        if fSudo:
            return utils.sudoProcessCall(['/bin/kill', '-%s' % (iSignal,), str(uPid)]) == 0;
        os.kill(uPid, iSignal);
        return True;
    except:
        reporter.logXcpt('uPid=%s' % (uPid,));
    return False;

def processInterrupt(uPid, fSudo = False):
    """
    Sends a SIGINT or equivalent to interrupt the specified process.
    Returns True on success, False on failure.

    On Windows hosts this may not work unless the process happens to be a
    process group leader.
    """
    if sys.platform == 'win32':
        fRc = winbase.processInterrupt(uPid)
    else:
        fRc = __processSudoKill(uPid, signal.SIGINT, fSudo);
    return fRc;

def sendUserSignal1(uPid, fSudo = False):
    """
    Sends a SIGUSR1 or equivalent to nudge the process into shutting down
    (VBoxSVC) or something.
    Returns True on success, False on failure or if not supported (win).

    On Windows hosts this may not work unless the process happens to be a
    process group leader.
    """
    if sys.platform == 'win32':
        fRc = False;
    else:
        fRc = __processSudoKill(uPid, signal.SIGUSR1, fSudo); # pylint: disable=E1101
    return fRc;

def processTerminate(uPid, fSudo = False):
    """
    Terminates the process in a nice manner (SIGTERM or equivalent).
    Returns True on success, False on failure (logged).
    """
    fRc = False;
    if sys.platform == 'win32':
        fRc = winbase.processTerminate(uPid);
    else:
        fRc = __processSudoKill(uPid, signal.SIGTERM, fSudo);
    return fRc;

def processKill(uPid, fSudo = False):
    """
    Terminates the process with extreme prejudice (SIGKILL).
    Returns True on success, False on failure.
    """
    fRc = False;
    if sys.platform == 'win32':
        fRc = winbase.processKill(uPid);
    else:
        fRc = __processSudoKill(uPid, signal.SIGKILL, fSudo); # pylint: disable=E1101
    return fRc;

def processKillWithNameCheck(uPid, sName):
    """
    Like processKill(), but checks if the process name matches before killing
    it.  This is intended for killing using potentially stale pid values.

    Returns True on success, False on failure.
    """

    if processCheckPidAndName(uPid, sName) is not True:
        return False;
    return processKill(uPid);


def processExists(uPid):
    """
    Checks if the specified process exits.
    This will only work if we can signal/open the process.

    Returns True if it positively exists, False otherwise.
    """
    if sys.platform == 'win32':
        fRc = winbase.processExists(uPid);
    else:
        try:
            os.kill(uPid, 0);
            fRc = True;
        except:
            reporter.logXcpt('uPid=%s' % (uPid,));
            fRc = False;
    return fRc;

def processCheckPidAndName(uPid, sName):
    """
    Checks if a process PID and NAME matches.
    """
    if sys.platform == 'win32':
        fRc = winbase.processCheckPidAndName(uPid, sName);
    else:
        sOs = utils.getHostOs();
        if sOs == 'linux':
            asPsCmd = ['/bin/ps',     '-p', '%u' % (uPid,), '-o', 'fname='];
        elif sOs == 'solaris':
            asPsCmd = ['/usr/bin/ps', '-p', '%u' % (uPid,), '-o', 'fname='];
        elif sOs == 'darwin':
            asPsCmd = ['/bin/ps',     '-p', '%u' % (uPid,), '-o', 'ucomm='];
        else:
            asPsCmd = None;

        if asPsCmd is not None:
            try:
                oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE);
                sCurName = oPs.communicate()[0];
                iExitCode = oPs.wait();
            except:
                reporter.logXcpt();
                return False;

            # ps fails with non-zero exit code if the pid wasn't found.
            if iExitCode is not 0:
                return False;
            if sCurName is None:
                return False;
            sCurName = sCurName.strip();
            if sCurName == '':
                return False;

            if os.path.basename(sName) == sName:
                sCurName = os.path.basename(sCurName);
            elif os.path.basename(sCurName) == sCurName:
                sName = os.path.basename(sName);

            if sCurName != sName:
                return False;

            fRc = True;
    return fRc;



#
# Classes
#

class GenError(Exception):
    """
    Exception class which only purpose it is to allow us to only catch our own
    exceptions.  Better design later.
    """

    def __init__(self, sWhat = "whatever"):
        Exception.__init__(self);
        self.sWhat = sWhat

    def str(self):
        """Get the message string."""
        return self.sWhat;


class InvalidOption(GenError):
    """
    Exception thrown by TestDriverBase.parseOption(). It contains the error message.
    """
    def __init__(self, sWhat):
        GenError.__init__(self, sWhat);


class QuietInvalidOption(GenError):
    """
    Exception thrown by TestDriverBase.parseOption(). Error already printed, just
    return failure.
    """
    def __init__(self):
        GenError.__init__(self, "");


class TdTaskBase(object):
    """
    The base task.
    """

    def __init__(self, sCaller):
        self.sDbgCreated    = '%s: %s' % (utils.getTimePrefix(), sCaller);
        self.fSignalled     = False;
        self.__oRLock       = threading.RLock();
        self.oCv            = threading.Condition(self.__oRLock);
        self.oOwner         = None;
        self.msStart        = timestampMilli();
        self.oLocker        = None;

    def __del__(self):
        """In case we need it later on."""
        pass;

    def toString(self):
        """
        Stringifies the object, mostly as a debug aid.
        """
        return '<%s: fSignalled=%s, __oRLock=%s, oCv=%s, oOwner=%s, oLocker=%s, msStart=%s, sDbgCreated=%s>' \
             % (type(self).__name__, self.fSignalled, self.__oRLock, self.oCv, repr(self.oOwner), self.oLocker, self.msStart,
                self.sDbgCreated,);

    def __str__(self):
        return self.toString();

    def lockTask(self):
        """ Wrapper around oCv.acquire(). """
        if True is True: # change to False for debugging deadlocks.
            self.oCv.acquire();
        else:
            msStartWait = timestampMilli();
            while self.oCv.acquire(0) is False:
                if timestampMilli() - msStartWait > 30*1000:
                    reporter.error('!!! timed out waiting for %s' % (self, ));
                    traceback.print_stack();
                    reporter.logAllStacks()
                    self.oCv.acquire();
                    break;
                time.sleep(0.5);
        self.oLocker = thread.get_ident()
        return None;

    def unlockTask(self):
        """ Wrapper around oCv.release(). """
        self.oLocker = None;
        self.oCv.release();
        return None;

    def getAgeAsMs(self):
        """
        Returns the number of milliseconds the task has existed.
        """
        return timestampMilli() - self.msStart;

    def setTaskOwner(self, oOwner):
        """
        Sets or clears the task owner.  (oOwner can be None.)

        Returns the previous owner, this means None if not owned.
        """
        self.lockTask();
        oOldOwner = self.oOwner;
        self.oOwner = oOwner;
        self.unlockTask();
        return oOldOwner;

    def signalTaskLocked(self):
        """
        Variant of signalTask that can be called while owning the lock.
        """
        fOld = self.fSignalled;
        if not fOld:
            reporter.log2('signalTaskLocked(%s)' % (self,));
        self.fSignalled = True;
        self.oCv.notifyAll()
        if self.oOwner is not None:
            self.oOwner.notifyAboutReadyTask(self);
        return fOld;

    def signalTask(self):
        """
        Signals the task, internal use only.

        Returns the previous state.
        """
        self.lockTask();
        fOld = self.signalTaskLocked();
        self.unlockTask();
        return fOld

    def resetTaskLocked(self):
        """
        Variant of resetTask that can be called while owning the lock.
        """
        fOld = self.fSignalled;
        self.fSignalled = False;
        return fOld;

    def resetTask(self):
        """
        Resets the task signal, internal use only.

        Returns the previous state.
        """
        self.lockTask();
        fOld = self.resetTaskLocked();
        self.unlockTask();
        return fOld

    def pollTask(self, fLocked = False):
        """
        Poll the signal status of the task.
        Returns True if signalled, False if not.

        Override this method.
        """
        if not fLocked:
            self.lockTask();
        fState = self.fSignalled;
        if not fLocked:
            self.unlockTask();
        return fState

    def waitForTask(self, cMsTimeout = 0):
        """
        Waits for the task to be signalled.

        Returns True if the task is/became ready before the timeout expired.
        Returns False if the task is still not after cMsTimeout have elapsed.

        Overriable.
        """
        self.lockTask();

        fState = self.pollTask(True);
        if not fState:
            # Don't wait more than 1s.  This allow lazy state polling.
            msStart = timestampMilli();
            while not fState:
                cMsElapsed = timestampMilli() - msStart;
                if cMsElapsed >= cMsTimeout:
                    break;

                cMsWait = cMsTimeout - cMsElapsed
                if cMsWait > 1000:
                    cMsWait = 1000;
                try:
                    self.oCv.wait(cMsWait / 1000.0);
                except:
                    pass;
                reporter.doPollWork('TdTaskBase.waitForTask');
                fState = self.pollTask(True);

        self.unlockTask();
        return fState;


class Process(TdTaskBase):
    """
    Child Process.
    """

    def __init__(self, sName, asArgs, uPid, hWin = None, uTid = None):
        TdTaskBase.__init__(self, utils.getCallerName());
        self.sName      = sName;
        self.asArgs     = asArgs;
        self.uExitCode  = -127;
        self.uPid       = uPid;
        self.hWin       = hWin;
        self.uTid       = uTid;
        self.sKindCrashReport = None;
        self.sKindCrashDump   = None;

    def toString(self):
        return '<%s uExitcode=%s, uPid=%s, sName=%s, asArgs=%s, hWin=%s, uTid=%s>' \
             % (TdTaskBase.toString(self), self.uExitCode, self.uPid, self.sName, self.asArgs, self.hWin, self.uTid);

    #
    # Instantiation methods.
    #

    @staticmethod
    def spawn(sName, *asArgsIn):
        """
        Similar to os.spawnl(os.P_NOWAIT,).

        """
        # Make argument array (can probably use asArgsIn directly, but wtf).
        asArgs = [];
        for sArg in asArgsIn:
            asArgs.append(sArg);

        # Special case: Windows.
        if sys.platform == 'win32':
            (uPid, hProcess, uTid) = winbase.processCreate(searchPath(sName), asArgs);
            if uPid == -1:
                return None;
            return Process(sName, asArgs, uPid, hProcess, uTid);

        # Unixy.
        try:
            uPid = os.spawnv(os.P_NOWAIT, sName, asArgs);
        except:
            reporter.logXcpt('sName=%s' % (sName,));
            return None;
        return Process(sName, asArgs, uPid);

    @staticmethod
    def spawnp(sName, *asArgsIn):
        """
        Similar to os.spawnlp(os.P_NOWAIT,).

        """
        return Process.spawn(searchPath(sName), *asArgsIn);

    #
    # Task methods
    #

    def pollTask(self, fLocked = False):
        """
        Overridden pollTask method.
        """
        if not fLocked:
            self.lockTask();

        fRc = self.fSignalled;
        if not fRc:
            if sys.platform == 'win32':
                if winbase.processPollByHandle(self.hWin):
                    try:
                        (uPid, uStatus) = os.waitpid(self.hWin, 0);
                        if uPid == self.hWin or uPid == self.uPid:
                            self.hWin.Detach(); # waitpid closed it, so it's now invalid.
                            self.hWin = None;
                            uPid = self.uPid;
                    except:
                        reporter.logXcpt();
                        uPid    = self.uPid;
                        uStatus = 0xffffffff;
                else:
                    uPid    = 0;
                    uStatus = 0;        # pylint: disable=redefined-variable-type
            else:
                try:
                    (uPid, uStatus) = os.waitpid(self.uPid, os.WNOHANG); # pylint: disable=E1101
                except:
                    reporter.logXcpt();
                    uPid    = self.uPid;
                    uStatus = 0xffffffff;

            # Got anything?
            if uPid == self.uPid:
                self.uExitCode = uStatus;
                reporter.log('Process %u -> %u (%#x)' % (uPid, uStatus, uStatus));
                self.signalTaskLocked();
                if self.uExitCode != 0 and (self.sKindCrashReport is not None or self.sKindCrashDump is not None):
                    reporter.error('Process "%s" returned/crashed with a non-zero status code!! rc=%u sig=%u%s (raw=%#x)'
                                   % ( self.sName, self.uExitCode >> 8, self.uExitCode & 0x7f,
                                       ' w/ core' if self.uExitCode & 0x80 else '', self.uExitCode))
                    utils.processCollectCrashInfo(self.uPid, reporter.log, self._addCrashFile);

            fRc = self.fSignalled;
        if not fLocked:
            self.unlockTask();
        return fRc;

    def _addCrashFile(self, sFile, fBinary):
        """
        Helper for adding a crash report or dump to the test report.
        """
        sKind = self.sKindCrashDump if fBinary else self.sKindCrashReport;
        if sKind is not None:
            reporter.addLogFile(sFile, sKind);
        return None;


    #
    # Methods
    #

    def enableCrashReporting(self, sKindCrashReport, sKindCrashDump):
        """
        Enabling (or disables) automatic crash reporting on systems where that
        is possible.  The two file kind parameters are on the form
        'crash/log/client' and 'crash/dump/client'.  If both are None,
        reporting will be disabled.
        """
        self.sKindCrashReport = sKindCrashReport;
        self.sKindCrashDump   = sKindCrashDump;
        return True;

    def isRunning(self):
        """
        Returns True if the process is still running, False if not.
        """
        return not self.pollTask();

    def wait(self, cMsTimeout = 0):
        """
        Wait for the process to exit.

        Returns True if the process exited withint the specified wait period.
        Returns False if still running.
        """
        return self.waitForTask(cMsTimeout);

    def getExitCode(self):
        """
        Returns the exit code of the process.
        The process must have exited or the result will be wrong.
        """
        if self.isRunning():
            return -127;
        return self.uExitCode >> 8;

    def interrupt(self):
        """
        Sends a SIGINT or equivalent to interrupt the process.
        Returns True on success, False on failure.

        On Windows hosts this may not work unless the process happens to be a
        process group leader.
        """
        if sys.platform == 'win32':
            return winbase.postThreadMesssageQuit(self.uTid);
        return processInterrupt(self.uPid);

    def sendUserSignal1(self):
        """
        Sends a SIGUSR1 or equivalent to nudge the process into shutting down
        (VBoxSVC) or something.
        Returns True on success, False on failure.

        On Windows hosts this may not work unless the process happens to be a
        process group leader.
        """
        #if sys.platform == 'win32':
        #    return winbase.postThreadMesssageClose(self.uTid);
        return sendUserSignal1(self.uPid);

    def terminate(self):
        """
        Terminates the process in a nice manner (SIGTERM or equivalent).
        Returns True on success, False on failure (logged).
        """
        if sys.platform == 'win32':
            return winbase.processTerminateByHandle(self.hWin);
        return processTerminate(self.uPid);

    def getPid(self):
        """ Returns the process id. """
        return self.uPid;


class SubTestDriverBase(object):
    """
    The base sub-test driver.

    It helps thinking of these as units/sets/groups of tests, where the test
    cases are (mostly) realized in python.

    The sub-test drivers are subordinates of one or more test drivers.  They
    can be viewed as test code libraries that is responsible for parts of a
    test driver run in different setups.  One example would be testing a guest
    additions component, which is applicable both to freshly installed guest
    additions and VMs with old guest.

    The test drivers invokes the sub-test drivers in a private manner during
    test execution, but some of the generic bits are done automagically by the
    base class: options, help, resources, various other actions.
    """

    def __init__(self, sName, oTstDrv):
        self.sName              = sName;
        self.oTstDrv            = oTstDrv;
        self.asRsrcs            = [];

    def showUsage(self):
        """
        Show usage information if any.

        The default implementation only prints the name.
        """
        reporter.log('');
        reporter.log('Options for sub-test driver %s:' % (self.sName,));
        return True;

    def parseOption(self, asArgs, iArg):
        """
        Parse an option. Override this.

        @param  asArgs      The argument vector.
        @param  iArg        The index of the current argument.

        @returns The index of the next argument if consumed, @a iArg if not.

        @throws  InvalidOption or QuietInvalidOption on syntax error or similar.
        """
        _ = asArgs;
        return iArg;


class TestDriverBase(object): # pylint: disable=R0902
    """
    The base test driver.
    """

    def __init__(self):
        self.fInterrupted       = False;

        # Actions.
        self.asSpecialActions   = ['extract', 'abort'];
        self.asNormalActions    = ['cleanup-before', 'verify', 'config', 'execute', 'cleanup-after' ];
        self.asActions          = [];
        self.sExtractDstPath    = None;

        # Options.
        self.fNoWipeClean       = False;

        # Tasks - only accessed by one thread atm, so no need for locking.
        self.aoTasks            = [];

        # Host info.
        self.sHost              = utils.getHostOs();
        self.sHostArch          = utils.getHostArch();

        #
        # Get our bearings and adjust the environment.
        #
        if not utils.isRunningFromCheckout():
            self.sBinPath = os.path.join(g_ksValidationKitDir, utils.getHostOs(), utils.getHostArch());
        else:
            self.sBinPath = os.path.join(g_ksValidationKitDir, os.pardir, os.pardir, os.pardir, 'out', utils.getHostOsDotArch(),
                                         os.environ.get('KBUILD_TYPE', os.environ.get('BUILD_TYPE', 'debug')),
                                         'validationkit', utils.getHostOs(), utils.getHostArch());
        self.sOrgShell = os.environ.get('SHELL');
        self.sOurShell = os.path.join(self.sBinPath, 'vts_shell' + exeSuff()); # No shell yet.
        os.environ['SHELL'] = self.sOurShell;

        self.sScriptPath = getDirEnv('TESTBOX_PATH_SCRIPTS');
        if self.sScriptPath is None:
            self.sScriptPath = os.path.abspath(os.path.join(os.getcwd(), '..'));
        os.environ['TESTBOX_PATH_SCRIPTS'] = self.sScriptPath;

        self.sScratchPath = getDirEnv('TESTBOX_PATH_SCRATCH', fTryCreate = True);
        if self.sScratchPath is None:
            sTmpDir = tempfile.gettempdir();
            if sTmpDir == '/tmp': # /var/tmp is generally more suitable on all platforms.
                sTmpDir = '/var/tmp';
            self.sScratchPath = os.path.abspath(os.path.join(sTmpDir, 'VBoxTestTmp'));
            if not os.path.isdir(self.sScratchPath):
                os.makedirs(self.sScratchPath, 0o700);
        os.environ['TESTBOX_PATH_SCRATCH'] = self.sScratchPath;

        self.sTestBoxName  = getEnv(   'TESTBOX_NAME', 'local');
        self.sTestSetId    = getEnv(   'TESTBOX_TEST_SET_ID', 'local');
        self.sBuildPath    = getDirEnv('TESTBOX_PATH_BUILDS');
        self.sUploadPath   = getDirEnv('TESTBOX_PATH_UPLOAD');
        self.sResourcePath = getDirEnv('TESTBOX_PATH_RESOURCES');
        if self.sResourcePath is None:
            if self.sHost == 'darwin':      self.sResourcePath = "/Volumes/testrsrc/";
            elif self.sHost == 'freebsd':   self.sResourcePath = "/mnt/testrsrc/";
            elif self.sHost == 'linux':     self.sResourcePath = "/mnt/testrsrc/";
            elif self.sHost == 'os2':       self.sResourcePath = "T:/";
            elif self.sHost == 'solaris':   self.sResourcePath = "/mnt/testrsrc/";
            elif self.sHost == 'win':       self.sResourcePath = "T:/";
            else: raise GenError('unknown host OS "%s"' % (self.sHost));

        # PID file for the testdriver.
        self.sPidFile = os.path.join(self.sScratchPath, 'testdriver.pid');

        # Some stuff for the log...
        reporter.log('scratch: %s' % (self.sScratchPath,));

        # Get the absolute timeout (seconds since epoch, see
        # utils.timestampSecond()). None if not available.
        self.secTimeoutAbs = os.environ.get('TESTBOX_TIMEOUT_ABS', None);
        if self.secTimeoutAbs is not None:
            self.secTimeoutAbs = long(self.secTimeoutAbs);
            reporter.log('secTimeoutAbs: %s' % (self.secTimeoutAbs,));
        else:
            reporter.log('TESTBOX_TIMEOUT_ABS not found in the environment');

        # Distance from secTimeoutAbs that timeouts should be adjusted to.
        self.secTimeoutFudge = 30;

        # List of sub-test drivers (SubTestDriverBase derivatives).
        self.aoSubTstDrvs    = [];          # type: list(SubTestDriverBase)

        # Use the scratch path for temporary files.
        if self.sHost in ['win', 'os2']:
            os.environ['TMP']     = self.sScratchPath;
            os.environ['TEMP']    = self.sScratchPath;
        os.environ['TMPDIR']      = self.sScratchPath;
        os.environ['IPRT_TMPDIR'] = self.sScratchPath; # IPRT/VBox specific.


    #
    # Resource utility methods.
    #

    def isResourceFile(self, sFile):
        """
        Checks if sFile is in in the resource set.
        """
        ## @todo need to deal with stuff in the validationkit.zip and similar.
        asRsrcs = self.getResourceSet();
        if sFile in asRsrcs:
            return os.path.isfile(os.path.join(self.sResourcePath, sFile));
        for sRsrc in asRsrcs:
            if sFile.startswith(sRsrc):
                sFull = os.path.join(self.sResourcePath, sRsrc);
                if os.path.isdir(sFull):
                    return os.path.isfile(os.path.join(self.sResourcePath, sRsrc));
        return False;

    def getFullResourceName(self, sName):
        """
        Returns the full resource name.
        """
        if os.path.isabs(sName): ## @todo Hack. Need to deal properly with stuff in the validationkit.zip and similar.
            return sName;
        return os.path.join(self.sResourcePath, sName);

    #
    # Scratch related utility methods.
    #

    def __wipeScratchRecurse(self, sDir):
        """
        Deletes all file and sub-directories in sDir.
        Returns the number of errors.
        """
        try:
            asNames = os.listdir(sDir);
        except:
            reporter.errorXcpt('os.listdir("%s")' % (sDir));
            return False;

        cErrors = 0;
        for sName in asNames:
            # Build full path and lstat the object.
            sFullName = os.path.join(sDir, sName)
            try:
                oStat = os.lstat(sFullName);
            except:
                reporter.errorXcpt('lstat("%s")' % (sFullName));
                cErrors = cErrors + 1;
                continue;

            if stat.S_ISDIR(oStat.st_mode):
                # Directory - recurse and try remove it.
                cErrors = cErrors + self.__wipeScratchRecurse(sFullName);
                try:
                    os.rmdir(sFullName);
                except:
                    reporter.errorXcpt('rmdir("%s")' % (sFullName));
                    cErrors = cErrors + 1;
            else:
                # File, symlink, fifo or something - remove/unlink.
                try:
                    os.remove(sFullName);
                except:
                    reporter.errorXcpt('remove("%s")' % (sFullName));
                    cErrors = cErrors + 1;
        return cErrors;

    def wipeScratch(self):
        """
        Removes the content of the scratch directory.
        Returns True on no errors, False + log entries on errors.
        """
        cErrors = self.__wipeScratchRecurse(self.sScratchPath);
        return cErrors == 0;

    #
    # Sub-test driver related methods.
    #

    def addSubTestDriver(self, oSubTstDrv):
        """
        Adds a sub-test driver.

        Returns True on success, false on failure.
        """
        assert isinstance(oSubTstDrv, SubTestDriverBase);
        if oSubTstDrv in self.aoSubTstDrvs:
            reporter.error('Attempt at adding sub-test driver %s twice.' % (oSubTstDrv.sName,));
            return False;
        self.aoSubTstDrvs.append(oSubTstDrv);
        return True;

    def showSubTstDrvUsage(self):
        """
        Shows the usage of the sub-test drivers.
        """
        for oSubTstDrv in self.aoSubTstDrvs:
            oSubTstDrv.showUsage();
        return True;

    def subTstDrvParseOption(self, asArgs, iArgs):
        """
        Lets the sub-test drivers have a go at the option.
        Returns the index of the next option if handled, otherwise iArgs.
        """
        for oSubTstDrv in self.aoSubTstDrvs:
            iNext = oSubTstDrv.parseOption(asArgs, iArgs)
            if iNext != iArgs:
                assert iNext > iArgs;
                assert iNext <= len(asArgs);
                return iNext;
        return iArgs;


    #
    # Task related methods.
    #

    def addTask(self, oTask):
        """
        Adds oTask to the task list.

        Returns True if the task was added.

        Returns False if the task was already in the task list.
        """
        if oTask in self.aoTasks:
            return False;
        #reporter.log2('adding task %s' % (oTask,));
        self.aoTasks.append(oTask);
        oTask.setTaskOwner(self);
        #reporter.log2('tasks now in list: %d - %s' % (len(self.aoTasks), self.aoTasks));
        return True;

    def removeTask(self, oTask):
        """
        Removes oTask to the task list.

        Returns oTask on success and None on failure.
        """
        try:
            #reporter.log2('removing task %s' % (oTask,));
            self.aoTasks.remove(oTask);
        except:
            return None;
        else:
            oTask.setTaskOwner(None);
        #reporter.log2('tasks left: %d - %s' % (len(self.aoTasks), self.aoTasks));
        return oTask;

    def removeAllTasks(self):
        """
        Removes all the task from the task list.

        Returns None.
        """
        aoTasks = self.aoTasks;
        self.aoTasks = [];
        for oTask in aoTasks:
            oTask.setTaskOwner(None);
        return None;

    def notifyAboutReadyTask(self, oTask):
        """
        Notificiation that there is a ready task.  May be called owning the
        task lock, so be careful wrt deadlocks.

        Remember to call super when overriding this.
        """
        if oTask is None: pass; # lint
        return None;

    def pollTasks(self):
        """
        Polls the task to see if any of them are ready.
        Returns the ready task, None if none are ready.
        """
        for oTask in self.aoTasks:
            if oTask.pollTask():
                return oTask;
        return None;

    def waitForTasksSleepWorker(self, cMsTimeout):
        """
        Overriable method that does the sleeping for waitForTask().

        cMsTimeout will not be larger than 1000, so there is normally no need
        to do any additional splitting up of the polling interval.

        Returns True if cMillieSecs elapsed.
        Returns False if some exception was raised while we waited or
        there turned out to be nothing to wait on.
        """
        try:
            self.aoTasks[0].waitForTask(cMsTimeout);
            return True;
        except Exception as oXcpt:
            reporter.log("waitForTasksSleepWorker: %s" % (str(oXcpt),));
            return False;

    def waitForTasks(self, cMsTimeout):
        """
        Waits for any of the tasks to require attention or a KeyboardInterrupt.
        Returns the ready task on success, None on timeout or interrupt.
        """
        try:
            #reporter.log2('waitForTasks: cMsTimeout=%d' % (cMsTimeout,));

            if cMsTimeout == 0:
                return self.pollTasks();

            if not self.aoTasks:
                return None;

            fMore = True;
            if cMsTimeout < 0:
                while fMore:
                    oTask = self.pollTasks();
                    if oTask is not None:
                        return oTask;
                    fMore = self.waitForTasksSleepWorker(1000);
            else:
                msStart = timestampMilli();
                while fMore:
                    oTask = self.pollTasks();
                    if oTask is not None:
                        #reporter.log2('waitForTasks: returning %s, msStart=%d' % \
                        #              (oTask, msStart));
                        return oTask;

                    cMsElapsed = timestampMilli() - msStart;
                    if cMsElapsed > cMsTimeout: # not ==, we want the final waitForEvents.
                        break;
                    cMsSleep = cMsTimeout - cMsElapsed;
                    if cMsSleep > 1000:
                        cMsSleep = 1000;
                    fMore = self.waitForTasksSleepWorker(cMsSleep);
        except KeyboardInterrupt:
            self.fInterrupted = True;
            reporter.errorXcpt('KeyboardInterrupt', 6);
        except:
            reporter.errorXcpt(None, 6);
        return None;

    #
    # PID file management methods.
    #

    def pidFileRead(self):
        """
        Worker that reads the PID file.
        Returns dictionary of PID with value (sName, fSudo), empty if no file.
        """
        dPids = {};
        if os.path.isfile(self.sPidFile):
            try:
                oFile = utils.openNoInherit(self.sPidFile, 'r');
                sContent = str(oFile.read());
                oFile.close();
            except:
                reporter.errorXcpt();
                return dPids;

            sContent = str(sContent).strip().replace('\n', ' ').replace('\r', ' ').replace('\t', ' ');
            for sProcess in sContent.split(' '):
                asFields = sProcess.split(':');
                if len(asFields) == 3 and asFields[0].isdigit():
                    try:
                        dPids[int(asFields[0])] = (asFields[2], asFields[1] == 'sudo');
                    except:
                        reporter.logXcpt('sProcess=%s' % (sProcess,));
                else:
                    reporter.log('%s: "%s"' % (self.sPidFile, sProcess));

        return dPids;

    def pidFileAdd(self, iPid, sName, fSudo = False):
        """
        Adds a PID to the PID file, creating the file if necessary.
        """
        try:
            oFile = utils.openNoInherit(self.sPidFile, 'a');
            oFile.write('%s:%s:%s\n'
                        % ( iPid,
                            'sudo' if fSudo else 'normal',
                             sName.replace(' ', '_').replace(':','_').replace('\n','_').replace('\r','_').replace('\t','_'),));
            oFile.close();
        except:
            reporter.errorXcpt();
            return False;
        ## @todo s/log/log2/
        reporter.log('pidFileAdd: added %s (%#x) %s fSudo=%s (new content: %s)'
                     % (iPid, iPid, sName, fSudo, self.pidFileRead(),));
        return True;

    def pidFileRemove(self, iPid, fQuiet = False):
        """
        Removes a PID from the PID file.
        """
        dPids = self.pidFileRead();
        if iPid not in dPids:
            if not fQuiet:
                reporter.log('pidFileRemove could not find %s in the PID file (content: %s)' % (iPid, dPids));
            return False;

        sName = dPids[iPid][0];
        del dPids[iPid];

        sPid = '';
        for iPid2 in dPids:
            sPid += '%s:%s:%s\n' % (iPid2, 'sudo' if dPids[iPid2][1] else 'normal', dPids[iPid2][0]);

        try:
            oFile = utils.openNoInherit(self.sPidFile, 'w');
            oFile.write(sPid);
            oFile.close();
        except:
            reporter.errorXcpt();
            return False;
        ## @todo s/log/log2/
        reporter.log('pidFileRemove: removed PID %d [%s] (new content: %s)' % (iPid, sName, self.pidFileRead(),));
        return True;

    def pidFileDelete(self):
        """Creates the testdriver PID file."""
        if os.path.isfile(self.sPidFile):
            try:
                os.unlink(self.sPidFile);
            except:
                reporter.logXcpt();
                return False;
            ## @todo s/log/log2/
            reporter.log('pidFileDelete: deleted "%s"' % (self.sPidFile,));
        return True;

    #
    # Misc helper methods.
    #

    def requireMoreArgs(self, cMinNeeded, asArgs, iArg):
        """
        Checks that asArgs has at least cMinNeeded args following iArg.

        Returns iArg + 1 if it checks out fine.
        Raise appropritate exception if not, ASSUMING that the current argument
        is found at iArg.
        """
        assert cMinNeeded >= 1;
        if iArg + cMinNeeded > len(asArgs):
            if cMinNeeded > 1:
                raise InvalidOption('The "%s" option takes %s values' % (asArgs[iArg], cMinNeeded,));
            raise InvalidOption('The "%s" option takes 1 value' % (asArgs[iArg],));
        return iArg + 1;

    def getBinTool(self, sName):
        """
        Returns the full path to the given binary validation kit tool.
        """
        return os.path.join(self.sBinPath, sName) + exeSuff();

    def adjustTimeoutMs(self, cMsTimeout, cMsMinimum = None):
        """
        Adjusts the given timeout (milliseconds) to take TESTBOX_TIMEOUT_ABS
        and cMsMinimum (optional) into account.

        Returns adjusted timeout.
        Raises no exceptions.
        """
        if self.secTimeoutAbs is not None:
            cMsToDeadline = self.secTimeoutAbs * 1000 - utils.timestampMilli();
            if cMsToDeadline >= 0:
                # Adjust for fudge and enforce the minimum timeout
                cMsToDeadline -= self.secTimeoutFudge * 1000;
                if cMsToDeadline < (cMsMinimum if cMsMinimum is not None else 10000):
                    cMsToDeadline = cMsMinimum if cMsMinimum is not None else 10000;

                # Is the timeout beyond the (adjusted) deadline, if so change it.
                if cMsTimeout > cMsToDeadline:
                    reporter.log('adjusting timeout: %s ms -> %s ms (deadline)\n' % (cMsTimeout, cMsToDeadline,));
                    return cMsToDeadline;
                reporter.log('adjustTimeoutMs: cMsTimeout (%s) > cMsToDeadline (%s)' % (cMsTimeout, cMsToDeadline,));
            else:
                # Don't bother, we've passed the deadline.
                reporter.log('adjustTimeoutMs: ooops! cMsToDeadline=%s (%s), timestampMilli()=%s, timestampSecond()=%s'
                             % (cMsToDeadline, cMsToDeadline*1000, utils.timestampMilli(), utils.timestampSecond()));

        # Only enforce the minimum timeout if specified.
        if cMsMinimum is not None and cMsTimeout < cMsMinimum:
            reporter.log('adjusting timeout: %s ms -> %s ms (minimum)\n' % (cMsTimeout, cMsMinimum,));
            cMsTimeout = cMsMinimum;

        return cMsTimeout;

    def prepareResultFile(self, sName = 'results.xml'):
        """
        Given a base name (no path, but extension if required), a scratch file
        name is computed and any previous file removed.

        Returns the full path to the file sName.
        Raises exception on failure.
        """
        sXmlFile = os.path.join(self.sScratchPath, sName);
        if os.path.exists(sXmlFile):
            os.unlink(sXmlFile);
        return sXmlFile;


    #
    # Overridable methods.
    #

    def showUsage(self):
        """
        Shows the usage.

        When overriding this, call super first.
        """
        sName = os.path.basename(sys.argv[0]);
        reporter.log('Usage: %s [options] <action(s)>' % (sName,));
        reporter.log('');
        reporter.log('Actions (in execution order):');
        reporter.log('  cleanup-before');
        reporter.log('      Cleanups done at the start of testing.');
        reporter.log('  verify');
        reporter.log('      Verify that all necessary resources are present.');
        reporter.log('  config');
        reporter.log('      Configure the tests.');
        reporter.log('  execute');
        reporter.log('      Execute the tests.');
        reporter.log('  cleanup-after');
        reporter.log('      Cleanups done at the end of the testing.');
        reporter.log('');
        reporter.log('Special Actions:');
        reporter.log('  all');
        reporter.log('      Alias for: %s' % (' '.join(self.asNormalActions),));
        reporter.log('  extract <path>');
        reporter.log('      Extract the test resources and put them in the specified');
        reporter.log('      path for off side/line testing.');
        reporter.log('  abort');
        reporter.log('      Aborts the test.');
        reporter.log('');
        reporter.log('Base Options:');
        reporter.log('  -h, --help');
        reporter.log('      Show this help message.');
        reporter.log('  -v, --verbose');
        reporter.log('      Increase logging verbosity, repeat for more logging.');
        reporter.log('  -d, --debug');
        reporter.log('      Increase the debug logging level, repeat for more info.');
        reporter.log('  --no-wipe-clean');
        reporter.log('      Do not wipe clean the scratch area during the two clean up');
        reporter.log('      actions.  This is for facilitating nested test driver execution.');
        return True;

    def parseOption(self, asArgs, iArg):
        """
        Parse an option. Override this.

        Keyword arguments:
        asArgs -- The argument vector.
        iArg   -- The index of the current argument.

        Returns iArg if the option was not recognized.
        Returns the index of the next argument when something is consumed.
        In the event of a syntax error, a InvalidOption or QuietInvalidOption
        should be thrown.
        """

        if asArgs[iArg] in ('--help', '-help', '-h', '-?', '/?', '/help', '/H', '-H'):
            self.showUsage();
            self.showSubTstDrvUsage();
            raise QuietInvalidOption();

        # options
        if asArgs[iArg] in ('--verbose', '-v'):
            reporter.incVerbosity()
        elif asArgs[iArg] in ('--debug', '-d'):
            reporter.incDebug()
        elif asArgs[iArg] == '--no-wipe-clean':
            self.fNoWipeClean = True;
        elif (asArgs[iArg] == 'all' or asArgs[iArg] in self.asNormalActions) \
          and self.asActions in self.asSpecialActions:
            raise InvalidOption('selected special action "%s" already' % (self.asActions[0], ));
        # actions
        elif asArgs[iArg] == 'all':
            self.asActions = [ 'all' ];
        elif asArgs[iArg] in self.asNormalActions:
            self.asActions.append(asArgs[iArg])
        elif asArgs[iArg] in self.asSpecialActions:
            if self.asActions != []:
                raise InvalidOption('selected special action "%s" already' % (self.asActions[0], ));
            self.asActions = [ asArgs[iArg] ];
            # extact <destination>
            if asArgs[iArg] == 'extract':
                iArg = iArg + 1;
                if iArg >= len(asArgs): raise InvalidOption('The "extract" action requires a destination directory');
                self.sExtractDstPath = asArgs[iArg];
        else:
            return iArg;
        return iArg + 1;

    def completeOptions(self):
        """
        This method is called after parsing all the options.
        Returns success indicator. Use the reporter to complain.

        Overriable, call super.
        """
        return True;

    def getResourceSet(self):
        """
        Returns a set of file and/or directory names relative to
        TESTBOX_PATH_RESOURCES.

        Override this, call super when using sub-test drivers.
        """
        asRsrcs = [];
        for oSubTstDrv in self.aoSubTstDrvs:
            asRsrcs.extend(oSubTstDrv.asRsrcs);
        return asRsrcs;

    def actionExtract(self):
        """
        Handle the action that extracts the test resources for off site use.
        Returns a success indicator and error details with the reporter.

        There is usually no need to override this.
        """
        fRc = True;
        asRsrcs = self.getResourceSet();
        for iRsrc, sRsrc in enumerate(asRsrcs):
            reporter.log('Resource #%s: "%s"' % (iRsrc, sRsrc));
            sSrcPath = os.path.normpath(os.path.abspath(os.path.join(self.sResourcePath, sRsrc.replace('/', os.path.sep))));
            sDstPath = os.path.normpath(os.path.join(self.sExtractDstPath, sRsrc.replace('/', os.path.sep)));

            sDstDir = os.path.dirname(sDstPath);
            if not os.path.exists(sDstDir):
                try:    os.makedirs(sDstDir, 0o775);
                except: fRc = reporter.errorXcpt('Error creating directory "%s":' % (sDstDir,));

            if os.path.isfile(sSrcPath):
                try:    utils.copyFileSimple(sSrcPath, sDstPath);
                except: fRc = reporter.errorXcpt('Error copying "%s" to "%s":' % (sSrcPath, sDstPath,));
            elif os.path.isdir(sSrcPath):
                fRc = reporter.error('Extracting directories have not been implemented yet');
            else:
                fRc = reporter.error('Missing or unsupported resource type: %s' % (sSrcPath,));
        return fRc;

    def actionVerify(self):
        """
        Handle the action that verify the test resources.
        Returns a success indicator and error details with the reporter.

        There is usually no need to override this.
        """

        asRsrcs = self.getResourceSet();
        for sRsrc in asRsrcs:
            # Go thru some pain to catch escape sequences.
            if sRsrc.find("//") >= 0:
                reporter.error('Double slash test resource name: "%s"' % (sRsrc));
                return False;
            if   sRsrc == ".." \
              or sRsrc.startswith("../") \
              or sRsrc.find("/../") >= 0 \
              or sRsrc.endswith("/.."):
                reporter.error('Relative path in test resource name: "%s"' % (sRsrc));
                return False;

            sFull = os.path.normpath(os.path.abspath(os.path.join(self.sResourcePath, sRsrc)));
            if not sFull.startswith(os.path.normpath(self.sResourcePath)):
                reporter.error('sFull="%s" self.sResourcePath=%s' % (sFull, self.sResourcePath));
                reporter.error('The resource "%s" seems to specify a relative path' % (sRsrc));
                return False;

            reporter.log2('Checking for resource "%s" at "%s" ...' % (sRsrc, sFull));
            if os.path.isfile(sFull):
                try:
                    oFile = utils.openNoInherit(sFull, "rb");
                    oFile.close();
                except Exception as oXcpt:
                    reporter.error('The file resource "%s" cannot be accessed: %s' % (sFull, oXcpt));
                    return False;
            elif os.path.isdir(sFull):
                if not os.path.isdir(os.path.join(sFull, '.')):
                    reporter.error('The directory resource "%s" cannot be accessed' % (sFull));
                    return False;
            elif os.path.exists(sFull):
                reporter.error('The resource "%s" is not a file or directory' % (sFull));
                return False;
            else:
                reporter.error('The resource "%s" was not found' % (sFull));
                return False;
        return True;

    def actionConfig(self):
        """
        Handle the action that configures the test.
        Returns True (success), False (failure) or None (skip the test),
        posting complaints and explanations with the reporter.

        Override this.
        """
        return True;

    def actionExecute(self):
        """
        Handle the action that executes the test.

        Returns True (success), False (failure) or None (skip the test),
        posting complaints and explanations with the reporter.

        Override this.
        """
        return True;

    def actionCleanupBefore(self):
        """
        Handle the action that cleans up spills from previous tests before
        starting the tests.  This is mostly about wiping the scratch space
        clean in local runs.  On a testbox the testbox script will use the
        cleanup-after if the test is interrupted.

        Returns True (success), False (failure) or None (skip the test),
        posting complaints and explanations with the reporter.

        Override this, but call super to wipe the scratch directory.
        """
        if self.fNoWipeClean is False:
            self.wipeScratch();
        return True;

    def actionCleanupAfter(self):
        """
        Handle the action that cleans up all spills from executing the test.

        Returns True (success) or False (failure) posting complaints and
        explanations with the reporter.

        Override this, but call super to wipe the scratch directory.
        """
        if self.fNoWipeClean is False:
            self.wipeScratch();
        return True;

    def actionAbort(self):
        """
        Handle the action that aborts a (presumed) running testdriver, making
        sure to include all it's children.

        Returns True (success) or False (failure) posting complaints and
        explanations with the reporter.

        Override this, but call super to kill the testdriver script and any
        other process covered by the testdriver PID file.
        """

        dPids = self.pidFileRead();
        reporter.log('The pid file contained: %s' % (dPids,));

        #
        # Try convince the processes to quit with increasing impoliteness.
        #
        if sys.platform == 'win32':
            afnMethods = [ processInterrupt, processTerminate ];
        else:
            afnMethods = [ sendUserSignal1, processInterrupt, processTerminate, processKill ];
        for fnMethod in afnMethods:
            for iPid in dPids:
                fnMethod(iPid, fSudo = dPids[iPid][1]);

            for i in range(10):
                if i > 0:
                    time.sleep(1);

                for iPid in dPids:
                    if not processExists(iPid):
                        reporter.log('%s (%s) terminated' % (dPids[iPid][0], iPid,));
                        self.pidFileRemove(iPid, fQuiet = True);
                        del dPids[iPid];

                if not dPids:
                    reporter.log('All done.');
                    return True;

                if i in [4, 8]:
                    reporter.log('Still waiting for: %s (method=%s)' % (dPids, fnMethod,));

        reporter.log('Failed to terminate the following processes: %s' % (dPids,));
        return False;


    def onExit(self, iRc):
        """
        Hook for doing very important cleanups on the way out.

        iRc is the exit code or -1 in the case of an unhandled exception.
        Returns nothing and shouldn't raise exceptions (will be muted+ignored).
        """
        _ = iRc;
        return None;


    #
    # main() - don't override anything!
    #

    def main(self, asArgs = None):
        """
        The main function of the test driver.

        Keyword arguments:
        asArgs -- The argument vector.  Defaults to sys.argv.

        Returns exit code. No exceptions.
        """

        #
        # Wrap worker in exception handler and always call a 'finally' like
        # method to do crucial cleanups on the way out.
        #
        try:
            iRc = self.innerMain(asArgs);
        except:
            try:
                self.onExit(-1);
            except:
                reporter.logXcpt();
            raise;
        self.onExit(iRc);
        return iRc;


    def innerMain(self, asArgs = None): # pylint: disable=R0915
        """
        Exception wrapped main() worker.
        """

        # parse the arguments.
        if asArgs is None:
            asArgs = list(sys.argv);
        iArg = 1;
        try:
            while iArg < len(asArgs):
                iNext = self.parseOption(asArgs, iArg);
                if iNext == iArg:
                    iNext = self.subTstDrvParseOption(asArgs, iArg);
                    if iNext == iArg:
                        raise InvalidOption('unknown option: %s' % (asArgs[iArg]))
                iArg = iNext;
        except QuietInvalidOption as oXcpt:
            return rtexitcode.RTEXITCODE_SYNTAX;
        except InvalidOption as oXcpt:
            reporter.error(oXcpt.str());
            return rtexitcode.RTEXITCODE_SYNTAX;
        except:
            reporter.error('unexpected exception while parsing argument #%s' % (iArg));
            traceback.print_exc();
            return rtexitcode.RTEXITCODE_SYNTAX;

        if not self.completeOptions():
            return rtexitcode.RTEXITCODE_SYNTAX;

        if self.asActions == []:
            reporter.error('no action was specified');
            reporter.error('valid actions: %s' % (self.asNormalActions + self.asSpecialActions + ['all']));
            return rtexitcode.RTEXITCODE_SYNTAX;

        # execte the actions.
        fRc = True;         # Tristate - True (success), False (failure), None (skipped).
        asActions = self.asActions;
        if 'extract' in asActions:
            reporter.log('*** extract action ***');
            asActions.remove('extract');
            fRc = self.actionExtract();
            reporter.log('*** extract action completed (fRc=%s) ***' % (fRc));
        elif 'abort' in asActions:
            reporter.appendToProcessName('/abort'); # Make it easier to spot in the log.
            reporter.log('*** abort action ***');
            asActions.remove('abort');
            fRc = self.actionAbort();
            reporter.log('*** abort action completed (fRc=%s) ***' % (fRc));
        else:
            if asActions == [ 'all' ]:
                asActions = self.asNormalActions;

            if 'verify' in asActions:
                reporter.log('*** verify action ***');
                asActions.remove('verify');
                fRc = self.actionVerify();
                if fRc is True: reporter.log("verified succeeded");
                else:           reporter.log("verified failed (fRc=%s)" % (fRc,));
                reporter.log('*** verify action completed (fRc=%s) ***' % (fRc,));

            if 'cleanup-before' in asActions:
                reporter.log('*** cleanup-before action ***');
                asActions.remove('cleanup-before');
                fRc2 = self.actionCleanupBefore();
                if fRc2 is not True: reporter.log("cleanup-before failed");
                if fRc2 is not True and fRc is True: fRc = fRc2;
                reporter.log('*** cleanup-before action completed (fRc2=%s, fRc=%s) ***' % (fRc2, fRc,));

            self.pidFileAdd(os.getpid(), os.path.basename(sys.argv[0]));

            if 'config' in asActions and fRc is True:
                asActions.remove('config');
                reporter.log('*** config action ***');
                fRc = self.actionConfig();
                if fRc is True:     reporter.log("config succeeded");
                elif fRc is None:   reporter.log("config skipping test");
                else:               reporter.log("config failed");
                reporter.log('*** config action completed (fRc=%s) ***' % (fRc,));

            if 'execute' in asActions and fRc is True:
                asActions.remove('execute');
                reporter.log('*** execute action ***');
                fRc = self.actionExecute();
                if fRc is True:     reporter.log("execute succeeded");
                elif fRc is None:   reporter.log("execute skipping test");
                else:               reporter.log("execute failed (fRc=%s)" % (fRc,));
                reporter.testCleanup();
                reporter.log('*** execute action completed (fRc=%s) ***' % (fRc,));

            if 'cleanup-after' in asActions:
                reporter.log('*** cleanup-after action ***');
                asActions.remove('cleanup-after');
                fRc2 = self.actionCleanupAfter();
                if fRc2 is not True: reporter.log("cleanup-after failed");
                if fRc2 is not True and fRc is True: fRc = fRc2;
                reporter.log('*** cleanup-after action completed (fRc2=%s, fRc=%s) ***' % (fRc2, fRc,));

            self.pidFileRemove(os.getpid());

        if asActions != [] and fRc is True:
            reporter.error('unhandled actions: %s' % (asActions,));
            fRc = False;

        # Done
        if fRc is None:
            reporter.log('*****************************************');
            reporter.log('*** The test driver SKIPPED the test. ***');
            reporter.log('*****************************************');
            return rtexitcode.RTEXITCODE_SKIPPED;
        if fRc is not True:
            reporter.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
            reporter.error('!!! The test driver FAILED (in case we forgot to mention it). !!!');
            reporter.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
            return rtexitcode.RTEXITCODE_FAILURE;
        reporter.log('*******************************************');
        reporter.log('*** The test driver exits successfully. ***');
        reporter.log('*******************************************');
        return rtexitcode.RTEXITCODE_SUCCESS;

# The old, deprecated name.
TestDriver = TestDriverBase; # pylint: disable=C0103


#
# Unit testing.
#

# pylint: disable=C0111
class TestDriverBaseTestCase(unittest.TestCase):
    def setUp(self):
        self.oTstDrv = TestDriverBase();
        self.oTstDrv.pidFileDelete();

    def tearDown(self):
        pass; # clean up scratch dir and such.

    def testPidFile(self):

        iPid1 = os.getpid() + 1;
        iPid2 = os.getpid() + 2;

        self.assertTrue(self.oTstDrv.pidFileAdd(iPid1, 'test1'));
        self.assertEqual(self.oTstDrv.pidFileRead(), {iPid1:('test1',False)});

        self.assertTrue(self.oTstDrv.pidFileAdd(iPid2, 'test2', fSudo = True));
        self.assertEqual(self.oTstDrv.pidFileRead(), {iPid1:('test1',False), iPid2:('test2',True)});

        self.assertTrue(self.oTstDrv.pidFileRemove(iPid1));
        self.assertEqual(self.oTstDrv.pidFileRead(), {iPid2:('test2',True)});

        self.assertTrue(self.oTstDrv.pidFileRemove(iPid2));
        self.assertEqual(self.oTstDrv.pidFileRead(), {});

        self.assertTrue(self.oTstDrv.pidFileDelete());

if __name__ == '__main__':
    unittest.main();
    # not reached.