[systemd-commits] 2 commits - src/shutdownd.c src/util.c src/utmp-wtmp.c

Lennart Poettering lennart at kemper.freedesktop.org
Mon Aug 16 12:25:19 PDT 2010


 src/shutdownd.c |   18 ++++++------------
 src/util.c      |    5 +++--
 src/utmp-wtmp.c |   17 +++++++----------
 3 files changed, 16 insertions(+), 24 deletions(-)

New commits:
commit 618e02c7b7f8e11fdb954a019342d745937baf55
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Aug 16 21:25:09 2010 +0200

    util: properly detect ttyname_r() failing

diff --git a/src/util.c b/src/util.c
index bc227f5..3bcce2f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2531,11 +2531,12 @@ char* getlogname_malloc(void) {
 
 int getttyname_malloc(char **r) {
         char path[PATH_MAX], *p, *c;
+        int k;
 
         assert(r);
 
-        if (ttyname_r(STDIN_FILENO, path, sizeof(path)) < 0)
-                return -errno;
+        if ((k = ttyname_r(STDIN_FILENO, path, sizeof(path))) != 0)
+                return -k;
 
         char_array_0(path);
 
commit 116205924ef498ab42df233c57e1c4357618ed8a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Aug 16 21:24:50 2010 +0200

    util: use format_timestamp() instead of ctime() wherever possible

diff --git a/src/shutdownd.c b/src/shutdownd.c
index 7f5e66d..d889824 100644
--- a/src/shutdownd.c
+++ b/src/shutdownd.c
@@ -107,15 +107,10 @@ static void warn_wall(struct shutdownd_command *c) {
         if (c->wall_message[0])
                 utmp_wall(c->wall_message);
         else {
-                time_t s;
-                char buf[27];
+                char date[FORMAT_TIMESTAMP_MAX];
                 const char* prefix;
                 char *l;
 
-                s = c->elapse / USEC_PER_SEC;
-                ctime_r(&s, buf);
-
-
                 if (c->mode == 'H')
                         prefix = "The system is going down for system halt at";
                 else if (c->mode == 'P')
@@ -125,7 +120,9 @@ static void warn_wall(struct shutdownd_command *c) {
                 else
                         assert_not_reached("Unknown mode!");
 
-                if (asprintf(&l, "%s %s!", prefix, strstrip(buf)) < 0)
+                if (asprintf(&l, "%s %s!",
+                             prefix,
+                             format_timestamp(date, sizeof(date), c->elapse)) < 0)
                         log_error("Failed to allocate wall message");
                 else {
                         utmp_wall(l);
@@ -260,8 +257,7 @@ int main(int argc, char *argv[]) {
                                 goto finish;
                         else if (k > 0 && c.elapse > 0) {
                                 struct itimerspec its;
-                                char buf[27];
-
+                                char date[FORMAT_TIMESTAMP_MAX];
 
                                 if (c.warn_wall) {
                                         /* Send wall messages every so often */
@@ -294,11 +290,9 @@ int main(int argc, char *argv[]) {
                                         goto finish;
                                 }
 
-                                ctime_r(&its.it_value.tv_sec, buf);
-
                                 sd_notifyf(false,
                                            "STATUS=Shutting down at %s...",
-                                           strstrip(buf));
+                                           format_timestamp(date, sizeof(date), c.elapse));
                         }
                 }
 
diff --git a/src/utmp-wtmp.c b/src/utmp-wtmp.c
index de3805e..77baaff 100644
--- a/src/utmp-wtmp.c
+++ b/src/utmp-wtmp.c
@@ -290,10 +290,9 @@ finish:
 
 int utmp_wall(const char *message) {
         struct utmpx *u;
-        char date[26];
+        char date[FORMAT_TIMESTAMP_MAX];
         char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
         int r;
-        time_t t;
 
         if (!(hn = gethostname_malloc()) ||
             !(un = getlogname_malloc())) {
@@ -301,18 +300,16 @@ int utmp_wall(const char *message) {
                 goto finish;
         }
 
-        if ((r = getttyname_malloc(&tty)) < 0)
-                goto finish;
-
-        time(&t);
-        assert_se(ctime_r(&t, date));
-        delete_chars(date, "\n\r");
+        getttyname_malloc(&tty);
 
         if (asprintf(&text,
                      "\a\r\n"
-                     "Broadcast message from %s@%s on %s (%s):\r\n\r\n"
+                     "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
                      "%s\r\n\r\n",
-                     un, hn, tty, date, message) < 0) {
+                     un, hn,
+                     tty ? " on " : "", strempty(tty),
+                     format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
+                     message) < 0) {
                 r = -ENOMEM;
                 goto finish;
         }


More information about the systemd-commits mailing list