speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/4] Change the test-runner program to use Unix sockets.


From: Christopher Brannon
Subject: [PATCH 1/4] Change the test-runner program to use Unix sockets.
Date: Tue, 8 Jun 2010 10:03:47 -0500

Since Unix sockets are now the default communication mechanism, the
test-runner program, found in src/tests/run_test.c, should use them
as well.  Otherwise, you have to jump through hoops just to run the
tests.
---
 src/tests/run_test.c |  107 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 80 insertions(+), 27 deletions(-)

diff --git a/src/tests/run_test.c b/src/tests/run_test.c
index 15a00c0..c508709 100644
--- a/src/tests/run_test.c
+++ b/src/tests/run_test.c
@@ -25,8 +25,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
@@ -101,30 +100,84 @@ wait_for(int fd, char* event)
     fflush(NULL);
 }
 
-int 
-init(char* client_name, char* conn_name)
+/*
+ * set_socket_path: establish the pathname that our Unix socket should
+ * have.  If the SPEECHD_SOCKET environment variable is set, then that
+ * will be our pathname.  Otherwise, the pathname
+ * is ~/.speech-dispatcher/speechd.sock.
+ */
+
+void
+set_socket_path(struct sockaddr_un *address)
+{
+       const char default_socket_path[] = ".speech-dispatcher/speechd.sock";
+       size_t default_path_length = strlen(default_socket_path);
+       size_t path_max = sizeof(address->sun_path);
+       char *path;
+       char *pathcopy = NULL;
+       char *home_dir;
+       size_t home_dir_length;
+
+       path = getenv("SPEECHD_SOCKET");
+       if ((path == NULL) || (strlen(path) == 0)) {
+               home_dir = getenv("HOME");
+               if (home_dir == NULL)
+                       FATAL
+                           ("Unable to find your home directory.  Cannot run 
tests.");
+
+               home_dir_length = strlen(home_dir);
+               if (home_dir_length == 0)
+                       FATAL
+                           ("Unable to find your home directory.  Cannot run 
tests.");
+
+               pathcopy = malloc(default_path_length + home_dir_length + 2);
+               /* The + 2 accounts for an extra slash. */
+               if (pathcopy == NULL)
+                       FATAL("Out of memory!");
+
+               strcpy(pathcopy, home_dir);
+               if (pathcopy[home_dir_length - 1] != '/') {
+                       pathcopy[home_dir_length] = '/';
+                       pathcopy[home_dir_length + 1] = '\0';
+               }
+               strcat(pathcopy, default_socket_path);
+               path = pathcopy;
+       }
+
+       strncpy(address->sun_path, path, path_max - 1);
+       address->sun_path[path_max - 1] = '\0';
+
+       if (pathcopy)
+               free(pathcopy); /* Don't need it anymore. */
+}
+
+/*
+ * init: create and connect our Unix-domain socket.
+ * Returns the file descriptor of the socket on success, or -1 on
+ * failure.
+ */
+
+int
+init(void)
 {
-  int sockfd;
-  struct sockaddr_in address;
-  char *env_port;
-  int port;
-
-  env_port = getenv("SPEECHD_PORT");
-  if (env_port != NULL)
-      port = strtol(env_port, NULL, 10);
-  else
-      port = SPEECHD_DEFAULT_PORT;
-
-  address.sin_addr.s_addr = inet_addr("127.0.0.1");
-  address.sin_port = htons(port);
-  address.sin_family = AF_INET;
-  sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
-  /* connect to server */
-  if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1)
-      return 0;
-
-  return sockfd;
+       int sockfd;
+       int connect_success;
+       struct sockaddr_un address;
+
+       set_socket_path(&address);
+       address.sun_family = AF_UNIX;
+       sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (sockfd != -1) {
+               connect_success =
+                   connect(sockfd, (struct sockaddr *)&address,
+                           SUN_LEN(&address));
+               if (connect_success == -1) {
+                       close(sockfd);
+                       sockfd = -1;
+               }
+       }
+
+       return sockfd;
 }
 
 int
@@ -165,8 +218,8 @@ main(int argc, char* argv[])
     line = malloc(1024 * sizeof(char));
     reply = malloc(4096 * sizeof(char));
 
-    sockk = init("run_test","user_test");
-    if(sockk == 0) FATAL("Can't connect to Speech Dispatcher");
+    sockk = init();
+    if(sockk == -1) FATAL("Can't connect to Speech Dispatcher");
 
     assert(line != 0);
 
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]