main: Mark end of array with NULL in order not to crash
[platform/upstream/connman.git] / src / counter.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <errno.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection;
33
34 static GHashTable *counter_table;
35 static GHashTable *owner_mapping;
36
37 struct connman_counter {
38         char *owner;
39         char *path;
40         unsigned int interval;
41         guint watch;
42 };
43
44 static void remove_counter(gpointer user_data)
45 {
46         struct connman_counter *counter = user_data;
47
48         DBG("owner %s path %s", counter->owner, counter->path);
49
50         __connman_rtnl_update_interval_remove(counter->interval);
51
52         __connman_service_counter_unregister(counter->path);
53
54         g_free(counter->owner);
55         g_free(counter->path);
56         g_free(counter);
57 }
58
59 static void owner_disconnect(DBusConnection *connection, void *user_data)
60 {
61         struct connman_counter *counter = user_data;
62
63         DBG("owner %s path %s", counter->owner, counter->path);
64
65         g_hash_table_remove(owner_mapping, counter->owner);
66         g_hash_table_remove(counter_table, counter->path);
67 }
68
69 int __connman_counter_register(const char *owner, const char *path,
70                                                 unsigned int interval)
71 {
72         struct connman_counter *counter;
73         int err;
74
75         DBG("owner %s path %s interval %u", owner, path, interval);
76
77         counter = g_hash_table_lookup(counter_table, path);
78         if (counter != NULL)
79                 return -EEXIST;
80
81         counter = g_try_new0(struct connman_counter, 1);
82         if (counter == NULL)
83                 return -ENOMEM;
84
85         counter->owner = g_strdup(owner);
86         counter->path = g_strdup(path);
87
88         err = __connman_service_counter_register(counter->path);
89         if (err < 0) {
90                 g_free(counter->owner);
91                 g_free(counter->path);
92                 g_free(counter);
93                 return err;
94         }
95
96         g_hash_table_replace(counter_table, counter->path, counter);
97         g_hash_table_replace(owner_mapping, counter->owner, counter);
98
99         counter->interval = interval;
100         __connman_rtnl_update_interval_add(counter->interval);
101
102         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
103                                         owner_disconnect, counter, NULL);
104
105         return 0;
106 }
107
108 int __connman_counter_unregister(const char *owner, const char *path)
109 {
110         struct connman_counter *counter;
111
112         DBG("owner %s path %s", owner, path);
113
114         counter = g_hash_table_lookup(counter_table, path);
115         if (counter == NULL)
116                 return -ESRCH;
117
118         if (g_strcmp0(owner, counter->owner) != 0)
119                 return -EACCES;
120
121         if (counter->watch > 0)
122                 g_dbus_remove_watch(connection, counter->watch);
123
124         g_hash_table_remove(owner_mapping, counter->owner);
125         g_hash_table_remove(counter_table, counter->path);
126
127         return 0;
128 }
129
130 void __connman_counter_send_usage(const char *path,
131                                         DBusMessage *message)
132 {
133         struct connman_counter *counter;
134
135         counter = g_hash_table_lookup(counter_table, path);
136         if (counter == NULL)
137                 return;
138
139         dbus_message_set_destination(message, counter->owner);
140         dbus_message_set_path(message, counter->path);
141         dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
142         dbus_message_set_member(message, "Usage");
143         dbus_message_set_no_reply(message, TRUE);
144
145         g_dbus_send_message(connection, message);
146 }
147
148 static void release_counter(gpointer key, gpointer value, gpointer user_data)
149 {
150         struct connman_counter *counter = value;
151         DBusMessage *message;
152
153         DBG("owner %s path %s", counter->owner, counter->path);
154
155         if (counter->watch > 0)
156                 g_dbus_remove_watch(connection, counter->watch);
157
158         message = dbus_message_new_method_call(counter->owner, counter->path,
159                                         CONNMAN_COUNTER_INTERFACE, "Release");
160         if (message == NULL)
161                 return;
162
163         dbus_message_set_no_reply(message, TRUE);
164
165         g_dbus_send_message(connection, message);
166 }
167
168 int __connman_counter_init(void)
169 {
170         DBG("");
171
172         connection = connman_dbus_get_connection();
173         if (connection == NULL)
174                 return -1;
175
176         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
177                                                         NULL, remove_counter);
178         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
179                                                                 NULL, NULL);
180
181         return 0;
182 }
183
184 void __connman_counter_cleanup(void)
185 {
186         DBG("");
187
188         if (connection == NULL)
189                 return;
190
191         g_hash_table_foreach(counter_table, release_counter, NULL);
192
193         g_hash_table_destroy(owner_mapping);
194         g_hash_table_destroy(counter_table);
195
196         dbus_connection_unref(connection);
197 }