Add checks for unavailable udev functions
[connman] / src / udev.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdarg.h>
27 #include <sys/types.h>
28
29 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
30 #include <libudev.h>
31
32 #include <glib.h>
33
34 #include "connman.h"
35
36 #ifdef NEED_UDEV_MONITOR_RECEIVE_DEVICE
37 static struct udev_device *udev_monitor_receive_device(struct udev_monitor *monitor);
38 {
39         return udev_monitor_get_device(monitor);
40 }
41 #endif
42
43 #ifdef NEED_UDEV_DEVICE_GET_ACTION
44 static const char *udev_device_get_action(struct udev_device *device)
45 {
46         return NULL;
47 }
48 #endif
49
50 static gboolean udev_event(GIOChannel *channel,
51                                 GIOCondition condition, gpointer user_data)
52 {
53         struct udev_monitor *monitor = user_data;
54         struct udev_device *device;
55         const char *action;
56
57         device = udev_monitor_receive_device(monitor);
58         if (device == NULL)
59                 return TRUE;
60
61         action = udev_device_get_action(device);
62         if (action == NULL)
63                 goto done;
64
65         connman_debug("=== %s ===", action);
66
67 done:
68         udev_device_unref(device);
69
70         return TRUE;
71 }
72
73 static struct udev *udev_ctx;
74 static struct udev_monitor *udev_mon;
75 static guint udev_watch = 0;
76
77 int __connman_udev_init(void)
78 {
79         GIOChannel *channel;
80         int fd;
81
82         DBG("");
83
84         udev_ctx = udev_new();
85         if (udev_ctx == NULL) {
86                 connman_error("Failed to create udev context");
87                 return -1;
88         }
89
90         udev_mon = udev_monitor_new_from_socket(udev_ctx,
91                                                 "@/org/moblin/connman/udev");
92         if (udev_mon == NULL) {
93                 connman_error("Failed to create udev monitor");
94                 udev_unref(udev_ctx);
95                 udev_ctx = NULL;
96                 return -1;
97         }
98
99         if (udev_monitor_enable_receiving(udev_mon) < 0) {
100                 connman_error("Failed to enable udev monitor");
101                 udev_unref(udev_ctx);
102                 udev_ctx = NULL;
103                 udev_monitor_unref(udev_mon);
104                 return -1;
105         }
106
107         fd = udev_monitor_get_fd(udev_mon);
108
109         channel = g_io_channel_unix_new(fd);
110         if (channel == NULL)
111                 return 0;
112
113         udev_watch = g_io_add_watch(channel, G_IO_IN, udev_event, udev_mon);
114
115         g_io_channel_unref(channel);
116
117         return 0;
118 }
119
120 void __connman_udev_cleanup(void)
121 {
122         DBG("");
123
124         if (udev_watch > 0)
125                 g_source_remove(udev_watch);
126
127         if (udev_ctx == NULL)
128                 return;
129
130         udev_monitor_unref(udev_mon);
131         udev_unref(udev_ctx);
132 }