5 * Copyright (C) 2007-2012 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
36 #include <sys/inotify.h>
42 #define ETC_LOCALTIME "/etc/localtime"
43 #define ETC_SYSCONFIG_CLOCK "/etc/sysconfig/clock"
44 #define USR_SHARE_ZONEINFO "/usr/share/zoneinfo"
46 static char *read_key_file(const char *pathname, const char *key)
49 char *map, *ptr, *str;
53 fd = open(pathname, O_RDONLY | O_CLOEXEC);
57 if (fstat(fd, &st) < 0) {
62 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
63 if (!map || map == MAP_FAILED) {
72 while (ptrlen > keylen + 1) {
73 int cmp = strncmp(ptr, key, keylen);
79 if (*(ptr - 1) == '\n' && *(ptr + keylen) == '=')
83 ptr = memchr(ptr + 1, key[0], ptrlen - 1);
87 ptrlen = st.st_size - (ptr - map);
93 ptrlen = st.st_size - (ptr - map);
95 end = memchr(ptr, '\n', ptrlen);
99 val = memchr(ptr, '"', ptrlen);
101 end = memchr(val + 1, '"', end - val - 1);
103 str = g_strndup(val + 1, end - val - 1);
107 str = g_strndup(ptr + keylen + 1, ptrlen - keylen - 1);
111 munmap(map, st.st_size);
118 static int compare_file(void *src_map, struct stat *src_st,
119 const char *pathname)
125 fd = open(pathname, O_RDONLY | O_CLOEXEC);
129 if (fstat(fd, &dst_st) < 0) {
134 if (src_st->st_size != dst_st.st_size) {
139 dst_map = mmap(0, dst_st.st_size, PROT_READ, MAP_SHARED, fd, 0);
140 if (!dst_map || dst_map == MAP_FAILED) {
145 result = memcmp(src_map, dst_map, src_st->st_size);
147 munmap(dst_map, dst_st.st_size);
154 static char *find_origin(void *src_map, struct stat *src_st,
155 const char *basepath, const char *subpath)
159 char *str, pathname[PATH_MAX];
164 strncpy(pathname, basepath, sizeof(pathname) - 1);
166 snprintf(pathname, sizeof(pathname),
167 "%s/%s", basepath, subpath);
169 dir = opendir(pathname);
173 while ((d = readdir(dir))) {
174 if (strcmp(d->d_name, ".") == 0 ||
175 strcmp(d->d_name, "..") == 0 ||
176 strcmp(d->d_name, "posix") == 0 ||
177 strcmp(d->d_name, "right") == 0)
183 snprintf(pathname, PATH_MAX,
184 "%s/%s", basepath, d->d_name);
186 snprintf(pathname, PATH_MAX,
187 "%s/%s/%s", basepath,
190 if (compare_file(src_map, src_st, pathname) == 0) {
191 str = g_strdup_printf("%s/%s",
199 * If there is no d_type support use fstatat()
200 * to check if d_name is directory
202 ret = fstatat(dirfd(dir), d->d_name, &buf, 0);
205 if ((buf.st_mode & S_IFDIR) == 0)
210 strncpy(pathname, d->d_name, sizeof(pathname));
212 snprintf(pathname, sizeof(pathname),
213 "%s/%s", subpath, d->d_name);
215 str = find_origin(src_map, src_st, basepath, pathname);
229 char *__connman_timezone_lookup(void)
236 zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
238 DBG("sysconfig zone %s", zone);
240 fd = open(ETC_LOCALTIME, O_RDONLY | O_CLOEXEC);
246 if (fstat(fd, &st) < 0)
249 if (S_ISREG(st.st_mode)) {
250 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
251 if (!map || map == MAP_FAILED) {
259 char pathname[PATH_MAX];
261 snprintf(pathname, PATH_MAX, "%s/%s",
262 USR_SHARE_ZONEINFO, zone);
264 if (compare_file(map, &st, pathname) != 0) {
271 zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
273 munmap(map, st.st_size);
282 DBG("localtime zone %s", zone);
287 static int write_file(void *src_map, struct stat *src_st, const char *pathname)
293 DBG("pathname %s", pathname);
295 if (lstat(pathname, &st) == 0) {
296 if (S_ISLNK(st.st_mode))
300 fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
304 written = write(fd, src_map, src_st->st_size);
314 int __connman_timezone_change(const char *zone)
317 char *map, pathname[PATH_MAX];
320 DBG("zone %s", zone);
322 snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
324 fd = open(pathname, O_RDONLY | O_CLOEXEC);
328 if (fstat(fd, &st) < 0) {
333 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
334 if (!map || map == MAP_FAILED) {
339 err = write_file(map, &st, ETC_LOCALTIME);
341 munmap(map, st.st_size);
348 static guint inotify_watch = 0;
350 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
360 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
365 status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
369 case G_IO_STATUS_NORMAL:
371 case G_IO_STATUS_AGAIN:
378 DBG("bytes read %zd", bytes_read);
380 while (bytes_read > 0) {
381 struct inotify_event *event = ptr;
383 if (bytes_read < sizeof(*event))
386 ptr += sizeof(*event);
387 bytes_read -= sizeof(*event);
392 if (bytes_read < event->len)
396 bytes_read -= event->len;
398 if (g_strcmp0(event->name, "localtime") == 0)
399 __connman_clock_update_timezone();
405 int __connman_timezone_init(void)
413 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
417 channel = g_io_channel_unix_new(fd);
423 g_io_channel_set_close_on_unref(channel, TRUE);
424 g_io_channel_set_encoding(channel, NULL, NULL);
425 g_io_channel_set_buffered(channel, FALSE);
427 inotify_watch = g_io_add_watch(channel,
428 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
431 g_io_channel_unref(channel);
433 dirname = g_path_get_dirname(ETC_LOCALTIME);
435 wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
436 IN_CLOSE_WRITE | IN_MOVED_TO);
446 void __connman_timezone_cleanup(void)
450 if (inotify_watch > 0) {
451 g_source_remove(inotify_watch);