timeserver: Trigger ntp on default changed notifier
[platform/upstream/connman.git] / src / timeserver.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 <glib.h>
29 #include <stdlib.h>
30 #include <gweb/gresolv.h>
31
32 #include "connman.h"
33
34 static char **system_timeservers = NULL;
35 static char **timeservers = NULL;
36
37 static GResolv *resolv = NULL;
38 static int resolv_id = 0;
39 static volatile int count;
40
41 static void resolv_debug(const char *str, void *data)
42 {
43         connman_info("%s: %s\n", (const char *) data, str);
44 }
45 static void save_timeservers(char **servers)
46 {
47         GKeyFile *keyfile;
48         int cnt;
49
50         keyfile = __connman_storage_load_global();
51         if (keyfile == NULL)
52                 keyfile = g_key_file_new();
53
54         for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++);
55
56         g_key_file_set_string_list(keyfile, "global", "Timeservers",
57                            (const gchar **)servers, cnt);
58
59         __connman_storage_save_global(keyfile);
60
61         g_key_file_free(keyfile);
62
63         return;
64 }
65
66 static char **load_timeservers()
67 {
68         GKeyFile *keyfile;
69         GError *error = NULL;
70         char **servers = NULL;
71
72         keyfile = __connman_storage_load_global();
73         if (keyfile == NULL)
74                 return NULL;
75
76         servers = g_key_file_get_string_list(keyfile, "global",
77                                                 "Timeservers", NULL, &error);
78         if (error) {
79                 DBG("Error loading timeservers: %s", error->message);
80                 g_error_free(error);
81         }
82
83         g_key_file_free(keyfile);
84
85         return servers;
86 }
87
88 /* Restart NTP procedure */
89 static void connman_timeserver_restart()
90 {
91         /* If service timeservers are in use, dont restart ntp */
92         if (timeservers != NULL)
93                 return;
94
95         if (resolv == NULL) {
96                 DBG("No online service.");
97                 return;
98         }
99
100         /* Cancel current lookup */
101         if(resolv_id > 0)
102                 g_resolv_cancel_lookup(resolv, resolv_id);
103
104         /* Reload system timeserver list */
105         if (system_timeservers != NULL) {
106                 g_strfreev(system_timeservers);
107                 system_timeservers = NULL;
108         }
109
110         system_timeservers = load_timeservers();
111
112         if (system_timeservers == NULL)
113                 return;
114
115         __connman_ntp_stop();
116
117         count = 0;
118
119         __connman_timeserver_sync_next();
120 }
121
122 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
123 {
124         int i;
125
126         DBG("status %d", status);
127
128         __sync_fetch_and_add(&count, 1);
129
130         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
131                 if (results != NULL) {
132                         for (i = 0; results[i]; i++)
133                                 DBG("result: %s", results[i]);
134
135                         __connman_ntp_start(results[0]);
136
137                         return;
138                 }
139         }
140
141         __connman_timeserver_sync_next();
142 }
143
144 void __connman_timeserver_sync_next()
145 {
146         if (system_timeservers == NULL ||
147         system_timeservers[count] == NULL)
148                 return;
149
150         DBG("Trying timeserver %s", system_timeservers[count]);
151
152         if (resolv)
153                 resolv_id = g_resolv_lookup_hostname(resolv,
154                         system_timeservers[count], resolv_result,
155                                                 NULL);
156 }
157
158 int __connman_timeserver_sync(struct connman_service *default_service)
159 {
160         struct connman_service *service;
161         char **nameservers;
162         int i;
163
164         if (default_service != NULL)
165                 service = default_service;
166         else
167                 service = __connman_service_get_default();
168
169         if (service == NULL)
170                 return -EINVAL;
171
172         DBG("service %p", service);
173
174         i = __connman_service_get_index(service);
175         if (i < 0)
176                 return -EINVAL;
177
178         nameservers = connman_service_get_nameservers(service);
179         if (nameservers == NULL)
180                 return -EINVAL;
181
182         resolv = g_resolv_new(i);
183         if (resolv == NULL)
184                 return -ENOMEM;
185
186         if (getenv("CONNMAN_RESOLV_DEBUG"))
187                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
188
189         for (i = 0; nameservers[i] != NULL; i++)
190                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
191
192         count = 0;
193
194         system_timeservers = load_timeservers();
195
196         timeservers = connman_service_get_timeservers(service);
197
198         if (timeservers != NULL && timeservers[0] != NULL) {
199                 DBG("Using service tiemservers");
200                 __connman_ntp_start(timeservers[0]);
201                 return 0;
202         }
203
204         if (system_timeservers == NULL || system_timeservers[count] == NULL) {
205                 DBG("No timeservers set.");
206                 return 0;
207         }
208
209         DBG("Trying server %s", system_timeservers[count]);
210
211         resolv_id = g_resolv_lookup_hostname(resolv, system_timeservers[count],
212                                                 resolv_result, NULL);
213         return 0;
214 }
215
216 void __connman_timeserver_stop()
217 {
218         DBG(" ");
219
220         if (resolv != NULL) {
221                 g_resolv_unref(resolv);
222                 resolv = NULL;
223         }
224
225         if (system_timeservers != NULL) {
226                 g_strfreev(system_timeservers);
227                 system_timeservers = NULL;
228         }
229
230         timeservers = NULL;
231
232         count = 0;
233
234         __connman_ntp_stop();
235 }
236
237 int __connman_timeserver_system_set(char **servers)
238 {
239         save_timeservers(servers);
240
241         connman_timeserver_restart();
242
243         return 0;
244 }
245
246 char **__connman_timeserver_system_get()
247 {
248         char **servers;
249
250         servers = load_timeservers();
251         return servers;
252 }
253
254 static void default_changed(struct connman_service *default_service)
255 {
256         DBG("");
257
258         if (default_service != NULL)
259                 __connman_timeserver_sync(default_service);
260         else
261                 __connman_timeserver_stop();
262 }
263
264 static struct connman_notifier timeserver_notifier = {
265         .name                   = "timeserver",
266         .default_changed        = default_changed,
267 };
268
269 int __connman_timeserver_init(void)
270 {
271         DBG("");
272
273         connman_notifier_register(&timeserver_notifier);
274
275         return 0;
276 }
277
278 void __connman_timeserver_cleanup(void)
279 {
280         DBG("");
281
282         connman_notifier_unregister(&timeserver_notifier);
283 }