X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftimezone.c;h=e346b11a65434c536e6eba5b337e826c30a6e549;hb=79b0d466b91dc6b5e0038d3ce5934912392be4b8;hp=faf4488b14ef23cc5edee370f6a2fa9e55d17055;hpb=755e960a3c0c30da850142544d4977d184435116;p=platform%2Fupstream%2Fconnman.git diff --git a/src/timezone.c b/src/timezone.c old mode 100644 new mode 100755 index faf4488..e346b11 --- a/src/timezone.c +++ b/src/timezone.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2010 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -23,6 +23,8 @@ #include #endif +#define _GNU_SOURCE +#include #include #include #include @@ -48,7 +50,7 @@ static char *read_key_file(const char *pathname, const char *key) off_t ptrlen, keylen; int fd; - fd = open(pathname, O_RDONLY); + fd = open(pathname, O_RDONLY | O_CLOEXEC); if (fd < 0) return NULL; @@ -58,7 +60,7 @@ static char *read_key_file(const char *pathname, const char *key) } map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (map == NULL || map == MAP_FAILED) { + if (!map || map == MAP_FAILED) { close(fd); return NULL; } @@ -79,25 +81,25 @@ static char *read_key_file(const char *pathname, const char *key) } ptr = memchr(ptr + 1, key[0], ptrlen - 1); - if (ptr == NULL) + if (!ptr) break; ptrlen = st.st_size - (ptr - map); } - if (ptr != NULL) { + if (ptr) { char *end, *val; ptrlen = st.st_size - (ptr - map); end = memchr(ptr, '\n', ptrlen); - if (end != NULL) + if (end) ptrlen = end - ptr; val = memchr(ptr, '"', ptrlen); - if (val != NULL) { + if (val) { end = memchr(val + 1, '"', end - val - 1); - if (end != NULL) + if (end) str = g_strndup(val + 1, end - val - 1); else str = NULL; @@ -120,7 +122,7 @@ static int compare_file(void *src_map, struct stat *src_st, void *dst_map; int fd, result; - fd = open(pathname, O_RDONLY); + fd = open(pathname, O_RDONLY | O_CLOEXEC); if (fd < 0) return -1; @@ -135,10 +137,10 @@ static int compare_file(void *src_map, struct stat *src_st, } dst_map = mmap(0, dst_st.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (dst_map == NULL || dst_map == MAP_FAILED) { + if (!dst_map || dst_map == MAP_FAILED) { close(fd); return -1; - } + } result = memcmp(src_map, dst_map, src_st->st_size); @@ -155,15 +157,17 @@ static char *find_origin(void *src_map, struct stat *src_st, DIR *dir; struct dirent *d; char *str, pathname[PATH_MAX]; + struct stat buf; + int ret; - if (subpath == NULL) - strncpy(pathname, basepath, sizeof(pathname)); + if (!subpath) + strncpy(pathname, basepath, sizeof(pathname) - 1); else snprintf(pathname, sizeof(pathname), "%s/%s", basepath, subpath); dir = opendir(pathname); - if (dir == NULL) + if (!dir) return NULL; while ((d = readdir(dir))) { @@ -175,7 +179,7 @@ static char *find_origin(void *src_map, struct stat *src_st, switch (d->d_type) { case DT_REG: - if (subpath == NULL) + if (!subpath) snprintf(pathname, PATH_MAX, "%s/%s", basepath, d->d_name); else @@ -184,20 +188,32 @@ static char *find_origin(void *src_map, struct stat *src_st, subpath, d->d_name); if (compare_file(src_map, src_st, pathname) == 0) { - closedir(dir); - return g_strdup_printf("%s/%s", + str = g_strdup_printf("%s/%s", subpath, d->d_name); + closedir(dir); + return str; } break; + case DT_UNKNOWN: + /* + * If there is no d_type support use fstatat() + * to check if d_name is directory + */ + ret = fstatat(dirfd(dir), d->d_name, &buf, 0); + if (ret < 0) + continue; + if ((buf.st_mode & S_IFDIR) == 0) + continue; + /* fall through */ case DT_DIR: - if (subpath == NULL) + if (!subpath) strncpy(pathname, d->d_name, sizeof(pathname)); else snprintf(pathname, sizeof(pathname), "%s/%s", subpath, d->d_name); str = find_origin(src_map, src_st, basepath, pathname); - if (str != NULL) { + if (str) { closedir(dir); return str; } @@ -221,7 +237,7 @@ char *__connman_timezone_lookup(void) DBG("sysconfig zone %s", zone); - fd = open(ETC_LOCALTIME, O_RDONLY); + fd = open(ETC_LOCALTIME, O_RDONLY | O_CLOEXEC); if (fd < 0) { g_free(zone); return NULL; @@ -232,14 +248,14 @@ char *__connman_timezone_lookup(void) if (S_ISREG(st.st_mode)) { map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (map == NULL || map == MAP_FAILED) { + if (!map || map == MAP_FAILED) { g_free(zone); zone = NULL; goto done; } - if (zone != NULL) { + if (zone) { char pathname[PATH_MAX]; snprintf(pathname, PATH_MAX, "%s/%s", @@ -251,7 +267,7 @@ char *__connman_timezone_lookup(void) } } - if (zone == NULL) + if (!zone) zone = find_origin(map, &st, USR_SHARE_ZONEINFO, NULL); munmap(map, st.st_size); @@ -270,12 +286,18 @@ done: static int write_file(void *src_map, struct stat *src_st, const char *pathname) { + struct stat st; int fd; ssize_t written; DBG("pathname %s", pathname); - fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (lstat(pathname, &st) == 0) { + if (S_ISLNK(st.st_mode)) + unlink(pathname); + } + + fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); if (fd < 0) return -EIO; @@ -299,7 +321,7 @@ int __connman_timezone_change(const char *zone) snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone); - fd = open(pathname, O_RDONLY); + fd = open(pathname, O_RDONLY | O_CLOEXEC); if (fd < 0) return -EINVAL; @@ -309,7 +331,7 @@ int __connman_timezone_change(const char *zone) } map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (map == NULL || map == MAP_FAILED) { + if (!map || map == MAP_FAILED) { close(fd); return -EIO; } @@ -393,7 +415,7 @@ int __connman_timezone_init(void) return -EIO; channel = g_io_channel_unix_new(fd); - if (channel == NULL) { + if (!channel) { close(fd); return -EIO; } @@ -411,7 +433,7 @@ int __connman_timezone_init(void) dirname = g_path_get_dirname(ETC_LOCALTIME); wd = inotify_add_watch(fd, dirname, IN_DONT_FOLLOW | - IN_MODIFY | IN_MOVED_TO); + IN_CLOSE_WRITE | IN_MOVED_TO); g_free(dirname);