ntdll: Rename local variables in heap_reallocate.
[wine.git] / dlls / mountmgr.sys / diskarb.c
blob462ff82c6917bc5e276ce0ab1ee252cca30f3669
1 /*
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
21 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
27 #include <assert.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
35 #include <DiskArbitration/DiskArbitration.h>
36 #endif
37 #if defined(HAVE_SYSTEMCONFIGURATION_SCDYNAMICSTORECOPYDHCPINFO_H) && defined(HAVE_SYSTEMCONFIGURATION_SCNETWORKCONFIGURATION_H)
38 #include <SystemConfiguration/SCDynamicStoreCopyDHCPInfo.h>
39 #include <SystemConfiguration/SCNetworkConfiguration.h>
40 #endif
42 #include "mountmgr.h"
43 #define USE_WS_PREFIX
44 #include "winsock2.h"
45 #include "ws2ipdef.h"
46 #include "dhcpcsdk.h"
47 #include "unixlib.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
53 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
55 typedef struct
57 uint64_t bus;
58 uint64_t port;
59 uint64_t target;
60 uint64_t lun;
61 } dk_scsi_identify_t;
63 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
65 static void appeared_callback( DADiskRef disk, void *context )
67 CFDictionaryRef dict = DADiskCopyDescription( disk );
68 const void *ref;
69 char device[64];
70 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 struct scsi_info scsi_info = { 0 };
75 BOOL removable = FALSE;
76 int fd;
78 if (!dict) return;
80 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumeUUID") )))
82 CFUUIDBytes bytes = CFUUIDGetUUIDBytes( ref );
83 memcpy( &guid, &bytes, sizeof(guid) );
84 guid_ptr = &guid;
87 /* get device name */
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) );
94 else
95 mount_point[0] = 0;
97 if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaKind") )))
99 if (!CFStringCompare( ref, CFSTR("IOCDMedia"), 0 ))
101 type = DEVICE_CDROM;
102 scsi_info.type = 5;
104 if (!CFStringCompare( ref, CFSTR("IODVDMedia"), 0 ) ||
105 !CFStringCompare( ref, CFSTR("IOBDMedia"), 0 ))
107 type = DEVICE_DVD;
108 scsi_info.type = 5;
110 if (!CFStringCompare( ref, CFSTR("IOMedia"), 0 ))
111 type = DEVICE_HARDDISK;
114 if ((ref = CFDictionaryGetValue( dict, CFSTR("DADeviceVendor") )))
116 CFIndex i;
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") )))
126 CFIndex i;
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") )))
136 CFIndex i;
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 */
165 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 );
173 done:
174 CFRelease( dict );
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 );
185 const void *ref;
186 char device[100];
188 if (!dict) return;
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 );
199 done:
200 CFRelease( dict );
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 );
216 CFRunLoopRun();
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( unsigned int option )
234 switch (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;
242 default:
243 FIXME( "unhandled option %u\n", option );
244 return 0;
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;
254 CFIndex i;
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;
263 char buf[16];
264 CFStringRef name;
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 );
273 break;
278 done:
279 if (services) CFRelease( services );
280 if (set) CFRelease( set );
281 CFRelease( prefs );
282 return ret;
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;
290 CFDataRef value;
291 DWORD ret = 0;
292 CFIndex len;
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 );
301 return 0;
303 CFRelease( service_id );
304 if (!(value = DHCPInfoGetOptionData( dict, map_option(params->req->id) )))
306 CFRelease( dict );
307 return 0;
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 unsigned int *ptr = (unsigned int *)(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 );
325 ret = sizeof(*ptr);
326 break;
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) );
340 ret = len;
341 break;
343 default:
344 FIXME( "option %u not supported\n", (unsigned int)params->req->id );
345 break;
348 CFRelease( dict );
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;
360 #endif