clock: Add support for timezone change notifications
[platform/upstream/connman.git] / src / timezone.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 <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/inotify.h>
35
36 #include <glib.h>
37
38 #include "connman.h"
39
40 #define ETC_LOCALTIME           "/etc/localtime"
41 #define ETC_SYSCONFIG_CLOCK     "/etc/sysconfig/clock"
42 #define USR_SHARE_ZONEINFO      "/usr/share/zoneinfo"
43
44 static char *read_key_file(const char *pathname, const char *key)
45 {
46         struct stat st;
47         char *map, *ptr, *str;
48         off_t ptrlen, keylen;
49         int fd;
50
51         fd = open(pathname, O_RDONLY);
52         if (fd < 0)
53                 return NULL;
54
55         if (fstat(fd, &st) < 0) {
56                 close(fd);
57                 return NULL;
58         }
59
60         map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
61         if (map == NULL || map == MAP_FAILED) {
62                 close(fd);
63                 return NULL;
64         }
65
66         ptr = map;
67         ptrlen = st.st_size;
68         keylen = strlen(key);
69
70         while (ptrlen > keylen + 1) {
71                 int cmp = strncmp(ptr, key, keylen);
72
73                 if (cmp == 0) {
74                         if (ptr == map)
75                                 break;
76
77                         if (*(ptr - 1) == '\n' && *(ptr + keylen) == '=')
78                                 break;
79                 }
80
81                 ptr = memchr(ptr + 1, key[0], ptrlen - 1);
82                 if (ptr == NULL)
83                         break;
84
85                 ptrlen = st.st_size - (ptr - map);
86         }
87
88         if (ptr != NULL) {
89                 char *end, *val;
90
91                 ptrlen = st.st_size - (ptr - map);
92
93                 end = memchr(ptr, '\n', ptrlen);
94                 if (end != NULL)
95                         ptrlen = end - ptr;
96
97                 val = memchr(ptr, '"', ptrlen);
98                 if (val != NULL) {
99                         end = memchr(val + 1, '"', end - val - 1);
100                         if (end != NULL)
101                                 str = g_strndup(val + 1, end - val - 1);
102                         else
103                                 str = NULL;
104                 } else
105                         str = g_strndup(ptr + keylen + 1, ptrlen - keylen - 1);
106         } else
107                 str = NULL;
108
109         munmap(map, st.st_size);
110
111         close(fd);
112
113         return str;
114 }
115
116 static int compare_file(void *src_map, struct stat *src_st,
117                                                 const char *pathname)
118 {
119         struct stat dst_st;
120         void *dst_map;
121         int fd, result;
122
123         fd = open(pathname, O_RDONLY);
124         if (fd < 0)
125                 return -1;
126
127         if (fstat(fd, &dst_st) < 0) {
128                 close(fd);
129                 return -1;
130         }
131
132         if (src_st->st_size != dst_st.st_size) {
133                 close(fd);
134                 return -1;
135         }
136
137         dst_map = mmap(0, dst_st.st_size, PROT_READ, MAP_SHARED, fd, 0);
138         if (dst_map == NULL || dst_map == MAP_FAILED) {
139                 close(fd);
140                 return -1;
141         }
142
143         result = memcmp(src_map, dst_map, src_st->st_size);
144
145         munmap(dst_map, dst_st.st_size);
146
147         close(fd);
148
149         return result;
150 }
151
152 static char *find_origin(void *src_map, struct stat *src_st,
153                                 const char *basepath, const char *subpath)
154 {
155         DIR *dir;
156         struct dirent *d;
157         char *str, pathname[PATH_MAX];
158
159         if (subpath == NULL)
160                 strncpy(pathname, basepath, sizeof(pathname));
161         else
162                 snprintf(pathname, sizeof(pathname),
163                                         "%s/%s", basepath, subpath);
164
165         dir = opendir(pathname);
166         if (dir == NULL)
167                 return NULL;
168
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)
174                         continue;
175
176                 switch (d->d_type) {
177                 case DT_REG:
178                         if (subpath == NULL)
179                                 snprintf(pathname, PATH_MAX,
180                                                 "%s/%s", basepath, d->d_name);
181                         else
182                                 snprintf(pathname, PATH_MAX,
183                                                 "%s/%s/%s", basepath,
184                                                         subpath, d->d_name);
185
186                         if (compare_file(src_map, src_st, pathname) == 0) {
187                                 closedir(dir);
188                                 return g_strdup_printf("%s/%s",
189                                                         subpath, d->d_name);
190                         }
191                         break;
192                 case DT_DIR:
193                         if (subpath == NULL)
194                                 strncpy(pathname, d->d_name, sizeof(pathname));
195                         else
196                                 snprintf(pathname, sizeof(pathname),
197                                                 "%s/%s", subpath, d->d_name);
198
199                         str = find_origin(src_map, src_st, basepath, pathname);
200                         if (str != NULL) {
201                                 closedir(dir);
202                                 return str;
203                         }
204                         break;
205                 }
206         }
207
208         closedir(dir);
209
210         return NULL;
211 }
212
213 char *__connman_timezone_lookup(void)
214 {
215         struct stat st;
216         void *map;
217         int fd;
218         char *zone;
219
220         zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
221
222         DBG("sysconfig zone %s", zone);
223
224         fd = open(ETC_LOCALTIME, O_RDONLY);
225         if (fd < 0) {
226                 g_free(zone);
227                 return NULL;
228         }
229
230         if (fstat(fd, &st) < 0)
231                 goto done;
232
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) {
236                         g_free(zone);
237                         zone = NULL;
238
239                         goto done;
240                 }
241
242                 if (zone != NULL) {
243                         char pathname[PATH_MAX];
244
245                         snprintf(pathname, PATH_MAX, "%s/%s",
246                                                 USR_SHARE_ZONEINFO, zone);
247
248                         if (compare_file(map, &st, pathname) != 0) {
249                                 g_free(zone);
250                                 zone = NULL;
251                         }
252                 }
253
254                 if (zone == NULL)
255                         zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
256
257                 munmap(map, st.st_size);
258         } else {
259                 g_free(zone);
260                 zone = NULL;
261         }
262
263 done:
264         close(fd);
265
266         DBG("localtime zone %s", zone);
267
268         return zone;
269 }
270
271 static guint inotify_watch = 0;
272
273 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
274                                                         gpointer user_data)
275 {
276         char buffer[256];
277         void *ptr = buffer;
278         GIOStatus status;
279         gsize bytes_read;
280
281         DBG("");
282
283         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
284                 inotify_watch = 0;
285                 return FALSE;
286         }
287
288         status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
289                                                         &bytes_read, NULL);
290
291         switch (status) {
292         case G_IO_STATUS_NORMAL:
293                 break;
294         case G_IO_STATUS_AGAIN:
295                 return TRUE;
296         default:
297                 inotify_watch = 0;
298                 return FALSE;
299         }
300
301         DBG("bytes read %ld", bytes_read);
302
303         while (bytes_read > 0) {
304                 struct inotify_event *event = ptr;
305
306                 if (bytes_read < sizeof(*event))
307                         break;
308
309                 ptr += sizeof(*event);
310                 bytes_read -= sizeof(*event);
311
312                 if (event->len == 0)
313                         continue;
314
315                 if (bytes_read < event->len)
316                         break;
317
318                 ptr += event->len;
319                 bytes_read -= event->len;
320
321                 if (g_strcmp0(event->name, "localtime") == 0)
322                         __connman_clock_update_timezone();
323         }
324
325         return TRUE;
326 }
327
328 int __connman_timezone_init(void)
329 {
330         GIOChannel *channel;
331         char *dirname;
332         int fd, wd;
333
334         DBG("");
335
336         fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
337         if (fd < 0)
338                 return -EIO;
339
340         channel = g_io_channel_unix_new(fd);
341         if (channel == NULL) {
342                 close(fd);
343                 return -EIO;
344         }
345
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);
349
350         inotify_watch = g_io_add_watch(channel,
351                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
352                                 inotify_data, NULL);
353
354         g_io_channel_unref(channel);
355
356         dirname = g_path_get_dirname(ETC_LOCALTIME);
357
358         wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
359                                                 IN_MODIFY | IN_MOVED_TO);
360
361         g_free(dirname);
362
363         return 0;
364 }
365
366 void __connman_timezone_cleanup(void)
367 {
368         DBG("");
369
370         if (inotify_watch > 0) {
371                 g_source_remove(inotify_watch);
372                 inotify_watch = 0;
373         }
374 }