diff --git a/channels/rdpdr/client/rdpdr_main.c b/channels/rdpdr/client/rdpdr_main.c index d69ad4cd7..50c5a929e 100644 --- a/channels/rdpdr/client/rdpdr_main.c +++ b/channels/rdpdr/client/rdpdr_main.c @@ -580,18 +580,6 @@ static DWORD WINAPI drive_hotplug_thread_func(LPVOID arg) #else -#ifdef __sun -#include -#else -#include -#endif -#define MAX_USB_DEVICES 100 - -typedef struct _hotplug_dev -{ - char* path; - BOOL to_add; -} hotplug_dev; static const char* automountLocations[] = { "/run/user/%lu/gvfs", "/run/media/%s", "/media/%s", "/media", "/mnt" }; @@ -649,6 +637,110 @@ static BOOL isAutomountLocation(const char* path) return FALSE; } +#define MAX_USB_DEVICES 100 + +typedef struct _hotplug_dev +{ + char* path; + BOOL to_add; +} hotplug_dev; + +static void +handle_mountpoint(hotplug_dev *dev_array, size_t *size, const char *mountpoint) +{ + if (!mountpoint) + return; + /* copy hotpluged device mount point to the dev_array */ + if (isAutomountLocation(mountpoint) && (*size < MAX_USB_DEVICES)) + { + dev_array[*size].path = _strdup(mountpoint); + dev_array[*size + 1].to_add = TRUE; + (*size)++; + } +} + +#ifdef __sun +#include +static UINT +handle_platform_mounts_sun(hotplug_dev *dev_array, size_t *size) +{ + FILE* f; + struct mnttab ent; + f = fopen("/etc/mnttab", "r"); + if (f == NULL) + { + WLog_ERR(TAG, "fopen failed!"); + return ERROR_OPEN_FAILED; + } + while (getmntent(f, &ent) == 0) + { + handle_mountpoint(dev_array, size, ent.mnt_mountp); + } + fclose(f); + return ERROR_SUCCESS; +} +#endif + +#if defined(__FreeBSD__) || defined(__OpenBSD__) +#include +static UINT +handle_platform_mounts_bsd(hotplug_dev *dev_array, size_t *size) +{ + int mntsize; + size_t idx; + struct statfs *mntbuf = NULL; + + mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); + if (!mntsize) + { + /* TODO: handle 'errno' */ + WLog_ERR(TAG, "getmntinfo failed!"); + return ERROR_OPEN_FAILED; + } + for (idx = 0; idx < (size_t)mntsize; idx ++) + { + handle_mountpoint(dev_array, size, mntbuf[idx].f_mntonname); + } + free(mntbuf); + return ERROR_SUCCESS; +} +#endif + +#if defined(__LINUX__) || defined (__linux__) +#include +static UINT +handle_platform_mounts_linux(hotplug_dev *dev_array, size_t *size) +{ + FILE* f; + struct mntent* ent; + f = fopen("/proc/mounts", "r"); + if (f == NULL) + { + WLog_ERR(TAG, "fopen failed!"); + return ERROR_OPEN_FAILED; + } + while ((ent = getmntent(f)) != NULL) + { + handle_mountpoint(dev_array, size, ent->mnt_dir); + } + fclose(f); + return ERROR_SUCCESS; +} +#endif + +static UINT +handle_platform_mounts(hotplug_dev *dev_array, size_t *size) +{ +#ifdef __sun + return handle_platform_mounts_sun(dev_array, size); +#elif defined(__FreeBSD__) || defined(__OpenBSD__) + return handle_platform_mounts_bsd(dev_array, size); +#elif defined(__LINUX__) || defined (__linux__) + return handle_platform_mounts_linux(dev_array, size); +#endif + return ERROR_CALL_NOT_IMPLEMENTED; +} + /** * Function description * @@ -656,56 +748,16 @@ static BOOL isAutomountLocation(const char* path) */ static UINT handle_hotplug(rdpdrPlugin* rdpdr) { - FILE* f; hotplug_dev dev_array[MAX_USB_DEVICES] = { 0 }; size_t i; size_t size = 0; int count, j; -#ifdef __sun - struct mnttab ent; -#else - struct mntent* ent; -#endif ULONG_PTR* keys = NULL; UINT32 ids[1]; - UINT error = 0; + UINT error = ERROR_SUCCESS; -#ifdef __sun - f = fopen("/etc/mnttab", "r"); -#else - f = fopen("/proc/mounts", "r"); -#endif + error = handle_platform_mounts(dev_array, &size); - if (f == NULL) - { - WLog_ERR(TAG, "fopen failed!"); - return ERROR_OPEN_FAILED; - } - -#ifdef __sun - while (getmntent(f, &ent) == 0) -#else - while ((ent = getmntent(f)) != NULL) -#endif - { - /* Copy the line, path must obviously be shorter */ -#ifdef __sun - const char* path = ent.mnt_mountp; -#else - const char* path = ent->mnt_dir; -#endif - - if (!path) - continue; - /* copy hotpluged device mount point to the dev_array */ - if (isAutomountLocation(path) && (size < MAX_USB_DEVICES)) - { - dev_array[size].path = _strdup(path); - dev_array[size++].to_add = TRUE; - } - } - - fclose(f); /* delete removed devices */ count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);