(#97600924) [PATCH v4 1/2] Revert "timezone: Fix compare_file comparison in timezone checking"

Message ID 96d30e276a714ca2804c4cd455895e7b@bytemark.co.uk
State New
Headers show
Series
  • (#97600924) [PATCH v4 1/2] Revert "timezone: Fix compare_file comparison in timezone checking"
Related show

Commit Message

Bytemark June 5, 2025, 8:06 a.m. UTC
-#-#- Please reply above this line -#-#-

You are registered as a CC on this ticket #97600924 and the ticket has been updated. You can reply to this email above or review the ticket by going to:
https://support.bytemark.co.uk/en/tickets/view/97600924

---------------------------------------------------------------

andrea.ricchi@amarulasolutions.com
User - 05/06/2025 09:04
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.
---
src/timezone.c | 50 +++++++++++---------------------------------------
1 file changed, 11 insertions(+), 39 deletions(-)

--
2.34.1
---------------------------------------------------------------

Ticket Details

Ticket #: 97600924
Subject: [PATCH v4 1/2] Revert "timezone: Fix compare_file comparison in timezone checking"
Department: Technical Support
Status: Open
Priority: Standard

You can review the ticket by going to:
https://support.bytemark.co.uk/en/tickets/view/97600924

Kind Regards,
Bytemark

To unsubscribe from this group and stop receiving emails from it, send an email to linux-amarula+unsubscribe@amarulasolutions.com.

Patch

diff --git a/src/timezone.c b/src/timezone.c
index 89c44895..f442bc2a 100644
--- a/src/timezone.c
+++ b/src/timezone.c
@@ -507,28 +507,18 @@  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);
+ 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)
+ if (symlink(target, link) < 0)
return -EIO;

return 0;
@@ -537,35 +527,17 @@  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;
+ g_autofree char *pathname = NULL;
+ int err;

DBG("zone %s", zone);

- snprintf(pathname, PATH_MAX, "%s/%s", USR_SHARE_ZONEINFO, zone);
+ pathname = g_strdup_printf("%s/%s", USR_SHARE_ZONEINFO, zone);

- fd = open(pathname, O_RDONLY | O_CLOEXEC);
- if (fd < 0)
+ 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);
-
- return err;
+ return write_symlink(pathname, connman_setting_get_string("Localtime"));
}

static guint inotify_watch = 0;
--
2.34.1
---------------------------------------------------------------

andrea.ricchi@amarulasolutions.com
User - 05/06/2025 09:04
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.
---
src/timezone.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/timezone.c b/src/timezone.c
index fba8b925..89c44895 100644
--- a/src/timezone.c
+++ b/src/timezone.c
@@ -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)