| 1 | /* |
| 2 | * lttng-ust-comm.c |
| 3 | * |
| 4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; only |
| 10 | * version 2.1 of the License. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <unistd.h> |
| 25 | #include <errno.h> |
| 26 | #include <lttng-sessiond-comm.h> |
| 27 | #include <ust/usterr-signal-safe.h> |
| 28 | |
| 29 | /* Socket from app (connect) to session daemon (listen) for communication */ |
| 30 | static int global_apps_socket = -1; |
| 31 | static char global_apps_sock_path[PATH_MAX] = DEFAULT_GLOBAL_APPS_UNIX_SOCK; |
| 32 | |
| 33 | /* TODO: allow global_apps_sock_path override */ |
| 34 | |
| 35 | static int local_apps_socket = -1; |
| 36 | static char local_apps_sock_path[PATH_MAX]; |
| 37 | |
| 38 | static |
| 39 | int connect_global_apps_socket(void) |
| 40 | { |
| 41 | int ret; |
| 42 | |
| 43 | ret = lttcomm_connect_unix_sock(global_apps_sock_path); |
| 44 | if (ret < 0) |
| 45 | return ret; |
| 46 | global_apps_socket = ret; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static |
| 52 | int connect_local_apps_socket(void) |
| 53 | { |
| 54 | const char *home_dir; |
| 55 | int ret; |
| 56 | |
| 57 | home_dir = (const char *) getenv("HOME"); |
| 58 | if (!home_dir) |
| 59 | return -ENOENT; |
| 60 | snprintf(local_apps_sock_path, PATH_MAX, |
| 61 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
| 62 | |
| 63 | ret = lttcomm_connect_unix_sock(local_apps_sock_path); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | local_apps_socket = ret; |
| 67 | |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | int register_app_to_sessiond(int socket) |
| 74 | { |
| 75 | ssize_t ret; |
| 76 | struct { |
| 77 | pid_t pid; |
| 78 | uid_t uid; |
| 79 | } reg_msg; |
| 80 | |
| 81 | reg_msg.pid = getpid(); |
| 82 | reg_msg.uid = getuid(); |
| 83 | |
| 84 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); |
| 85 | if (ret >= 0 && ret != sizeof(reg_msg)) |
| 86 | return -EIO; |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * sessiond monitoring thread: monitor presence of global and per-user |
| 92 | * sessiond by polling the application common named pipe. |
| 93 | */ |
| 94 | /* TODO */ |
| 95 | |
| 96 | void __attribute__((constructor)) lttng_ust_comm_init(void) |
| 97 | { |
| 98 | int ret; |
| 99 | |
| 100 | init_usterr(); |
| 101 | |
| 102 | #if 0 |
| 103 | /* Connect to the global sessiond apps socket */ |
| 104 | ret = connect_global_apps_socket(); |
| 105 | if (ret) { |
| 106 | ERR("Error connecting to global apps socket"); |
| 107 | } |
| 108 | #endif //0 |
| 109 | |
| 110 | /* Connect to the per-user (local) sessiond apps socket */ |
| 111 | ret = connect_local_apps_socket(); |
| 112 | if (ret) { |
| 113 | ERR("Error connecting to local apps socket"); |
| 114 | } |
| 115 | |
| 116 | if (global_apps_socket >= 0) { |
| 117 | ret = register_app_to_sessiond(global_apps_socket); |
| 118 | if (ret < 0) { |
| 119 | ERR("Error registering app to global apps socket"); |
| 120 | } |
| 121 | } |
| 122 | if (local_apps_socket >= 0) { |
| 123 | ret = register_app_to_sessiond(local_apps_socket); |
| 124 | if (ret < 0) { |
| 125 | ERR("Error registering app to local apps socket"); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void __attribute__((destructor)) lttng_ust_comm_exit(void) |
| 131 | { |
| 132 | int ret; |
| 133 | |
| 134 | #if 0 |
| 135 | ERR("dest %d", global_apps_socket); |
| 136 | if (global_apps_socket >= 0) { |
| 137 | ret = unregister_app_to_sessiond(global_apps_socket); |
| 138 | if (ret < 0) { |
| 139 | ERR("Error registering app to global apps socket"); |
| 140 | } |
| 141 | ret = close(global_apps_socket); |
| 142 | if (ret) { |
| 143 | ERR("Error closing global apps socket"); |
| 144 | } |
| 145 | } |
| 146 | #endif |
| 147 | if (local_apps_socket >= 0) { |
| 148 | ret = close(local_apps_socket); |
| 149 | if (ret) { |
| 150 | ERR("Error closing local apps socket"); |
| 151 | } |
| 152 | } |
| 153 | } |