INN commit: trunk (3 files)

INN Commit rra at isc.org
Thu Aug 18 14:10:05 UTC 2011


    Date: Thursday, August 18, 2011 @ 07:10:05
  Author: iulius
Revision: 9348

update the messages library from rra-c-util 3.7

* New prototypes for message_handlers_* and message_log_* functions:
size_t for the count/len argument, instead of a signed int.

* Add a flush at the end of message_log_stdout.

Modified:
  trunk/include/inn/messages.h
  trunk/lib/messages.c
  trunk/tests/lib/messages-t.c

------------------------+
 include/inn/messages.h |   26 ++---
 lib/messages.c         |   55 ++++++------
 tests/lib/messages-t.c |  214 ++++++++++++++++++++++++++++-------------------
 3 files changed, 170 insertions(+), 125 deletions(-)

Modified: include/inn/messages.h
===================================================================
--- include/inn/messages.h	2011-08-18 13:56:07 UTC (rev 9347)
+++ include/inn/messages.h	2011-08-18 14:10:05 UTC (rev 9348)
@@ -54,25 +54,25 @@
    take a count of the number of handlers and then function pointers for each
    of those handlers.  These functions are not thread-safe; they set global
    variables. */
-extern void message_handlers_debug(int count, ...);
-extern void message_handlers_notice(int count, ...);
-extern void message_handlers_warn(int count, ...);
-extern void message_handlers_die(int count, ...);
+extern void message_handlers_debug(size_t count, ...);
+extern void message_handlers_notice(size_t count, ...);
+extern void message_handlers_warn(size_t count, ...);
+extern void message_handlers_die(size_t count, ...);
 
 /* Some useful handlers, intended to be passed to message_handlers_*.  All
    handlers take the length of the formatted message, the format, a variadic
    argument list, and the errno setting if any. */
-extern void message_log_stdout(int, const char *, va_list, int);
-extern void message_log_stderr(int, const char *, va_list, int);
-extern void message_log_syslog_debug(int, const char *, va_list, int);
-extern void message_log_syslog_info(int, const char *, va_list, int);
-extern void message_log_syslog_notice(int, const char *, va_list, int);
-extern void message_log_syslog_warning(int, const char *, va_list, int);
-extern void message_log_syslog_err(int, const char *, va_list, int);
-extern void message_log_syslog_crit(int, const char *, va_list, int);
+extern void message_log_stdout(size_t, const char *, va_list, int);
+extern void message_log_stderr(size_t, const char *, va_list, int);
+extern void message_log_syslog_debug(size_t, const char *, va_list, int);
+extern void message_log_syslog_info(size_t, const char *, va_list, int);
+extern void message_log_syslog_notice(size_t, const char *, va_list, int);
+extern void message_log_syslog_warning(size_t, const char *, va_list, int);
+extern void message_log_syslog_err(size_t, const char *, va_list, int);
+extern void message_log_syslog_crit(size_t, const char *, va_list, int);
 
 /* The type of a message handler. */
-typedef void (*message_handler_func)(int, const char *, va_list, int);
+typedef void (*message_handler_func)(size_t, const char *, va_list, int);
 
 /* If non-NULL, called before exit and its return value passed to exit. */
 extern int (*message_fatal_cleanup)(void);

Modified: lib/messages.c
===================================================================
--- lib/messages.c	2011-08-18 13:56:07 UTC (rev 9347)
+++ lib/messages.c	2011-08-18 14:10:05 UTC (rev 9348)
@@ -86,9 +86,9 @@
 **  the handler list, the count of handlers, and the argument list.
 */
 static void
