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
33 #include <sys/ioctl.h>
34 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
35 #include <DiskArbitration/DiskArbitration.h>
37 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
38 #include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>
39 #include <SystemConfiguration/SCNetworkConfiguration.h>
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
53 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
63 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
65 static void appeared_callback( DADiskRef disk
, void *context
)
67 CFDictionaryRef dict
= DADiskCopyDescription( disk
);
70 char mount_point
[PATH_MAX
];
72 GUID guid
, *guid_ptr
= NULL
;
73 enum device_type type
= DEVICE_UNKNOWN
;
74 struct scsi_info scsi_info
= { 0 };
75 BOOL removable
= FALSE
;
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
, scsi_info
.model
, sizeof(scsi_info
.model
), kCFStringEncodingASCII
);
119 model_len
+= CFStringGetLength( ref
);
120 /* Pad to 8 characters */
121 for (i
= 0; i
< (CFIndex
)8 - CFStringGetLength( ref
); ++i
)
122 scsi_info
.model
[model_len
++] = ' ';
124 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DADeviceModel") )))
128 CFStringGetCString( ref
, scsi_info
.model
+model_len
, sizeof(scsi_info
.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 scsi_info
.model
[model_len
++] = ' ';
134 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DADeviceRevision") )))
138 CFStringGetCString( ref
, scsi_info
.model
+model_len
, sizeof(scsi_info
.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 scsi_info
.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 if ((ref
= CFDictionaryGetValue( dict
, CFSTR("DAMediaRemovable") )))
149 removable
= CFBooleanGetValue( ref
);
151 if (!access( device
, R_OK
) &&
152 (fd
= open( device
, O_RDONLY
)) >= 0)
154 dk_scsi_identify_t dsi
;
156 if (ioctl( fd
, DKIOCSCSIIDENTIFY
, &dsi
) >= 0)
158 scsi_info
.addr
.PortNumber
= dsi
.bus
;
159 scsi_info
.addr
.PathId
= dsi
.port
;
160 scsi_info
.addr
.TargetId
= dsi
.target
;
161 scsi_info
.addr
.Lun
= dsi
.lun
;
162 scsi_info
.init_id
= 255; /* FIXME */
163 strcpy( scsi_info
.driver
, "WINE SCSI" ); /* FIXME */
169 queue_device_op( ADD_DOS_DEVICE
, device
, device
, mount_point
, type
, guid_ptr
, NULL
, &scsi_info
);
171 if (guid_ptr
) queue_device_op( ADD_VOLUME
, device
, device
, mount_point
, DEVICE_HARDDISK_VOL
, guid_ptr
, NULL
, &scsi_info
);
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 queue_device_op( REMOVE_DEVICE
, device
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
203 void run_diskarbitration_loop(void)
205 DASessionRef session
= DASessionCreate( NULL
);
207 if (!session
) return;
209 DASessionScheduleWithRunLoop( session
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
210 DARegisterDiskAppearedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
211 appeared_callback
, NULL
);
212 DARegisterDiskDisappearedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
213 disappeared_callback
, NULL
);
214 DARegisterDiskDescriptionChangedCallback( session
, kDADiskDescriptionMatchVolumeMountable
,
215 kDADiskDescriptionWatchVolumePath
, changed_callback
, NULL
);
217 DASessionUnscheduleFromRunLoop( session
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
218 CFRelease( session
);
221 #else /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
223 void run_diskarbitration_loop(void)
225 TRACE( "Skipping, Disk Arbitration support not compiled in\n" );
228 #endif /* HAVE_DISKARBITRATION_DISKARBITRATION_H */
230 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
232 static UInt8
map_option( ULONG option
)
236 case OPTION_SUBNET_MASK
: return 1;
237 case OPTION_ROUTER_ADDRESS
: return 3;
238 case OPTION_HOST_NAME
: return 12;
239 case OPTION_DOMAIN_NAME
: return 15;
240 case OPTION_BROADCAST_ADDRESS
: return 28;
241 case OPTION_MSFT_IE_PROXY
: return 252;
243 FIXME( "unhandled option %u\n", option
);
248 static CFStringRef
find_service_id( const char *unix_name
)
250 SCPreferencesRef prefs
;
251 SCNetworkSetRef set
= NULL
;
252 CFArrayRef services
= NULL
;
253 CFStringRef id
, ret
= NULL
;
256 if (!(prefs
= SCPreferencesCreate( NULL
, CFSTR("mountmgr.sys"), NULL
))) return NULL
;
257 if (!(set
= SCNetworkSetCopyCurrent( prefs
))) goto done
;
258 if (!(services
= SCNetworkSetCopyServices( set
))) goto done
;
260 for (i
= 0; i
< CFArrayGetCount( services
); i
++)
262 SCNetworkServiceRef service
;
266 service
= CFArrayGetValueAtIndex( services
, i
);
267 name
= SCNetworkInterfaceGetBSDName( SCNetworkServiceGetInterface(service
) );
268 if (name
&& CFStringGetCString( name
, buf
, sizeof(buf
), kCFStringEncodingUTF8
))
270 if (!strcmp( buf
, unix_name
) && (id
= SCNetworkServiceGetServiceID( service
)))
272 ret
= CFStringCreateCopy( NULL
, id
);
279 if (services
) CFRelease( services
);
280 if (set
) CFRelease( set
);
285 NTSTATUS
dhcp_request( void *args
)
287 const struct dhcp_request_params
*params
= args
;
288 CFStringRef service_id
= find_service_id( params
->unix_name
);
289 CFDictionaryRef dict
;
294 params
->req
->offset
= 0;
295 params
->req
->size
= 0;
297 if (!service_id
) return 0;
298 if (!(dict
= SCDynamicStoreCopyDHCPInfo( NULL
, service_id
)))
300 CFRelease( service_id
);
303 CFRelease( service_id
);
304 if (!(value
= DHCPInfoGetOptionData( dict
, map_option(params
->req
->id
) )))
309 len
= CFDataGetLength( value
);
311 switch (params
->req
->id
)
313 case OPTION_SUBNET_MASK
:
314 case OPTION_ROUTER_ADDRESS
:
315 case OPTION_BROADCAST_ADDRESS
:
317 DWORD
*ptr
= (DWORD
*)(params
->buffer
+ params
->offset
);
318 if (len
== sizeof(*ptr
) && params
->size
>= sizeof(*ptr
))
320 CFDataGetBytes( value
, CFRangeMake(0, len
), (UInt8
*)ptr
);
321 params
->req
->offset
= params
->offset
;
322 params
->req
->size
= sizeof(*ptr
);
323 TRACE( "returning %08x\n", *ptr
);
328 case OPTION_HOST_NAME
:
329 case OPTION_DOMAIN_NAME
:
330 case OPTION_MSFT_IE_PROXY
:
332 char *ptr
= params
->buffer
+ params
->offset
;
333 if (params
->size
>= len
)
335 CFDataGetBytes( value
, CFRangeMake(0, len
), (UInt8
*)ptr
);
336 params
->req
->offset
= params
->offset
;
337 params
->req
->size
= len
;
338 TRACE( "returning %s\n", debugstr_an(ptr
, len
) );
344 FIXME( "option %u not supported\n", params
->req
->id
);
349 *params
->ret_size
= ret
;
350 return STATUS_SUCCESS
;
353 #elif !defined(SONAME_LIBDBUS_1)
355 NTSTATUS
dhcp_request( void *args
)
357 return STATUS_NOT_SUPPORTED
;