~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to src/uevent.c

  • Committer: Balint Reczey
  • Date: 2020-06-11 18:46:02 UTC
  • Revision ID: balint.reczey@canonical.com-20200611184602-2rv1zan3xu723x2u
Moved to git at https://git.launchpad.net/update-notifier

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifdef HAVE_CONFIG_H
2
 
#include "config.h"
3
 
#endif
4
 
#ifdef HAVE_GUDEV
5
 
#include <sys/wait.h>
6
 
 
7
 
#include <ctype.h>
8
 
 
9
 
#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
10
 
#include <gudev/gudev.h>
11
 
#include <sys/utsname.h>
12
 
 
13
 
gchar *hplip_helper[] = { "/usr/bin/hp-plugin-ubuntu", NULL };
14
 
 
15
 
static inline void
16
 
g_debug_uevent(const char *msg, ...)
17
 
{
18
 
   va_list va;
19
 
   va_start(va, msg);
20
 
   g_logv("uevent",G_LOG_LEVEL_DEBUG, msg, va);
21
 
   va_end(va);
22
 
}
23
 
 
24
 
static gboolean scp_checked = FALSE;
25
 
 
26
 
static gboolean
27
 
deal_with_scp(GUdevDevice *device)
28
 
{
29
 
    GError *error = NULL;
30
 
 
31
 
    /* only do this once */
32
 
    if (scp_checked)
33
 
        return FALSE;
34
 
 
35
 
    /* check if we just added a printer */
36
 
    if ((g_strcmp0(g_udev_device_get_sysfs_attr(device, "bInterfaceClass"), "07") != 0 ||
37
 
                g_strcmp0(g_udev_device_get_sysfs_attr(device, "bInterfaceSubClass"), "01") != 0) &&
38
 
            !g_str_has_prefix(g_udev_device_get_name (device), "lp")) {
39
 
        g_debug_uevent ("deal_with_scp: devpath=%s: not a printer", g_udev_device_get_sysfs_path(device));
40
 
        return FALSE;
41
 
    }
42
 
 
43
 
    g_debug_uevent ("deal_with_scp: devpath=%s: printer identified", g_udev_device_get_sysfs_path(device));
44
 
 
45
 
    scp_checked = TRUE;
46
 
 
47
 
    gchar* ps_argv[] = {"ps", "h", "-ocommand", "-Cpython", NULL};
48
 
    gchar* ps_out;
49
 
    if (!g_spawn_sync (NULL, ps_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
50
 
                &ps_out, NULL, NULL, &error)) {
51
 
        g_warning ("deal_with_scp: error calling ps: %s", error->message);
52
 
        g_error_free (error);
53
 
        return TRUE;
54
 
    }
55
 
    g_debug_uevent ("deal_with_scp: running python processes:%s", ps_out);
56
 
    if (strstr (ps_out, "system-config-printer") != NULL) {
57
 
        g_debug_uevent ("deal_with_scp: system-config-printer already running");
58
 
        return TRUE;
59
 
    }
60
 
 
61
 
    g_debug_uevent ("deal_with_scp: launching system-config-printer");
62
 
    gchar* scp_argv[] = {"system-config-printer-applet", NULL};
63
 
    error = NULL;
64
 
    if (!g_spawn_async(NULL, scp_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error)) {
65
 
        g_warning("%s could not be called: %s", scp_argv[0], error->message);
66
 
        g_error_free (error);
67
 
    }
68
 
    return TRUE;
69
 
}
70
 
 
71
 
static void
72
 
on_uevent (GUdevClient *client,
73
 
           gchar *action,
74
 
           GUdevDevice *device,
75
 
           gpointer user_data)
76
 
{
77
 
    g_debug_uevent ("uevent.c on_uevent: action=%s, devpath=%s", action, g_udev_device_get_sysfs_path(device));
78
 
 
79
 
    if (g_strcmp0 (action, "add") != 0 && g_strcmp0 (action, "change") != 0)
80
 
        return;
81
 
 
82
 
    if (deal_with_scp(device))
83
 
        return;
84
 
}
85
 
 
86
 
void
87
 
uevent_init(void)
88
 
{
89
 
    const gchar* subsystems[] = {"firmware", "usb", NULL};
90
 
 
91
 
    struct utsname u;
92
 
    if (uname (&u) != 0) {
93
 
        g_warning("uname() failed, not monitoring devices");
94
 
        return;
95
 
    }
96
 
 
97
 
    GUdevClient* gudev = g_udev_client_new (subsystems);
98
 
    g_signal_connect (gudev, "uevent", G_CALLBACK (on_uevent), NULL);
99
 
 
100
 
    /* cold plug HPLIP devices */
101
 
    GList *usb_devices, *elem;
102
 
    usb_devices = g_udev_client_query_by_subsystem (gudev, "usb");
103
 
    for (elem = usb_devices; elem != NULL; elem = g_list_next(elem)) {
104
 
       deal_with_scp(elem->data);
105
 
       g_object_unref(elem->data);
106
 
    }
107
 
    g_list_free(usb_devices);
108
 
}
109
 
#else
110
 
#include <glib.h>
111
 
void
112
 
uevent_init(void)
113
 
{
114
 
    g_warning("Printer monitoring disabled.");
115
 
}
116
 
#endif // HAVE_GUDEV