Skip to contentSkip to conversation

Revert "timezone: Fix compare_file comparison in timezone checking"

PATCH Superseded Andrea Ricchi 2 participants 3 messages Jun 3, 2025 – Jun 4, 2025

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Revert "timezone: Fix compare_file comparison in timezone checking"

This reverts commit f20ccd19a62bd01aa117bfc8e7c388d2a16cde05.

compare_file is intended to work with symlinks, enabling full and accurate matching between the requested timezone and the zoneinfo database.

---
Changes v2 -> v3:
- Added to version v3

 src/timezone.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/timezone.c +2 -2
index fba8b925..89c44895 100644
@@ -124,8 +124,8 @@ static int compare_file(void *src_map, struct stat *src_st,
 
 	DBG("real path %s path name %s", real_path, pathname);
 
-	if (real_path && !g_strcmp0(real_path, pathname))
-		return 0;
+	if (real_path && g_strcmp0(real_path, pathname))
+		return -1;
 
 	fd = open(pathname, O_RDONLY | O_CLOEXEC);
 	if (fd < 0)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 2/2 timezone: Replace Localtime file copy with symbolic link

Based on the man 5 localtime documentation the /etc/localtime file is expected to be a symbolic link, not a standalone regular file.

This change replaces write_file with write_symlink, avoiding unnecessary file I/O and memory mapping. It ensures Localtime accurately reflects the selected timezone without duplicating data. This also fixes __connman_timezone_lookup(), which requires Localtime to be a symlink to function correctly.

---

Changes v2 -> v3:
- Splitted commit

Changes v1 -> v2:
- Change commit message

 src/timezone.c | 49 +++++++++++++------------------------------------
 1 file changed, 13 insertions(+), 36 deletions(-)
src/timezone.c +13 -36
index 89c44895..9b820477 100644
@@ -507,28 +507,20 @@ done:
 	return zone;
 }
 
-static int write_file(void *src_map, struct stat *src_st, const char *pathname)
+static int write_symlink(const char *target, const char *link)
 {
 	struct stat st;
-	int fd;
-	ssize_t written;
 
-	DBG("pathname %s", pathname);
+	DBG("pathname %s", link);
 
-	if (lstat(pathname, &st) == 0) {
-		if (S_ISLNK(st.st_mode))
-			unlink(pathname);
+	// Remove existing file or symlink if it exists
+	if (lstat(link, &st) == 0) {
+		if (S_ISLNK(st.st_mode) || S_ISREG(st.st_mode))
+			unlink(link);
 	}
 
-	fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
-	if (fd < 0)
-		return -EIO;
-
-	written = write(fd, src_map, src_st->st_size);
-
-	close(fd);
-
-	if (written < 0)
+	// Create the symbolic link
+	if (symlink(target, link) < 0)
 		return -EIO;
 
 	return 0;
@@ -537,33 +529,18 @@ static int write_file(void *src_map, struct stat *src_st, const char *pathname)
 int __connman_timezone_change(const char *zone)
 {
 	struct stat st;
-	char *map, pathname[PATH_MAX];
-	int fd, err;
+	char pathname[PATH_MAX];
+	int err;
 
 	DBG("zone %s", zone);
 
 	snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
 
-	fd = open(pathname, O_RDONLY | O_CLOEXEC);
-	if (fd < 0)
+	// Just check that the file exists and is a regular file
+	if (stat(pathname, &st) < 0 || !S_ISREG(st.st_mode))
 		return -EINVAL;
 
-	if (fstat(fd, &st) < 0) {
-		close(fd);
-		return -EIO;
-	}
-
-	map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-	if (!map || map == MAP_FAILED) {
-		close(fd);
-		return -EIO;
-	}
-
-	err = write_file(map, &st, connman_setting_get_string("Localtime"));
-
-	munmap(map, st.st_size);
-
-	close(fd);
+	err = write_symlink(pathname, connman_setting_get_string("Localtime"));
 
 	return err;
 }
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 2/2] timezone: Replace Localtime file copy with symbolic link

Hi Andrea,

Quoted 11 lines · Andrea Ricchi

Based on the man 5 localtime documentation the /etc/localtime file is expected to be a symbolic link, not a standalone regular file.

This change replaces write_file with write_symlink, avoiding unnecessary file I/O and memory mapping. It ensures Localtime accurately reflects the selected timezone without duplicating data. This also fixes __connman_timezone_lookup(), which requires Localtime to be a symlink to function correctly.

nit: ConnMan doesn't use this tag, so please leave it out.

Quoted 23 lines

---

Changes v2 -> v3:
- Splitted commit

Changes v1 -> v2:
- Change commit message

  src/timezone.c | 49 +++++++++++++------------------------------------
  1 file changed, 13 insertions(+), 36 deletions(-)
src/timezone.c +1 -1
index 89c44895..9b820477 100644
@@ -507,28 +507,20 @@ done:
  	return zone;
  }
 
