summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/common/VBoxService/VBoxServiceAutoMount.cpp
blob: 75bf718a44a1aeb5e344d42b966759178d0609c0 (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
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
/* $Id: VBoxServiceAutoMount.cpp $ */
/** @file
 * VBoxService - Auto-mounting for Shared Folders, only Linux & Solaris atm.
 */

/*
 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/** @page pg_vgsvc_automount VBoxService - Shared Folder Automounter
 *
 * The Shared Folder Automounter subservice mounts shared folders upon request
 * from the host.
 *
 * This retrieves shared folder automount requests from Main via the VMMDev.
 * The current implemention only does this once, for some inexplicable reason,
 * so the run-time addition of automounted shared folders are not heeded.
 *
 * This subservice is only used on linux and solaris.  On Windows the current
 * thinking is this is better of done from VBoxTray, some one argue that for
 * drive letter assigned shared folders it would be better to do some magic here
 * (obviously not involving NDAddConnection).
 *
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/dir.h>
#include <iprt/mem.h>
#include <iprt/path.h>
#include <iprt/semaphore.h>
#include <iprt/sort.h>
#include <iprt/string.h>
#include <VBox/err.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/shflsvc.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"

#ifdef RT_OS_WINDOWS
#elif defined(RT_OS_OS2)
# define INCL_DOSFILEMGR
# define INCL_ERRORS
# define OS2EMX_PLAIN_CHAR
# include <os2emx.h>
#else
# include <errno.h>
# include <grp.h>
# include <sys/mount.h>
# ifdef RT_OS_SOLARIS
#  include <sys/mntent.h>
#  include <sys/mnttab.h>
#  include <sys/vfs.h>
# elif defined(RT_OS_LINUX)
#  include <mntent.h>
#  include <paths.h>
#  include <sys/utsname.h>
RT_C_DECLS_BEGIN
#  include "../../linux/sharedfolders/vbsfmount.h"
RT_C_DECLS_END
# else
#  error "Port me!"
# endif
# include <unistd.h>
#endif



/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** @def VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR
 * Default mount directory (unix only).
 */
#ifndef VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR
# define VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR      "/media"
#endif

/** @def VBOXSERVICE_AUTOMOUNT_DEFAULT_PREFIX
 * Default mount prefix (unix only).
 */
#ifndef VBOXSERVICE_AUTOMOUNT_DEFAULT_PREFIX
# define VBOXSERVICE_AUTOMOUNT_DEFAULT_PREFIX   "sf_"
#endif

#ifndef _PATH_MOUNTED
# ifdef RT_OS_SOLARIS
#  define _PATH_MOUNTED                          "/etc/mnttab"
# else
#  define _PATH_MOUNTED                          "/etc/mtab"
# endif
#endif

/** @def VBOXSERVICE_AUTOMOUNT_MIQF
 * The drive letter / path mount point flag.  */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
# define VBOXSERVICE_AUTOMOUNT_MIQF             SHFL_MIQF_DRIVE_LETTER
#else
# define VBOXSERVICE_AUTOMOUNT_MIQF             SHFL_MIQF_PATH
#endif


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Automounter mount table entry.
 *
 * This holds the information returned by SHFL_FN_QUERY_MAP_INFO and
 * additional mount state info.  We only keep entries for mounted mappings.
 */
typedef struct VBSVCAUTOMOUNTERENTRY
{
    /** The root ID. */
    uint32_t     idRoot;
    /** The root ID version. */
    uint32_t     uRootIdVersion;
    /** Map info flags, SHFL_MIF_XXX. */
    uint64_t     fFlags;
    /** The shared folder (mapping) name. */
    char        *pszName;
    /** The configured mount point, NULL if none. */
    char        *pszMountPoint;
    /** The actual mount point, NULL if not mount.  */
    char        *pszActualMountPoint;
} VBSVCAUTOMOUNTERENTRY;
/** Pointer to an automounter entry.   */
typedef VBSVCAUTOMOUNTERENTRY *PVBSVCAUTOMOUNTERENTRY;

/** Automounter mount table. */
typedef struct VBSVCAUTOMOUNTERTABLE
{
    /** Current number of entries in the array. */
    uint32_t                cEntries;
    /** Max number of entries the array can hold w/o growing it. */
    uint32_t                cAllocated;
    /** Pointer to an array of entry pointers. */
    PVBSVCAUTOMOUNTERENTRY   *papEntries;
} VBSVCAUTOMOUNTERTABLE;
/** Pointer to an automounter mount table.   */
typedef  VBSVCAUTOMOUNTERTABLE *PVBSVCAUTOMOUNTERTABLE;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** The semaphore we're blocking on. */
static RTSEMEVENTMULTI  g_hAutoMountEvent = NIL_RTSEMEVENTMULTI;
/** The Shared Folders service client ID. */
static uint32_t         g_idClientSharedFolders = 0;
/** Set if we can wait on changes to the mappings. */
static bool             g_fHostSupportsWaitAndInfoQuery = false;

#ifdef RT_OS_OS2
/** The attachment tag we use to identify attchments that belongs to us. */
static char const       g_szTag[] = "VBoxAutomounter";
#elif defined(RT_OS_LINUX)
/** Tag option value that lets us identify mounts that belongs to us. */
static char const       g_szTag[] = "VBoxAutomounter";
#elif defined(RT_OS_SOLARIS)
/** Dummy mount option that lets us identify mounts that belongs to us. */
static char const       g_szTag[] = "VBoxAutomounter";
#endif



/**
 * @interface_method_impl{VBOXSERVICE,pfnInit}
 */
static DECLCALLBACK(int) vbsvcAutomounterInit(void)
{
    VGSvcVerbose(3, "vbsvcAutomounterInit\n");

    int rc = RTSemEventMultiCreate(&g_hAutoMountEvent);
    AssertRCReturn(rc, rc);

    rc = VbglR3SharedFolderConnect(&g_idClientSharedFolders);
    if (RT_SUCCESS(rc))
    {
        VGSvcVerbose(3, "vbsvcAutomounterInit: Service Client ID: %#x\n", g_idClientSharedFolders);
        g_fHostSupportsWaitAndInfoQuery = RT_SUCCESS(VbglR3SharedFolderCancelMappingsChangesWaits(g_idClientSharedFolders));
    }
    else
    {
        /* If the service was not found, we disable this service without
           causing VBoxService to fail. */
        if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
        {
            VGSvcVerbose(0, "vbsvcAutomounterInit: Shared Folders service is not available\n");
            rc = VERR_SERVICE_DISABLED;
        }
        else
            VGSvcError("Control: Failed to connect to the Shared Folders service! Error: %Rrc\n", rc);
        RTSemEventMultiDestroy(g_hAutoMountEvent);
        g_hAutoMountEvent = NIL_RTSEMEVENTMULTI;
    }

    return rc;
}


#if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) /* The old code: */

/**
 * @todo Integrate into RTFsQueryMountpoint()?
 */
static bool vbsvcAutoMountShareIsMountedOld(const char *pszShare, char *pszMountPoint, size_t cbMountPoint)
{
    AssertPtrReturn(pszShare, false);
    AssertPtrReturn(pszMountPoint, false);
    AssertReturn(cbMountPoint, false);

    bool fMounted = false;

# if defined(RT_OS_SOLARIS)
    /** @todo What to do if we have a relative path in mtab instead
     *       of an absolute one ("temp" vs. "/media/temp")?
     * procfs contains the full path but not the actual share name ...
     * FILE *pFh = setmntent("/proc/mounts", "r+t"); */
    FILE *pFh = fopen(_PATH_MOUNTED, "r");
    if (!pFh)
        VGSvcError("vbsvcAutoMountShareIsMountedOld: Could not open mount tab '%s'!\n", _PATH_MOUNTED);
    else
    {
        mnttab mntTab;
        while ((getmntent(pFh, &mntTab)))
        {
            if (!RTStrICmp(mntTab.mnt_special, pszShare))
            {
                fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", mntTab.mnt_mountp)
                         ? true : false;
                break;
            }
        }
        fclose(pFh);
    }
# elif defined(RT_OS_LINUX)
    FILE *pFh = setmntent(_PATH_MOUNTED, "r+t"); /** @todo r=bird: why open it for writing? (the '+') */
    if (pFh == NULL)
        VGSvcError("vbsvcAutoMountShareIsMountedOld: Could not open mount tab '%s'!\n", _PATH_MOUNTED);
    else
    {
        mntent *pMntEnt;
        while ((pMntEnt = getmntent(pFh)))
        {
            if (!RTStrICmp(pMntEnt->mnt_fsname, pszShare))
            {
                fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", pMntEnt->mnt_dir)
                         ? true : false;
                break;
            }
        }
        endmntent(pFh);
    }
# else
#  error "PORTME!"
# endif

    VGSvcVerbose(4, "vbsvcAutoMountShareIsMountedOld: Share '%s' at mount point '%s' = %s\n",
                       pszShare, fMounted ? pszMountPoint : "<None>", fMounted ? "Yes" : "No");
    return fMounted;
}


/**
 * Unmounts a shared folder.
 *
 * @returns VBox status code
 * @param   pszMountPoint   The shared folder mount point.
 */
static int vbsvcAutoMountUnmountOld(const char *pszMountPoint)
{
    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);

    int rc = VINF_SUCCESS;
    uint8_t uTries = 0;
    int r;
    while (uTries++ < 3)
    {
        r = umount(pszMountPoint);
        if (r == 0)
            break;
/** @todo r=bird: Why do sleep 5 seconds after the final retry?
 *  May also be a good idea to check for EINVAL or other signs that someone
 *  else have already unmounted the share. */
        RTThreadSleep(5000); /* Wait a while ... */
    }
    if (r == -1)  /** @todo r=bird: RTThreadSleep set errno.  */
        rc = RTErrConvertFromErrno(errno);
    return rc;
}


/**
 * Prepares a mount point (create it, set group and mode).
 *
 * @returns VBox status code
 * @param   pszMountPoint   The mount point.
 * @param   pszShareName    Unused.
 * @param   gidGroup        The group ID.
 */
static int vbsvcAutoMountPrepareMountPointOld(const char *pszMountPoint, const char *pszShareName, RTGID gidGroup)
{
    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pszShareName, VERR_INVALID_PARAMETER);

    /** @todo r=bird: There is no reason why gidGroup should have write access?
     *        Seriously, what kind of non-sense is this? */

    RTFMODE fMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG; /* Owner (=root) and the group (=vboxsf) have full access. */
    int rc = RTDirCreateFullPath(pszMountPoint, fMode);
    if (RT_SUCCESS(rc))
    {
        rc = RTPathSetOwnerEx(pszMountPoint, NIL_RTUID /* Owner, unchanged */, gidGroup, RTPATH_F_ON_LINK);
        if (RT_SUCCESS(rc))
        {
            rc = RTPathSetMode(pszMountPoint, fMode);
            if (RT_FAILURE(rc))
            {
                if (rc == VERR_WRITE_PROTECT)
                {
                    VGSvcVerbose(3, "vbsvcAutoMountPrepareMountPointOld: Mount directory '%s' already is used/mounted\n",
                                 pszMountPoint);
                    rc = VINF_SUCCESS;
                }
                else
                    VGSvcError("vbsvcAutoMountPrepareMountPointOld: Could not set mode %RTfmode for mount directory '%s', rc = %Rrc\n",
                               fMode, pszMountPoint, rc);
            }
        }
        else
            VGSvcError("vbsvcAutoMountPrepareMountPointOld: Could not set permissions for mount directory '%s', rc = %Rrc\n",
                       pszMountPoint, rc);
    }
    else
        VGSvcError("vbsvcAutoMountPrepareMountPointOld: Could not create mount directory '%s' with mode %RTfmode, rc = %Rrc\n",
                   pszMountPoint, fMode, rc);
    return rc;
}


/**
 * Mounts a shared folder.
 *
 * @returns VBox status code reflecting unmount and mount point preparation
 *          results, but not actual mounting
 *
 * @param   pszShareName    The shared folder name.
 * @param   pszMountPoint   The mount point.
 */
static int vbsvcAutoMountSharedFolderOld(const char *pszShareName, const char *pszMountPoint)
{
    /*
     * Linux and solaris share the same mount structure.
     */
    struct group *grp_vboxsf = getgrnam("vboxsf");
    if (!grp_vboxsf)
    {
        VGSvcError("vbsvcAutoMountWorker: Group 'vboxsf' does not exist\n");
        return VINF_SUCCESS;
    }

    int rc = vbsvcAutoMountPrepareMountPointOld(pszMountPoint, pszShareName, grp_vboxsf->gr_gid);
    if (RT_SUCCESS(rc))
    {
# ifdef RT_OS_SOLARIS
        int const fFlags = MS_OPTIONSTR;
        char szOptBuf[MAX_MNTOPT_STR] = { '\0', };
        RTStrPrintf(szOptBuf, sizeof(szOptBuf), "uid=0,gid=%d,dmode=0770,fmode=0770,dmask=0000,fmask=0000", grp_vboxsf->gr_gid);
        int r = mount(pszShareName,
                      pszMountPoint,
                      fFlags,
                      "vboxfs",
                      NULL,                     /* char *dataptr */
                      0,                        /* int datalen */
                      szOptBuf,
                      sizeof(szOptBuf));
        if (r == 0)
            VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' was mounted to '%s'\n", pszShareName, pszMountPoint);
        else if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
            VGSvcError("vbsvcAutoMountWorker: Could not mount shared folder '%s' to '%s', error = %s\n",
                       pszShareName, pszMountPoint, strerror(errno));

# else /* RT_OS_LINUX */
        struct utsname uts;
        AssertStmt(uname(&uts) != -1, strcpy(uts.release, "4.4.0"));

        unsigned long const fFlags = MS_NODEV;
        char szOpts[MAX_MNTOPT_STR] = { '\0' };
        ssize_t cchOpts = RTStrPrintf2(szOpts, sizeof(szOpts), "uid=0,gid=%d,dmode=0770,fmode=0770,dmask=0000,fmask=0000",
                                       grp_vboxsf->gr_gid);
        if (cchOpts > 0 && RTStrVersionCompare(uts.release, "2.6.0") < 0)
            cchOpts = RTStrPrintf2(&szOpts[cchOpts], sizeof(szOpts) - cchOpts, ",sf_name=%s", pszShareName);
        if (cchOpts <= 0)
        {
            VGSvcError("vbsvcAutomounterMountIt: szOpts overflow! %zd (share %s)\n", cchOpts, pszShareName);
            return VERR_BUFFER_OVERFLOW;
        }

        int r = mount(pszShareName,
                      pszMountPoint,
                      "vboxsf",
                      fFlags,
                      szOpts);
        if (r == 0)
        {
            VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' was mounted to '%s'\n", pszShareName, pszMountPoint);

            r = vbsfmount_complete(pszShareName, pszMountPoint, fFlags, szOpts);
            switch (r)
            {
                case 0: /* Success. */
                    errno = 0; /* Clear all errors/warnings. */
                    break;
                case 1:
                    VGSvcError("vbsvcAutoMountWorker: Could not update mount table (malloc failure)\n");
                    break;
                case 2:
                    VGSvcError("vbsvcAutoMountWorker: Could not open mount table for update: %s\n", strerror(errno));
                    break;
                case 3:
                    /* VGSvcError("vbsvcAutoMountWorker: Could not add an entry to the mount table: %s\n", strerror(errno)); */
                    errno = 0;
                    break;
                default:
                    VGSvcError("vbsvcAutoMountWorker: Unknown error while completing mount operation: %d\n", r);
                    break;
            }
        }
        else /* r == -1, we got some error in errno.  */
        {
            switch (errno)
            {
                /* If we get EINVAL here, the system already has mounted the Shared Folder to another
                 * mount point. */
                case EINVAL:
                    VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' is already mounted!\n", pszShareName);
                    /* Ignore this error! */
                    break;
                case EBUSY:
                    /* Ignore these errors! */
                    break;
                default:
                    VGSvcError("vbsvcAutoMountWorker: Could not mount shared folder '%s' to '%s': %s (%d)\n",
                               pszShareName, pszMountPoint, strerror(errno), errno);
                    rc = RTErrConvertFromErrno(errno);
                    break;
            }
        }
# endif
    }
    VGSvcVerbose(3, "vbsvcAutoMountWorker: Mounting returned with rc=%Rrc\n", rc);
    return rc;
}


/**
 * Processes shared folder mappings retrieved from the host.
 *
 * @returns VBox status code.
 * @param   paMappings      The mappings.
 * @param   cMappings       The number of mappings.
 * @param   pszMountDir     The mount directory.
 * @param   pszSharePrefix  The share prefix.
 * @param   uClientID       The shared folder service (HGCM) client ID.
 */
static int vbsvcAutoMountProcessMappingsOld(PCVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings,
                                            const char *pszMountDir, const char *pszSharePrefix, uint32_t uClientID)
{
    if (cMappings == 0)
        return VINF_SUCCESS;
    AssertPtrReturn(paMappings, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pszMountDir, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pszSharePrefix, VERR_INVALID_PARAMETER);
    AssertReturn(uClientID > 0, VERR_INVALID_PARAMETER);

    /** @todo r=bird: Why is this loop schitzoid about status codes? It quits if
     * RTPathJoin fails (i.e. if the user specifies a very long name), but happily
     * continues if RTStrAPrintf failes (mem alloc).
     *
     * It also happily continues if the 'vboxsf' group is missing, which is a waste
     * of effort... In fact, retrieving the group ID could probably be done up
     * front, outside the loop. */
    int rc = VINF_SUCCESS;
    for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
    {
        char *pszShareName = NULL;
        rc = VbglR3SharedFolderGetName(uClientID, paMappings[i].u32Root, &pszShareName);
        if (   RT_SUCCESS(rc)
            && *pszShareName)
        {
            VGSvcVerbose(3, "vbsvcAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);

            /** @todo r=bird: why do you copy things twice here and waste heap space?
             * szMountPoint has a fixed size.
             * @code
             * char szMountPoint[RTPATH_MAX];
             * rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, *pszSharePrefix ? pszSharePrefix : pszShareName);
             * if (RT_SUCCESS(rc) && *pszSharePrefix)
             *     rc = RTStrCat(szMountPoint, sizeof(szMountPoint), pszShareName);
             * @endcode */
            char *pszShareNameFull = NULL;
            if (RTStrAPrintf(&pszShareNameFull, "%s%s", pszSharePrefix, pszShareName) > 0)
            {
                char szMountPoint[RTPATH_MAX];
                rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, pszShareNameFull);
                if (RT_SUCCESS(rc))
                {
                    VGSvcVerbose(4, "vbsvcAutoMountWorker: Processing mount point '%s'\n", szMountPoint);

                    /*
                     * Already mounted?
                     */
                    /** @todo r-bird: this does not take into account that a shared folder could
                     *        be mounted twice... We're really just interested in whether the
                     *        folder is mounted on 'szMountPoint', no where else... */
                    bool fSkip = false;
                    char szAlreadyMountedOn[RTPATH_MAX];
                    if (vbsvcAutoMountShareIsMountedOld(pszShareName, szAlreadyMountedOn, sizeof(szAlreadyMountedOn)))
                    {
                        /* Do if it not mounted to our desired mount point */
                        if (RTStrICmp(szMountPoint, szAlreadyMountedOn))
                        {
                            VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder '%s' already mounted on '%s', unmounting ...\n",
                                         pszShareName, szAlreadyMountedOn);
                            rc = vbsvcAutoMountUnmountOld(szAlreadyMountedOn);
                            if (RT_SUCCESS(rc))
                                fSkip = false;
                            else
                                VGSvcError("vbsvcAutoMountWorker: Failed to unmount '%s', %s (%d)! (rc=%Rrc)\n",
                                           szAlreadyMountedOn, strerror(errno), errno, rc); /** @todo errno isn't reliable at this point */
                        }
                        if (fSkip)
                            VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder '%s' already mounted on '%s', skipping\n",
                                         pszShareName, szAlreadyMountedOn);
                    }
                    if (!fSkip)
                    {
                        /*
                         * Mount it.
                         */
                        rc = vbsvcAutoMountSharedFolderOld(pszShareName, szMountPoint);
                    }
                }
                else
                    VGSvcError("vbsvcAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc);
                RTStrFree(pszShareNameFull);
            }
            else
                VGSvcError("vbsvcAutoMountWorker: Unable to allocate full share name\n");
            RTStrFree(pszShareName);
        }
        else
            VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
                             paMappings[i].u32Root, rc);
    } /* for cMappings. */
    return rc;
}

#endif /* defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) - the old code*/


/**
 * Service worker function for old host.
 *
 * This only mount stuff on startup.
 *
 * @returns VBox status code.
 * @param   pfShutdown          Shutdown indicator.
 */
static int vbsvcAutoMountWorkerOld(bool volatile *pfShutdown)
{
#if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX)
    /*
     * We only do a single pass here.
     */
    uint32_t cMappings;
    PVBGLR3SHAREDFOLDERMAPPING paMappings;
    int rc = VbglR3SharedFolderGetMappings(g_idClientSharedFolders, true /* Only process auto-mounted folders */,
                                           &paMappings, &cMappings);
    if (   RT_SUCCESS(rc)
        && cMappings)
    {
        char *pszMountDir;
        rc = VbglR3SharedFolderGetMountDir(&pszMountDir);
        if (rc == VERR_NOT_FOUND)
            rc = RTStrDupEx(&pszMountDir, VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR);
        if (RT_SUCCESS(rc))
        {
            VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount dir set to '%s'\n", pszMountDir);

            char *pszSharePrefix;
            rc = VbglR3SharedFolderGetMountPrefix(&pszSharePrefix);
            if (RT_SUCCESS(rc))
            {
                VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount prefix set to '%s'\n", pszSharePrefix);
# ifdef USE_VIRTUAL_SHARES
                /* Check for a fixed/virtual auto-mount share. */
                if (VbglR3SharedFolderExists(g_idClientSharedFolders, "vbsfAutoMount"))
                    VGSvcVerbose(3, "vbsvcAutoMountWorker: Host supports auto-mount root\n");
                else
                {
# endif
                    VGSvcVerbose(3, "vbsvcAutoMountWorker: Got %u shared folder mappings\n", cMappings);
                    rc = vbsvcAutoMountProcessMappingsOld(paMappings, cMappings, pszMountDir, pszSharePrefix,
                                                          g_idClientSharedFolders);
# ifdef USE_VIRTUAL_SHARES
                }
# endif
                RTStrFree(pszSharePrefix);
            } /* Mount share prefix. */
            else
                VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mount prefix, rc = %Rrc\n", rc);
            RTStrFree(pszMountDir);
        }
        else
            VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder directory, rc = %Rrc\n", rc);
        VbglR3SharedFolderFreeMappings(paMappings);
    }
    else if (RT_FAILURE(rc))
        VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
    else
        VGSvcVerbose(3, "vbsvcAutoMountWorker: No shared folder mappings found\n");

#else
    int rc = VINF_SUCCESS;
#endif /* defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) */


    /*
     * Wait on shutdown (this used to be a silly RTThreadSleep(500) loop).
     */
    while (!*pfShutdown)
    {
        rc = RTSemEventMultiWait(g_hAutoMountEvent, RT_MS_1MIN);
        if (rc != VERR_TIMEOUT)
            break;
    }

    VGSvcVerbose(3, "vbsvcAutoMountWorkerOld: Finished with rc=%Rrc\n", rc);
    return rc;
}

