Rename sessiond comm lib into ust comm
[lttng-ust.git] / libust / lttng-ust-comm.c
index 759a4452a5acc4cb068ece267751a2400984837e..c9cb777709dab0d84ffffebe26352fcfdfbde563 100644 (file)
 #include <sys/socket.h>
 #include <unistd.h>
 #include <errno.h>
-#include <lttng-sessiond-comm.h>
+#include <ust/lttng-ust-abi.h>
+#include <lttng-ust-comm.h>
 #include <ust/usterr-signal-safe.h>
+#include <pthread.h>
 
 /* Socket from app (connect) to session daemon (listen) for communication */
-static int global_apps_socket = -1;
 static char global_apps_sock_path[PATH_MAX] = DEFAULT_GLOBAL_APPS_UNIX_SOCK;
+static pthread_t global_ust_listener;
 
 /* TODO: allow global_apps_sock_path override */
 
-static int local_apps_socket = -1;
 static char local_apps_sock_path[PATH_MAX];
+static pthread_t local_ust_listener;
 
 static
-int connect_global_apps_socket(void)
-{
-       int ret;
-
-       ret = lttcomm_connect_unix_sock(global_apps_sock_path);
-       if (ret < 0)
-               return ret;
-       global_apps_socket = ret;
-
-       return 0;
-}
-
-static
-int connect_local_apps_socket(void)
+int setup_local_apps_socket(void)
 {
        const char *home_dir;
-       int ret;
 
        home_dir = (const char *) getenv("HOME");
        if (!home_dir)
                return -ENOENT;
        snprintf(local_apps_sock_path, PATH_MAX,
                 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
-
-       ret = lttcomm_connect_unix_sock(local_apps_sock_path);
-       if (ret < 0)
-               return ret;
-       local_apps_socket = ret;
-
-
        return 0;
 }
 
@@ -74,10 +55,14 @@ int register_app_to_sessiond(int socket)
 {
        ssize_t ret;
        struct {
+               uint32_t major;
+               uint32_t minor;
                pid_t pid;
                uid_t uid;
        } reg_msg;
 
+       reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
+       reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
        reg_msg.pid = getpid();
        reg_msg.uid = getuid();
 
@@ -87,6 +72,117 @@ int register_app_to_sessiond(int socket)
        return ret;
 }
 
+
+static
+int handle_message(int sock, struct lttcomm_ust_msg *lum)
+{
+       ssize_t len;
+       int ret;
+
+       switch (lum->cmd_type) {
+       case UST_CREATE_SESSION:
+       {
+               struct lttcomm_ust_reply lur;
+
+               DBG("Handling create session message");
+               memset(&lur, 0, sizeof(lur));
+               lur.cmd_type = UST_CREATE_SESSION;
+
+               /* ... */
+               ret = 0;
+
+               if (!ret)
+                       lur.ret_code = LTTCOMM_OK;
+               else
+                       lur.ret_code = LTTCOMM_SESSION_FAIL;
+               lur.ret_val = 42;
+               len = lttcomm_send_unix_sock(sock, &lur, sizeof(lur));
+               switch (len) {
+               case sizeof(lur):
+                       printf("message successfully sent\n");
+                       break;
+               case -1:
+                       if (errno == ECONNRESET) {
+                               printf("remote end closed connection\n");
+                               return 0;
+                       }
+                       return -1;
+               default:
+                       printf("incorrect message size: %zd\n", len);
+                       return -1;
+               }
+               break;
+       }
+       default:
+               ERR("Unimplemented command %d", (int) lum->cmd_type);
+               return -1;
+       }
+       return 0;
+}
+
+static
+void *ust_listener_thread(void *arg)
+{
+       const char *sock_path = (const char *) arg;
+       int sock;
+       int ret;
+
+       /* Restart trying to connect to the session daemon */
+restart:
+
+       /* Check for sessiond availability with pipe TODO */
+
+       /* Register */
+       ret = lttcomm_connect_unix_sock(sock_path);
+       if (ret < 0) {
+               ERR("Error connecting to global apps socket");
+       }
+       sock = ret;
+       ret = register_app_to_sessiond(sock);
+       if (ret < 0) {
+               ERR("Error registering app to local apps socket");
+               sleep(5);
+               goto restart;
+       }
+       for (;;) {
+               ssize_t len;
+               struct lttcomm_ust_msg lum;
+
+               /* Receive session handle */
+               len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
+               switch (len) {
+               case 0: /* orderly shutdown */
+                       DBG("ltt-sessiond has performed an orderly shutdown\n");
+                       goto end;
+               case sizeof(lum):
+                       DBG("message received\n");
+                       ret = handle_message(sock, &lum);
+                       if (ret) {
+                               ERR("Error handling message\n");
+                       }
+                       continue;
+               case -1:
+                       if (errno == ECONNRESET) {
+                               ERR("remote end closed connection\n");
+                               goto end;
+                       }
+                       goto end;
+               default:
+                       ERR("incorrect message size: %zd\n", len);
+                       continue;
+               }
+
+       }
+end:
+       ret = close(sock);
+       if (ret) {
+               ERR("Error closing local apps socket");
+       }
+       goto restart;   /* try to reconnect */
+       return NULL;
+}
+
+
 /*
  * sessiond monitoring thread: monitor presence of global and per-user
  * sessiond by polling the application common named pipe.
@@ -99,55 +195,35 @@ void __attribute__((constructor)) lttng_ust_comm_init(void)
 
        init_usterr();
 
-#if 0
-       /* Connect to the global sessiond apps socket */
-       ret = connect_global_apps_socket();
-       if (ret) {
-               ERR("Error connecting to global apps socket");
-       }
-#endif //0
-
        /* Connect to the per-user (local) sessiond apps socket */
-       ret = connect_local_apps_socket();
+       ret = setup_local_apps_socket();
        if (ret) {
-               ERR("Error connecting to local apps socket");
-       }
-
-       if (global_apps_socket >= 0) {
-               ret = register_app_to_sessiond(global_apps_socket);
-               if (ret < 0) {
-                       ERR("Error registering app to global apps socket");
-               }
-       }
-       if (local_apps_socket >= 0) {
-               ret = register_app_to_sessiond(local_apps_socket);
-               if (ret < 0) {
-                       ERR("Error registering app to local apps socket");
-               }
+               ERR("Error setting up to local apps socket");
        }
+#if 0
+       ret = pthread_create(&global_ust_listener, NULL,
+                       ust_listener_thread, global_apps_sock_path);
+#endif //0
+       ret = pthread_create(&local_ust_listener, NULL,
+                       ust_listener_thread, local_apps_sock_path);
 }
 
 void __attribute__((destructor)) lttng_ust_comm_exit(void)
 {
        int ret;
 
-#if 0
-       ERR("dest %d", global_apps_socket);
-       if (global_apps_socket >= 0) {
-               ret = unregister_app_to_sessiond(global_apps_socket);
-               if (ret < 0) {
-                       ERR("Error registering app to global apps socket");
-               }
-               ret = close(global_apps_socket);
-               if (ret) {
-                       ERR("Error closing global apps socket");
-               }
+       /*
+        * Using pthread_cancel here because:
+        * A) we don't want to hang application teardown.
+        * B) the thread is not allocating any resource.
+        */
+       ret = pthread_cancel(global_ust_listener);
+       if (ret) {
+               ERR("Error cancelling global ust listener thread");
        }
-#endif
-       if (local_apps_socket >= 0) {
-               ret = close(local_apps_socket);
-               if (ret) {
-                       ERR("Error closing local apps socket");
-               }
+       ret = pthread_cancel(local_ust_listener);
+       if (ret) {
+               ERR("Error cancelling local ust listener thread");
        }
+       lttng_ust_abi_exit();
 }
This page took 0.027621 seconds and 4 git commands to generate.