]> rtime.felk.cvut.cz Git - frescor/forb.git/commitdiff
Fixed error in unix_broadcast()
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 5 Oct 2008 20:58:49 +0000 (22:58 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 5 Oct 2008 20:58:49 +0000 (22:58 +0200)
On my home box, dirent() returns DT_UNKNOWN as d_type so this value must
also be checked.

proto_unix.c
tests/proto_unix.c

index d22466708c2834cca9e2473b695b0fdc50b99934..ad3def1e4fa4efbfc85348fd5e0c0abe411e2713 100644 (file)
@@ -66,6 +66,7 @@ unix_broadcast(forb_port_t *port, const void *buf, size_t len)
        struct dirent *dirent;
        unix_addr_t addr;
        int ret;
+       int success = 0;
        
        dir = opendir("/tmp");
        if (!dir) return -errno;
@@ -73,11 +74,15 @@ unix_broadcast(forb_port_t *port, const void *buf, size_t len)
        addr.sun_family = AF_UNIX;
        
        while ((dirent = readdir(dir))) {
-               if (1 &&
+               bool forb_socket = 
 #ifdef _DIRENT_HAVE_D_TYPE
-                   dirent->d_type == DT_SOCK &&
+                       (dirent->d_type == DT_SOCK ||
+                        dirent->d_type == DT_UNKNOWN) &&
 #endif
-                   strncmp(dirent->d_name, "forb-", 5) == 0)
+                       (strncmp(dirent->d_name, "forb-", 5) == 0);
+               
+/*             printf("d_name=%s d_type=%d (%d)\n", dirent->d_name, dirent->d_type, DT_SOCK); */
+               if (forb_socket)
                {
                        strcpy(addr.sun_path, "/tmp/");
                        strncat(addr.sun_path, dirent->d_name, sizeof(addr.sun_path));
@@ -86,7 +91,9 @@ unix_broadcast(forb_port_t *port, const void *buf, size_t len)
                                     (struct sockaddr*)&addr, sizeof(addr));
                        /* We do not care about errors in brodcasts -
                         * the socket may nomore be active */
-                       if (ret != len) {
+                       if (ret == len) {
+                               success++;
+                       } else {
 /*                             perror("unix_broadcast"); */
 /*                             return ret; */
                        }
@@ -94,7 +101,12 @@ unix_broadcast(forb_port_t *port, const void *buf, size_t len)
                        
        }
        closedir(dir);
-       return len;
+       if (success) {
+               ret = len;
+       } else {
+               ret = -1;
+       }
+       return ret;
 }
 
 static const forb_proto_t proto_unix = {
index 3c70208d9c0671fb75ece50b412a005a57a35086..ce7316aa49f5fd16e6f3c806a955d212250efa8c 100644 (file)
@@ -13,6 +13,13 @@ const char *msg = "Hello world!";
 const char *msg2 = "Blablabla!";
 #define NUM_PORTS 10
 
+char *timeout_msg = "timeout\n";
+
+void timeout(int signal)
+{
+       error(1, 0, timeout_msg);
+}
+
 int main()
 {
        int len;
@@ -20,6 +27,8 @@ int main()
        forb_port_t port[NUM_PORTS];
        int i, ret;
 
+       signal(SIGALRM, timeout);
+
        for (i=0; i<NUM_PORTS; i++) {
                forb_server_id_init(&server[i]);
                if (forb_unix_port_init(&port[i], &server[i]) != 0)
@@ -31,6 +40,7 @@ int main()
        for (i=1; i<NUM_PORTS; i++) {
                forb_peer_t peer;
                char buf[100];
+               char tmsg[100];
 
                peer.server_id = server[i];
                peer.port = &port[0];
@@ -40,6 +50,9 @@ int main()
                if (ret != len) 
                        error(1, errno, "send(to %d) = %d", i, ret);
 
+               sprintf(tmsg, "Timeout when sending from 0 to %d\n", i);
+               timeout_msg = tmsg;
+               alarm(1);       /* timeout 1s */
                ret = unix_recv(&port[i], buf, sizeof(buf));
                if (ret != len) 
                        error(1, errno, "recv(port[%d]) = %d", i, ret);
@@ -57,7 +70,11 @@ int main()
        
        for (i=0; i<NUM_PORTS; i++) {
                char buf[100];
+               char tmsg[100];
 
+               sprintf(tmsg, "Timeout when receiving broadcast for port %d\n", i);
+               timeout_msg = tmsg;
+               alarm(1);
                ret = unix_recv(&port[i], buf, sizeof(buf));
                if (ret != len) 
                        error(1, errno, "broadcast recv(port[%d]) = %d should be %d", i, ret, len);