[go: up one dir, main page]

Menu

[r59]: / trunk / udev.c  Maximize  Restore  History

Download this file

119 lines (105 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* $Id$
*
* scanbd - KMUX scanner button daemon
*
* Copyright (C) 2008 Wilhelm Meier (wilhelm.meier@fh-kl.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "udev.h"
#ifdef USE_LIBUDEV
static pthread_t udev_tid = 0;
static void* udev_thread(void* arg)
{
slog(SLOG_DEBUG, "udev thread started");
(void)arg;
// we only expect the main thread to handle signals
sigset_t mask;
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
struct udev* udev = udev_new();
if (!udev) {
slog(SLOG_DEBUG, "can't create udev");
return NULL;
}
struct udev_monitor* mon = udev_monitor_new_from_netlink(udev, "udev");
if (!mon) {
slog(SLOG_DEBUG, "Can't contact udev");
return NULL;
}
// udev_monitor_filter_add_match_subsystem_devtype(mon, "hidraw", NULL);
if (udev_monitor_enable_receiving(mon) < 0) {
slog(SLOG_DEBUG, "Can't enable udev receiving");
return NULL;
}
while(true) {
struct udev_device* device = udev_monitor_receive_device(mon);
if (!device) {
slog(SLOG_WARN, "no device from udev");
}
else {
assert(device);
slog(SLOG_INFO, "new devive");
const char* s = 0;
s = udev_device_get_devtype(device);
if (s) {
slog(SLOG_INFO, "udev device type: %s", s);
if (strcmp(s, UDEV_DEVICE_TYPE) == 0) {
s = udev_device_get_action(device);
if (s) {
slog(SLOG_INFO, "udev device action: %s", s);
if (strcmp(s, UDEV_ADD_ACTION) == 0) {
dbus_signal_device_added();
}
if (strcmp(s, UDEV_REMOVE_ACTION) == 0) {
dbus_signal_device_removed();
}
}
}
}
udev_device_unref(device);
}
}
return NULL;
}
void udev_start_udev_thread(void)
{
slog(SLOG_DEBUG, "start udev thread");
if (pthread_create(&udev_tid, NULL, udev_thread, NULL) < 0){
slog(SLOG_ERROR, "Can't create udev thread: %s", strerror(errno));
return;
}
}
void udev_stop_udev_thread(void) {
slog(SLOG_DEBUG, "stop udev thread");
if (udev_tid == 0) {
slog(SLOG_DEBUG, "no udev thread to stop");
return;
}
if (pthread_cancel(udev_tid) < 0) {
if (errno == ESRCH) {
slog(SLOG_WARN, "udev thread was already cancelled");
}
else {
slog(SLOG_WARN, "unknown error from pthread_cancel: %s", strerror(errno));
}
}
if (pthread_join(udev_tid, NULL) < 0) {
slog(SLOG_ERROR, "pthread_join: %s", strerror(errno));
}
udev_tid = 0;
}
#endif