tstHostHardwareLinux.cpp revision 531c00db9f8998c8b606845f72098698c48e6290
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/* $Id$ */
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/** @file
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Test executable for quickly excercising/debugging the Linux host hardware
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * bits.
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync */
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync/*
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Copyright (C) 2008 Oracle Corporation
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync *
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * available from http://www.virtualbox.org. This file is free software;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * General Public License (GPL) as published by the Free Software
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync */
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <HostHardwareLinux.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <VBox/err.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/initterm.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/param.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/stream.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/thread.h>
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync#include <iprt/linux/sysfs.h>
1e2bc03fd1fc133bd3a066b1557471e157df78f6vboxsync
5bcfdf9ef0306239498361e5021d008ad77bf539vboxsync#include <iprt/cdefs.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <iprt/types.h>
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync
c312e1b81dffe42e0fb766020fb8defaeade05d6vboxsync#include <errno.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <string.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#include <stdlib.h>
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncint doHotplugEvent(VBoxMainHotplugWaiter *waiter, RTMSINTERVAL cMillies)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync{
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync int rc;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync while (true)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync rc = waiter->Wait (cMillies);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync break;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_FAILURE(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf("Failed!\n");
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync exit(1);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync }
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_SUCCESS(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync break;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync }
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync return rc;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync}
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync
b0dfb334954c0552bb583967a3077ec88fd00471vboxsyncint main()
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync{
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTR3Init();
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync int rc = VINF_SUCCESS;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync VBoxMainDriveInfo driveInfo;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc = driveInfo.updateFloppies();
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_SUCCESS(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc = driveInfo.updateDVDs();
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_FAILURE(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf("Failed to update the host drive information, error %Rrc\n",
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync return 1;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync }
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf ("Listing floppy drives detected:\n");
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync for (DriveInfoList::const_iterator it = driveInfo.FloppyBegin();
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync it != driveInfo.FloppyEnd(); ++it)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync RTPrintf (" device: %s", it->mDevice.c_str());
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync if (!it->mUdi.isEmpty())
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync RTPrintf (", udi: %s", it->mUdi.c_str());
54828795a553ed0731f308ebda81675ad2c39d58vboxsync if (!it->mDescription.isEmpty())
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf (", description: %s", it->mDescription.c_str());
54828795a553ed0731f308ebda81675ad2c39d58vboxsync RTPrintf ("\n");
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync }
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf ("Listing DVD drives detected:\n");
54828795a553ed0731f308ebda81675ad2c39d58vboxsync for (DriveInfoList::const_iterator it = driveInfo.DVDBegin();
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync it != driveInfo.DVDEnd(); ++it)
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf (" device: %s", it->mDevice.c_str());
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (!it->mUdi.isEmpty())
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf (", udi: %s", it->mUdi.c_str());
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync if (!it->mDescription.isEmpty())
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync RTPrintf (", description: %s", it->mDescription.c_str());
50f0e2e83362e100d306a411980d555d46aa00a8vboxsync RTPrintf ("\n");
54828795a553ed0731f308ebda81675ad2c39d58vboxsync }
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync#ifdef VBOX_USB_WITH_SYSFS
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync VBoxMainUSBDeviceInfo deviceInfo;
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync AssertReturn(VBoxMainUSBDevInfoInit(&deviceInfo), 1);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc = USBDevInfoUpdateDevices(&deviceInfo);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync if (RT_FAILURE(rc))
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync {
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync RTPrintf ("Failed to update the host USB device information, error %Rrc\n",
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync rc);
b0dfb334954c0552bb583967a3077ec88fd00471vboxsync return 1;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync }
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync RTPrintf ("Listing USB devices detected:\n");
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync USBDeviceInfoList_iterator it;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync USBDeviceInfoList_iter_init(&it, USBDevInfoBegin(&deviceInfo));
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync for (; !USBDeviceInfoList_iter_eq(&it, USBDevInfoEnd(&deviceInfo));
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync USBDeviceInfoList_iter_incr(&it))
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync {
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync char szProduct[1024];
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync USBDeviceInfo *pInfo = USBDeviceInfoList_iter_target(&it);
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync if (RTLinuxSysFsReadStrFile(szProduct, sizeof(szProduct),
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync "%s/product", pInfo->mSysfsPath) == -1)
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync {
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync if (errno != ENOENT)
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync {
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync RTPrintf ("Failed to get the product name for device %s: error %s\n",
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync pInfo->mDevice, strerror(errno));
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync return 1;
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync }
80523be8dba75b5eb32569fd72ddf54f3b009025vboxsync else
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync szProduct[0] = '\0';
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync }
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync RTPrintf (" device: %s (%s), sysfs path: %s\n", szProduct, pInfo->mDevice,
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync pInfo->mSysfsPath);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync RTPrintf (" interfaces:\n");
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync USBInterfaceList_iterator it2;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync USBInterfaceList_iter_init(&it2, USBInterfaceList_begin(&pInfo->mInterfaces));
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync for (; !USBInterfaceList_iter_eq(&it2, USBInterfaceList_end(&pInfo->mInterfaces));
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync USBInterfaceList_iter_incr(&it2))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync {
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync char szDriver[RTPATH_MAX];
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync char *pszIf = *USBInterfaceList_iter_target(&it2);
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync strcpy(szDriver, "none");
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync ssize_t size = RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver),
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync "%s/driver", pszIf);
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync if (size == -1 && errno != ENOENT)
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync {
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync RTPrintf ("Failed to get the driver for interface %s of device %s: error %s\n",
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync pszIf, pInfo->mDevice, strerror(errno));
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync return 1;
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync }
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync if (RTLinuxSysFsExists("%s/driver", pszIf) != (size != -1))
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync {
b60e4b0625949fd68ed97f1353e2174c5b3192e5vboxsync RTPrintf ("RTLinuxSysFsExists did not return the expected value for the driver link of interface %s of device %s.\n",
b60e4b0625949fd68ed97f1353e2174c5b3192e5vboxsync pszIf, pInfo->mDevice);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync return 1;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync }
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync uint64_t u64InterfaceClass;
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync u64InterfaceClass = RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass",
5d05aa26ae1949e6f0bbc149d8b8e39495710ac7vboxsync pszIf);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync RTPrintf (" sysfs path: %s, driver: %s, interface class: 0x%x\n",
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync pszIf, szDriver, u64InterfaceClass);
8d29e9dc0d280b7b26834132b9ce14a3a845a7fdvboxsync }
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync }
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync VBoxMainHotplugWaiter waiter;
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync RTPrintf ("Waiting for a hotplug event for five seconds...\n");
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync doHotplugEvent(&waiter, 5000);
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync RTPrintf ("Waiting for a hotplug event, Ctrl-C to abort...\n");
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync doHotplugEvent(&waiter, RT_INDEFINITE_WAIT);
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync RTPrintf ("Testing interrupting a hotplug event...\n");
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync waiter.Interrupt();
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync rc = doHotplugEvent(&waiter, 5000);
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync RTPrintf ("%s\n", rc == VERR_INTERRUPTED ? "Success!" : "Failed!");
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync#endif /* VBOX_USB_WITH_SYSFS */
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync return 0;
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync}
64e0c74b525c440a571ce06f3eb6234d75913d76vboxsync
052deaa01d8fcd5cec4dff857833538940b751c3vboxsync