diskarb.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Devices support using the MacOS Disk Arbitration library.
 *
 * Copyright 2006 Alexandre Julliard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21 22 23 24
#if 0
#pragma makedep unix
#endif

25 26 27 28
#include "config.h"

#include <assert.h>
#include <errno.h>
29
#include <fcntl.h>
30 31
#include <stdarg.h>
#include <stdio.h>
32
#include <unistd.h>
33
#include <sys/ioctl.h>
34 35 36
#ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
#include <DiskArbitration/DiskArbitration.h>
#endif
37 38 39 40
#if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
#include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>
#include <SystemConfiguration/SCNetworkConfiguration.h>
#endif
41

42
#include "mountmgr.h"
43 44 45 46
#define USE_WS_PREFIX
#include "winsock2.h"
#include "ws2ipdef.h"
#include "dhcpcsdk.h"
47 48
#include "unixlib.h"

49
#include "wine/debug.h"
50

51
WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
52

53
#ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
54

55 56 57 58 59 60 61 62 63 64
typedef struct
{
    uint64_t bus;
    uint64_t port;
    uint64_t target;
    uint64_t lun;
} dk_scsi_identify_t;

#define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)

65 66 67 68 69 70
static void appeared_callback( DADiskRef disk, void *context )
{
    CFDictionaryRef dict = DADiskCopyDescription( disk );
    const void *ref;
    char device[64];
    char mount_point[PATH_MAX];
71
    size_t model_len = 0;
72
    GUID guid, *guid_ptr = NULL;
73
    enum device_type type = DEVICE_UNKNOWN;
74 75
    struct scsi_info scsi_info = { 0 };
    BOOL removable = FALSE;
76
    int fd;
77 78 79

    if (!dict) return;

80 81 82 83 84 85 86
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumeUUID") )))
    {
        CFUUIDBytes bytes = CFUUIDGetUUIDBytes( ref );
        memcpy( &guid, &bytes, sizeof(guid) );
        guid_ptr = &guid;
    }

87
    /* get device name */
88
    if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
89 90 91 92 93 94 95 96
    strcpy( device, "/dev/r" );
    CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );

    if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumePath") )))
        CFURLGetFileSystemRepresentation( ref, true, (UInt8 *)mount_point, sizeof(mount_point) );
    else
        mount_point[0] = 0;

97
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaKind") )))
98
    {
99
        if (!CFStringCompare( ref, CFSTR("IOCDMedia"), 0 ))
100
        {
101
            type = DEVICE_CDROM;
102
            scsi_info.type = 5;
103
        }
104 105
        if (!CFStringCompare( ref, CFSTR("IODVDMedia"), 0 ) ||
            !CFStringCompare( ref, CFSTR("IOBDMedia"), 0 ))
106
        {
107
            type = DEVICE_DVD;
108
            scsi_info.type = 5;
109
        }
110 111
        if (!CFStringCompare( ref, CFSTR("IOMedia"), 0 ))
            type = DEVICE_HARDDISK;
112 113
    }

114 115 116 117
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceVendor") )))
    {
        CFIndex i;

118
        CFStringGetCString( ref, scsi_info.model, sizeof(scsi_info.model), kCFStringEncodingASCII );
119 120 121
        model_len += CFStringGetLength( ref );
        /* Pad to 8 characters */
        for (i = 0; i < (CFIndex)8 - CFStringGetLength( ref ); ++i)
122
            scsi_info.model[model_len++] = ' ';
123 124 125 126 127
    }
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceModel") )))
    {
        CFIndex i;

128
        CFStringGetCString( ref, scsi_info.model+model_len, sizeof(scsi_info.model)-model_len, kCFStringEncodingASCII );
129 130 131
        model_len += CFStringGetLength( ref );
        /* Pad to 16 characters */
        for (i = 0; i < (CFIndex)16 - CFStringGetLength( ref ); ++i)
132
            scsi_info.model[model_len++] = ' ';
133 134 135 136 137
    }
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceRevision") )))
    {
        CFIndex i;

138
        CFStringGetCString( ref, scsi_info.model+model_len, sizeof(scsi_info.model)-model_len, kCFStringEncodingASCII );
139 140 141
        model_len += CFStringGetLength( ref );
        /* Pad to 4 characters */
        for (i = 0; i < (CFIndex)4 - CFStringGetLength( ref ); ++i)
142
            scsi_info.model[model_len++] = ' ';
143 144
    }

145 146
    TRACE( "got mount notification for '%s' on '%s' uuid %s\n",
           device, mount_point, wine_dbgstr_guid(guid_ptr) );
147

148 149
    if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )))
        removable = CFBooleanGetValue( ref );
150

151 152
    if (!access( device, R_OK ) &&
        (fd = open( device, O_RDONLY )) >= 0)
153 154 155 156 157
    {
        dk_scsi_identify_t dsi;

        if (ioctl( fd, DKIOCSCSIIDENTIFY, &dsi ) >= 0)
        {
158 159 160 161 162 163
            scsi_info.addr.PortNumber = dsi.bus;
            scsi_info.addr.PathId = dsi.port;
            scsi_info.addr.TargetId = dsi.target;
            scsi_info.addr.Lun = dsi.lun;
            scsi_info.init_id = 255; /* FIXME */
            strcpy( scsi_info.driver, "WINE SCSI" ); /* FIXME */
164 165 166 167
        }
        close( fd );
    }

168
    if (removable)
169
        queue_device_op( ADD_DOS_DEVICE, device, device, mount_point, type, guid_ptr, NULL, &scsi_info );
170
    else
171
        if (guid_ptr) queue_device_op( ADD_VOLUME, device, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr, NULL, &scsi_info );
172

173
done:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    CFRelease( dict );
}

static void changed_callback( DADiskRef disk, CFArrayRef keys, void *context )
{
    appeared_callback( disk, context );
}

static void disappeared_callback( DADiskRef disk, void *context )
{
    CFDictionaryRef dict = DADiskCopyDescription( disk );
    const void *ref;
    char device[100];

    if (!dict) return;

    /* get device name */
191
    if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
192 193 194
    strcpy( device, "/dev/r" );
    CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );

195
    TRACE( "got unmount notification for '%s'\n", device );
196

197
    queue_device_op( REMOVE_DEVICE, device, NULL, NULL, 0, NULL, NULL, NULL );
198

199
done:
200 201 202
    CFRelease( dict );
}

203
void run_diskarbitration_loop(void)
204 205 206
{
    DASessionRef session = DASessionCreate( NULL );

207
    if (!session) return;
208 209 210 211 212 213 214 215 216 217 218 219 220

    DASessionScheduleWithRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
    DARegisterDiskAppearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
                                    appeared_callback, NULL );
    DARegisterDiskDisappearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
                                       disappeared_callback, NULL );
    DARegisterDiskDescriptionChangedCallback( session, kDADiskDescriptionMatchVolumeMountable,
                                              kDADiskDescriptionWatchVolumePath, changed_callback, NULL );
    CFRunLoopRun();
    DASessionUnscheduleFromRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
    CFRelease( session );
}

221
#else  /*  HAVE_DISKARBITRATION_DISKARBITRATION_H */
222

223
void run_diskarbitration_loop(void)
224
{
225
    TRACE( "Skipping, Disk Arbitration support not compiled in\n" );
226 227
}

228
#endif  /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

#if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)

static UInt8 map_option( ULONG option )
{
    switch (option)
    {
    case OPTION_SUBNET_MASK:         return 1;
    case OPTION_ROUTER_ADDRESS:      return 3;
    case OPTION_HOST_NAME:           return 12;
    case OPTION_DOMAIN_NAME:         return 15;
    case OPTION_BROADCAST_ADDRESS:   return 28;
    case OPTION_MSFT_IE_PROXY:       return 252;
    default:
        FIXME( "unhandled option %u\n", option );
        return 0;
    }
}

248
static CFStringRef find_service_id( const char *unix_name )
249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
    SCPreferencesRef prefs;
    SCNetworkSetRef set = NULL;
    CFArrayRef services = NULL;
    CFStringRef id, ret = NULL;
    CFIndex i;

    if (!(prefs = SCPreferencesCreate( NULL, CFSTR("mountmgr.sys"), NULL ))) return NULL;
    if (!(set = SCNetworkSetCopyCurrent( prefs ))) goto done;
    if (!(services = SCNetworkSetCopyServices( set ))) goto done;

    for (i = 0; i < CFArrayGetCount( services ); i++)
    {
        SCNetworkServiceRef service;
263
        char buf[16];
264 265 266 267
        CFStringRef name;

        service = CFArrayGetValueAtIndex( services, i );
        name = SCNetworkInterfaceGetBSDName( SCNetworkServiceGetInterface(service) );
268
        if (name && CFStringGetCString( name, buf, sizeof(buf), kCFStringEncodingUTF8 ))
269
        {
270
            if (!strcmp( buf, unix_name ) && (id = SCNetworkServiceGetServiceID( service )))
271 272 273 274 275 276 277 278 279 280 281 282 283 284
            {
                ret = CFStringCreateCopy( NULL, id );
                break;
            }
        }
    }

done:
    if (services) CFRelease( services );
    if (set) CFRelease( set );
    CFRelease( prefs );
    return ret;
}

285
NTSTATUS dhcp_request( void *args )
286
{
287 288
    const struct dhcp_request_params *params = args;
    CFStringRef service_id = find_service_id( params->unix_name );
289 290 291 292 293
    CFDictionaryRef dict;
    CFDataRef value;
    DWORD ret = 0;
    CFIndex len;

294 295
    params->req->offset = 0;
    params->req->size   = 0;
296 297 298 299 300 301 302 303

    if (!service_id) return 0;
    if (!(dict = SCDynamicStoreCopyDHCPInfo( NULL, service_id )))
    {
        CFRelease( service_id );
        return 0;
    }
    CFRelease( service_id );
304
    if (!(value = DHCPInfoGetOptionData( dict, map_option(params->req->id) )))
305 306 307 308 309 310
    {
        CFRelease( dict );
        return 0;
    }
    len = CFDataGetLength( value );

311
    switch (params->req->id)
312 313 314 315 316
    {
    case OPTION_SUBNET_MASK:
    case OPTION_ROUTER_ADDRESS:
    case OPTION_BROADCAST_ADDRESS:
    {
317 318
        DWORD *ptr = (DWORD *)(params->buffer + params->offset);
        if (len == sizeof(*ptr) && params->size >= sizeof(*ptr))
319 320
        {
            CFDataGetBytes( value, CFRangeMake(0, len), (UInt8 *)ptr );
321 322
            params->req->offset = params->offset;
            params->req->size   = sizeof(*ptr);
323 324 325 326 327 328 329 330 331
            TRACE( "returning %08x\n", *ptr );
        }
        ret = sizeof(*ptr);
        break;
    }
    case OPTION_HOST_NAME:
    case OPTION_DOMAIN_NAME:
    case OPTION_MSFT_IE_PROXY:
    {
332 333
        char *ptr = params->buffer + params->offset;
        if (params->size >= len)
334 335
        {
            CFDataGetBytes( value, CFRangeMake(0, len), (UInt8 *)ptr );
336 337
            params->req->offset = params->offset;
            params->req->size   = len;
338 339 340 341 342 343
            TRACE( "returning %s\n", debugstr_an(ptr, len) );
        }
        ret = len;
        break;
    }
    default:
344
        FIXME( "option %u not supported\n", params->req->id );
345 346 347 348
        break;
    }

    CFRelease( dict );
349 350
    *params->ret_size = ret;
    return STATUS_SUCCESS;
351 352 353 354
}

#elif !defined(SONAME_LIBDBUS_1)

355
NTSTATUS dhcp_request( void *args )
356
{
357
    return STATUS_NOT_SUPPORTED;
358 359 360
}

#endif