#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
/**
 * Assembles the mount directory and prefix into @a pszDst.
 *
 * Will fall back on defaults if we have trouble with the configuration from the
 * host.  This ASSUMES that @a cbDst is rather large and won't cause trouble
 * with the default.
 *
 * @returns IPRT status code.
 * @param   pszDst          Where to return the prefix.
 * @param   cbDst           The size of the prefix buffer.
 */
static int vbsvcAutomounterQueryMountDirAndPrefix(char *pszDst, size_t cbDst)
{
    /*
     * Query the config first.
     */
    /* Mount directory: */
    const char *pszDir = VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR;
    char       *pszCfgDir;
    int rc = VbglR3SharedFolderGetMountDir(&pszCfgDir);
    if (RT_SUCCESS(rc))
    {
        if (*pszCfgDir == '/')
            pszDir = pszCfgDir;
    }
    else
        pszCfgDir = NULL;

    /* Prefix: */
    const char *pszPrefix = VBOXSERVICE_AUTOMOUNT_DEFAULT_PREFIX;
    char *pszCfgPrefix;
    rc = VbglR3SharedFolderGetMountPrefix(&pszCfgPrefix);
    if (RT_SUCCESS(rc))
    {
        if (   strchr(pszCfgPrefix, '/')  == NULL
            && strchr(pszCfgPrefix, '\\') == NULL
            && strcmp(pszCfgPrefix, "..") != 0)
            pszPrefix = pszCfgPrefix;
    }
    else
        pszCfgPrefix = NULL;

    /*
     * Try combine the two.
     */
    rc = RTPathAbs(pszDir, pszDst, cbDst);
    if (RT_SUCCESS(rc))
    {
        if (*pszPrefix)
        {
            rc = RTPathAppend(pszDst, cbDst, pszPrefix);
            if (RT_FAILURE(rc))
                VGSvcError("vbsvcAutomounterQueryMountDirAndPrefix: RTPathAppend(%s,,%s) -> %Rrc\n", pszDst, pszPrefix, rc);
        }
        else
        {
            rc = RTPathEnsureTrailingSeparator(pszDst, cbDst);
            if (RT_FAILURE(rc))
                VGSvcError("vbsvcAutomounterQueryMountDirAndPrefix: RTPathEnsureTrailingSeparator(%s) -> %Rrc\n", pszDst, rc);
        }
    }
    else
        VGSvcError("vbsvcAutomounterQueryMountDirAndPrefix: RTPathAbs(%s) -> %Rrc\n", pszDir, rc);


    /*
     * Return the default dir + prefix if the above failed.
     */
    if (RT_FAILURE(rc))
    {
        rc = RTStrCopy(pszDst, cbDst, VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR "/" VBOXSERVICE_AUTOMOUNT_DEFAULT_PREFIX);
        AssertRC(rc);
    }

    RTStrFree(pszCfgDir);
    RTStrFree(pszCfgPrefix);
    return rc;
}
#endif /* !RT_OS_WINDOW && !RT_OS_OS2 */


/**
 * @callback_method_impl{FNRTSORTCMP, For sorting mount table by root ID. }
 */
static DECLCALLBACK(int) vbsvcAutomounterCompareEntry(void const *pvElement1, void const *pvElement2, void *pvUser)
{
    RT_NOREF_PV(pvUser);
    PVBSVCAUTOMOUNTERENTRY pEntry1 = (PVBSVCAUTOMOUNTERENTRY)pvElement1;
    PVBSVCAUTOMOUNTERENTRY pEntry2 = (PVBSVCAUTOMOUNTERENTRY)pvElement2;
    return pEntry1->idRoot < pEntry2->idRoot ? -1
         : pEntry1->idRoot > pEntry2->idRoot ? 1 : 0;
}


/**
 * Worker for vbsvcAutomounterPopulateTable for adding discovered entries.
 *
 * This is puts dummies in for missing values, depending on
 * vbsvcAutomounterPopulateTable to query them later.
 *
 * @returns VINF_SUCCESS or VERR_NO_MEMORY;
 * @param   pMountTable     The mount table to add an entry to.
 * @param   pszName         The shared folder name.
 * @param   pszMountPoint   The mount point.
 */
static int vbsvcAutomounterAddEntry(PVBSVCAUTOMOUNTERTABLE pMountTable, const char *pszName, const char *pszMountPoint)
{
    VGSvcVerbose(2, "vbsvcAutomounterAddEntry: %s -> %s\n", pszMountPoint, pszName);
    PVBSVCAUTOMOUNTERENTRY pEntry = (PVBSVCAUTOMOUNTERENTRY)RTMemAlloc(sizeof(*pEntry));
    pEntry->idRoot              = UINT32_MAX;
    pEntry->uRootIdVersion      = UINT32_MAX;
    pEntry->fFlags              = UINT64_MAX;
    pEntry->pszName             = RTStrDup(pszName);
    pEntry->pszMountPoint       = NULL;
    pEntry->pszActualMountPoint = RTStrDup(pszMountPoint);
    if (pEntry->pszName && pEntry->pszActualMountPoint)
    {
        if (pMountTable->cEntries + 1 <= pMountTable->cAllocated)
        {
            pMountTable->papEntries[pMountTable->cEntries++] = pEntry;
            return VINF_SUCCESS;
        }

        void *pvNew = RTMemRealloc(pMountTable->papEntries, (pMountTable->cAllocated + 8) * sizeof(pMountTable->papEntries[0]));
        if (pvNew)
        {
            pMountTable->cAllocated += 8;
            pMountTable->papEntries = (PVBSVCAUTOMOUNTERENTRY *)pvNew;

            pMountTable->papEntries[pMountTable->cEntries++] = pEntry;
            return VINF_SUCCESS;
        }
    }
    RTMemFree(pEntry->pszActualMountPoint);
    RTMemFree(pEntry->pszName);
    RTMemFree(pEntry);
    return VERR_NO_MEMORY;
}


/**
 * Populates the mount table as best we can with existing automount entries.
 *
 * @returns VINF_SUCCESS or VERR_NO_MEMORY;
 * @param   pMountTable     The mount table (empty).
 */
