From 2691221a8b9f13505dffd0d381d8a73344037f1c Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Sat, 20 Aug 2011 14:36:39 -0400 Subject: [PATCH] Add work-in-progress lttng-ust-comm Signed-off-by: Mathieu Desnoyers --- libust/Makefile.am | 3 +- libust/lttng-ust-comm.c | 153 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 libust/lttng-ust-comm.c diff --git a/libust/Makefile.am b/libust/Makefile.am index 0fa4a2b5..d72d4691 100644 --- a/libust/Makefile.am +++ b/libust/Makefile.am @@ -28,6 +28,7 @@ libust_la_LIBADD = \ -lrt \ -luuid \ $(top_builddir)/snprintf/libustsnprintf.la \ - $(top_builddir)/libringbuffer/libringbuffer.la + $(top_builddir)/libringbuffer/libringbuffer.la \ + $(top_builddir)/liblttng-sessiond-comm/liblttng-sessiond-comm.la libust_la_CFLAGS = -DUST_COMPONENT="libust" -fno-strict-aliasing diff --git a/libust/lttng-ust-comm.c b/libust/lttng-ust-comm.c new file mode 100644 index 00000000..759a4452 --- /dev/null +++ b/libust/lttng-ust-comm.c @@ -0,0 +1,153 @@ +/* + * lttng-ust-comm.c + * + * Copyright (C) 2011 David Goulet + * Copyright (C) 2011 Mathieu Desnoyers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; only + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +/* 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; + +/* TODO: allow global_apps_sock_path override */ + +static int local_apps_socket = -1; +static char local_apps_sock_path[PATH_MAX]; + +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) +{ + 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; +} + +static +int register_app_to_sessiond(int socket) +{ + ssize_t ret; + struct { + pid_t pid; + uid_t uid; + } reg_msg; + + reg_msg.pid = getpid(); + reg_msg.uid = getuid(); + + ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); + if (ret >= 0 && ret != sizeof(reg_msg)) + return -EIO; + return ret; +} + +/* + * sessiond monitoring thread: monitor presence of global and per-user + * sessiond by polling the application common named pipe. + */ +/* TODO */ + +void __attribute__((constructor)) lttng_ust_comm_init(void) +{ + int ret; + + 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(); + 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"); + } + } +} + +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"); + } + } +#endif + if (local_apps_socket >= 0) { + ret = close(local_apps_socket); + if (ret) { + ERR("Error closing local apps socket"); + } + } +} -- 2.34.1