chiark / gitweb /
timedate: assorted improvements
authorLennart Poettering <lennart@poettering.net>
Fri, 14 Sep 2012 18:02:52 +0000 (20:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 14 Sep 2012 18:02:52 +0000 (20:02 +0200)
- Make writing/reading of /etc/timezone dependendent of HAVE_SYSV_COMPAT

- Introduce symlink_atomic() after all, and use it

- Use relative symlink for /etc/localtime

TODO
src/shared/path-util.c
src/shared/path-util.h
src/shared/util.c
src/shared/util.h
src/timedate/timedated.c

diff --git a/TODO b/TODO
index 887d684ced04244e059e31d51da091afa6e09e85..78da71791201fb5a9898b2719fbf86527270619c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -53,6 +53,10 @@ Bugfixes:
 
 Features:
 
+* hw watchdog: optionally try to use the preset watchdog timeout instead of always overriding it
+
+* after deserializing sockets in socket.c we should reapply sockopts and things
+
 * journald: warn if we drop messages we forward to the syslog socket
 
 * does vasprintf advance the struct vaargs? http://pastie.org/pastes/4712773/text
index b51a68d6d2ff7c47d1949cdbc3f2497f064dc9f1..70c8a8af06f9b2ea9db57784db96a405dc272e88 100644 (file)
@@ -264,12 +264,12 @@ char *path_kill_slashes(char *path) {
         return path;
 }
 
-bool path_startswith(const char *path, const char *prefix) {
+char* path_startswith(const char *path, const char *prefix) {
         assert(path);
         assert(prefix);
 
         if ((path[0] == '/') != (prefix[0] == '/'))
-                return false;
+                return NULL;
 
         for (;;) {
                 size_t a, b;
@@ -278,19 +278,19 @@ bool path_startswith(const char *path, const char *prefix) {
                 prefix += strspn(prefix, "/");
 
                 if (*prefix == 0)
-                        return true;
+                        return (char*) path;
 
                 if (*path == 0)
-                        return false;
+                        return NULL;
 
                 a = strcspn(path, "/");
                 b = strcspn(prefix, "/");
 
                 if (a != b)
-                        return false;
+                        return NULL;
 
                 if (memcmp(path, prefix, a) != 0)
-                        return false;
+                        return NULL;
 
                 path += a;
                 prefix += b;
index a441783b1506f5724e08ffbccd43ff5307c189dc..e81821a28fcbed829f5534452fd6191c0c163f5e 100644 (file)
@@ -32,7 +32,7 @@ bool path_is_absolute(const char *p);
 char *path_make_absolute(const char *p, const char *prefix);
 char *path_make_absolute_cwd(const char *p);
 char *path_kill_slashes(char *path);
-bool path_startswith(const char *path, const char *prefix);
+char *path_startswith(const char *path, const char *prefix);
 bool path_equal(const char *a, const char *b);
 
 char **path_strv_make_absolute_cwd(char **l);
index 6a40cf1d805d0d991a2b2dc6df244f76fa4f1a90..add3fdce53906bc89441facbbfdcc248f4c0785a 100644 (file)
@@ -4644,49 +4644,9 @@ int copy_file(const char *from, const char *to) {
         return 0;
 }
 
-int symlink_or_copy(const char *from, const char *to) {
-        char *pf = NULL, *pt = NULL;
-        struct stat a, b;
-        int r;
-
-        assert(from);
-        assert(to);
-
-        if (path_get_parent(from, &pf) < 0 ||
-            path_get_parent(to, &pt) < 0) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        if (stat(pf, &a) < 0 ||
-            stat(pt, &b) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
-        if (a.st_dev != b.st_dev) {
-                free(pf);
-                free(pt);
-
-                return copy_file(from, to);
-        }
-
-        if (symlink(from, to) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
-        r = 0;
-
-finish:
-        free(pf);
-        free(pt);
-
-        return r;
-}
-
-int symlink_or_copy_atomic(const char *from, const char *to) {
-        char *t, *x;
+int symlink_atomic(const char *from, const char *to) {
+        char *x;
+        _cleanup_free_ char *t;
         const char *fn;
         size_t k;
         unsigned long long ull;
@@ -4714,22 +4674,16 @@ int symlink_or_copy_atomic(const char *from, const char *to) {
 
         *x = 0;
 
-        r = symlink_or_copy(from, t);
-        if (r < 0) {
-                unlink(t);
-                free(t);
-                return r;
-        }
+        if (symlink(from, t) < 0)
+                return -errno;
 
         if (rename(t, to) < 0) {
                 r = -errno;
                 unlink(t);
-                free(t);
                 return r;
         }
 
-        free(t);
-        return r;
+        return 0;
 }
 
 bool display_is_local(const char *display) {
index a3f825b5bb9f3f6cd78734e3dec34d997d16615d..2b75ba62f97f459e422f7ae8e9e372548e70ac83 100644 (file)
@@ -430,8 +430,8 @@ int terminal_vhangup(const char *name);
 int vt_disallocate(const char *name);
 
 int copy_file(const char *from, const char *to);
-int symlink_or_copy(const char *from, const char *to);
-int symlink_or_copy_atomic(const char *from, const char *to);
+
+int symlink_atomic(const char *from, const char *to);
 
 int fchmod_umask(int fd, mode_t mode);
 
index 9ca2eec5a8aa18ea3ae7ad0443c8c5721a3f14ff..acfb507548aad462736391230bae0ceb292c7a4a 100644 (file)
@@ -34,6 +34,7 @@
 #include "def.h"
 #include "hwclock.h"
 #include "conf-files.h"
+#include "path-util.h"
 
 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
@@ -76,9 +77,6 @@
         BUS_GENERIC_INTERFACES_LIST             \
         "org.freedesktop.timedate1\0"
 
-/* Must start and end with '/' */
-#define ZONEINFO_PATH "/usr/share/zoneinfo/"
-
 const char timedate_interface[] _introspect_("timedate1") = INTERFACE;
 
 typedef struct TZ {
@@ -132,7 +130,7 @@ static bool valid_timezone(const char *name) {
         if (slash)
                 return false;
 
-        t = strappend(ZONEINFO_PATH, name);
+        t = strappend("/usr/share/zoneinfo/", name);
         if (!t)
                 return false;
 
@@ -148,56 +146,29 @@ static bool valid_timezone(const char *name) {
         return true;
 }
 
-static void verify_timezone(void) {
-        char *p, *a = NULL, *b = NULL;
-        size_t l, q;
-        int j, k;
-
-        if (!tz.zone)
-                return;
-
-        p = strappend(ZONEINFO_PATH, tz.zone);
-        if (!p) {
-                log_oom();
-                return;
-        }
-
-        k = read_full_file(p, &b, &q);
-        free(p);
-
-        j = read_full_file("/etc/localtime", &a, &l);
-
-        if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
-                log_warning("/etc/localtime and /etc/timezone out of sync.");
-                free(tz.zone);
-                tz.zone = NULL;
-        }
-
-        free(a);
-        free(b);
-}
-
 static int read_data(void) {
         int r;
-        char *t = NULL;
+        _cleanup_free_ char *t = NULL;
 
         free_data();
 
         r = readlink_malloc("/etc/localtime", &t);
         if (r < 0) {
                 if (r == -EINVAL)
-                        log_warning("/etc/localtime should be a symbolic link to a timezone data file in " ZONEINFO_PATH);
+                        log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
                 else
-                        log_warning("Failed to get target of %s: %s", "/etc/localtime", strerror(-r));
+                        log_warning("Failed to get target of /etc/localtime: %s", strerror(-r));
         } else {
-                /* we only support the trivial relative link of (/etc/)..$ABSOLUTE */
-                int rel_link_offset = startswith(t, "..") ? strlen("..") : 0;
+                const char *e;
 
-                if (!startswith(t + rel_link_offset, ZONEINFO_PATH))
-                        log_warning("/etc/localtime should be a symbolic link to a timezone data file in " ZONEINFO_PATH);
+                e = path_startswith(t, "/usr/share/zoneinfo/");
+                if (!e)
+                        e = path_startswith(t, "../usr/share/zoneinfo/");
+
+                if (!e)
+                        log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
                 else {
-                        tz.zone = strdup(t + rel_link_offset + strlen(ZONEINFO_PATH));
-                        free(t);
+                        tz.zone = strdup(e);
                         if (!tz.zone)
                                 return log_oom();
 
@@ -205,8 +176,7 @@ static int read_data(void) {
                 }
         }
 
-        free(t);
-
+#ifdef HAVE_SYSV_COMPAT
         r = read_one_line_file("/etc/timezone", &tz.zone);
         if (r < 0) {
                 if (r != -ENOENT)
@@ -221,6 +191,7 @@ static int read_data(void) {
                         log_warning("Failed to read /etc/sysconfig/clock: %s", strerror(-r));
 #endif
         }
+#endif
 
 have_timezone:
         if (isempty(tz.zone)) {
@@ -228,8 +199,6 @@ have_timezone:
                 tz.zone = NULL;
         }
 
-        verify_timezone();
-
         tz.local_rtc = hwclock_is_localtime() > 0;
 
         return 0;
@@ -237,34 +206,36 @@ have_timezone:
 
 static int write_data_timezone(void) {
         int r = 0;
-        char *p;
+        _cleanup_free_ char *p = NULL;
         struct stat st;
 
         if (!tz.zone) {
-                if (unlink("/etc/timezone") < 0 && errno != ENOENT)
+                if (unlink("/etc/localtime") < 0 && errno != ENOENT)
                         r = -errno;
 
-                if (unlink("/etc/localtime") < 0 && errno != ENOENT)
+#ifdef HAVE_SYSV_COMPAT
+                if (unlink("/etc/timezone") < 0 && errno != ENOENT)
                         r = -errno;
+#endif
 
                 return r;
         }
 
-        p = strappend(ZONEINFO_PATH, tz.zone);
+        p = strappend("../usr/share/zoneinfo/", tz.zone);
         if (!p)
                 return log_oom();
 
-        r = symlink(p, "/etc/localtime");
-        free(p);
-
+        r = symlink_atomic(p, "/etc/localtime");
         if (r < 0)
-                return -errno;
+                return r;
 
+#ifdef HAVE_SYSV_COMPAT
         if (stat("/etc/timezone", &st) == 0 && S_ISREG(st.st_mode)) {
                 r = write_one_line_file_atomic("/etc/timezone", tz.zone);
                 if (r < 0)
                         return r;
         }
+#endif
 
         return 0;
 }