static int vbsvcAutomounterPopulateTable(PVBSVCAUTOMOUNTERTABLE pMountTable)
{
    int rc;

#ifdef RT_OS_WINDOWS
    /*
     * Loop thru the drive letters and check out each of them using QueryDosDeviceW.
     */
    static const char s_szDevicePath[] = "\\Device\\VBoxMiniRdr\\;";
    for (char chDrive = 'Z'; chDrive >= 'A'; chDrive--)
    {
        RTUTF16 const wszMountPoint[4] = { (RTUTF16)chDrive, ':', '\0', '\0' };
        RTUTF16       wszTargetPath[RTPATH_MAX];
        DWORD const   cwcResult = QueryDosDeviceW(wszMountPoint, wszTargetPath, RT_ELEMENTS(wszTargetPath));
        if (   cwcResult > sizeof(s_szDevicePath)
            && RTUtf16NICmpAscii(wszTargetPath, RT_STR_TUPLE(s_szDevicePath)) == 0)
        {
            PCRTUTF16 pwsz = &wszTargetPath[RT_ELEMENTS(s_szDevicePath) - 1];
            Assert(pwsz[-1] == ';');
            if (   (pwsz[0] & ~(RTUTF16)0x20) == chDrive
                && pwsz[1] == ':'
                && pwsz[2] == '\\')
            {
                /* For now we'll just use the special capitalization of the
                   "server" name to identify it as our work.  We could check
                   if the symlink is from \Global?? or \??, but that trick does
                   work for older OS versions (<= XP) or when running the
                   service manually for testing/wathever purposes. */
                /** @todo Modify the windows shared folder driver to allow tagging drives.*/
                if (RTUtf16NCmpAscii(&pwsz[3], RT_STR_TUPLE("VBoxSvr\\")) == 0)
                {
                    pwsz += 3 + 8;
                    if (*pwsz != '\\' && *pwsz)
                    {
                        /* The shared folder name should follow immediately after the server prefix. */
                        char *pszMountedName = NULL;
                        rc = RTUtf16ToUtf8(pwsz, &pszMountedName);
                        if (RT_SUCCESS(rc))
                        {
                            char const szMountPoint[4] = { chDrive, ':', '\0', '\0' };
                            rc = vbsvcAutomounterAddEntry(pMountTable, pszMountedName, szMountPoint);
                            RTStrFree(pszMountedName);
                        }
                        if (RT_FAILURE(rc))
                            return rc;
                    }
                    else
                        VGSvcVerbose(2, "vbsvcAutomounterPopulateTable: Malformed, not ours: %ls -> %ls\n",
                                     wszMountPoint, wszTargetPath);
                }
                else
                    VGSvcVerbose(3, "vbsvcAutomounterPopulateTable: Not ours: %ls -> %ls\n", wszMountPoint, wszTargetPath);
            }
        }
    }

#elif defined(RT_OS_OS2)
    /*
     * Just loop thru the drive letters and check the attachment of each.
     */
    for (char chDrive = 'Z'; chDrive >= 'A'; chDrive--)
    {
        char const szMountPoint[4] = { chDrive, ':', '\0', '\0' };
        union
        {
            FSQBUFFER2  FsQueryBuf;
            char        achPadding[1024];
        } uBuf;
        RT_ZERO(uBuf);
        ULONG  cbBuf = sizeof(uBuf) - 2;
        APIRET rcOs2 = DosQueryFSAttach(szMountPoint, 0, FSAIL_QUERYNAME, &uBuf.FsQueryBuf, &cbBuf);
        if (rcOs2 == NO_ERROR)
        {
            const char *pszFsdName = (const char *)&uBuf.FsQueryBuf.szName[uBuf.FsQueryBuf.cbName + 1];
            if (   uBuf.FsQueryBuf.iType == FSAT_REMOTEDRV
                && RTStrICmpAscii(pszFsdName, "VBOXSF") == 0)
            {
                const char *pszMountedName = (const char *)&pszFsdName[uBuf.FsQueryBuf.cbFSDName + 1];
                const char *pszTag = pszMountedName + strlen(pszMountedName) + 1; /* (Safe. Always two trailing zero bytes, see above.) */
                if (strcmp(pszTag, g_szTag) == 0)
                {
                    rc = vbsvcAutomounterAddEntry(pMountTable, pszMountedName, szMountPoint);
                    if (RT_FAILURE(rc))
                        return rc;
                }
            }
        }
    }

#elif defined(RT_OS_LINUX)
    /*
     * Scan the mount table file for the mount point and then match file system
     * and device/share.  We identify our mounts by mount path + prefix for now,
     * but later we may use the same approach as on solaris.
     */
    FILE *pFile = setmntent("/proc/mounts", "r");
    int iErrMounts = errno;
    if (!pFile)
        pFile = setmntent("/etc/mtab", "r");
    if (pFile)
    {
        rc = VWRN_NOT_FOUND;
        struct mntent *pEntry;
        while ((pEntry = getmntent(pFile)) != NULL)
            if (strcmp(pEntry->mnt_type, "vboxsf") == 0)
                if (strstr(pEntry->mnt_opts, g_szTag) != NULL)
                {
                    rc = vbsvcAutomounterAddEntry(pMountTable, pEntry->mnt_fsname, pEntry->mnt_dir);
                    if (RT_FAILURE(rc))
                    {
                        endmntent(pFile);
                        return rc;
                    }
                }
        endmntent(pFile);
    }
    else
        VGSvcError("vbsvcAutomounterQueryMountPoint: Could not open mount tab '%s' (errno=%d) or '/proc/mounts' (errno=%d)\n",
                   _PATH_MOUNTED, errno, iErrMounts);

#elif defined(RT_OS_SOLARIS)
    /*
     * Look thru the system mount table and inspect the vboxsf mounts.
     */
    FILE *pFile = fopen(_PATH_MOUNTED, "r");
    if (pFile)
    {
        rc = VINF_SUCCESS;
        struct mnttab Entry;
        while (getmntent(pFile, &Entry) == 0)
            if (strcmp(Entry.mnt_fstype, "vboxfs") == 0)
            {
                /* Look for the dummy automounter option. */
                if (   Entry.mnt_mntopts != NULL
                    && strstr(Entry.mnt_mntopts, g_szTag) != NULL)
                {
                    rc = vbsvcAutomounterAddEntry(pMountTable, Entry.mnt_special, Entry.mnt_mountp);
                    if (RT_FAILURE(rc))
                    {
                        fclose(pFile);
                        return rc;
                    }
                }
            }
        fclose(pFile);
    }
    else
        VGSvcError("vbsvcAutomounterQueryMountPoint: Could not open mount tab '%s' (errno=%d)\n", _PATH_MOUNTED, errno);

#else
# error "PORTME!"
#endif

    /*
     * Try reconcile the detected folders with data from the host.
     */
    uint32_t                    cMappings = 0;
    PVBGLR3SHAREDFOLDERMAPPING  paMappings = NULL;
    rc = VbglR3SharedFolderGetMappings(g_idClientSharedFolders, true /*fAutoMountOnly*/, &paMappings, &cMappings);
    if (RT_SUCCESS(rc))
    {
        for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
        {
            uint32_t const idRootSrc = paMappings[i].u32Root;

            uint32_t uRootIdVer = UINT32_MAX;
            uint64_t fFlags     = 0;
            char    *pszName    = NULL;
            char    *pszMntPt   = NULL;
            int rc2 = VbglR3SharedFolderQueryFolderInfo(g_idClientSharedFolders, idRootSrc,  VBOXSERVICE_AUTOMOUNT_MIQF,
                                                        &pszName, &pszMntPt, &fFlags, &uRootIdVer);
            if (RT_SUCCESS(rc2))
            {
                uint32_t iPrevHit = UINT32_MAX;
                for (uint32_t iTable = 0; iTable < pMountTable->cEntries; iTable++)
                {
                    PVBSVCAUTOMOUNTERENTRY pEntry = pMountTable->papEntries[iTable];
                    if (RTStrICmp(pEntry->pszName, pszName) == 0)
                    {
                        VGSvcVerbose(2, "vbsvcAutomounterPopulateTable: Identified %s -> %s: idRoot=%u ver=%u fFlags=%#x AutoMntPt=%s\n",
                                     pEntry->pszActualMountPoint, pEntry->pszName, idRootSrc, uRootIdVer, fFlags, pszMntPt);
                        pEntry->fFlags         = fFlags;
                        pEntry->idRoot         = idRootSrc;
                        pEntry->uRootIdVersion = uRootIdVer;
                        RTStrFree(pEntry->pszMountPoint);
                        pEntry->pszMountPoint = RTStrDup(pszMntPt);
                        if (!pEntry->pszMountPoint)
                        {
                            rc = VERR_NO_MEMORY;
                            break;
                        }

                        /* If multiple mappings of the same folder, pick the first or the one
                           with matching mount point. */
                        if (iPrevHit == UINT32_MAX)
                            iPrevHit = iTable;
                        else if (RTPathCompare(pszMntPt, pEntry->pszActualMountPoint) == 0)
                        {
                            if (iPrevHit != UINT32_MAX)
                                pMountTable->papEntries[iPrevHit]->uRootIdVersion -= 1;
                            iPrevHit = iTable;
                        }
                        else
                            pEntry->uRootIdVersion -= 1;
                    }
                }

                RTStrFree(pszName);
                RTStrFree(pszMntPt);
            }
            else
                VGSvcError("vbsvcAutomounterPopulateTable: VbglR3SharedFolderQueryFolderInfo(%u) failed: %Rrc\n", idRootSrc, rc2);
        }

        VbglR3SharedFolderFreeMappings(paMappings);

        /*
         * Sort the table by root ID.
         */
        if (pMountTable->cEntries > 1)
            RTSortApvShell((void **)pMountTable->papEntries, pMountTable->cEntries, vbsvcAutomounterCompareEntry, NULL);

        for (uint32_t iTable = 0; iTable < pMountTable->cEntries; iTable++)
        {
            PVBSVCAUTOMOUNTERENTRY pEntry = pMountTable->papEntries[iTable];
            if (pMountTable->papEntries[iTable]->idRoot != UINT32_MAX)
                VGSvcVerbose(1, "vbsvcAutomounterPopulateTable: #%u: %s -> %s idRoot=%u ver=%u fFlags=%#x AutoMntPt=%s\n",
                             iTable, pEntry->pszActualMountPoint, pEntry->pszName, pEntry->idRoot, pEntry->uRootIdVersion,
                             pEntry->fFlags, pEntry->pszMountPoint);
            else
                VGSvcVerbose(1, "vbsvcAutomounterPopulateTable: #%u: %s -> %s - not identified!\n",
                             iTable, pEntry->pszActualMountPoint, pEntry->pszName);
        }
    }
    else
        VGSvcError("vbsvcAutomounterPopulateTable: VbglR3SharedFolderGetMappings failed: %Rrc\n", rc);
    return rc;
}


/**
 * Checks whether the shared folder @a pszName is mounted on @a pszMountPoint.
 *
 * @returns Exactly one of the following IPRT status codes;
 * @retval  VINF_SUCCESS if mounted
 * @retval  VWRN_NOT_FOUND if nothing is mounted at @a pszMountPoint.
 * @retval  VERR_RESOURCE_BUSY if a different shared folder is mounted there.
 * @retval  VERR_ACCESS_DENIED if a non-shared folder file system is mounted
 *          there.
 *
 * @param   pszMountPoint   The mount point to check.
 * @param   pszName         The name of the shared folder (mapping).
 */
static int vbsvcAutomounterQueryMountPoint(const char *pszMountPoint, const char *pszName)
{
    VGSvcVerbose(4, "vbsvcAutomounterQueryMountPoint: pszMountPoint=%s pszName=%s\n", pszMountPoint, pszName);

#ifdef RT_OS_WINDOWS
    /*
     * We could've used RTFsQueryType here but would then have to
     * calling RTFsQueryLabel for the share name hint, ending up
     * doing the same work twice.  We could also use QueryDosDeviceW,
     * but output is less clear...
     */
    PRTUTF16 pwszMountPoint = NULL;
    int rc = RTStrToUtf16(pszMountPoint, &pwszMountPoint);
    if (RT_SUCCESS(rc))
    {
        DWORD   uSerial = 0;
        DWORD   cchCompMax = 0;
        DWORD   fFlags = 0;
        RTUTF16 wszLabel[512];
        RTUTF16 wszFileSystem[256];
        RT_ZERO(wszLabel);
        RT_ZERO(wszFileSystem);
        if (GetVolumeInformationW(pwszMountPoint, wszLabel, RT_ELEMENTS(wszLabel) - 1, &uSerial, &cchCompMax, &fFlags,
                                  wszFileSystem, RT_ELEMENTS(wszFileSystem) - 1))
        {
            if (RTUtf16ICmpAscii(wszFileSystem, "VBoxSharedFolderFS") == 0)
            {
                char *pszLabel = NULL;
                rc = RTUtf16ToUtf8(wszLabel, &pszLabel);
                if (RT_SUCCESS(rc))
                {
                    const char *pszMountedName = pszLabel;
                    if (RTStrStartsWith(pszMountedName, "VBOX_"))
                        pszMountedName += sizeof("VBOX_") - 1;
                    if (RTStrICmp(pszMountedName, pszName) == 0)
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s'.\n",
                                     pszName, pszMountPoint);
                        rc = VINF_SUCCESS;
                    }
                    else
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s', not '%s'...\n",
                                     pszMountedName, pszMountPoint, pszName);
                        rc = VERR_RESOURCE_BUSY;
                    }
                    RTStrFree(pszLabel);
                }
                else
                {
                    VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: RTUtf16ToUtf8(%ls,) failed: %Rrc\n", wszLabel, rc);
                    rc = VERR_RESOURCE_BUSY;
                }
            }
            else
            {
                VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found a '%ls' with label '%ls' mount at '%s', not '%s'...\n",
                             wszFileSystem, wszLabel, pszMountPoint, pszName);
                rc = VERR_ACCESS_DENIED;
            }
        }
        else
        {
            rc = GetLastError();
            if (rc != ERROR_PATH_NOT_FOUND || g_cVerbosity >= 4)
                VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: GetVolumeInformationW('%ls',,,,) failed: %u\n", pwszMountPoint, rc);
            if (rc == ERROR_PATH_NOT_FOUND)
                rc = VWRN_NOT_FOUND;
            else if (   RT_C_IS_ALPHA(pszMountPoint[0])
                     && pszMountPoint[1] == ':'
                     && (   pszMountPoint[2] == '\0'
                         || (RTPATH_IS_SLASH(pszMountPoint[2]) && pszMountPoint[3] == '\0')))
            {
                /* See whether QueryDosDeviceW thinks its a malfunctioning shared folder or
                   something else (it doesn't access the file system).  We've seen
                   VERR_NET_HOST_NOT_FOUND here for shared folders that was removed on the
                   host side.

                   Note! This duplicates code from vbsvcAutomounterPopulateTable. */
                rc = VERR_ACCESS_DENIED;
                static const char s_szDevicePath[] = "\\Device\\VBoxMiniRdr\\;";
                wszFileSystem[0] = pwszMountPoint[0];
                wszFileSystem[1] = pwszMountPoint[1];
                wszFileSystem[2] = '\0';
                DWORD const cwcResult = QueryDosDeviceW(wszFileSystem, wszLabel, RT_ELEMENTS(wszLabel));
                if (   cwcResult > sizeof(s_szDevicePath)
                    && RTUtf16NICmpAscii(wszLabel, RT_STR_TUPLE(s_szDevicePath)) == 0)
                {
                    PCRTUTF16 pwsz = &wszLabel[RT_ELEMENTS(s_szDevicePath) - 1];
                    Assert(pwsz[-1] == ';');
                    if (   (pwsz[0] & ~(RTUTF16)0x20) == (wszFileSystem[0] & ~(RTUTF16)0x20)
                        && pwsz[1] == ':'
                        && pwsz[2] == '\\')
                    {
                        if (RTUtf16NICmpAscii(&pwsz[3], RT_STR_TUPLE("VBoxSvr\\")) == 0)
                        {
                            pwsz += 3 + 8;
                            char *pszMountedName = NULL;
                            rc = RTUtf16ToUtf8(pwsz, &pszMountedName);
                            if (RT_SUCCESS(rc))
                            {
                                if (RTStrICmp(pszMountedName, pszName) == 0)
                                {
                                    rc = VINF_SUCCESS;
                                    VGSvcVerbose(2, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s' (using QueryDosDeviceW).\n",
                                                 pszName, pszMountPoint);
                                }
                                else
                                {
                                    VGSvcVerbose(2, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s' (using QueryDosDeviceW), not '%s'...\n",
                                                 pszMountedName, pszMountPoint, pszName);
                                    rc = VERR_RESOURCE_BUSY;
                                }
                                RTStrFree(pszMountedName);
                            }
                            else
                            {
                                VGSvcVerbose(2, "vbsvcAutomounterQueryMountPoint: RTUtf16ToUtf8 failed: %Rrc\n", rc);
                                AssertRC(rc);
                                rc = VERR_RESOURCE_BUSY;
                            }
                        }
                    }
                }
            }
            else
                rc = VERR_ACCESS_DENIED;
        }
        RTUtf16Free(pwszMountPoint);
    }
    else
    {
        VGSvcError("vbsvcAutomounterQueryMountPoint: RTStrToUtf16(%s,) -> %Rrc\n", pszMountPoint, rc);
        rc = VWRN_NOT_FOUND;
    }
    return rc;

