Author: asomers
Date: Thu Apr 14 15:24:45 2016
New Revision: 297970
URL: https://svnweb.freebsd.org/changeset/base/297970

Log:
  MFC 297838
  
  Fix an intermittent bug in sbin/devd/client_test.stream
  
  In case where the two events were being received in separate reads, the
  event buffer was being null-terminated at the wrong offset.
  
  Also, factored out some common code between the tests, and fixed a comment.

Modified:
  stable/10/sbin/devd/tests/client_test.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/sbin/devd/tests/client_test.c
==============================================================================
--- stable/10/sbin/devd/tests/client_test.c     Thu Apr 14 14:44:23 2016        
(r297969)
+++ stable/10/sbin/devd/tests/client_test.c     Thu Apr 14 15:24:45 2016        
(r297970)
@@ -34,6 +34,10 @@ __FBSDID("$FreeBSD$");
 #include <sys/un.h>
 
 #include <atf-c.h>
+
+const char create_pat[] = "!system=DEVFS subsystem=CDEV type=CREATE cdev=md";
+const char destroy_pat[] = "!system=DEVFS subsystem=CDEV type=DESTROY cdev=md";
+
 /* Helper functions*/
 
 /*
@@ -63,6 +67,24 @@ create_two_events(void)
        ATF_REQUIRE_EQ(0, pclose(destroy_stdout));
 }
 
+/* Setup and return an open client socket */
+static int
+common_setup(int socktype, const char* sockpath) {
+       struct sockaddr_un devd_addr;
+       int s, error;
+
+       memset(&devd_addr, 0, sizeof(devd_addr));
+       devd_addr.sun_family = PF_LOCAL;
+       strlcpy(devd_addr.sun_path, sockpath, sizeof(devd_addr.sun_path));
+       s = socket(PF_LOCAL, socktype, 0);
+       ATF_REQUIRE(s >= 0);
+       error = connect(s, (struct sockaddr*)&devd_addr, SUN_LEN(&devd_addr));
+       ATF_REQUIRE_EQ(0, error);
+
+       create_two_events();
+       return (s);
+}
+
 /*
  * Test Cases
  */
@@ -75,27 +97,10 @@ ATF_TC_WITHOUT_HEAD(seqpacket);
 ATF_TC_BODY(seqpacket, tc)
 {
        int s;
-       int error;
-       struct sockaddr_un devd_addr;
        bool got_create_event = false;
        bool got_destroy_event = false;
-       const char create_pat[] =
-               "!system=DEVFS subsystem=CDEV type=CREATE cdev=md";
-       const char destroy_pat[] =
-               "!system=DEVFS subsystem=CDEV type=DESTROY cdev=md";
-
-       memset(&devd_addr, 0, sizeof(devd_addr));
-       devd_addr.sun_family = PF_LOCAL;
-       strlcpy(devd_addr.sun_path, "/var/run/devd.seqpacket.pipe",
-                       sizeof(devd_addr.sun_path));
-
-       s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-       ATF_REQUIRE(s >= 0);
-       error = connect(s, (struct sockaddr*)&devd_addr, SUN_LEN(&devd_addr));
-       ATF_REQUIRE_EQ(0, error);
-
-       create_two_events();
 
+       s = common_setup(SOCK_SEQPACKET, "/var/run/devd.seqpacket.pipe");
        /*
         * Loop until both events are detected on _different_ reads
         * There may be extra events due to unrelated system activity
@@ -132,31 +137,14 @@ ATF_TC_WITHOUT_HEAD(stream);
 ATF_TC_BODY(stream, tc)
 {
        int s;
-       int error;
-       struct sockaddr_un devd_addr;
        bool got_create_event = false;
        bool got_destroy_event = false;
-       const char create_pat[] =
-               "!system=DEVFS subsystem=CDEV type=CREATE cdev=md";
-       const char destroy_pat[] =
-               "!system=DEVFS subsystem=CDEV type=DESTROY cdev=md";
        ssize_t len = 0;
 
-       memset(&devd_addr, 0, sizeof(devd_addr));
-       devd_addr.sun_family = PF_LOCAL;
-       strlcpy(devd_addr.sun_path, "/var/run/devd.pipe",
-                       sizeof(devd_addr.sun_path));
-
-       s = socket(PF_LOCAL, SOCK_STREAM, 0);
-       ATF_REQUIRE(s >= 0);
-       error = connect(s, (struct sockaddr*)&devd_addr, SUN_LEN(&devd_addr));
-       ATF_REQUIRE_EQ(0, error);
-
-       create_two_events();
-
+       s = common_setup(SOCK_STREAM, "/var/run/devd.pipe");
        /*
-        * Loop until both events are detected on _different_ reads
-        * There may be extra events due to unrelated system activity
+        * Loop until both events are detected on the same or different reads.
+        * There may be extra events due to unrelated system activity.
         * If we never get both events, then the test will timeout.
         */
        while (!(got_create_event && got_destroy_event)) {
@@ -169,7 +157,7 @@ ATF_TC_BODY(stream, tc)
                ATF_REQUIRE(newlen != -1);
                len += newlen;
                /* NULL terminate the result */
-               event[newlen] = '\0';
+               event[len] = '\0';
                printf("%s", event);
 
                create_pos = strstr(event, create_pat);
_______________________________________________
svn-src-stable-10@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-stable-10
To unsubscribe, send any mail to "svn-src-stable-10-unsubscr...@freebsd.org"

Reply via email to