mountmgr: Move the DBus support to the Unix library.
[wine.git] / dlls / mountmgr.sys / unixlib.c
blobcaecb0140a89023e46200b20f22b70a6dcddd825
1 /*
2 * MountMgr Unix interface
4 * Copyright 2021 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 <errno.h>
28 #include <fcntl.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include "unixlib.h"
37 static struct run_loop_params run_loop_params;
39 static char *get_dosdevices_path( const char *dev )
41 const char *home = getenv( "HOME" );
42 const char *prefix = getenv( "WINEPREFIX" );
43 size_t len = (prefix ? strlen(prefix) : strlen(home) + strlen("/.wine")) + sizeof("/dosdevices/") + strlen(dev);
44 char *path = malloc( len );
46 if (path)
48 if (prefix) strcpy( path, prefix );
49 else
51 strcpy( path, home );
52 strcat( path, "/.wine" );
54 strcat( path, "/dosdevices/" );
55 strcat( path, dev );
57 return path;
60 static BOOL is_valid_device( struct stat *st )
62 #if defined(linux) || defined(__sun__)
63 return S_ISBLK( st->st_mode );
64 #else
65 /* disks are char devices on *BSD */
66 return S_ISCHR( st->st_mode );
67 #endif
70 static void detect_devices( const char **paths, char *names, ULONG size )
72 while (*paths)
74 char unix_path[32];
75 unsigned int i = 0;
77 for (;;)
79 int len = sprintf( unix_path, *paths, i++ );
80 if (len + 2 > size) break;
81 if (access( unix_path, F_OK ) != 0) break;
82 strcpy( names, unix_path );
83 names += len + 1;
84 size -= len + 1;
86 paths++;
88 *names = 0;
91 void queue_device_op( enum device_op op, const char *udi, const char *device,
92 const char *mount_point, enum device_type type, const GUID *guid,
93 const char *serial, const struct scsi_info *scsi_info )
95 struct device_info *info;
96 char *str, *end;
98 info = calloc( 1, sizeof(*info) );
99 str = info->str_buffer;
100 end = info->str_buffer + sizeof(info->str_buffer);
101 info->op = op;
102 info->type = type;
103 #define ADD_STR(s) if (s && str + strlen(s) + 1 <= end) \
105 info->s = strcpy( str, s ); \
106 str += strlen(str) + 1; \
108 ADD_STR(udi);
109 ADD_STR(device);
110 ADD_STR(mount_point);
111 ADD_STR(serial);
112 #undef ADD_STR
113 if (guid)
115 info->guid_buffer = *guid;
116 info->guid = &info->guid_buffer;
118 if (scsi_info)
120 info->scsi_buffer = *scsi_info;
121 info->scsi_info = &info->scsi_buffer;
123 NtQueueApcThread( run_loop_params.op_thread, run_loop_params.op_apc, (ULONG_PTR)info, 0, 0 );
126 static NTSTATUS run_loop( void *args )
128 const struct run_loop_params *params = args;
130 run_loop_params = *params;
131 run_dbus_loop();
132 return STATUS_SUCCESS;
135 static NTSTATUS dequeue_device_op( void *args )
137 const struct dequeue_device_op_params *params = args;
138 struct device_info *src = (struct device_info *)params->arg;
139 struct device_info *dst = params->info;
141 /* copy info to client address space and fix up pointers */
142 *dst = *src;
143 if (dst->udi) dst->udi = (char *)dst + (src->udi - (char *)src);
144 if (dst->device) dst->device = (char *)dst + (src->device - (char *)src);
145 if (dst->mount_point) dst->mount_point = (char *)dst + (src->mount_point - (char *)src);
146 if (dst->serial) dst->serial = (char *)dst + (src->serial - (char *)src);
147 if (dst->guid) dst->guid = &dst->guid_buffer;
148 if (dst->scsi_info) dst->scsi_info = &dst->scsi_buffer;
150 free( src );
151 return STATUS_SUCCESS;
154 /* find or create a DOS drive for the corresponding Unix device */
155 static NTSTATUS add_drive( void *args )
157 const struct add_drive_params *params = args;
158 char *path, *p;
159 char in_use[26];
160 struct stat dev_st, drive_st;
161 int drive, first, last, avail = 0;
163 if (stat( params->device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return STATUS_NO_SUCH_DEVICE;
165 if (!(path = get_dosdevices_path( "a::" ))) return STATUS_NO_MEMORY;
166 p = path + strlen(path) - 3;
168 memset( in_use, 0, sizeof(in_use) );
170 switch (params->type)
172 case DEVICE_FLOPPY:
173 first = 0;
174 last = 2;
175 break;
176 case DEVICE_CDROM:
177 case DEVICE_DVD:
178 first = 3;
179 last = 26;
180 break;
181 default:
182 first = 2;
183 last = 26;
184 break;
187 while (avail != -1)
189 avail = -1;
190 for (drive = first; drive < last; drive++)
192 if (in_use[drive]) continue; /* already checked */
193 *p = 'a' + drive;
194 if (stat( path, &drive_st ) == -1)
196 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
198 if (avail == -1)
200 p[2] = 0;
201 /* if mount point symlink doesn't exist either, it's available */
202 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
203 p[2] = ':';
206 else in_use[drive] = 1;
208 else
210 in_use[drive] = 1;
211 if (!is_valid_device( &drive_st )) continue;
212 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
215 if (avail != -1)
217 /* try to use the one we found */
218 drive = avail;
219 *p = 'a' + drive;
220 if (symlink( params->device, path ) != -1) goto done;
221 /* failed, retry the search */
224 free( path );
225 return STATUS_OBJECT_NAME_COLLISION;
227 done:
228 free( path );
229 *params->letter = drive;
230 return STATUS_SUCCESS;
233 static NTSTATUS get_dosdev_symlink( void *args )
235 const struct get_dosdev_symlink_params *params = args;
236 char *path;
237 int ret;
239 if (!(path = get_dosdevices_path( params->dev ))) return STATUS_NO_MEMORY;
241 ret = readlink( path, params->dest, params->size );
242 free( path );
243 if (ret == -1) return STATUS_NO_SUCH_DEVICE;
244 if (ret == params->size) return STATUS_BUFFER_TOO_SMALL;
245 params->dest[ret] = 0;
246 return STATUS_SUCCESS;
249 static NTSTATUS set_dosdev_symlink( void *args )
251 const struct set_dosdev_symlink_params *params = args;
252 char *path;
253 NTSTATUS status = STATUS_SUCCESS;
255 if (!(path = get_dosdevices_path( params->dev ))) return STATUS_NO_MEMORY;
257 if (params->dest && params->dest[0])
259 unlink( path );
260 if (symlink( params->dest, path ) == -1) status = STATUS_ACCESS_DENIED;
262 else unlink( path );
264 free( path );
265 return status;
268 static NTSTATUS get_volume_dos_devices( void *args )
270 const struct get_volume_dos_devices_params *params = args;
271 struct stat dev_st, drive_st;
272 char *path;
273 int i;
275 if (stat( params->mount_point, &dev_st ) == -1) return STATUS_NO_SUCH_DEVICE;
276 if (!(path = get_dosdevices_path( "a:" ))) return STATUS_NO_MEMORY;
278 *params->dosdev = 0;
279 for (i = 0; i < 26; i++)
281 path[strlen(path) - 2] = 'a' + i;
282 if (stat( path, &drive_st ) != -1 && drive_st.st_rdev == dev_st.st_rdev) *params->dosdev |= 1 << i;
284 free( path );
285 return STATUS_SUCCESS;
288 static NTSTATUS read_volume_file( void *args )
290 const struct read_volume_file_params *params = args;
291 int ret, fd = -1;
292 char *name = malloc( strlen(params->volume) + strlen(params->file) + 2 );
294 sprintf( name, "%s/%s", params->volume, params->file );
296 if (name[0] != '/')
298 char *path = get_dosdevices_path( name );
299 if (path) fd = open( path, O_RDONLY );
300 free( path );
302 else fd = open( name, O_RDONLY );
304 free( name );
305 if (fd == -1) return STATUS_NO_SUCH_FILE;
306 ret = read( fd, params->buffer, *params->size );
307 close( fd );
308 if (ret == -1) return STATUS_NO_SUCH_FILE;
309 *params->size = ret;
310 return STATUS_SUCCESS;
313 static NTSTATUS match_unixdev( void *args )
315 const struct match_unixdev_params *params = args;
316 struct stat st;
318 return !stat( params->device, &st ) && st.st_rdev == params->unix_dev;
321 static NTSTATUS detect_serial_ports( void *args )
323 const struct detect_ports_params *params = args;
324 static const char *paths[] =
326 #ifdef linux
327 "/dev/ttyS%u",
328 "/dev/ttyUSB%u",
329 "/dev/ttyACM%u",
330 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
331 "/dev/cuau%u",
332 #elif defined(__DragonFly__)
333 "/dev/cuaa%u",
334 #endif
335 NULL
338 detect_devices( paths, params->names, params->size );
339 return STATUS_SUCCESS;
342 static NTSTATUS detect_parallel_ports( void *args )
344 const struct detect_ports_params *params = args;
345 static const char *paths[] =
347 #ifdef linux
348 "/dev/lp%u",
349 #endif
350 NULL
353 detect_devices( paths, params->names, params->size );
354 return STATUS_SUCCESS;
357 static NTSTATUS set_shell_folder( void *args )
359 const struct set_shell_folder_params *params = args;
360 const char *folder = params->folder;
361 const char *backup = params->backup;
362 const char *link = params->link;
363 struct stat st;
364 const char *home;
365 char *homelink = NULL;
366 NTSTATUS status = STATUS_SUCCESS;
368 if (link && (!strcmp( link, "$HOME" ) || !strncmp( link, "$HOME/", 6 )) && (home = getenv( "HOME" )))
370 link += 5;
371 homelink = malloc( strlen(home) + strlen(link) + 1 );
372 strcpy( homelink, home );
373 strcat( homelink, link );
374 link = homelink;
377 /* ignore nonexistent link targets */
378 if (link && (stat( link, &st ) || !S_ISDIR( st.st_mode )))
380 status = STATUS_OBJECT_NAME_NOT_FOUND;
381 goto done;
384 if (!lstat( folder, &st )) /* move old folder/link out of the way */
386 if (S_ISLNK( st.st_mode ))
388 unlink( folder );
390 else if (link && S_ISDIR( st.st_mode ))
392 if (rmdir( folder )) /* non-empty dir, try to make a backup */
394 if (!backup || rename( folder, backup ))
396 status = STATUS_OBJECT_NAME_COLLISION;
397 goto done;
401 else goto done; /* nothing to do, folder already exists */
404 if (link) symlink( link, folder );
405 else
407 if (backup && !lstat( backup, &st ) && S_ISDIR( st.st_mode )) rename( backup, folder );
408 else mkdir( folder, 0777 );
411 done:
412 free( homelink );
413 return status;
416 static NTSTATUS get_shell_folder( void *args )
418 const struct get_shell_folder_params *params = args;
419 int ret = readlink( params->folder, params->buffer, params->size - 1 );
421 if (ret < 0) return STATUS_OBJECT_NAME_NOT_FOUND;
422 params->buffer[ret] = 0;
423 return STATUS_SUCCESS;
426 const unixlib_entry_t __wine_unix_call_funcs[] =
428 run_loop,
429 dequeue_device_op,
430 add_drive,
431 get_dosdev_symlink,
432 set_dosdev_symlink,
433 get_volume_dos_devices,
434 read_volume_file,
435 match_unixdev,
436 detect_serial_ports,
437 detect_parallel_ports,
438 set_shell_folder,
439 get_shell_folder,
440 dhcp_request,