#elif defined(RT_OS_OS2)
    /*
     * Query file system attachment info for the given drive letter.
     */
    union
    {
        FSQBUFFER2  FsQueryBuf;
        char        achPadding[512];
    } uBuf;
    RT_ZERO(uBuf);

    ULONG cbBuf = sizeof(uBuf);
    APIRET rcOs2 = DosQueryFSAttach(pszMountPoint, 0, FSAIL_QUERYNAME, &uBuf.FsQueryBuf, &cbBuf);
    int rc;
    if (rcOs2 == NO_ERROR)
    {
        const char *pszFsdName = (const char *)&uBuf.FsQueryBuf.szName[uBuf.FsQueryBuf.cbName + 1];
        if (   uBuf.FsQueryBuf.iType == FSAT_REMOTEDRV
            && RTStrICmpAscii(pszFsdName, "VBOXSF") == 0)
        {
            const char *pszMountedName = (const char *)&pszFsdName[uBuf.FsQueryBuf.cbFSDName + 1];
            if (RTStrICmp(pszMountedName, pszName) == 0)
            {
                VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s'.\n",
                             pszName, pszMountPoint);
                rc = VINF_SUCCESS;
            }
            else
            {
                VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s', not '%s'...\n",
                             pszMountedName, pszMountPoint, pszName);
                rc = VERR_RESOURCE_BUSY;
            }
        }
        else
        {
            VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found a '%s' type %u mount at '%s', not '%s'...\n",
                         pszFsdName, uBuf.FsQueryBuf.iType, pszMountPoint, pszName);
            rc = VERR_ACCESS_DENIED;
        }
    }
    else
    {
        rc = VWRN_NOT_FOUND;
        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: DosQueryFSAttach(%s) -> %u\n", pszMountPoint, rcOs2);
        AssertMsgStmt(rcOs2 != ERROR_BUFFER_OVERFLOW && rcOs2 != ERROR_INVALID_PARAMETER,
                      ("%s -> %u\n", pszMountPoint, rcOs2), rc = VERR_ACCESS_DENIED);
    }
    return rc;

#elif defined(RT_OS_LINUX)
    /*
     * Scan one of the mount table file for the mount point and then
     * match file system and device/share.
     */
    FILE *pFile = setmntent("/proc/mounts", "r");
    int rc = errno;
    if (!pFile)
        pFile = setmntent(_PATH_MOUNTED, "r");
    if (pFile)
    {
        rc = VWRN_NOT_FOUND;
        struct mntent *pEntry;
        while ((pEntry = getmntent(pFile)) != NULL)
            if (RTPathCompare(pEntry->mnt_dir, pszMountPoint) == 0)
            {
                if (strcmp(pEntry->mnt_type, "vboxsf") == 0)
                {
                    if (RTStrICmp(pEntry->mnt_fsname, pszName) == 0)
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s'.\n",
                                     pszName, pszMountPoint);
                        rc = VINF_SUCCESS;
                    }
                    else
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s', not '%s'...\n",
                                     pEntry->mnt_fsname, pszMountPoint, pszName);
                        rc = VERR_RESOURCE_BUSY;
                    }
                }
                else
                {
                    VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found a '%s' mount of '%s' at '%s', not '%s'...\n",
                                 pEntry->mnt_type, pEntry->mnt_fsname, pszMountPoint, pszName);
                    rc = VERR_ACCESS_DENIED;
                }
                /* We continue searching in case of stacked mounts, we want the last one. */
            }
        endmntent(pFile);
    }
    else
    {
        VGSvcError("vbsvcAutomounterQueryMountPoint: Could not open mount tab '/proc/mounts' (errno=%d) or '%s' (errno=%d)\n",
                   rc, _PATH_MOUNTED, errno);
        rc = VERR_ACCESS_DENIED;
    }
    return rc;

#elif defined(RT_OS_SOLARIS)
    /*
     * Similar to linux.
     */
    int rc;
    FILE *pFile = fopen(_PATH_MOUNTED, "r");
    if (pFile)
    {
        rc = VWRN_NOT_FOUND;
        struct mnttab Entry;
        while (getmntent(pFile, &Entry) == 0)
            if (RTPathCompare(Entry.mnt_mountp, pszMountPoint) == 0)
            {
                if (strcmp(Entry.mnt_fstype, "vboxfs") == 0)
                {
                    if (RTStrICmp(Entry.mnt_special, pszName) == 0)
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s'.\n",
                                     pszName, pszMountPoint);
                        rc = VINF_SUCCESS;
                    }
                    else
                    {
                        VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found shared folder '%s' at '%s', not '%s'...\n",
                                     Entry.mnt_special, pszMountPoint, pszName);
                        rc = VERR_RESOURCE_BUSY;
                    }
                }
                else
                {
                    VGSvcVerbose(3, "vbsvcAutomounterQueryMountPoint: Found a '%s' mount of '%s' at '%s', not '%s'...\n",
                                 Entry.mnt_fstype, Entry.mnt_special, pszMountPoint, pszName);
                    rc = VERR_ACCESS_DENIED;
                }
                /* We continue searching in case of stacked mounts, we want the last one. */
            }
        fclose(pFile);
    }
    else
    {
        VGSvcError("vbsvcAutomounterQueryMountPoint: Could not open mount tab '%s' (errno=%d)\n", _PATH_MOUNTED, errno);
        rc = VERR_ACCESS_DENIED;
    }
    return rc;
#else
# error "PORTME"
#endif
}


/**
 * Worker for vbsvcAutomounterMountNewEntry that does the OS mounting.
 *
 * @returns IPRT status code.
 * @param   pEntry      The entry to try mount.
 */
