[systemd-commits] 3 commits - TODO src/cgtop src/journal src/login src/shared src/systemctl

Lennart Poettering lennart at kemper.freedesktop.org
Thu Oct 18 15:08:04 PDT 2012


 TODO                         |    2 
 src/cgtop/cgtop.c            |   10 ++--
 src/journal/journal-verify.c |    4 -
 src/journal/journalctl.c     |   24 +----------
 src/login/loginctl.c         |   17 -------
 src/shared/pager.c           |    2 
 src/shared/util.c            |   93 ++++++++++++++++++++-----------------------
 src/shared/util.h            |    6 +-
 src/systemctl/systemctl.c    |   18 --------
 9 files changed, 62 insertions(+), 114 deletions(-)

New commits:
commit ed757c0cb03eef50e8d9aeb4682401c3e9486f0b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 19 00:06:47 2012 +0200

    util: unify line caching and column caching

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index ee421e3..f2e6276 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -443,9 +443,9 @@ static int display(Hashmap *a) {
 
         qsort(array, n, sizeof(Group*), group_compare);
 
-        rows = fd_lines(STDOUT_FILENO);
-        if (rows <= 0)
-                rows = 25;
+        rows = lines();
+        if (rows <= 10)
+                rows = 10;
 
         path_columns = columns() - 42;
         if (path_columns < 10)
@@ -653,7 +653,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        signal(SIGWINCH, columns_cache_reset);
+        signal(SIGWINCH, columns_lines_cache_reset);
 
         while (!quit) {
                 Hashmap *c;
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index f4b6518..d1338d2 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -197,7 +197,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case 'f':
                         arg_follow = true;
-                        signal(SIGWINCH, columns_cache_reset);
                         break;
 
                 case 'o':
@@ -834,6 +833,8 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
+        signal(SIGWINCH, columns_lines_cache_reset);
+
         if (arg_action == ACTION_NEW_ID128) {
                 r = generate_new_id128();
                 goto finish;
diff --git a/src/shared/util.c b/src/shared/util.c
index 527a580..ef30cb2 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -73,6 +73,7 @@ int saved_argc = 0;
 char **saved_argv = NULL;
 
 static volatile unsigned cached_columns = 0;
+static volatile unsigned cached_lines = 0;
 
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
@@ -3812,20 +3813,6 @@ unsigned columns(void) {
         return c;
 }
 
-/* intended to be used as a SIGWINCH sighandler */
-void columns_cache_reset(int signum) {
-        cached_columns = 0;
-}
-
-bool on_tty(void) {
-        static int cached_on_tty = -1;
-
-        if (_unlikely_(cached_on_tty < 0))
-                cached_on_tty = isatty(STDOUT_FILENO) > 0;
-
-        return cached_on_tty;
-}
-
 int fd_lines(int fd) {
         struct winsize ws;
         zero(ws);
@@ -3840,23 +3827,40 @@ int fd_lines(int fd) {
 }
 
 unsigned lines(void) {
-        static __thread int parsed_lines = 0;
         const char *e;
+        unsigned l;
 
-        if (_likely_(parsed_lines > 0))
-                return parsed_lines;
+        if (_likely_(cached_lines > 0))
+                return cached_lines;
 
+        l = 0;
         e = getenv("LINES");
         if (e)
-                parsed_lines = atoi(e);
+                safe_atou(e, &l);
 
-        if (parsed_lines <= 0)
-                parsed_lines = fd_lines(STDOUT_FILENO);
+        if (l <= 0)
+                l = fd_lines(STDOUT_FILENO);
 
-        if (parsed_lines <= 0)
-                parsed_lines = 25;
+        if (l <= 0)
+                l = 24;
 
-        return parsed_lines;
+        cached_lines = l;
+        return cached_lines;
+}
+
+/* intended to be used as a SIGWINCH sighandler */
+void columns_lines_cache_reset(int signum) {
+        cached_columns = 0;
+        cached_lines = 0;
+}
+
+bool on_tty(void) {
+        static int cached_on_tty = -1;
+
+        if (_unlikely_(cached_on_tty < 0))
+                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+
+        return cached_on_tty;
 }
 
 int running_in_chroot(void) {
diff --git a/src/shared/util.h b/src/shared/util.h
index e19f76c..affb669 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -384,11 +384,11 @@ int status_welcome(void);
 
 int fd_columns(int fd);
 unsigned columns(void);
-void columns_cache_reset(int _unused_ signum);
-bool on_tty(void);
-
 int fd_lines(int fd);
 unsigned lines(void);
+void columns_lines_cache_reset(int _unused_ signum);
+
+bool on_tty(void);
 
 int running_in_chroot(void);
 

commit 8481248b9fbddc6d5e6ff26eb23505ef13dc85f7
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 23:59:41 2012 +0200

    util: unify usage of on_tty() in util.c

diff --git a/TODO b/TODO
index bb7d8ee..1c33ead 100644
--- a/TODO
+++ b/TODO
@@ -19,6 +19,8 @@ F18:
 
 Features:
 
+* drop --follow from systemctl
+
 * don't show cgroup in "systemctl status" if empty/non-existant, especially for foreign .mount units
 
 * timedated: export boolean that clarifies whether NTP is even available
diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c
index 551f04a..1a67d5a 100644
--- a/src/journal/journal-verify.c
+++ b/src/journal/journal-verify.c
@@ -178,7 +178,7 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {
         unsigned n, i, j, k;
         usec_t z, x;
 
-        if (!isatty(STDOUT_FILENO))
+        if (!on_tty())
                 return;
 
         z = now(CLOCK_MONOTONIC);
@@ -212,7 +212,7 @@ static void draw_progress(uint64_t p, usec_t *last_usec) {
 static void flush_progress(void) {
         unsigned n, i;
 
-        if (!isatty(STDOUT_FILENO))
+        if (!on_tty())
                 return;
 
         n = (3 * columns()) / 4;
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index d6f19e9..f4b6518 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -400,21 +400,6 @@ static int parse_argv(int argc, char *argv[]) {
         return 1;
 }
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static int generate_new_id128(void) {
         sd_id128_t id;
         int r;
@@ -697,7 +682,7 @@ static int setup_keys(void) {
                 goto finish;
         }
 
-        if (isatty(STDOUT_FILENO)) {
+        if (on_tty()) {
                 fprintf(stderr,
                         "\n"
                         "The new key pair has been generated. The " ANSI_HIGHLIGHT_ON "secret sealing key" ANSI_HIGHLIGHT_OFF " has been written to\n"
@@ -719,7 +704,7 @@ static int setup_keys(void) {
 
         printf("/%llx-%llx\n", (unsigned long long) n, (unsigned long long) arg_interval);
 
-        if (isatty(STDOUT_FILENO)) {
+        if (on_tty()) {
                 char tsb[FORMAT_TIMESPAN_MAX], *hn;
 
                 fprintf(stderr,
@@ -982,8 +967,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        on_tty();
-
         if (!arg_no_pager && !arg_follow)
                 pager_open();
 
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 97c6617..24941fe 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -50,26 +50,9 @@ static enum transport {
 static bool arg_ask_password = true;
 static const char *arg_host = NULL;
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static void pager_open_if_enabled(void) {
 
         /* Cache result before we open the pager */
-        on_tty();
-
         if (arg_no_pager)
                 return;
 
diff --git a/src/shared/pager.c b/src/shared/pager.c
index 6799787..488a12c 100644
--- a/src/shared/pager.c
+++ b/src/shared/pager.c
@@ -57,7 +57,7 @@ int pager_open(void) {
                 if (!*pager || streq(pager, "cat"))
                         return 0;
 
-        if (isatty(STDOUT_FILENO) <= 0)
+        if (!on_tty())
                 return 0;
 
         /* Determine and cache number of columns before we spawn the
diff --git a/src/shared/util.c b/src/shared/util.c
index 462b541..527a580 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2175,28 +2175,25 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
 }
 
 int ask(char *ret, const char *replies, const char *text, ...) {
-        bool on_tty;
 
         assert(ret);
         assert(replies);
         assert(text);
 
-        on_tty = isatty(STDOUT_FILENO);
-
         for (;;) {
                 va_list ap;
                 char c;
                 int r;
                 bool need_nl = true;
 
-                if (on_tty)
+                if (on_tty())
                         fputs(ANSI_HIGHLIGHT_ON, stdout);
 
                 va_start(ap, text);
                 vprintf(text, ap);
                 va_end(ap);
 
-                if (on_tty)
+                if (on_tty())
                         fputs(ANSI_HIGHLIGHT_OFF, stdout);
 
                 fflush(stdout);
@@ -3820,6 +3817,15 @@ void columns_cache_reset(int signum) {
         cached_columns = 0;
 }
 
+bool on_tty(void) {
+        static int cached_on_tty = -1;
+
+        if (_unlikely_(cached_on_tty < 0))
+                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+
+        return cached_on_tty;
+}
+
 int fd_lines(int fd) {
         struct winsize ws;
         zero(ws);
diff --git a/src/shared/util.h b/src/shared/util.h
index 662c3d1..e19f76c 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -385,6 +385,7 @@ int status_welcome(void);
 int fd_columns(int fd);
 unsigned columns(void);
 void columns_cache_reset(int _unused_ signum);
+bool on_tty(void);
 
 int fd_lines(int fd);
 unsigned lines(void);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 82c801e..d77d534 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -132,26 +132,8 @@ static bool private_bus = false;
 static int daemon_reload(DBusConnection *bus, char **args);
 static void halt_now(enum action a);
 
-static bool on_tty(void) {
-        static int t = -1;
-
-        /* Note that this is invoked relatively early, before we start
-         * the pager. That means the value we return reflects whether
-         * we originally were started on a tty, not if we currently
-         * are. But this is intended, since we want colour and so on
-         * when run in our own pager. */
-
-        if (_unlikely_(t < 0))
-                t = isatty(STDOUT_FILENO) > 0;
-
-        return t;
-}
-
 static void pager_open_if_enabled(void) {
 
-        /* Cache result before we open the pager */
-        on_tty();
-
         if (arg_no_pager)
                 return;
 

commit 28917d7dc711746795f7e6468c06c1983a5cdf53
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Oct 18 23:50:26 2012 +0200

    util: simplify column caching logic

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index 9eb2d2f..ee421e3 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -447,7 +447,7 @@ static int display(Hashmap *a) {
         if (rows <= 0)
                 rows = 25;
 
-        path_columns = columns_uncached() - 42;
+        path_columns = columns() - 42;
         if (path_columns < 10)
                 path_columns = 10;
 
@@ -653,6 +653,8 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        signal(SIGWINCH, columns_cache_reset);
+
         while (!quit) {
                 Hashmap *c;
                 usec_t t;
diff --git a/src/shared/util.c b/src/shared/util.c
index 1c97a8a..462b541 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -72,7 +72,7 @@
 int saved_argc = 0;
 char **saved_argv = NULL;
 
-static int parsed_columns = 0;
+static volatile unsigned cached_columns = 0;
 
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
@@ -3793,46 +3793,31 @@ int fd_columns(int fd) {
         return ws.ws_col;
 }
 
-static unsigned columns_cached(bool cached) {
-        static __thread int env_columns = -1;
+unsigned columns(void) {
         const char *e;
+        unsigned c;
 
-        if (_likely_(parsed_columns > 0 && cached))
-                return parsed_columns;
-
-        if (_unlikely_(env_columns == -1)) {
-                e = getenv("COLUMNS");
-                if (e)
-                        env_columns = atoi(e);
-                else
-                        env_columns = 0;
-        }
-
-        if (env_columns > 0) {
-                parsed_columns = env_columns;
-                return parsed_columns;
-        }
+        if (_likely_(cached_columns > 0))
+                return cached_columns;
 
-        if (parsed_columns <= 0 || !cached)
-                parsed_columns = fd_columns(STDOUT_FILENO);
-
-        if (parsed_columns <= 0)
-                parsed_columns = 80;
+        c = 0;
+        e = getenv("COLUMNS");
+        if (e)
+                safe_atou(e, &c);
 
-        return parsed_columns;
-}
+        if (c <= 0)
+                c = fd_columns(STDOUT_FILENO);
 
-unsigned columns(void) {
-        return columns_cached(true);
-}
+        if (c <= 0)
+                c = 80;
 
-unsigned columns_uncached(void) {
-        return columns_cached(false);
+        cached_columns = c;
+        return c;
 }
 
 /* intended to be used as a SIGWINCH sighandler */
 void columns_cache_reset(int signum) {
-        parsed_columns = 0;
+        cached_columns = 0;
 }
 
 int fd_lines(int fd) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 50911eb..662c3d1 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -384,7 +384,6 @@ int status_welcome(void);
 
 int fd_columns(int fd);
 unsigned columns(void);
-unsigned columns_uncached(void);
 void columns_cache_reset(int _unused_ signum);
 
 int fd_lines(int fd);



More information about the systemd-commits mailing list