From 2333b442e64bb082de2e22056b9eda52aff4a6c6 Mon Sep 17 00:00:00 2001 From: Alok Barsode Date: Fri, 3 Feb 2012 20:25:55 +0200 Subject: [PATCH] timeserver: Add functions to store/restore timeservers via the clock API --- include/timeserver.h | 3 ++ src/clock.c | 34 ++++-------- src/connman.h | 2 + src/timeserver.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 24 deletions(-) diff --git a/include/timeserver.h b/include/timeserver.h index c5019ed..3749482 100644 --- a/include/timeserver.h +++ b/include/timeserver.h @@ -40,6 +40,9 @@ int connman_timeserver_append(const char *server); int connman_timeserver_remove(const char *server); void connman_timeserver_sync(void); +int __connman_timeserver_system_append(const char *server); +int __connman_timeserver_system_remove(const char *server); + struct connman_timeserver_driver { const char *name; int priority; diff --git a/src/clock.c b/src/clock.c index c98c502..0d7f870 100644 --- a/src/clock.c +++ b/src/clock.c @@ -45,7 +45,6 @@ static enum time_updates time_updates_config = TIME_UPDATES_AUTO; static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO; static char *timezone_config = NULL; -static char **timeservers_config = NULL; static const char *time_updates2string(enum time_updates value) { @@ -98,14 +97,17 @@ static enum timezone_updates string2timezone_updates(const char *value) static void append_timeservers(DBusMessageIter *iter, void *user_data) { int i; + char **timeservers = __connman_timeserver_system_get(); - if (timeservers_config == NULL) + if (timeservers == NULL) return; - for (i = 0; timeservers_config[i] != NULL; i++) { + for (i = 0; timeservers[i] != NULL; i++) { dbus_message_iter_append_basic(iter, - DBUS_TYPE_STRING, ×ervers_config[i]); + DBUS_TYPE_STRING, ×ervers[i]); } + + g_strfreev(timeservers); } static DBusMessage *get_properties(DBusConnection *conn, @@ -251,38 +253,23 @@ static DBusMessage *set_property(DBusConnection *conn, DBUS_TYPE_STRING, &strval); } else if (g_str_equal(name, "Timeservers") == TRUE) { DBusMessageIter entry; - GString *str; if (type != DBUS_TYPE_ARRAY) return __connman_error_invalid_arguments(msg); - str = g_string_new(NULL); - if (str == NULL) - return __connman_error_invalid_arguments(msg); - dbus_message_iter_recurse(&value, &entry); + if (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_INVALID) + __connman_timeserver_system_append(NULL); + while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) { const char *val; dbus_message_iter_get_basic(&entry, &val); + __connman_timeserver_system_append(val); dbus_message_iter_next(&entry); - - if (str->len > 0) - g_string_append_printf(str, " %s", val); - else - g_string_append(str, val); } - g_strfreev(timeservers_config); - - if (str->len > 0) - timeservers_config = g_strsplit_set(str->str, " ", 0); - else - timeservers_config = NULL; - - g_string_free(str, TRUE); - connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE, "Timeservers", DBUS_TYPE_STRING, append_timeservers, NULL); @@ -355,5 +342,4 @@ void __connman_clock_cleanup(void) __connman_timezone_cleanup(); g_free(timezone_config); - g_strfreev(timeservers_config); } diff --git a/src/connman.h b/src/connman.h index d6ce040..f55bcbd 100644 --- a/src/connman.h +++ b/src/connman.h @@ -319,6 +319,8 @@ int __connman_utsname_set_domainname(const char *domainname); int __connman_timeserver_init(void); void __connman_timeserver_cleanup(void); +char **__connman_timeserver_system_get(); + typedef void (* dhcp_cb) (struct connman_network *network, connman_bool_t success); int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback); diff --git a/src/timeserver.c b/src/timeserver.c index 038d44b..3bdc74f 100644 --- a/src/timeserver.c +++ b/src/timeserver.c @@ -32,6 +32,49 @@ static GSList *driver_list = NULL; static GHashTable *server_hash = NULL; +static void save_timeservers(char **servers) +{ + GKeyFile *keyfile; + int cnt; + + keyfile = __connman_storage_load_global(); + if (keyfile == NULL) + keyfile = g_key_file_new(); + + for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++); + + g_key_file_set_string_list(keyfile, "global", "Timeservers", + (const gchar **)servers, cnt); + + __connman_storage_save_global(keyfile); + + g_key_file_free(keyfile); + + return; +} + +static char **load_timeservers() +{ + GKeyFile *keyfile; + GError *error = NULL; + char **servers = NULL; + + keyfile = __connman_storage_load_global(); + if (keyfile == NULL) + return NULL; + + servers = g_key_file_get_string_list(keyfile, "global", + "Timeservers", NULL, &error); + if (error) { + DBG("Error loading timeservers: %s", error->message); + g_error_free(error); + } + + g_key_file_free(keyfile); + + return servers; +} + static gint compare_priority(gconstpointer a, gconstpointer b) { const struct connman_timeserver_driver *driver1 = a; @@ -155,6 +198,110 @@ void connman_timeserver_sync(void) } } +int __connman_timeserver_system_append(const char *server) +{ + int len; + char **servers = NULL; + + if (server == NULL) { + save_timeservers(servers); + return 0; + } + + DBG("server %s", server); + + servers = load_timeservers(); + + if (servers != NULL) { + int i; + + for (i = 0; servers[i] != NULL; i++) + if (g_strcmp0(servers[i], server) == 0) { + g_strfreev(servers); + return -EEXIST; + } + + len = g_strv_length(servers); + servers = g_try_renew(char *, servers, len + 2); + } else { + len = 0; + servers = g_try_new0(char *, len + 2); + } + + if (servers == NULL) + return -ENOMEM; + + servers[len] = g_strdup(server); + servers[len + 1] = NULL; + + save_timeservers(servers); + + g_strfreev(servers); + + return 0; +} + +int __connman_timeserver_system_remove(const char *server) +{ + char **servers; + char **temp; + int len, i, j; + + if (server == NULL) + return -EINVAL; + + DBG("server %s", server); + + servers = load_timeservers(); + + if (servers == NULL) + return 0; + + len = g_strv_length(servers); + if (len == 1) { + if (g_strcmp0(servers[0], server) != 0) { + g_strfreev(servers); + return 0; + } + + g_strfreev(servers); + servers = NULL; + save_timeservers(servers); + return 0; + } + + temp = g_try_new0(char *, len - 1); + if (temp == NULL) { + g_strfreev(servers); + return -ENOMEM; + } + + for (i = 0, j = 0; i < len; i++) { + if (g_strcmp0(servers[i], server) != 0) { + temp[j] = g_strdup(servers[i]); + j++; + } + } + temp[len - 1] = NULL; + + g_strfreev(servers); + servers = g_strdupv(temp); + g_strfreev(temp); + + save_timeservers(servers); + g_strfreev(servers); + + return 0; +} + +char **__connman_timeserver_system_get() +{ + char **servers; + + servers = load_timeservers(); + return servers; +} + int __connman_timeserver_init(void) { DBG(""); -- 2.7.4