1/*
2 * Copyright 2014 by VMware, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#ifdef HAVE_LIBUDEV
33#include <libudev.h>
34#include <stdlib.h>
35#include <string.h>
36
37#define KERNEL_DEVNAME "VirtualPS/2 VMware VMMouse"
38
39/**
40 * vmmouse_uses_kernel_driver - Check whether there's an active
41 *    vmmouse driver in the kernel.
42 *
43 * Returns 0 if there was no kernel driver found.
44 * Returns non-zero on error or if there was an active driver found.
45 *
46 * Scans the input subsystem for devices matching KERNEL_DEVNAME. These
47 * devices are assumed to be active vmmouse drivers.
48 */
49int vmmouse_uses_kernel_driver(void)
50{
51    struct udev *udev;
52    struct udev_enumerate *enumerate;
53    struct udev_list_entry *devices, *dev_list_entry;
54    struct udev_device *dev;
55
56    udev = udev_new();
57    if (!udev)
58	return 1;
59
60    /*
61     * Udev error return codes that are not caught immediately are
62     * typically caught in the input argument check in the udev
63     * function calls following the failing call!
64     */
65    enumerate = udev_enumerate_new(udev);
66    if (udev_enumerate_add_match_subsystem(enumerate, "input"))
67	goto out_err;
68    if (udev_enumerate_scan_devices(enumerate))
69	goto out_err;
70
71    devices = udev_enumerate_get_list_entry(enumerate);
72    udev_list_entry_foreach(dev_list_entry, devices) {
73	const char *path, *name;
74
75	path = udev_list_entry_get_name(dev_list_entry);
76	dev = udev_device_new_from_syspath(udev, path);
77	if (!dev)
78	    goto out_err;
79	name = udev_device_get_sysattr_value(dev, "name");
80	if (name && !strcasecmp(name, KERNEL_DEVNAME))
81	    goto out_found;
82
83	udev_device_unref(dev);
84    }
85
86    udev_enumerate_unref(enumerate);
87    udev_unref(udev);
88
89    return 0;
90
91  out_found:
92    udev_device_unref(dev);
93  out_err:
94    udev_enumerate_unref(enumerate);
95    udev_unref(udev);
96
97    return 1;
98}
99#else
100int vmmouse_uses_kernel_driver(void)
101{
102   return 0;
103}
104#endif
105