-message_handlers(message_handler_func **list, int count, va_list args)
+message_handlers(message_handler_func **list, size_t count, va_list args)
 {
-    int i;
+    size_t i;
 
     if (*list != stdout_handlers && *list != stderr_handlers)
         free(*list);
@@ -106,7 +106,7 @@
 */
 #define HANDLER_FUNCTION(type)                                  \
     void                                                        \
-    message_handlers_ ## type(int count, ...)                   \
+    message_handlers_ ## type(size_t count, ...)                \
     {                                                           \
         va_list args;                                           \
                                                                 \
@@ -124,7 +124,7 @@
 **  Print a message to stdout, supporting message_program_name.
 */
 void
-message_log_stdout(int len UNUSED, const char *fmt, va_list args, int err)
+message_log_stdout(size_t len UNUSED, const char *fmt, va_list args, int err)
 {
     if (message_program_name != NULL)
         fprintf(stdout, "%s: ", message_program_name);
@@ -132,6 +132,7 @@
     if (err)
         fprintf(stdout, ": %s", strerror(err));
     fprintf(stdout, "\n");
+    fflush(stdout);
 }
 
 
@@ -140,7 +141,7 @@
 **  stdout so that errors and regular output occur in the right order.
 */
 void
-message_log_stderr(int len UNUSED, const char *fmt, va_list args, int err)
+message_log_stderr(size_t len UNUSED, const char *fmt, va_list args, int err)
 {
     fflush(stdout);
     if (message_program_name != NULL)
@@ -159,14 +160,14 @@
 **  argument.
 */
 static void
-message_log_syslog(int pri, int len, const char *fmt, va_list args, int err)
+message_log_syslog(int pri, size_t len, const char *fmt, va_list args, int err)
 {
     char *buffer;
 
     buffer = malloc(len + 1);
     if (buffer == NULL) {
-        fprintf(stderr, "failed to malloc %u bytes at %s line %d: %s",
-                len + 1, __FILE__, __LINE__, strerror(errno));
+        fprintf(stderr, "failed to malloc %lu bytes at %s line %d: %s",
+                (unsigned long) len + 1, __FILE__, __LINE__, strerror(errno));
         exit(message_fatal_cleanup ? (*message_fatal_cleanup)() : 1);
     }
     vsnprintf(buffer, len + 1, fmt, args);
@@ -182,11 +183,11 @@
 **  Do the same sort of wrapper to generate all of the separate syslog logging
 **  functions.
 */
-#define SYSLOG_FUNCTION(name, type)                                     \
-    void                                                                \
-    message_log_syslog_ ## name(int l, const char *f, va_list a, int e) \
-    {                                                                   \
-        message_log_syslog(LOG_ ## type, l, f, a, e);                   \
+#define SYSLOG_FUNCTION(name, type)                                        \
+    void                                                                   \
+    message_log_syslog_ ## name(size_t l, const char *f, va_list a, int e) \
+    {                                                                      \
+        message_log_syslog(LOG_ ## type, l, f, a, e);                      \
     }
 SYSLOG_FUNCTION(debug,   DEBUG)
 SYSLOG_FUNCTION(info,    INFO)
@@ -208,7 +209,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
 
     if (debug_handlers == NULL)
         return;
@@ -219,7 +220,7 @@
         return;
     for (log = debug_handlers; *log != NULL; log++) {
         va_start(args, format);
-        (**log)(length, format, args, 0);
+        (**log)((size_t) length, format, args, 0);
         va_end(args);
     }
 }
@@ -232,7 +233,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
 
     va_start(args, format);
     length = vsnprintf(NULL, 0, format, args);
@@ -241,7 +242,7 @@
         return;
     for (log = notice_handlers; *log != NULL; log++) {
         va_start(args, format);
-        (**log)(length, format, args, 0);
+        (**log)((size_t) length, format, args, 0);
         va_end(args);
     }
 }
@@ -251,7 +252,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
     int error = errno;
 
     va_start(args, format);
@@ -261,7 +262,7 @@
         return;
     for (log = notice_handlers; *log != NULL; log++) {
         va_start(args, format);
-        (**log)(length, format, args, error);
+        (**log)((size_t) length, format, args, error);
         va_end(args);
     }
 }
@@ -271,7 +272,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
 
     va_start(args, format);
     length = vsnprintf(NULL, 0, format, args);
@@ -280,7 +281,7 @@
         return;
     for (log = warn_handlers; *log != NULL; log++) {
         va_start(args, format);
-        (**log)(length, format, args, 0);
+        (**log)((size_t) length, format, args, 0);
         va_end(args);
     }
 }
@@ -290,7 +291,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
     int error = errno;
 
     va_start(args, format);
@@ -300,7 +301,7 @@
         return;
     for (log = warn_handlers; *log != NULL; log++) {
         va_start(args, format);
-        (**log)(length, format, args, error);
+        (**log)((size_t) length, format, args, error);
         va_end(args);
     }
 }
@@ -310,7 +311,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
 
     va_start(args, format);
     length = vsnprintf(NULL, 0, format, args);
@@ -318,7 +319,7 @@
     if (length >= 0)
         for (log = die_handlers; *log != NULL; log++) {
             va_start(args, format);
-            (**log)(length, format, args, 0);
+            (**log)((size_t) length, format, args, 0);
             va_end(args);
         }
     exit(message_fatal_cleanup ? (*message_fatal_cleanup)() : 1);
@@ -329,7 +330,7 @@
 {
     va_list args;
     message_handler_func *log;
-    int length;
+    ssize_t length;
     int error = errno;
 
     va_start(args, format);
@@ -338,7 +339,7 @@
     if (length >= 0)
         for (log = die_handlers; *log != NULL; log++) {
             va_start(args, format);
-            (**log)(length, format, args, error);
+            (**log)((size_t) length, format, args, error);
             va_end(args);
         }
     exit(message_fatal_cleanup ? (*message_fatal_cleanup)() : 1);

Modified: tests/lib/messages-t.c
===================================================================
--- tests/lib/messages-t.c	2011-08-18 13:56:07 UTC (rev 9347)
+++ tests/lib/messages-t.c	2011-08-18 14:10:05 UTC (rev 9348)
@@ -1,6 +1,37 @@
-/* $Id$ */
-/* Test suite for error handling routines. */
+/*
+ * Test suite for error handling routines.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ * Copyright 2002, 2004, 2005 Russ Allbery <rra at stanford.edu>
+ * Copyright 2009, 2010
+ *     The Board of Trustees of the Leland Stanford Junior University
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
 
+#define LIBTEST_NEW_FORMAT 1
+
 #include "config.h"
 #include "clibrary.h"
 #include <errno.h>
@@ -12,31 +43,38 @@
 #include "inn/libinn.h"
 #include "libtest.h"
 
-#define END     (char *) 0
+typedef void (*test_function_type)(void);
+void is_function_output(test_function_type, int status, const char *output,
+                        const char *format, ...)
+    __attribute__((__format__(printf, 4, 5)));
 
-/* Test function type. */
-typedef void (*test_function_t)(void);
-
-/* Fork and execute the provided function, connecting stdout and stderr to a
-   pipe.  Captures the output into the provided buffer and returns the exit
-   status as a waitpid status value. */
-static int
-run_test(test_function_t function, char *buf, size_t buflen)
+/*
+ * Given a function, an expected exit status, and expected output, runs that
+ * function in a subprocess, capturing stdout and stderr via a pipe, and
+ * compare the combination of stdout and stderr with the expected output and
+ * the exit status with the expected status.  Expects the function to always
+ * exit (not die from a signal).
+ */
+void
+is_function_output(test_function_type function, int status, const char *output,
+                   const char *format, ...)
 {
     int fds[2];
     pid_t child;
-    ssize_t count, status;
+    char *buf, *msg;
+    ssize_t count, ret, buflen;
     int rval;
+    va_list args;
 
     /* Flush stdout before we start to avoid odd forking issues. */
     fflush(stdout);
 
     /* Set up the pipe and call the function, collecting its output. */
     if (pipe(fds) == -1)
-        sysdie("can't create pipe");
+        sysbail("can't create pipe");
     child = fork();
     if (child == (pid_t) -1) {
-        sysdie("can't fork");
+        sysbail("can't fork");
     } else if (child == 0) {
         /* In child.  Set up our stdout and stderr. */
         close(fds[0]);
@@ -50,23 +88,43 @@
         fflush(stdout);
         _exit(0);
     } else {
-        /* In the parent; close the extra file descriptor, read the output
-           if any, and then collect the exit status. */
+        /*
+         * In the parent; close the extra file descriptor, read the output if
+         * any, and then collect the exit status.
+         */
         close(fds[1]);
+        buflen = BUFSIZ;
+        buf = xmalloc(buflen);
         count = 0;
         do {
-            status = read(fds[0], buf + count, buflen - count - 1);
-            if (status > 0)
-                count += status;
-        } while (status > 0);
+            ret = read(fds[0], buf + count, buflen - count - 1);
+            if (ret > 0)
+                count += ret;
+            if (count >= buflen - 1) {
+                buflen += BUFSIZ;
+                buf = xrealloc(buf, buflen);
+            }
+        } while (ret > 0);
         buf[count < 0 ? 0 : count] = '\0';
         if (waitpid(child, &rval, 0) == (pid_t) -1)
-            sysdie("waitpid failed");
+            sysbail("waitpid failed");
     }
-    return rval;
+
+    /* Now, check the results against what we expected. */
+    va_start(args, format);
+    if (xvasprintf(&msg, format, args) < 0)
+        bail("cannot format test description");
+    va_end(args);
+    ok(WIFEXITED(rval), "%s (exited)", msg);
+    is_int(status, WEXITSTATUS(rval), "%s (status)", msg);
+    is_string(output, buf, "%s (output)", msg);
+    free(buf);
+    free(msg);
 }
 
-/* Test functions. */
+/*
+ * Test functions.
+ */
 static void test1(void) { warn("warning"); }
 static void test2(void) { die("fatal"); }
 static void test3(void) { errno = EPERM; syswarn("permissions"); }
@@ -172,87 +230,73 @@
     notice("third");
 }
 
-/* Given the test number, intended exit status and message, and the function
-   to run, print ok or not ok. */
-static void
-test_error(int n, int status, const char *output, test_function_t function)
-{
-    int real_status;
-    char buf[256];
-    int succeeded = 1;
 
-    real_status = run_test(function, buf, sizeof(buf));
-    if (!WIFEXITED(real_status) || status != WEXITSTATUS(real_status)) {
-        diag("  unexpected exit status %d\n", real_status);
-        succeeded = 0;
-    }
-    if (strcmp(output, buf)) {
-        diag("  unexpected output: %s", buf);
-        diag("    expected output: %s", output);
-        succeeded = 0;
-    }
-    ok(n, succeeded);
-}
-
-/* Given the test number, intended status, intended message sans the
-   appended strerror output, errno, and the function to run, print ok or not
-   ok. */
+/*
+ * Given the intended status, intended message sans the appended strerror
+ * output, errno, and the function to run, check the output.
+ */
 static void
-test_strerror(int n, int status, const char *output, int error,
-              test_function_t function)
+test_strerror(int status, const char *output, int error,
+              test_function_type function)
 {
-    char *full_output;
+    char *full_output, *name;
 
-    full_output = concat(output, ": ", strerror(error), "\n", END);
-    test_error(n, status, full_output, function);
+    full_output = concat(output, ": ", strerror(error), "\n", (char *) NULL);
+    xasprintf(&name, "strerror %lu", testnum / 3 + 1);
+    is_function_output(function, status, full_output, "%s", name);
     free(full_output);
+    free(name);
 }
 
-/* Run the tests. */
+
+/*
+ * Run the tests.
+ */
 int
 main(void)
 {
     char buff[32];
+    char *output;
 
-    test_init(24);
+    plan(24 * 3);
 
-    test_error(1, 0, "warning\n", test1);
-    test_error(2, 1, "fatal\n", test2);
-    test_strerror(3, 0, "permissions", EPERM, test3);
-    test_strerror(4, 1, "fatal access", EACCES, test4);
-    test_error(5, 0, "test5: warning\n", test5);
-    test_error(6, 1, "test6: fatal\n", test6);
-    test_strerror(7, 0, "test7: perms 7", EPERM, test7);
-    test_strerror(8, 1, "test8: fatal", EACCES, test8);
-    test_error(9, 10, "fatal\n", test9);
-    test_strerror(10, 10, "fatal perm", EPERM, test10);
-    test_strerror(11, 10, "1st test11: fatal", EPERM, test11);
-    test_error(12, 0, "7 0 warning\n", test12);
-    test_error(13, 1, "5 0 fatal\n", test13);
+    is_function_output(test1, 0, "warning\n", "test1");
+    is_function_output(test2, 1, "fatal\n", "test2");
+    test_strerror(0, "permissions", EPERM, test3);
+    test_strerror(1, "fatal access", EACCES, test4);
+    is_function_output(test5, 0, "test5: warning\n", "test5");
+    is_function_output(test6, 1, "test6: fatal\n", "test6");
+    test_strerror(0, "test7: perms 7", EPERM, test7);
+    test_strerror(1, "test8: fatal", EACCES, test8);
+    is_function_output(test9, 10, "fatal\n", "test9");
+    test_strerror(10, "fatal perm", EPERM, test10);
+    test_strerror(10, "1st test11: fatal", EPERM, test11);
+    is_function_output(test12, 0, "7 0 warning\n", "test12");
+    is_function_output(test13, 1, "5 0 fatal\n", "test13");
 
     sprintf(buff, "%d", EPERM);
 
-    test_error(14, 0,
-               concat("7 ", buff, " warning\n7 ", buff, " warning\n", END),
-               test14);
-    test_error(15, 10,
-               concat("5 ", buff, " fatal\n5 ", buff, " fatal\n", END),
-               test15);
-    test_error(16, 0,
-               concat("test16: warning: ", strerror(EPERM), "\n7 ", buff,
-                      " warning\n", END),
-               test16);
+    xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM);
+    is_function_output(test14, 0, output, "test14");
+    free(output);
+    xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM);
+    is_function_output(test15, 10, output, "test15");
+    free(output);
+    xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM),
+              EPERM);
+    is_function_output(test16, 0, output, "test16");
+    free(output);
 
-    test_error(17, 0, "notice\n", test17);
-    test_error(18, 0, "test18: notice\n", test18);
-    test_error(19, 0, "", test19);
-    test_error(20, 0, "3 0 foo\n", test20);
-    test_error(21, 0, "test23: baz\n", test21);
+    is_function_output(test17, 0, "notice\n", "test17");
+    is_function_output(test18, 0, "test18: notice\n", "test18");
+    is_function_output(test19, 0, "", "test19");
+    is_function_output(test20, 0, "3 0 foo\n", "test20");
+    is_function_output(test21, 0, "test23: baz\n", "test21");
 
     /* Make sure that it's possible to turn off a message type entirely. */ 
-    test_error(22, 1, "", test22);
-    test_error(23, 0, "", test23);
-    test_error(24, 0, "first\nthird\n", test24);
+    is_function_output(test22, 1, "", "test22");
+    is_function_output(test23, 0, "", "test23");
+    is_function_output(test24, 0, "first\nthird\n", "test24");
 
     return 0;
 }




More information about the inn-committers mailing list