VirtualBox

Ignore:
Timestamp:
Jan 28, 2014 7:29:52 PM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
91891
Message:

Main/HostDnsService:

  • Generic part could be used with threading on or off: OS which can monitor changes on DNS structures might need this (e.g. Windows, Darwin, Linux)
  • ResolvConf based (Linux, Solaris, FreeBSD ans Os/2) doesn't use scanf based resovl.conf parsing new Slirp's parser used instead. It's configured to provide strings instead of addresses. (to resolve xtracker/7034c2)
  • Darwin monitoring code has been refactored.
  • linux changed to handle cases described in xtracker/7034c3
  • Windows monitors changes on register changes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/darwin/HostDnsServiceDarwin.cpp

    r50213 r50263  
    3232
    3333
    34 
    35 SCDynamicStoreRef g_store;
    36 CFRunLoopSourceRef g_DnsWatcher;
    37 CFRunLoopRef g_RunLoopRef;
    38 RTTHREAD g_DnsMonitoringThread;
    39 RTSEMEVENT g_DnsInitEvent;
     34struct HostDnsServiceDarwin::Data
     35{
     36    SCDynamicStoreRef m_store;
     37    CFRunLoopSourceRef m_DnsWatcher;
     38    CFRunLoopRef m_RunLoopRef;
     39    CFRunLoopSourceRef m_Stopper;
     40    bool m_fStop;
     41    RTSEMEVENT m_evtStop;
     42    static void performShutdownCallback(void *);
     43};
     44
    4045
    4146static const CFStringRef kStateNetworkGlobalDNSKey = CFSTR("State:/Network/Global/DNS");
    4247
    43 static int hostMonitoringRoutine(RTTHREAD ThreadSelf, void *pvUser)
    44 {
    45     NOREF(ThreadSelf);
    46     NOREF(pvUser);
    47     g_RunLoopRef = CFRunLoopGetCurrent();
    48     AssertReturn(g_RunLoopRef, VERR_INTERNAL_ERROR);
    49 
    50     CFRetain(g_RunLoopRef);
     48
     49HostDnsServiceDarwin::HostDnsServiceDarwin():HostDnsMonitor(true),m(NULL)
     50{
     51    m = new HostDnsServiceDarwin::Data();
     52}
     53
     54
     55HostDnsServiceDarwin::~HostDnsServiceDarwin()
     56{
     57    if (!m)
     58        return;
     59
     60    monitorThreadShutdown();
     61
     62    CFRelease(m->m_RunLoopRef);
     63   
     64    CFRelease(m->m_DnsWatcher);
     65
     66    CFRelease(m->m_store);
     67
     68    RTSemEventDestroy(m->m_evtStop);
     69
     70    delete m;
     71    m = NULL;
     72}
     73
     74
     75void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *, void *, void *info)
     76{
     77    HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
     78
     79    ALock l(pThis);
     80    pThis->updateInfo();
     81    pThis->notifyAll();
     82}
     83
     84
     85HRESULT HostDnsServiceDarwin::init()
     86{
     87    SCDynamicStoreContext ctx;
     88    RT_ZERO(ctx);
     89
     90    ctx.info = this;
     91
     92    m->m_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
     93                                   (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
     94                                   &ctx);
     95    AssertReturn(m->m_store, E_FAIL);
     96
     97    m->m_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, m->m_store, 0);
     98    if (!m->m_DnsWatcher)
     99        return E_OUTOFMEMORY;
     100
     101    int rc = RTSemEventCreate(&m->m_evtStop);
     102    AssertRCReturn(rc, E_FAIL);
     103
     104    CFRunLoopSourceContext sctx;
     105    RT_ZERO(sctx);
     106    sctx.perform = HostDnsServiceDarwin::Data::performShutdownCallback;
     107    m->m_Stopper = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &sctx);
     108    AssertReturn(m->m_Stopper, E_FAIL);
     109
     110    HRESULT hrc = HostDnsMonitor::init();
     111    AssertComRCReturn(hrc, hrc);
     112
     113    return updateInfo();
     114}
     115
     116
     117void HostDnsServiceDarwin::monitorThreadShutdown()
     118{
     119    ALock l(this);
     120    if (!m->m_fStop)
     121    {
     122        CFRunLoopSourceSignal(m->m_Stopper);
     123        CFRunLoopWakeUp(m->m_RunLoopRef);
     124       
     125        RTSemEventWait(m->m_evtStop, RT_INDEFINITE_WAIT);
     126    }
     127}
     128
     129
     130int HostDnsServiceDarwin::monitorWorker()
     131{
     132    m->m_RunLoopRef = CFRunLoopGetCurrent();
     133    AssertReturn(m->m_RunLoopRef, VERR_INTERNAL_ERROR);
     134
     135    CFRetain(m->m_RunLoopRef);
    51136
    52137    CFArrayRef watchingArrayRef = CFArrayCreate(NULL,
     
    55140    if (!watchingArrayRef)
    56141    {
    57         CFRelease(g_DnsWatcher);
     142        CFRelease(m->m_DnsWatcher);
    58143        return E_OUTOFMEMORY;
    59144    }
    60145
    61     if(SCDynamicStoreSetNotificationKeys(g_store, watchingArrayRef, NULL))
    62         CFRunLoopAddSource(CFRunLoopGetCurrent(), g_DnsWatcher, kCFRunLoopCommonModes);
     146    if(SCDynamicStoreSetNotificationKeys(m->m_store, watchingArrayRef, NULL))
     147        CFRunLoopAddSource(CFRunLoopGetCurrent(), m->m_DnsWatcher, kCFRunLoopCommonModes);
    63148
    64149    CFRelease(watchingArrayRef);
    65150
    66     RTSemEventSignal(g_DnsInitEvent);
    67 
    68     CFRunLoopRun();
    69 
    70     CFRelease(g_RunLoopRef);
     151    monitorThreadInitializationDone();
     152
     153    while (!m->m_fStop)
     154    {
     155        CFRunLoopRun();
     156    }
     157
     158    CFRelease(m->m_RunLoopRef);
     159
     160    /* We're notifying stopper thread. */
     161    RTSemEventSignal(m->m_evtStop);
    71162
    72163    return VINF_SUCCESS;
     
    74165
    75166
    76 HostDnsServiceDarwin::HostDnsServiceDarwin(){}
    77 
    78 
    79 HostDnsServiceDarwin::~HostDnsServiceDarwin()
    80 {
    81     if (g_RunLoopRef)
    82         CFRunLoopStop(g_RunLoopRef);
    83 
    84     CFRelease(g_DnsWatcher);
    85 
    86     CFRelease(g_store);
    87 }
    88 
    89 
    90 void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *arg0, void *arg1, void *info)
    91 {
    92     HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
    93 
    94     NOREF(arg0); /* SCDynamicStore */
    95     NOREF(arg1); /* CFArrayRef */
    96 
    97     ALock l(pThis);
    98     pThis->updateInfo();
    99     pThis->notifyAll();
    100 }
    101 
    102 
    103 HRESULT HostDnsServiceDarwin::init()
    104 {
    105     SCDynamicStoreContext ctx;
    106     RT_ZERO(ctx);
    107 
    108     ctx.info = this;
    109 
    110     g_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
    111                                    (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
    112                                    &ctx);
    113     AssertReturn(g_store, E_FAIL);
    114 
    115     g_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, g_store, 0);
    116     if (!g_DnsWatcher)
    117         return E_OUTOFMEMORY;
    118 
    119     HRESULT hrc = HostDnsMonitor::init();
    120     AssertComRCReturn(hrc, hrc);
    121 
    122     int rc = RTSemEventCreate(&g_DnsInitEvent);
    123     AssertRCReturn(rc, E_FAIL);
    124 
    125     rc = RTThreadCreate(&g_DnsMonitoringThread, hostMonitoringRoutine,
    126                         this, 128 * _1K, RTTHREADTYPE_IO, 0, "dns-monitor");
    127     AssertRCReturn(rc, E_FAIL);
    128 
    129     RTSemEventWait(g_DnsInitEvent, RT_INDEFINITE_WAIT);
    130     return updateInfo();
    131 }
    132 
    133 
    134167HRESULT HostDnsServiceDarwin::updateInfo()
    135168{
    136     CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(g_store,
     169    CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(m->m_store,
    137170                                                            kStateNetworkGlobalDNSKey);
    138171    /**
     
    216249    return S_OK;
    217250}
     251
     252void HostDnsServiceDarwin::Data::performShutdownCallback(void *info)
     253{
     254    HostDnsServiceDarwin::Data *pThis = static_cast<HostDnsServiceDarwin::Data *>(info);
     255    pThis->m_fStop = true;
     256}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette