2 * Devices support using the MacOS Disk Arbitration library.
4 * Copyright 2006 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
32 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
33 #include <DiskArbitration/DiskArbitration.h>
35 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
36 #include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>
37 #include <SystemConfiguration/SCNetworkConfiguration.h>
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
51 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
61 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
63 static void appeared_callback( DADiskRef disk
, void *context
)
65 CFDictionaryRef dict
= DADiskCopyDescription( disk
);
68 char mount_point
[PATH_MAX
];
71 GUID guid
, *guid_ptr
= NULL
;
72 enum device_type type
= DEVICE_UNKNOWN
;
74 SCSI_ADDRESS scsi_addr
;
75 UNICODE_STRING devname
;
80 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAVolumeUUID") )))
82 CFUUIDBytes bytes
= CFUUIDGetUUIDBytes( ref
);
83 memcpy( &guid
, &bytes
, sizeof(guid
) );
88 if (!(ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaBSDName") ))) goto done
;
89 strcpy( device
, "/dev/r" );
90 CFStringGetCString( ref
, device
+ 6, sizeof(device
) - 6, kCFStringEncodingASCII
);
92 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAVolumePath") )))
93 CFURLGetFileSystemRepresentation( ref
, true, (UInt8
*)mount_point
, sizeof(mount_point
) );
97 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaKind") )))
99 if (!CFStringCompare( ref
, CFSTR("IOCDMedia"), 0 ))
104 if (!CFStringCompare( ref
, CFSTR("IODVDMedia"), 0 ) ||
105 !CFStringCompare( ref
, CFSTR("IOBDMedia"), 0 ))
110 if (!CFStringCompare( ref
, CFSTR("IOMedia"), 0 ))
111 type
= DEVICE_HARDDISK
;
114 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DADeviceVendor") )))
118 CFStringGetCString( ref
, model
, sizeof(model
), kCFStringEncodingASCII
);
119 model_len
+= CFStringGetLength( ref
);
120 /* Pad to 8 characters */
121 for (i
= 0; i
< (CFIndex
)8 - CFStringGetLength( ref
); ++i
)
122 model
[model_len
++] = ' ';
124 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DADeviceModel") )))
128 CFStringGetCString( ref
, model
+model_len
, sizeof(model
)-model_len
, kCFStringEncodingASCII
);
129 model_len
+= CFStringGetLength( ref
);
130 /* Pad to 16 characters */
131 for (i
= 0; i
< (CFIndex
)16 - CFStringGetLength( ref
); ++i
)
132 model
[model_len
++] = ' ';
134 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DADeviceRevision") )))
138 CFStringGetCString( ref
, model
+model_len
, sizeof(model
)-model_len
, kCFStringEncodingASCII
);
139 model_len
+= CFStringGetLength( ref
);
140 /* Pad to 4 characters */
141 for (i
= 0; i
< (CFIndex
)4 - CFStringGetLength( ref
); ++i
)
142 model
[model_len
++] = ' ';
145 TRACE( "got mount notification for '%s' on '%s' uuid %s\n",
146 device
, mount_point
, wine_dbgstr_guid(guid_ptr
) );
148 devname
.Buffer
= NULL
;
149 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref
))
150 add_dos_device( -1, device
, device
, mount_point
, type
, guid_ptr
, &devname
);
152 if (guid_ptr
) add_volume( device
, device
, mount_point
, DEVICE_HARDDISK_VOL
, guid_ptr
, NULL
);
154 if (!access( device
, R_OK
) &&
155 (fd
= open( device
, O_RDONLY
)) >= 0)
157 dk_scsi_identify_t dsi
;
159 if (ioctl( fd
, DKIOCSCSIIDENTIFY
, &dsi
) >= 0)
161 scsi_addr
.PortNumber
= dsi
.bus
;
162 scsi_addr
.PathId
= dsi
.port
;
163 scsi_addr
.TargetId
= dsi
.target
;
164 scsi_addr
.Lun
= dsi
.lun
;
166 /* FIXME: get real controller Id for SCSI */
167 /* FIXME: get real driver name */
168 create_scsi_entry( &scsi_addr
, 255, "WINE SCSI", pdtype
, model
, &devname
);
177 static void changed_callback( DADiskRef disk
, CFArrayRef keys
, void *context
)
179 appeared_callback( disk
, context
);
182 static void disappeared_callback( DADiskRef disk
, void *context
)
184 CFDictionaryRef dict
= DADiskCopyDescription( disk
);
190 /* get device name */
191 if (!(ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaBSDName") ))) goto done
;
192 strcpy( device
, "/dev/r" );
193 CFStringGetCString( ref
, device
+ 6, sizeof(device
) - 6, kCFStringEncodingASCII
);
195 TRACE( "got unmount notification for '%s'\n", device
);
197 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref
))
198 remove_dos_device( -1, device
);
200 remove_volume( device
);
206 static DWORD WINAPI
runloop_thread( void *arg
)
208 DASessionRef session
= DASessionCreate( NULL
);
210 if (!session
) return 1;
212 DASessionScheduleWithRunLoop( session
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
213 DARegisterDiskAppearedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
214 appeared_callback
, NULL
);
215 DARegisterDiskDisappearedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
216 disappeared_callback
, NULL
);
217 DARegisterDiskDescriptionChangedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
218 kDADiskDescriptionWatchVolumePath
, changed_callback
, NULL
);
220 DASessionUnscheduleFromRunLoop( session
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
221 CFRelease( session
);
225 void initialize_diskarbitration(void)
229 if (!(handle
= CreateThread( NULL
, 0, runloop_thread
, NULL
, 0, NULL
))) return;
230 CloseHandle( handle
);
233 #else /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
235 void initialize_diskarbitration(void)
237 TRACE( "Skipping, Disk Arbitration support not compiled in\n" );
240 #endif /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
242 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
244 static UInt8
map_option( ULONG option
)
248 case OPTION_SUBNET_MASK
: return 1;
249 case OPTION_ROUTER_ADDRESS
: return 3;
250 case OPTION_HOST_NAME
: return 12;
251 case OPTION_DOMAIN_NAME
: return 15;
252 case OPTION_BROADCAST_ADDRESS
: return 28;
253 case OPTION_MSFT_IE_PROXY
: return 252;
255 FIXME( "unhandled option %u\n", option
);
260 #define IF_NAMESIZE 16
261 static BOOL
map_adapter_name( const NET_LUID
*luid
, WCHAR
*unix_name
, DWORD len
)
263 return !ConvertInterfaceLuidToAlias( luid
, unix_name
, len
);
266 static CFStringRef
find_service_id( const NET_LUID
*adapter
)
268 SCPreferencesRef prefs
;
269 SCNetworkSetRef set
= NULL
;
270 CFArrayRef services
= NULL
;
271 CFStringRef id
, ret
= NULL
;
272 WCHAR unix_name
[IF_NAMESIZE
];
275 if (!map_adapter_name( adapter
, unix_name
, ARRAY_SIZE(unix_name
) )) return NULL
;
276 if (!(prefs
= SCPreferencesCreate( NULL
, CFSTR("mountmgr.sys"), NULL
))) return NULL
;
277 if (!(set
= SCNetworkSetCopyCurrent( prefs
))) goto done
;
278 if (!(services
= SCNetworkSetCopyServices( set
))) goto done
;
280 for (i
= 0; i
< CFArrayGetCount( services
); i
++)
282 SCNetworkServiceRef service
;
283 UniChar buf
[IF_NAMESIZE
] = {0};
286 service
= CFArrayGetValueAtIndex( services
, i
);
287 name
= SCNetworkInterfaceGetBSDName( SCNetworkServiceGetInterface(service
) );
288 if (name
&& CFStringGetLength( name
) < ARRAY_SIZE( buf
))
290 CFStringGetCharacters( name
, CFRangeMake(0, CFStringGetLength(name
)), buf
);
291 if (!lstrcmpW( buf
, unix_name
) && (id
= SCNetworkServiceGetServiceID( service
)))
293 ret
= CFStringCreateCopy( NULL
, id
);
300 if (services
) CFRelease( services
);
301 if (set
) CFRelease( set
);
306 ULONG
get_dhcp_request_param( const NET_LUID
*adapter
, struct mountmgr_dhcp_request_param
*param
, char *buf
, ULONG offset
,
309 CFStringRef service_id
= find_service_id( adapter
);
310 CFDictionaryRef dict
;
318 if (!service_id
) return 0;
319 if (!(dict
= SCDynamicStoreCopyDHCPInfo( NULL
, service_id
)))
321 CFRelease( service_id
);
324 CFRelease( service_id
);
325 if (!(value
= DHCPInfoGetOptionData( dict
, map_option(param
->id
) )))
330 len
= CFDataGetLength( value
);
334 case OPTION_SUBNET_MASK
:
335 case OPTION_ROUTER_ADDRESS
:
336 case OPTION_BROADCAST_ADDRESS
:
338 DWORD
*ptr
= (DWORD
*)(buf
+ offset
);
339 if (len
== sizeof(*ptr
) && size
>= sizeof(*ptr
))
341 CFDataGetBytes( value
, CFRangeMake(0, len
), (UInt8
*)ptr
);
342 param
->offset
= offset
;
343 param
->size
= sizeof(*ptr
);
344 TRACE( "returning %08x\n", *ptr
);
349 case OPTION_HOST_NAME
:
350 case OPTION_DOMAIN_NAME
:
351 case OPTION_MSFT_IE_PROXY
:
353 char *ptr
= buf
+ offset
;
356 CFDataGetBytes( value
, CFRangeMake(0, len
), (UInt8
*)ptr
);
357 param
->offset
= offset
;
359 TRACE( "returning %s\n", debugstr_an(ptr
, len
) );
365 FIXME( "option %u not supported\n", param
->id
);
373 #elif !defined(SONAME_LIBDBUS_1)
375 ULONG
get_dhcp_request_param( const NET_LUID
*adapter
, struct mountmgr_dhcp_request_param
*param
, char *buf
, ULONG offset
,
378 FIXME( "support not compiled in\n" );