static int vbsvcAutomounterMountIt(PVBSVCAUTOMOUNTERENTRY pEntry)
{
    VGSvcVerbose(3, "vbsvcAutomounterMountIt: Trying to mount '%s' (idRoot=%#x) on '%s'...\n",
                 pEntry->pszName, pEntry->idRoot, pEntry->pszActualMountPoint);
#ifdef RT_OS_WINDOWS
    /*
     * Attach the shared folder using WNetAddConnection2W.
     *
     * According to google we should get a drive symlink in \\GLOBAL?? when
     * we are running under the system account.  Otherwise it will be a session
     * local link (\\??).
     */
    Assert(RT_C_IS_UPPER(pEntry->pszActualMountPoint[0]) && pEntry->pszActualMountPoint[1] == ':' && pEntry->pszActualMountPoint[2] == '\0');
    RTUTF16 wszDrive[4] = { (RTUTF16)pEntry->pszActualMountPoint[0], ':', '\0', '\0' };

    RTUTF16 wszPrefixedName[RTPATH_MAX];
    int rc = RTUtf16CopyAscii(wszPrefixedName, RT_ELEMENTS(wszPrefixedName), "\\\\VBoxSvr\\");
    AssertRC(rc);

    size_t const offName = RTUtf16Len(wszPrefixedName);
    PRTUTF16 pwszName = &wszPrefixedName[offName];
    rc = RTStrToUtf16Ex(pEntry->pszName, RTSTR_MAX, &pwszName, sizeof(wszPrefixedName) - offName, NULL);
    if (RT_FAILURE(rc))
    {
        VGSvcError("vbsvcAutomounterMountIt: RTStrToUtf16Ex failed on '%s': %Rrc\n", pEntry->pszName, rc);
        return rc;
    }

    VGSvcVerbose(3, "vbsvcAutomounterMountIt: wszDrive='%ls', wszPrefixedName='%ls'\n",
                 wszDrive, wszPrefixedName);

    NETRESOURCEW NetRsrc;
    RT_ZERO(NetRsrc);
    NetRsrc.dwType          = RESOURCETYPE_DISK;
    NetRsrc.lpLocalName     = wszDrive;
    NetRsrc.lpRemoteName    = wszPrefixedName;
    NetRsrc.lpProvider      = L"VirtualBox Shared Folders"; /* Only try our provider. */
    NetRsrc.lpComment       = pwszName;

    DWORD dwErr = WNetAddConnection2W(&NetRsrc, NULL /*pwszPassword*/, NULL /*pwszUserName*/, 0 /*dwFlags*/);
    if (dwErr == NO_ERROR)
    {
        VGSvcVerbose(0, "vbsvcAutomounterMountIt: Successfully mounted '%s' on '%s'\n",
                     pEntry->pszName, pEntry->pszActualMountPoint);
        return VINF_SUCCESS;
    }
    VGSvcError("vbsvcAutomounterMountIt: Failed to attach '%s' to '%s': %Rrc (%u)\n",
               pEntry->pszName, pEntry->pszActualMountPoint, RTErrConvertFromWin32(dwErr), dwErr);
    return VERR_OPEN_FAILED;

#elif defined(RT_OS_OS2)
    /*
     * It's a rather simple affair on OS/2.
     *
     * In order to be able to detect our mounts we add a 2nd string after
     * the folder name that tags the attachment.  The IFS will remember this
     * and return it when DosQueryFSAttach is called.
     *
     * Note! Kernel currently accepts limited 7-bit ASCII names.  We could
     *       change that to UTF-8 if we like as that means no extra string
     *       encoding conversion fun here.
     */
    char    szzNameAndTag[256];
    size_t  cchName = strlen(pEntry->pszName);
    if (cchName + 1 + sizeof(g_szTag) <= sizeof(szzNameAndTag))
    {
        memcpy(szzNameAndTag, pEntry->pszName, cchName);
        szzNameAndTag[cchName] = '\0';
        memcpy(&szzNameAndTag[cchName + 1], g_szTag, sizeof(g_szTag));

        APIRET rc = DosFSAttach(pEntry->pszActualMountPoint, "VBOXSF", szzNameAndTag, cchName + 1 + sizeof(g_szTag), FS_ATTACH);
        if (rc == NO_ERROR)
        {
            VGSvcVerbose(0, "vbsvcAutomounterMountIt: Successfully mounted '%s' on '%s'\n",
                         pEntry->pszName, pEntry->pszActualMountPoint);
            return VINF_SUCCESS;
        }
        VGSvcError("vbsvcAutomounterMountIt: DosFSAttach failed to attach '%s' to '%s': %u\n",
                   pEntry->pszName, pEntry->pszActualMountPoint, rc);
    }
    else
        VGSvcError("vbsvcAutomounterMountIt: Share name for attach to '%s' is too long: %u chars - '%s'\n",
                   pEntry->pszActualMountPoint, cchName, pEntry->pszName);
    return VERR_OPEN_FAILED;

#else
    /*
     * Common work for unix-like systems: Get group, make sure mount directory exist.
     */
    int rc = RTDirCreateFullPath(pEntry->pszActualMountPoint,
                                 RTFS_UNIX_IRWXU | RTFS_UNIX_IXGRP | RTFS_UNIX_IRGRP | RTFS_UNIX_IXOTH | RTFS_UNIX_IROTH);
    if (RT_FAILURE(rc))
    {
        VGSvcError("vbsvcAutomounterMountIt: Failed to create mount path '%s' for share '%s': %Rrc\n",
                   pEntry->pszActualMountPoint, pEntry->pszName, rc);
        return rc;
    }

    gid_t gidMount;
    struct group *grp_vboxsf = getgrnam("vboxsf");
    if (grp_vboxsf)
        gidMount = grp_vboxsf->gr_gid;
    else
    {
        VGSvcError("vbsvcAutomounterMountIt: Group 'vboxsf' does not exist\n");
        gidMount = 0;
    }

#  if defined(RT_OS_LINUX)
    /*
     * Linux a bit more work...
     */
    struct utsname uts;
    AssertStmt(uname(&uts) != -1, strcpy(uts.release, "4.4.0"));

    /* Built mount option string.  Need st_name for pre 2.6.0 kernels. */
    unsigned long const fFlags = MS_NODEV;
    char szOpts[MAX_MNTOPT_STR] = { '\0' };
    ssize_t cchOpts = RTStrPrintf2(szOpts, sizeof(szOpts),
                                   "uid=0,gid=%d,dmode=0770,fmode=0770,dmask=0000,fmask=0000,tag=%s", gidMount, g_szTag);
    if (RTStrVersionCompare(uts.release, "2.6.0") < 0 && cchOpts > 0)
        cchOpts += RTStrPrintf2(&szOpts[cchOpts], sizeof(szOpts) - cchOpts, ",sf_name=%s", pEntry->pszName);
    if (cchOpts <= 0)
    {
        VGSvcError("vbsvcAutomounterMountIt: szOpts overflow! %zd\n", cchOpts);
        return VERR_BUFFER_OVERFLOW;
    }

    /* Do the mounting. The fallback w/o tag is for the Linux vboxsf fork
       which lagged a lot behind when it first appeared in 5.6. */
    errno = 0;
    rc = mount(pEntry->pszName, pEntry->pszActualMountPoint, "vboxsf", fFlags, szOpts);
    if (rc != 0 && errno == EINVAL && RTStrVersionCompare(uts.release, "5.6.0") >= 0)
    {
        VGSvcVerbose(2, "vbsvcAutomounterMountIt: mount returned EINVAL, retrying without the tag.\n");
        *strstr(szOpts, ",tag=") = '\0';
        errno = 0;
        rc = mount(pEntry->pszName, pEntry->pszActualMountPoint, "vboxsf", fFlags, szOpts);
        if (rc == 0)
            VGSvcVerbose(0, "vbsvcAutomounterMountIt: Running outdated vboxsf module without support for the 'tag' option?\n");
    }
    if (rc == 0)
    {
        VGSvcVerbose(0, "vbsvcAutomounterMountIt: Successfully mounted '%s' on '%s'\n",
                     pEntry->pszName, pEntry->pszActualMountPoint);

        errno = 0;
        rc = vbsfmount_complete(pEntry->pszName, pEntry->pszActualMountPoint, fFlags, szOpts);
        if (rc != 0) /* Ignorable. /etc/mtab is probably a link to /proc/mounts. */
            VGSvcVerbose(1, "vbsvcAutomounterMountIt: vbsfmount_complete failed: %s (%d/%d)\n",
                         rc == 1 ? "malloc" : rc == 2 ? "setmntent" : rc == 3 ? "addmntent" : "unknown", rc, errno);
        return VINF_SUCCESS;
    }

    if (errno == EINVAL)
        VGSvcError("vbsvcAutomounterMountIt: Failed to mount '%s' on '%s' because it is probably mounted elsewhere arleady! (%d,%d)\n",
                   pEntry->pszName, pEntry->pszActualMountPoint, rc, errno);
    else
        VGSvcError("vbsvcAutomounterMountIt: Failed to mount '%s' on '%s': %s (%d,%d)\n",
                   pEntry->pszName, pEntry->pszActualMountPoint, strerror(errno), rc, errno);
    return VERR_WRITE_ERROR;

#  elif defined(RT_OS_SOLARIS)
    /*
     * Solaris is rather simple compared to linux.
     *
     * The ',VBoxService=auto' option (g_szTag) is ignored by the kernel but helps
     * us identify our own mounts on restart.  See vbsvcAutomounterPopulateTable().
     *
     * Note! Must pass MAX_MNTOPT_STR rather than cchOpts to mount, as it may fail
     *       with EOVERFLOW in vfs_buildoptionstr() during domount() otherwise.
     */
    char szOpts[MAX_MNTOPT_STR] = { '\0', };
    ssize_t cchOpts = RTStrPrintf2(szOpts, sizeof(szOpts),
                                   "uid=0,gid=%d,dmode=0770,fmode=0770,dmask=0000,fmask=0000,tag=%s", gidMount, g_szTag);
    if (cchOpts <= 0)
    {
        VGSvcError("vbsvcAutomounterMountIt: szOpts overflow! %zd\n", cchOpts);
        return VERR_BUFFER_OVERFLOW;
    }

    rc = mount(pEntry->pszName, pEntry->pszActualMountPoint, MS_OPTIONSTR, "vboxfs",
               NULL /*dataptr*/, 0 /* datalen */, szOpts, MAX_MNTOPT_STR);
    if (rc == 0)
    {
        VGSvcVerbose(0, "vbsvcAutomounterMountIt: Successfully mounted '%s' on '%s'\n",
                     pEntry->pszName, pEntry->pszActualMountPoint);
        return VINF_SUCCESS;
    }

    rc = errno;
    VGSvcError("vbsvcAutomounterMountIt: mount failed for '%s' on '%s' (szOpts=%s): %s (%d)\n",
               pEntry->pszName, pEntry->pszActualMountPoint, szOpts, strerror(rc), rc);
    return VERR_OPEN_FAILED;

# else
#  error "PORTME!"
# endif
#endif
}


/**
 * Attempts to mount the given shared folder, adding it to the mount table on
 * success.
 *
 * @returns iTable + 1 on success, iTable on failure.
 * @param   pTable          The mount table.
 * @param   iTable          The mount table index at which to add the mount.
 * @param   pszName         The name of the shared folder mapping.
 * @param   pszMntPt        The mount point (hint) specified by the host.
 * @param   fFlags          The shared folder flags, SHFL_MIF_XXX.
 * @param   idRoot          The root ID.
 * @param   uRootIdVersion  The root ID version.
 * @param   fAutoMntPt      Whether to try automatically assign a mount point if
 *                          pszMntPt doesn't work out.  This is set in pass \#3.
 */
