5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
34 #include <sys/inotify.h>
40 #define ETC_LOCALTIME "/etc/localtime"
41 #define ETC_SYSCONFIG_CLOCK "/etc/sysconfig/clock"
42 #define USR_SHARE_ZONEINFO "/usr/share/zoneinfo"
44 static char *read_key_file(const char *pathname, const char *key)
47 char *map, *ptr, *str;
51 fd = open(pathname, O_RDONLY);
55 if (fstat(fd, &st) < 0) {
60 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
61 if (map == NULL || map == MAP_FAILED) {
70 while (ptrlen > keylen + 1) {
71 int cmp = strncmp(ptr, key, keylen);
77 if (*(ptr - 1) == '\n' && *(ptr + keylen) == '=')
81 ptr = memchr(ptr + 1, key[0], ptrlen - 1);
85 ptrlen = st.st_size - (ptr - map);
91 ptrlen = st.st_size - (ptr - map);
93 end = memchr(ptr, '\n', ptrlen);
97 val = memchr(ptr, '"', ptrlen);
99 end = memchr(val + 1, '"', end - val - 1);
101 str = g_strndup(val + 1, end - val - 1);
105 str = g_strndup(ptr + keylen + 1, ptrlen - keylen - 1);
109 munmap(map, st.st_size);
116 static int compare_file(void *src_map, struct stat *src_st,
117 const char *pathname)
123 fd = open(pathname, O_RDONLY);
127 if (fstat(fd, &dst_st) < 0) {
132 if (src_st->st_size != dst_st.st_size) {
137 dst_map = mmap(0, dst_st.st_size, PROT_READ, MAP_SHARED, fd, 0);
138 if (dst_map == NULL || dst_map == MAP_FAILED) {
143 result = memcmp(src_map, dst_map, src_st->st_size);
145 munmap(dst_map, dst_st.st_size);
152 static char *find_origin(void *src_map, struct stat *src_st,
153 const char *basepath, const char *subpath)
157 char *str, pathname[PATH_MAX];
160 strncpy(pathname, basepath, sizeof(pathname));
162 snprintf(pathname, sizeof(pathname),
163 "%s/%s", basepath, subpath);
165 dir = opendir(pathname);
169 while ((d = readdir(dir))) {
170 if (strcmp(d->d_name, ".") == 0 ||
171 strcmp(d->d_name, "..") == 0 ||
172 strcmp(d->d_name, "posix") == 0 ||
173 strcmp(d->d_name, "right") == 0)
179 snprintf(pathname, PATH_MAX,
180 "%s/%s", basepath, d->d_name);
182 snprintf(pathname, PATH_MAX,
183 "%s/%s/%s", basepath,
186 if (compare_file(src_map, src_st, pathname) == 0) {
188 return g_strdup_printf("%s/%s",
194 strncpy(pathname, d->d_name, sizeof(pathname));
196 snprintf(pathname, sizeof(pathname),
197 "%s/%s", subpath, d->d_name);
199 str = find_origin(src_map, src_st, basepath, pathname);
213 char *__connman_timezone_lookup(void)
220 zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
222 DBG("sysconfig zone %s", zone);
224 fd = open(ETC_LOCALTIME, O_RDONLY);
230 if (fstat(fd, &st) < 0)
233 if (S_ISREG(st.st_mode)) {
234 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
235 if (map == NULL || map == MAP_FAILED) {
243 char pathname[PATH_MAX];
245 snprintf(pathname, PATH_MAX, "%s/%s",
246 USR_SHARE_ZONEINFO, zone);
248 if (compare_file(map, &st, pathname) != 0) {
255 zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
257 munmap(map, st.st_size);
266 DBG("localtime zone %s", zone);
271 static guint inotify_watch = 0;
273 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
283 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
288 status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
292 case G_IO_STATUS_NORMAL:
294 case G_IO_STATUS_AGAIN:
301 DBG("bytes read %ld", bytes_read);
303 while (bytes_read > 0) {
304 struct inotify_event *event = ptr;
306 if (bytes_read < sizeof(*event))
309 ptr += sizeof(*event);
310 bytes_read -= sizeof(*event);
315 if (bytes_read < event->len)
319 bytes_read -= event->len;
321 if (g_strcmp0(event->name, "localtime") == 0)
322 __connman_clock_update_timezone();
328 int __connman_timezone_init(void)
336 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
340 channel = g_io_channel_unix_new(fd);
341 if (channel == NULL) {
346 g_io_channel_set_close_on_unref(channel, TRUE);
347 g_io_channel_set_encoding(channel, NULL, NULL);
348 g_io_channel_set_buffered(channel, FALSE);
350 inotify_watch = g_io_add_watch(channel,
351 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
354 g_io_channel_unref(channel);
356 dirname = g_path_get_dirname(ETC_LOCALTIME);
358 wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
359 IN_MODIFY | IN_MOVED_TO);
366 void __connman_timezone_cleanup(void)
370 if (inotify_watch > 0) {
371 g_source_remove(inotify_watch);