timezone: Remove /etc/localtime if it's a symbolic link
[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 <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);
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 == NULL || 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 == NULL)
84                         break;
85
86                 ptrlen = st.st_size - (ptr - map);
87         }
88
89         if (ptr != NULL) {
90                 char *end, *val;
91
92                 ptrlen = st.st_size - (ptr - map);
93
94                 end = memchr(ptr, '\n', ptrlen);
95                 if (end != NULL)
96                         ptrlen = end - ptr;
97
98                 val = memchr(ptr, '"', ptrlen);
99                 if (val != NULL) {
100                         end = memchr(val + 1, '"', end - val - 1);
101                         if (end != NULL)
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);
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 == NULL || 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
160         if (subpath == NULL)
161                 strncpy(pathname, basepath, sizeof(pathname));
162         else
163                 snprintf(pathname, sizeof(pathname),
164                                         "%s/%s", basepath, subpath);
165
166         dir = opendir(pathname);
167         if (dir == NULL)
168                 return NULL;
169
170         while ((d = readdir(dir))) {
171                 if (strcmp(d->d_name, ".") == 0 ||
172                                 strcmp(d->d_name, "..") == 0 ||
173                                 strcmp(d->d_name, "posix") == 0 ||
174                                 strcmp(d->d_name, "right") == 0)
175                         continue;
176
177                 switch (d->d_type) {
178                 case DT_REG:
179                         if (subpath == NULL)
180                                 snprintf(pathname, PATH_MAX,
181                                                 "%s/%s", basepath, d->d_name);
182                         else
183                                 snprintf(pathname, PATH_MAX,
184                                                 "%s/%s/%s", basepath,
185                                                         subpath, d->d_name);
186
187                         if (compare_file(src_map, src_st, pathname) == 0) {
188                                 closedir(dir);
189                                 return g_strdup_printf("%s/%s",
190                                                         subpath, d->d_name);
191                         }
192                         break;
193                 case DT_DIR:
194                         if (subpath == NULL)
195                                 strncpy(pathname, d->d_name, sizeof(pathname));
196                         else
197                                 snprintf(pathname, sizeof(pathname),
198                                                 "%s/%s", subpath, d->d_name);
199
200                         str = find_origin(src_map, src_st, basepath, pathname);
201                         if (str != NULL) {
202                                 closedir(dir);
203                                 return str;
204                         }
205                         break;
206                 }
207         }
208
209         closedir(dir);
210
211         return NULL;
212 }
213
214 char *__connman_timezone_lookup(void)
215 {
216         struct stat st;
217         void *map;
218         int fd;
219         char *zone;
220
221         zone = read_key_file(ETC_SYSCONFIG_CLOCK, "ZONE");
222
223         DBG("sysconfig zone %s", zone);
224
225         fd = open(ETC_LOCALTIME, O_RDONLY);
226         if (fd < 0) {
227                 g_free(zone);
228                 return NULL;
229         }
230
231         if (fstat(fd, &st) < 0)
232                 goto done;
233
234         if (S_ISREG(st.st_mode)) {
235                 map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
236                 if (map == NULL || map == MAP_FAILED) {
237                         g_free(zone);
238                         zone = NULL;
239
240                         goto done;
241                 }
242
243                 if (zone != NULL) {
244                         char pathname[PATH_MAX];
245
246                         snprintf(pathname, PATH_MAX, "%s/%s",
247                                                 USR_SHARE_ZONEINFO, zone);
248
249                         if (compare_file(map, &st, pathname) != 0) {
250                                 g_free(zone);
251                                 zone = NULL;
252                         }
253                 }
254
255                 if (zone == NULL)
256                         zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL);
257
258                 munmap(map, st.st_size);
259         } else {
260                 g_free(zone);
261                 zone = NULL;
262         }
263
264 done:
265         close(fd);
266
267         DBG("localtime zone %s", zone);
268
269         return zone;
270 }
271
272 static int write_file(void *src_map, struct stat *src_st, const char *pathname)
273 {
274         struct stat st;
275         int fd;
276         ssize_t written;
277
278         DBG("pathname %s", pathname);
279
280         if (lstat(pathname, &st) == 0) {
281                 if (S_ISLNK(st.st_mode))
282                         unlink(pathname);
283         }
284
285         fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
286         if (fd < 0)
287                 return -EIO;
288
289         written = write(fd, src_map, src_st->st_size);
290
291         close(fd);
292
293         if (written < 0)
294                 return -EIO;
295
296         return 0;
297 }
298
299 int __connman_timezone_change(const char *zone)
300 {
301         struct stat st;
302         char *map, pathname[PATH_MAX];
303         int fd, err;
304
305         DBG("zone %s", zone);
306
307         snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
308
309         fd = open(pathname, O_RDONLY);
310         if (fd < 0)
311                 return -EINVAL;
312
313         if (fstat(fd, &st) < 0) {
314                 close(fd);
315                 return -EIO;
316         }
317
318         map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
319         if (map == NULL || map == MAP_FAILED) {
320                 close(fd);
321                 return -EIO;
322         }
323
324         err = write_file(map, &st, ETC_LOCALTIME);
325
326         munmap(map, st.st_size);
327
328         close(fd);
329
330         return err;
331 }
332
333 static guint inotify_watch = 0;
334
335 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
336                                                         gpointer user_data)
337 {
338         char buffer[256];
339         void *ptr = buffer;
340         GIOStatus status;
341         gsize bytes_read;
342
343         DBG("");
344
345         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
346                 inotify_watch = 0;
347                 return FALSE;
348         }
349
350         status = g_io_channel_read_chars(channel, buffer, sizeof(buffer),
351                                                         &bytes_read, NULL);
352
353         switch (status) {
354         case G_IO_STATUS_NORMAL:
355                 break;
356         case G_IO_STATUS_AGAIN:
357                 return TRUE;
358         default:
359                 inotify_watch = 0;
360                 return FALSE;
361         }
362
363         DBG("bytes read %zd", bytes_read);
364
365         while (bytes_read > 0) {
366                 struct inotify_event *event = ptr;
367
368                 if (bytes_read < sizeof(*event))
369                         break;
370
371                 ptr += sizeof(*event);
372                 bytes_read -= sizeof(*event);
373
374                 if (event->len == 0)
375                         continue;
376
377                 if (bytes_read < event->len)
378                         break;
379
380                 ptr += event->len;
381                 bytes_read -= event->len;
382
383                 if (g_strcmp0(event->name, "localtime") == 0)
384                         __connman_clock_update_timezone();
385         }
386
387         return TRUE;
388 }
389
390 int __connman_timezone_init(void)
391 {
392         GIOChannel *channel;
393         char *dirname;
394         int fd, wd;
395
396         DBG("");
397
398         fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
399         if (fd < 0)
400                 return -EIO;
401
402         channel = g_io_channel_unix_new(fd);
403         if (channel == NULL) {
404                 close(fd);
405                 return -EIO;
406         }
407
408         g_io_channel_set_close_on_unref(channel, TRUE);
409         g_io_channel_set_encoding(channel, NULL, NULL);
410         g_io_channel_set_buffered(channel, FALSE);
411
412         inotify_watch = g_io_add_watch(channel,
413                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
414                                 inotify_data, NULL);
415
416         g_io_channel_unref(channel);
417
418         dirname = g_path_get_dirname(ETC_LOCALTIME);
419
420         wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW |
421                                                 IN_MODIFY | IN_MOVED_TO);
422
423         g_free(dirname);
424
425         if (wd < 0)
426                 return -EIO;
427
428         return 0;
429 }
430
431 void __connman_timezone_cleanup(void)
432 {
433         DBG("");
434
435         if (inotify_watch > 0) {
436                 g_source_remove(inotify_watch);
437                 inotify_watch = 0;
438         }
439 }