Imported Upstream connman version 1.38
[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                                 if (!subpath)
191                                         str = g_strdup(d->d_name);
192                                 else
193                                         str = g_strdup_printf("%s/%s",
194                                                         subpath, d->d_name);
195                                 closedir(dir);
196                                 return str;
197                         }
198                         break;
199                 case DT_UNKNOWN:
200                         /*
201                          * If there is no d_type support use fstatat()
202                          * to check if d_name is directory
203                          */
204                         ret = fstatat(dirfd(dir), d->d_name, &buf, 0);
205                         if (ret < 0)
206                                 continue;
207                         if ((buf.st_mode & S_IFDIR) == 0)
208                                 continue;
209                         /* fall through */
210                 case DT_DIR:
211                         if (!subpath)
212                                 strncpy(pathname, d->d_name, sizeof(pathname));
213                         else
214                                 snprintf(pathname, sizeof(pathname),
215                                                 "%s/%s", subpath, d->d_name);
216
217                         str = find_origin(src_map, src_st, basepath, pathname);
218                         if (str) {
219                                 closedir(dir);
220                                 return str;
221                         }
222                         break;
223                 }
224         }
225
226         closedir(dir);
227
228         return NULL;
229 }
230
231 char *__connman_timezone_lookup(void)
232 {
233         struct stat st;
234         void *map;
235         int fd;
236         char *zone;
237
238         zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
239
240         DBG("sysconfig zone %s", zone);
241
242         fd = open(ETC_LOCALTIME, O_RDONLY | O_CLOEXEC);
243         if (fd < 0) {
244                 g_free(zone);
245                 return NULL;
246         }
247
248         if (fstat(fd, &st) < 0)
249                 goto done;
250
251         if (S_ISREG(st.st_mode)) {
252                 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
253                 if (!map || map == MAP_FAILED) {
254                         g_free(zone);
255                         zone = NULL;
256
257                         goto done;
258                 }
259
260                 if (zone) {
261                         char pathname[PATH_MAX];
262
263                         snprintf(pathname, PATH_MAX, "%s/%s",
264                                                 USR_SHARE_ZONEINFO, zone);
265
266                         if (compare_file(map, &st, pathname) != 0) {
267                                 g_free(zone);
268                                 zone = NULL;
269                         }
270                 }
271
272                 if (!zone)
273                         zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
274
275                 munmap(map, st.st_size);
276         } else {
277                 g_free(zone);
278                 zone = NULL;
279         }
280
281 done:
282         close(fd);
283
284         DBG("localtime zone %s", zone);
285
286         return zone;
287 }
288
289 static int write_file(void *src_map, struct stat *src_st, const char *pathname)
290 {
291         struct stat st;
292         int fd;
293         ssize_t written;
294
295         DBG("pathname %s", pathname);
296
297         if (lstat(pathname, &st) == 0) {
298                 if (S_ISLNK(st.st_mode))
299                         unlink(pathname);
300         }
301
302         fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
303         if (fd < 0)
304                 return -EIO;
305
306         written = write(fd, src_map, src_st->st_size);
307
308         close(fd);
309
310         if (written < 0)
311                 return -EIO;
312
313         return 0;
314 }
315
316 int __connman_timezone_change(const char *zone)
317 {
318         struct stat st;
319         char *map, pathname[PATH_MAX];
320         int fd, err;
321
322         DBG("zone %s", zone);
323
324         snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
325
326         fd = open(pathname, O_RDONLY | O_CLOEXEC);
327         if (fd < 0)
328                 return -EINVAL;
329
330         if (fstat(fd, &st) < 0) {
331                 close(fd);
332                 return -EIO;
333         }
334
335         map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
336         if (!map || map == MAP_FAILED) {
337                 close(fd);
338                 return -EIO;
339         }
340
341         err = write_file(map, &st, ETC_LOCALTIME);
342
343         munmap(map, st.st_size);
344
345         close(fd);
346
347         return err;
348 }
349
350 static guint inotify_watch = 0;
351
352 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
353                                                         gpointer user_data)
354 {
355         char buffer[256];
356         void *ptr = buffer;
357         GIOStatus status;
358         gsize bytes_read;
359
360         DBG("");
361
362         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
363                 inotify_watch = 0;
364                 return FALSE;
365         }
366
367         status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
368                                                         &bytes_read, NULL);
369
370         switch (status) {
371         case G_IO_STATUS_NORMAL:
372                 break;
373         case G_IO_STATUS_AGAIN:
374                 return TRUE;
375         default:
376                 inotify_watch = 0;
377                 return FALSE;
378         }
379
380         DBG("bytes read %zd", bytes_read);
381
382         while (bytes_read > 0) {
383                 struct inotify_event *event = ptr;
384
385                 if (bytes_read < sizeof(*event))
386                         break;
387
388                 ptr += sizeof(*event);
389                 bytes_read -= sizeof(*event);
390
391                 if (event->len == 0)
392                         continue;
393
394                 if (bytes_read < event->len)
395                         break;
396
397                 ptr += event->len;
398                 bytes_read -= event->len;
399
400                 if (g_strcmp0(event->name, "localtime") == 0)
401                         __connman_clock_update_timezone();
402         }
403
404         return TRUE;
405 }
406
407 int __connman_timezone_init(void)
408 {
409         GIOChannel *channel;
410         char *dirname;
411         int fd, wd;
412
413         DBG("");
414
415         fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
416         if (fd < 0)
417                 return -EIO;
418
419         channel = g_io_channel_unix_new(fd);
420         if (!channel) {
421                 close(fd);
422                 return -EIO;
423         }
424
425         g_io_channel_set_close_on_unref(channel, TRUE);
426         g_io_channel_set_encoding(channel, NULL, NULL);
427         g_io_channel_set_buffered(channel, FALSE);
428
429         inotify_watch = g_io_add_watch(channel,
430                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
431                                 inotify_data, NULL);
432
433         g_io_channel_unref(channel);
434
435         dirname = g_path_get_dirname(ETC_LOCALTIME);
436
437         wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
438                                                 IN_CLOSE_WRITE | IN_MOVED_TO);
439
440         g_free(dirname);
441
442         if (wd < 0)
443                 return -EIO;
444
445         return 0;
446 }
447
448 void __connman_timezone_cleanup(void)
449 {
450         DBG("");
451
452         if (inotify_watch > 0) {
453                 g_source_remove(inotify_watch);
454                 inotify_watch = 0;
455         }
456 }