static uint32_t vbsvcAutomounterMountNewEntry(PVBSVCAUTOMOUNTERTABLE pTable, uint32_t iTable,
                                              const char *pszName, const char *pszMntPt, uint64_t fFlags,
                                              uint32_t idRoot, uint32_t uRootIdVersion, bool fAutoMntPt)
{
    VGSvcVerbose(3, "vbsvcAutomounterMountNewEntry: #%u: '%s' at '%s'%s\n",
                 iTable, pszName, pszMntPt, fAutoMntPt ? " auto-assign" : "");

    /*
     * First we need to figure out the actual mount point.
     */
    char szActualMountPoint[RTPATH_MAX];

#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
    /*
     * Drive letter based.  We only care about the first two characters
     * and ignore the rest (see further down).
     */
    char chNextLetter = 'Z';
    if (RT_C_IS_ALPHA(pszMntPt[0]) && pszMntPt[1] == ':')
        szActualMountPoint[0] = RT_C_TO_UPPER(pszMntPt[0]);
    else if (!fAutoMntPt)
        return iTable;
    else
        szActualMountPoint[0] = chNextLetter--;
    szActualMountPoint[1] = ':';
    szActualMountPoint[2] = '\0';

    int rc;
    for (;;)
    {
        rc = vbsvcAutomounterQueryMountPoint(szActualMountPoint, pszName);
        if (rc == VWRN_NOT_FOUND)
            break;

        /* next */
        if (chNextLetter == 'A' || !fAutoMntPt)
            return iTable;
        szActualMountPoint[0] = chNextLetter--;
    }

#else
    /*
     * Path based #1: Host specified mount point.
     */

    /* Skip DOS drive letter if there is a UNIX mount point path following it: */
    if (   pszMntPt[0] != '/'
        && pszMntPt[0] != '\0'
        && pszMntPt[1] == ':'
        && pszMntPt[2] == '/')
        pszMntPt += 2;

    /* Try specified mount point if it starts with a UNIX slash: */
    int rc = VERR_ACCESS_DENIED;
    if (*pszMntPt == '/')
    {
        rc = RTPathAbs(pszMntPt, szActualMountPoint, sizeof(szActualMountPoint));
        if (RT_SUCCESS(rc))
        {
            static const char * const s_apszBlacklist[] =
            { "/", "/dev", "/bin", "/sbin", "/lib", "/etc", "/var", "/tmp", "/usr", "/usr/bin", "/usr/sbin", "/usr/lib" };
            for (size_t i = 0; i < RT_ELEMENTS(s_apszBlacklist); i++)
                if (strcmp(szActualMountPoint, s_apszBlacklist[i]) == 0)
                {
                    rc = VERR_ACCESS_DENIED;
                    break;
                }
            if (RT_SUCCESS(rc))
                rc = vbsvcAutomounterQueryMountPoint(szActualMountPoint, pszName);
        }
    }
    if (rc != VWRN_NOT_FOUND)
    {
        if (!fAutoMntPt)
            return iTable;

        /*
         * Path based #2: Mount dir + prefix + share.
         */
        rc = vbsvcAutomounterQueryMountDirAndPrefix(szActualMountPoint, sizeof(szActualMountPoint));
        if (RT_SUCCESS(rc))
        {
            /* Append a sanitized share name: */
            size_t const offShare = strlen(szActualMountPoint);
            size_t offDst = offShare;
            size_t offSrc = 0;
            for (;;)
            {
                char ch = pszName[offSrc++];
                if (ch == ' ' || ch == '/' || ch == '\\' || ch == ':' || ch == '$')
                    ch = '_';
                else if (!ch)
                    break;
                else if (ch < 0x20 || ch == 0x7f)
                    continue;
                if (offDst < sizeof(szActualMountPoint) - 1)
                    szActualMountPoint[offDst++] = ch;
            }
            szActualMountPoint[offDst] = '\0';
            if (offDst > offShare)
            {
                rc = vbsvcAutomounterQueryMountPoint(szActualMountPoint, pszName);
                if (rc != VWRN_NOT_FOUND)
                {
                    /*
                     * Path based #3: Mount dir + prefix + share + _ + number.
                     */
                    if (offDst + 2 >= sizeof(szActualMountPoint))
                        return iTable;

                    szActualMountPoint[offDst++] = '_';
                    for (uint32_t iTry = 1; iTry < 10 && rc != VWRN_NOT_FOUND; iTry++)
                    {
                        szActualMountPoint[offDst] = '0' + iTry;
                        szActualMountPoint[offDst + 1] = '\0';
                        rc = vbsvcAutomounterQueryMountPoint(szActualMountPoint, pszName);
                    }
                    if (rc != VWRN_NOT_FOUND)
                       return iTable;
                }
            }
            else
                VGSvcError("vbsvcAutomounterMountNewEntry: Bad share name: %.*Rhxs", strlen(pszName), pszName);
        }
        else
            VGSvcError("vbsvcAutomounterMountNewEntry: Failed to construct basic auto mount point for '%s'", pszName);
    }
#endif

    /*
     * Prepare a table entry and ensure space in the table..
     */
    if (pTable->cEntries + 1 > pTable->cAllocated)
    {
        void *pvEntries = RTMemRealloc(pTable->papEntries, sizeof(pTable->papEntries[0]) * (pTable->cAllocated + 8));
        if (!pvEntries)
        {
            VGSvcError("vbsvcAutomounterMountNewEntry: Out of memory for growing table (size %u)\n", pTable->cAllocated);
            return iTable;
        }
        pTable->cAllocated += 8;
        pTable->papEntries = (PVBSVCAUTOMOUNTERENTRY *)pvEntries;
    }

    PVBSVCAUTOMOUNTERENTRY pEntry = (PVBSVCAUTOMOUNTERENTRY)RTMemAlloc(sizeof(*pEntry));
    if (pEntry)
    {
        pEntry->idRoot              = idRoot;
        pEntry->uRootIdVersion      = uRootIdVersion;
        pEntry->fFlags              = fFlags;
        pEntry->pszName             = RTStrDup(pszName);
        pEntry->pszMountPoint       = RTStrDup(pszMntPt);
        pEntry->pszActualMountPoint = RTStrDup(szActualMountPoint);
        if (pEntry->pszName && pEntry->pszMountPoint && pEntry->pszActualMountPoint)
        {
            /*
             * Now try mount it.
             */
            rc = vbsvcAutomounterMountIt(pEntry);
            if (RT_SUCCESS(rc))
            {
                uint32_t cToMove = pTable->cEntries - iTable;
                if (cToMove > 0)
                    memmove(&pTable->papEntries[iTable + 1], &pTable->papEntries[iTable], cToMove * sizeof(pTable->papEntries[0]));
                pTable->papEntries[iTable] = pEntry;
                pTable->cEntries++;
                return iTable + 1;
            }
        }
        else
            VGSvcError("vbsvcAutomounterMountNewEntry: Out of memory for table entry!\n");
        RTMemFree(pEntry->pszActualMountPoint);
        RTMemFree(pEntry->pszMountPoint);
        RTMemFree(pEntry->pszName);
        RTMemFree(pEntry);
    }
    else
        VGSvcError("vbsvcAutomounterMountNewEntry: Out of memory for table entry!\n");
    return iTable;
}



/**
 * Does the actual unmounting.
 *
 * @returns Exactly one of the following IPRT status codes;
 * @retval  VINF_SUCCESS if successfully umounted or nothing was mounted there.
 * @retval  VERR_TRY_AGAIN if the shared folder is busy.
 * @retval  VERR_RESOURCE_BUSY if a different shared folder is mounted there.
 * @retval  VERR_ACCESS_DENIED if a non-shared folder file system is mounted
 *          there.
 *
 * @param   pszMountPoint       The mount point.
 * @param   pszName             The shared folder (mapping) name.
 */
static int vbsvcAutomounterUnmount(const char *pszMountPoint, const char *pszName)
{
    /*
     * Retry for 5 seconds in a hope that busy mounts will quiet down.
     */
    for (unsigned iTry = 0; ; iTry++)
    {
        /*
         * Check what's mounted there before we start umounting stuff.
         */
        int rc = vbsvcAutomounterQueryMountPoint(pszMountPoint, pszName);
        if (rc == VINF_SUCCESS)
        { /* pszName is mounted there */ }
        else if (rc == VWRN_NOT_FOUND) /* nothing mounted there */
            return VINF_SUCCESS;
        else
        {
            Assert(rc == VERR_RESOURCE_BUSY || rc == VERR_ACCESS_DENIED);
            return VERR_RESOURCE_BUSY;
        }

        /*
         * Do host specific unmounting.
         */
#ifdef RT_OS_WINDOWS
        Assert(RT_C_IS_UPPER(pszMountPoint[0]) && pszMountPoint[1] == ':' && pszMountPoint[2] == '\0');
        RTUTF16 const wszDrive[4] = { (RTUTF16)pszMountPoint[0], ':', '\0', '\0' };
        DWORD dwErr = WNetCancelConnection2W(wszDrive, 0 /*dwFlags*/, FALSE /*fForce*/);
        if (dwErr == NO_ERROR)
            return VINF_SUCCESS;
        VGSvcVerbose(2, "vbsvcAutomounterUnmount: WNetCancelConnection2W returns %u for '%s' ('%s')\n", dwErr, pszMountPoint, pszName);
        if (dwErr == ERROR_NOT_CONNECTED)
            return VINF_SUCCESS;

#elif defined(RT_OS_OS2)
        APIRET rcOs2 = DosFSAttach(pszMountPoint, "VBOXSF", NULL, 0, FS_DETACH);
        if (rcOs2 == NO_ERROR)
            return VINF_SUCCESS;
        VGSvcVerbose(2, "vbsvcAutomounterUnmount: DosFSAttach failed on '%s' ('%s'): %u\n", pszMountPoint, pszName, rcOs2);
        if (rcOs2 == ERROR_INVALID_FSD_NAME)
            return VERR_ACCESS_DENIED;
        if (   rcOs2 == ERROR_INVALID_DRIVE
            || rcOs2 == ERROR_INVALID_PATH)
            return VERR_TRY_AGAIN;

#else
        int rc2 = umount(pszMountPoint);
        if (rc2 == 0)
        {
            /* Remove the mount directory if not directly under the root dir. */
            RTPATHPARSED Parsed;
            RT_ZERO(Parsed);
            RTPathParse(pszMountPoint, &Parsed, sizeof(Parsed), RTPATH_STR_F_STYLE_HOST);
            if (Parsed.cComps >= 3)
                RTDirRemove(pszMountPoint);

            return VINF_SUCCESS;
        }
        rc2 = errno;
        VGSvcVerbose(2, "vbsvcAutomounterUnmount: umount failed on '%s' ('%s'): %d\n", pszMountPoint, pszName, rc2);
        if (rc2 != EBUSY && rc2 != EAGAIN)
            return VERR_ACCESS_DENIED;
#endif

        /*
         * Check what's mounted there before we start delaying.
         */
        RTThreadSleep(8); /* fudge */
        rc = vbsvcAutomounterQueryMountPoint(pszMountPoint, pszName);
        if (rc == VINF_SUCCESS)
        { /* pszName is mounted there */ }
        else if (rc == VWRN_NOT_FOUND) /* nothing mounted there */
            return VINF_SUCCESS;
        else
        {
            Assert(rc == VERR_RESOURCE_BUSY || rc == VERR_ACCESS_DENIED);
            return VERR_RESOURCE_BUSY;
        }

        if (iTry >= 5)
            return VERR_TRY_AGAIN;
        RTThreadSleep(1000);
    }
}


/**
 * Unmounts a mount table entry and evicts it from the table if successful.
 *
 * @returns The next iTable (same value on success, +1 on failure).
 * @param   pTable              The mount table.
 * @param   iTable              The table entry.
 * @param   pszReason           Why we're here.
 */
static uint32_t vbsvcAutomounterUnmountEntry(PVBSVCAUTOMOUNTERTABLE pTable, uint32_t iTable, const char *pszReason)
{
    Assert(iTable < pTable->cEntries);
    PVBSVCAUTOMOUNTERENTRY pEntry = pTable->papEntries[iTable];
    VGSvcVerbose(2, "vbsvcAutomounterUnmountEntry: #%u: '%s' at '%s' (reason: %s)\n",
                 iTable, pEntry->pszName, pEntry->pszActualMountPoint, pszReason);

    /*
     * Do we need to umount the entry?  Return if unmount fails and we .
     */
    if (pEntry->pszActualMountPoint)
    {
        int rc = vbsvcAutomounterUnmount(pEntry->pszActualMountPoint, pEntry->pszName);
        if (rc == VERR_TRY_AGAIN)
        {
            VGSvcVerbose(1, "vbsvcAutomounterUnmountEntry: Keeping '%s' -> '%s' (VERR_TRY_AGAIN)\n",
                         pEntry->pszActualMountPoint, pEntry->pszName);
            return iTable + 1;
        }
    }

    /*
     * Remove the entry by shifting up the ones after it.
     */
    pTable->cEntries -= 1;
    uint32_t cAfter = pTable->cEntries - iTable;
    if (cAfter)
        memmove(&pTable->papEntries[iTable], &pTable->papEntries[iTable + 1], cAfter * sizeof(pTable->papEntries[0]));
    pTable->papEntries[pTable->cEntries] = NULL;

    RTStrFree(pEntry->pszActualMountPoint);
    pEntry->pszActualMountPoint = NULL;
    RTStrFree(pEntry->pszMountPoint);
    pEntry->pszMountPoint = NULL;
    RTStrFree(pEntry->pszName);
    pEntry->pszName = NULL;
    RTMemFree(pEntry);

    return iTable;
}


/**
 * @callback_method_impl{FNRTSORTCMP, For sorting the mappings by ID. }
 */