-static int write_file(void *src_map, struct stat *src_st, const char *pathname)
+static int write_symlink(const char *target, const char *link)
  {
  	struct stat st;

src/timezone.c done: · line 516

Quoted 10 lines
src/timezone.c +2 -6
-	int fd;
-	ssize_t written;
 
-	DBG("pathname %s", pathname);
+	DBG("pathname %s", link);
 
-	if (lstat(pathname, &st) == 0) {
-		if (S_ISLNK(st.st_mode))
-			unlink(pathname);
+	// Remove existing file or symlink if it exists

No C++ style comments please

Quoted 22 lines
src/timezone.c +5 -9
+	if (lstat(link, &st) == 0) {
+		if (S_ISLNK(st.st_mode) || S_ISREG(st.st_mode))
+			unlink(link);
  	}
 
-	fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
-	if (fd < 0)
-		return -EIO;
-
-	written = write(fd, src_map, src_st->st_size);
-
-	close(fd);
-
-	if (written < 0)
+	// Create the symbolic link
+	if (symlink(target, link) < 0)
  		return -EIO;
 
  	return 0;
@@ -537,33 +529,18 @@ static int write_file(void *src_map, struct stat *src_st, const char *pathname)
  int __connman_timezone_change(const char *zone)
  {

src/timezone.c static int write_file(void *src_map, struct stat *src_st, const char *pathname) · line 538

Quoted 10 lines
src/timezone.c +2 -2
  	struct stat st;
-	char *map, pathname[PATH_MAX];
-	int fd, err;
+	char pathname[PATH_MAX];
+	int err;
 
  	DBG("zone %s", zone);
 
  	snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
 

It is really unlikely that the use of snprintf will cause problems, but generally its use along with PATH_MAX is discouraged. Given that this is off the hot path, I'd recommend using something like:

g_autofree char *pathname = NULL;
...
pathname = g_strdup_printf("%s/%s", USR_SHARE_ZONEINFO, zone);

src/timezone.c static int write_file(void *src_map, struct stat *src_st, const char *pathname) · line 539

Quoted 3 lines
src/timezone.c +1 -2
-	fd = open(pathname, O_RDONLY | O_CLOEXEC);
-	if (fd < 0)
+	// Just check that the file exists and is a regular file

C++ comments again :)

Quoted 10 lines
src/timezone.c +1 -7
+	if (stat(pathname, &st) < 0 || !S_ISREG(st.st_mode))
  		return -EINVAL;
 
-	if (fstat(fd, &st) < 0) {
-		close(fd);
-		return -EIO;
-	}
-
-	map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-	if (!map || map == MAP_FAILED) {

src/timezone.c static int write_file(void *src_map, struct stat *src_st, const char *pathname) · line 543

Quoted 10 lines
src/timezone.c +1 -9
-		close(fd);
-		return -EIO;
-	}
-
-	err = write_file(map, &st, connman_setting_get_string("Localtime"));
-
-	munmap(map, st.st_size);
-
-	close(fd);
+	err = write_symlink(pathname, connman_setting_get_string("Localtime"));

Might as well just use:

return write_symlink(...) and drop 'err' variable entirely.

src/timezone.c static int write_file(void *src_map, struct stat *src_st, const char *pathname) · line 546

Quoted 3 lines
src/timezone.c +0 -0
  	return err;
  }

Regards,
-Denis