Imported Upstream version 1.37
[platform/upstream/connman.git] / src / timezone.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/inotify.h>
36
37 #include <glib.h>
38
39 #include "connman.h"
40
41 #define ETC_LOCALTIME           "/etc/localtime"
42 #define ETC_SYSCONFIG_CLOCK     "/etc/sysconfig/clock"
43 #define USR_SHARE_ZONEINFO      "/usr/share/zoneinfo"
44
45 static char *read_key_file(const char *pathname, const char *key)
46 {
47         struct stat st;
48         char *map, *ptr, *str;
49         off_t ptrlen, keylen;
50         int fd;
51
52         fd = open(pathname, O_RDONLY | O_CLOEXEC);
53         if (fd < 0)
54                 return NULL;
55
56         if (fstat(fd, &st) < 0) {
57                 close(fd);
58                 return NULL;
59         }
60
61         map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
62         if (!map || map == MAP_FAILED) {
63                 close(fd);
64                 return NULL;
65         }
66
67         ptr = map;
68         ptrlen = st.st_size;
69         keylen = strlen(key);
70
71         while (ptrlen > keylen + 1) {
72                 int cmp = strncmp(ptr, key, keylen);
73
74                 if (cmp == 0) {
75                         if (ptr == map)
76                                 break;
77
78                         if (*(ptr - 1) == '\n' && *(ptr + keylen) == '=')
79                                 break;
80                 }
81
82                 ptr = memchr(ptr + 1, key[0], ptrlen - 1);
83                 if (!ptr)
84                         break;
85
86                 ptrlen = st.st_size - (ptr - map);
87         }
88
89         if (ptr) {
90                 char *end, *val;
91
92                 ptrlen = st.st_size - (ptr - map);
93
94                 end = memchr(ptr, '\n', ptrlen);
95                 if (end)
96                         ptrlen = end - ptr;
97
98                 val = memchr(ptr, '"', ptrlen);
99                 if (val) {
100                         end = memchr(val + 1, '"', end - val - 1);
101                         if (end)
102                                 str = g_strndup(val + 1, end - val - 1);
103                         else
104                                 str = NULL;
105                 } else
106                         str = g_strndup(ptr + keylen + 1, ptrlen - keylen - 1);
107         } else
108                 str = NULL;
109
110         munmap(map, st.st_size);
111
112         close(fd);
113
114         return str;
115 }
116
117 static int compare_file(void *src_map, struct stat *src_st,
118                                                 const char *pathname)
119 {
120         struct stat dst_st;
121         void *dst_map;
122         int fd, result;
123
124         fd = open(pathname, O_RDONLY | O_CLOEXEC);
125         if (fd < 0)
126                 return -1;
127
128         if (fstat(fd, &dst_st) < 0) {
129                 close(fd);
130                 return -1;
131         }
132
133         if (src_st->st_size != dst_st.st_size) {
134                 close(fd);
135                 return -1;
136         }
137
138         dst_map = mmap(0, dst_st.st_size, PROT_READ, MAP_SHARED, fd, 0);
139         if (!dst_map || dst_map == MAP_FAILED) {
140                 close(fd);
141                 return -1;
142         }
143
144         result = memcmp(src_map, dst_map, src_st->st_size);
145
146         munmap(dst_map, dst_st.st_size);
147
148         close(fd);
149
150         return result;
151 }
152
153 static char *find_origin(void *src_map, struct stat *src_st,
154                                 const char *basepath, const char *subpath)
155 {
156         DIR *dir;
157         struct dirent *d;
158         char *str, pathname[PATH_MAX];
159         struct stat buf;
160         int ret;
161
162         if (!subpath)
163                 strncpy(pathname, basepath, sizeof(pathname) - 1);
164         else
165                 snprintf(pathname, sizeof(pathname),
166                                         "%s/%s", basepath, subpath);
167
168         dir = opendir(pathname);
169         if (!dir)
170                 return NULL;
171
172         while ((d = readdir(dir))) {
173                 if (strcmp(d->d_name, ".") == 0 ||
174                                 strcmp(d->d_name, "..") == 0 ||
175                                 strcmp(d->d_name, "posix") == 0 ||
176                                 strcmp(d->d_name, "right") == 0)
177                         continue;
178
179                 switch (d->d_type) {
180                 case DT_REG:
181                         if (!subpath)
182                                 snprintf(pathname, PATH_MAX,
183                                                 "%s/%s", basepath, d->d_name);
184                         else
185                                 snprintf(pathname, PATH_MAX,
186                                                 "%s/%s/%s", basepath,
187                                                         subpath, d->d_name);
188
189                         if (compare_file(src_map, src_st, pathname) == 0) {
190                                 str = g_strdup_printf("%s/%s",
191                                                         subpath, d->d_name);
192                                 closedir(dir);
193                                 return str;
194                         }
195                         break;
196                 case DT_UNKNOWN:
197                         /*
198                          * If there is no d_type support use fstatat()
199                          * to check if d_name is directory
200                          */
201                         ret = fstatat(dirfd(dir), d->d_name, &buf, 0);
202                         if (ret < 0)
203                                 continue;
204                         if ((buf.st_mode & S_IFDIR) == 0)
205                                 continue;
206                         /* fall through */
207                 case DT_DIR:
208                         if (!subpath)
209                                 strncpy(pathname, d->d_name, sizeof(pathname));
210                         else
211                                 snprintf(pathname, sizeof(pathname),
212                                                 "%s/%s", subpath, d->d_name);
213
214                         str = find_origin(src_map, src_st, basepath, pathname);
215                         if (str) {
216                                 closedir(dir);
217                                 return str;
218                         }
219                         break;
220                 }
221         }
222
223         closedir(dir);
224
225         return NULL;
226 }
227
228 char *__connman_timezone_lookup(void)
229 {
230         struct stat st;
231         void *map;
232         int fd;
233         char *zone;
234
235         zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
236
237         DBG("sysconfig zone %s", zone);
238
239         fd = open(ETC_LOCALTIME, O_RDONLY | O_CLOEXEC);
240         if (fd < 0) {
241                 g_free(zone);
242                 return NULL;
243         }
244
245         if (fstat(fd, &st) < 0)
246                 goto done;
247
248         if (S_ISREG(st.st_mode)) {
249                 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
250                 if (!map || map == MAP_FAILED) {
251                         g_free(zone);
252                         zone = NULL;
253
254                         goto done;
255                 }
256
257                 if (zone) {
258                         char pathname[PATH_MAX];
259
260                         snprintf(pathname, PATH_MAX, "%s/%s",
261                                                 USR_SHARE_ZONEINFO, zone);
262
263                         if (compare_file(map, &st, pathname) != 0) {
264                                 g_free(zone);
265                                 zone = NULL;
266                         }
267                 }
268
269                 if (!zone)
270                         zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
271
272                 munmap(map, st.st_size);
273         } else {
274                 g_free(zone);
275                 zone = NULL;
276         }
277
278 done:
279         close(fd);
280
281         DBG("localtime zone %s", zone);
282
283         return zone;
284 }
285
286 static int write_file(void *src_map, struct stat *src_st, const char *pathname)
287 {
288         struct stat st;
289         int fd;
290         ssize_t written;
291
292         DBG("pathname %s", pathname);
293
294         if (lstat(pathname, &st) == 0) {
295                 if (S_ISLNK(st.st_mode))
296                         unlink(pathname);
297         }
298
299         fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
300         if (fd < 0)
301                 return -EIO;
302
303         written = write(fd, src_map, src_st->st_size);
304
305         close(fd);
306
307         if (written < 0)
308                 return -EIO;
309
310         return 0;
311 }
312
313 int __connman_timezone_change(const char *zone)
314 {
315         struct stat st;
316         char *map, pathname[PATH_MAX];
317         int fd, err;
318
319         DBG("zone %s", zone);
320
321         snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
322
323         fd = open(pathname, O_RDONLY | O_CLOEXEC);
324         if (fd < 0)
325                 return -EINVAL;
326
327         if (fstat(fd, &st) < 0) {
328                 close(fd);
329                 return -EIO;
330         }
331
332         map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
333         if (!map || map == MAP_FAILED) {
334                 close(fd);
335                 return -EIO;
336         }
337
338         err = write_file(map, &st, ETC_LOCALTIME);
339
340         munmap(map, st.st_size);
341
342         close(fd);
343
344         return err;
345 }
346
347 static guint inotify_watch = 0;
348
349 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
350                                                         gpointer user_data)
351 {
352         char buffer[256];
353         void *ptr = buffer;
354         GIOStatus status;
355         gsize bytes_read;
356
357         DBG("");
358
359         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
360                 inotify_watch = 0;
361                 return FALSE;
362         }
363
364         status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
365                                                         &bytes_read, NULL);
366
367         switch (status) {
368         case G_IO_STATUS_NORMAL:
369                 break;
370         case G_IO_STATUS_AGAIN:
371                 return TRUE;
372         default:
373                 inotify_watch = 0;
374                 return FALSE;
375         }
376
377         DBG("bytes read %zd", bytes_read);
378
379         while (bytes_read > 0) {
380                 struct inotify_event *event = ptr;
381
382                 if (bytes_read < sizeof(*event))
383                         break;
384
385                 ptr += sizeof(*event);
386                 bytes_read -= sizeof(*event);
387
388                 if (event->len == 0)
389                         continue;
390
391                 if (bytes_read < event->len)
392                         break;
393
394                 ptr += event->len;
395                 bytes_read -= event->len;
396
397                 if (g_strcmp0(event->name, "localtime") == 0)
398                         __connman_clock_update_timezone();
399         }
400
401         return TRUE;
402 }
403
404 int __connman_timezone_init(void)
405 {
406         GIOChannel *channel;
407         char *dirname;
408         int fd, wd;
409
410         DBG("");
411
412         fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
413         if (fd < 0)
414                 return -EIO;
415
416         channel = g_io_channel_unix_new(fd);
417         if (!channel) {
418                 close(fd);
419                 return -EIO;
420         }
421
422         g_io_channel_set_close_on_unref(channel, TRUE);
423         g_io_channel_set_encoding(channel, NULL, NULL);
424         g_io_channel_set_buffered(channel, FALSE);
425
426         inotify_watch = g_io_add_watch(channel,
427                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
428                                 inotify_data, NULL);
429
430         g_io_channel_unref(channel);
431
432         dirname = g_path_get_dirname(ETC_LOCALTIME);
433
434         wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
435                                                 IN_CLOSE_WRITE | IN_MOVED_TO);
436
437         g_free(dirname);
438
439         if (wd < 0)
440                 return -EIO;
441
442         return 0;
443 }
444
445 void __connman_timezone_cleanup(void)
446 {
447         DBG("");
448
449         if (inotify_watch > 0) {
450                 g_source_remove(inotify_watch);
451                 inotify_watch = 0;
452         }
453 }