static DECLCALLBACK(int) vbsvcSharedFolderMappingCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
{
    RT_NOREF_PV(pvUser);
    PVBGLR3SHAREDFOLDERMAPPING pMapping1 = (PVBGLR3SHAREDFOLDERMAPPING)pvElement1;
    PVBGLR3SHAREDFOLDERMAPPING pMapping2 = (PVBGLR3SHAREDFOLDERMAPPING)pvElement2;
    return pMapping1->u32Root < pMapping2->u32Root ? -1 : pMapping1->u32Root != pMapping2->u32Root ? 1 : 0;
}


/**
 * Refreshes the mount table.
 *
 * @returns true if we've processed the current config, false if we failed to
 *          query the mappings.
 * @param   pTable          The mount table to refresh.
 */
static bool vbsvcAutomounterRefreshTable(PVBSVCAUTOMOUNTERTABLE pTable)
{
    /*
     * Query the root IDs of all auto-mountable shared folder mappings.
     */
    uint32_t                    cMappings = 0;
    PVBGLR3SHAREDFOLDERMAPPING  paMappings = NULL;
    int rc = VbglR3SharedFolderGetMappings(g_idClientSharedFolders, true /*fAutoMountOnly*/, &paMappings, &cMappings);
    if (RT_FAILURE(rc))
    {
        VGSvcError("vbsvcAutomounterRefreshTable: VbglR3SharedFolderGetMappings failed: %Rrc\n", rc);
        return false;
    }

    /*
     * Walk the table and the mappings in parallel, so we have to make sure
     * they are both sorted by root ID.
     */
    if (cMappings > 1)
        RTSortShell(paMappings, cMappings, sizeof(paMappings[0]), vbsvcSharedFolderMappingCompare, NULL);

    /*
     * Pass #1: Do all the umounting.
     *
     * By doing the umount pass separately from the mount pass, we can
     * better handle changing involving the same mount points (switching
     * mount points between two shares, new share on same mount point but
     * with lower root ID, ++).
     */
    uint32_t iTable = 0;
    for (uint32_t iSrc = 0; iSrc < cMappings; iSrc++)
    {
        /*
         * Unmount table entries up to idRootSrc.
         */
        uint32_t const idRootSrc = paMappings[iSrc].u32Root;
        while (   iTable < pTable->cEntries
               && pTable->papEntries[iTable]->idRoot < idRootSrc)
            iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "dropped");

        /*
         * If the paMappings entry and the mount table entry has the same
         * root ID, umount if anything has changed or if we cannot query
         * the mapping data.
         */
        if (iTable < pTable->cEntries)
        {
            PVBSVCAUTOMOUNTERENTRY pEntry = pTable->papEntries[iTable];
            if (pEntry->idRoot == idRootSrc)
            {
                uint32_t uRootIdVer = UINT32_MAX;
                uint64_t fFlags     = 0;
                char    *pszName    = NULL;
                char    *pszMntPt   = NULL;
                rc = VbglR3SharedFolderQueryFolderInfo(g_idClientSharedFolders, idRootSrc, VBOXSERVICE_AUTOMOUNT_MIQF,
                                                       &pszName, &pszMntPt, &fFlags, &uRootIdVer);
                if (RT_FAILURE(rc))
                    iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "VbglR3SharedFolderQueryFolderInfo failed");
                else if (pEntry->uRootIdVersion != uRootIdVer)
                    iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "root ID version changed");
                else if (RTPathCompare(pEntry->pszMountPoint, pszMntPt) != 0)
                    iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "mount point changed");
                else if (RTStrICmp(pEntry->pszName, pszName) != 0)
                    iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "name changed");
                else
                {
                    VGSvcVerbose(3, "vbsvcAutomounterRefreshTable: Unchanged: %s -> %s\n", pEntry->pszMountPoint, pEntry->pszName);
                    iTable++;
                }
                if (RT_SUCCESS(rc))
                {
                    RTStrFree(pszName);
                    RTStrFree(pszMntPt);
                }
            }
        }
    }

    while (iTable < pTable->cEntries)
        iTable = vbsvcAutomounterUnmountEntry(pTable, iTable, "dropped (tail)");

    VGSvcVerbose(4, "vbsvcAutomounterRefreshTable: %u entries in mount table after pass #1.\n", pTable->cEntries);

    /*
     * Pass #2: Try mount new folders that has mount points assigned.
     * Pass #3: Try mount new folders not mounted in pass #2.
     */
    for (uint32_t iPass = 2; iPass <= 3; iPass++)
    {
        iTable = 0;
        for (uint32_t iSrc = 0; iSrc < cMappings; iSrc++)
        {
            uint32_t const idRootSrc = paMappings[iSrc].u32Root;

            /*
             * Skip tabel entries we couldn't umount in pass #1.
             */
            while (   iTable < pTable->cEntries
                   && pTable->papEntries[iTable]->idRoot < idRootSrc)
            {
                VGSvcVerbose(4, "vbsvcAutomounterRefreshTable: %u/#%u/%#u: Skipping idRoot=%u %s\n",
                             iPass, iSrc, iTable, pTable->papEntries[iTable]->idRoot, pTable->papEntries[iTable]->pszName);
                iTable++;
            }

            /*
             * New share?
             */
            if (   iTable >= pTable->cEntries
                || pTable->papEntries[iTable]->idRoot != idRootSrc)
            {
                uint32_t uRootIdVer = UINT32_MAX;
                uint64_t fFlags     = 0;
                char    *pszName    = NULL;
                char    *pszMntPt   = NULL;
                rc = VbglR3SharedFolderQueryFolderInfo(g_idClientSharedFolders, idRootSrc, VBOXSERVICE_AUTOMOUNT_MIQF,
                                                       &pszName, &pszMntPt, &fFlags, &uRootIdVer);
                if (RT_SUCCESS(rc))
                {
                    VGSvcVerbose(4, "vbsvcAutomounterRefreshTable: %u/#%u/%#u: Mounting idRoot=%u/%u %s\n", iPass, iSrc, iTable,
                                 idRootSrc, iTable >= pTable->cEntries ? UINT32_MAX : pTable->papEntries[iTable]->idRoot, pszName);
                    iTable = vbsvcAutomounterMountNewEntry(pTable, iTable, pszName, pszMntPt, fFlags,
                                                           idRootSrc, uRootIdVer, iPass == 3);

                    RTStrFree(pszName);
                    RTStrFree(pszMntPt);
                }
                else
                    VGSvcVerbose(1, "vbsvcAutomounterRefreshTable: VbglR3SharedFolderQueryFolderInfo failed: %Rrc\n", rc);
            }
            else
                VGSvcVerbose(4, "vbsvcAutomounterRefreshTable: %u/#%u/%#u: idRootSrc=%u vs idRoot=%u %s\n", iPass, iSrc,
                             iTable, idRootSrc, pTable->papEntries[iTable]->idRoot, pTable->papEntries[iTable]->pszName);
        }
    }

    VbglR3SharedFolderFreeMappings(paMappings);
    return true;
}


/**
 * @interface_method_impl{VBOXSERVICE,pfnWorker}
 */
static DECLCALLBACK(int) vbsvcAutomounterWorker(bool volatile *pfShutdown)
{
    /*
     * Tell the control thread that it can continue spawning services.
     */
    RTThreadUserSignal(RTThreadSelf());

    /* Divert old hosts to original auto-mount code. */
    if (!g_fHostSupportsWaitAndInfoQuery)
        return vbsvcAutoMountWorkerOld(pfShutdown);

    /*
     * Initialize the state in case we're restarted...
     */
    VBSVCAUTOMOUNTERTABLE MountTable  = { 0, 0, NULL };
    int rc = vbsvcAutomounterPopulateTable(&MountTable);
    if (RT_FAILURE(rc))
    {
        VGSvcError("vbsvcAutomounterWorker: vbsvcAutomounterPopulateTable failed (%Rrc), quitting!\n", rc);
        return rc;
    }

    /*
     * Work loop.
     */
    uint32_t uConfigVer    = UINT32_MAX;
    uint32_t uNewVersion   = 0;
    bool     fForceRefresh = true;
    while (!*pfShutdown)
    {
        /*
         * Update the mounts.
         */
        if (   uConfigVer != uNewVersion
            || fForceRefresh)
        {
            fForceRefresh = !vbsvcAutomounterRefreshTable(&MountTable);
            uConfigVer    = uNewVersion;
        }

        /*
         * Wait for more to do.
         */
        if (!*pfShutdown)
        {
            uNewVersion = uConfigVer - 1;
            VGSvcVerbose(2, "vbsvcAutomounterWorker: Waiting with uConfigVer=%u\n", uConfigVer);
            rc = VbglR3SharedFolderWaitForMappingsChanges(g_idClientSharedFolders, uConfigVer, &uNewVersion);
            VGSvcVerbose(2, "vbsvcAutomounterWorker: Woke up with uNewVersion=%u and rc=%Rrc\n", uNewVersion, rc);

            /* Delay a little before doing a table refresh so the GUI can finish
               all its updates.  Delay a little longer on non-shutdown failure to
               avoid eating too many CPU cycles if something goes wrong here... */
            if (!*pfShutdown)
                RTSemEventMultiWait(g_hAutoMountEvent, RT_SUCCESS(rc) ? 256 : 1000);
        }
    }

    /*
     * Destroy the mount table.
     */
    while (MountTable.cEntries-- > 0)
        RTMemFree(MountTable.papEntries[MountTable.cEntries]);
    MountTable.papEntries = NULL;

    VGSvcVerbose(3, "vbsvcAutomounterWorker: Finished\n");
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{VBOXSERVICE,pfnStop}
 */
static DECLCALLBACK(void) vbsvcAutomounterStop(void)
{
    RTSemEventMultiSignal(g_hAutoMountEvent);
    if (g_fHostSupportsWaitAndInfoQuery)
        VbglR3SharedFolderCancelMappingsChangesWaits(g_idClientSharedFolders);
}


/**
 * @interface_method_impl{VBOXSERVICE,pfnTerm}
 */
static DECLCALLBACK(void) vbsvcAutomounterTerm(void)
{
    VGSvcVerbose(3, "vbsvcAutoMountTerm\n");

    if (g_fHostSupportsWaitAndInfoQuery)
        VbglR3SharedFolderCancelMappingsChangesWaits(g_idClientSharedFolders);

    VbglR3SharedFolderDisconnect(g_idClientSharedFolders);
    g_idClientSharedFolders = 0;

    if (g_hAutoMountEvent != NIL_RTSEMEVENTMULTI)
    {
        RTSemEventMultiDestroy(g_hAutoMountEvent);
        g_hAutoMountEvent = NIL_RTSEMEVENTMULTI;
    }
}


/**
 * The 'automount' service description.
 */
VBOXSERVICE g_AutoMount =
{
    /* pszName. */
    "automount",
    /* pszDescription. */
    "Automounter for Shared Folders",
    /* pszUsage. */
    NULL,
    /* pszOptions. */
    NULL,
    /* methods */
    VGSvcDefaultPreInit,
    VGSvcDefaultOption,
    vbsvcAutomounterInit,
    vbsvcAutomounterWorker,
    vbsvcAutomounterStop,
    vbsvcAutomounterTerm
};