Commit | Line | Data |
---|---|---|
2691221a MD |
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> | |
d9e99d10 | 28 | #include <pthread.h> |
2691221a MD |
29 | |
30 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
2691221a | 31 | static char global_apps_sock_path[PATH_MAX] = DEFAULT_GLOBAL_APPS_UNIX_SOCK; |
d9e99d10 | 32 | static pthread_t global_ust_listener; |
2691221a MD |
33 | |
34 | /* TODO: allow global_apps_sock_path override */ | |
35 | ||
2691221a | 36 | static char local_apps_sock_path[PATH_MAX]; |
d9e99d10 | 37 | static pthread_t local_ust_listener; |
2691221a MD |
38 | |
39 | static | |
9eb62b9c | 40 | int setup_local_apps_socket(void) |
2691221a MD |
41 | { |
42 | const char *home_dir; | |
2691221a MD |
43 | |
44 | home_dir = (const char *) getenv("HOME"); | |
45 | if (!home_dir) | |
46 | return -ENOENT; | |
47 | snprintf(local_apps_sock_path, PATH_MAX, | |
48 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); | |
2691221a MD |
49 | return 0; |
50 | } | |
51 | ||
52 | static | |
53 | int register_app_to_sessiond(int socket) | |
54 | { | |
55 | ssize_t ret; | |
56 | struct { | |
e44418f3 MD |
57 | uint32_t major; |
58 | uint32_t minor; | |
2691221a MD |
59 | pid_t pid; |
60 | uid_t uid; | |
61 | } reg_msg; | |
62 | ||
e44418f3 MD |
63 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
64 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a MD |
65 | reg_msg.pid = getpid(); |
66 | reg_msg.uid = getuid(); | |
67 | ||
68 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); | |
69 | if (ret >= 0 && ret != sizeof(reg_msg)) | |
70 | return -EIO; | |
71 | return ret; | |
72 | } | |
73 | ||
d9e99d10 MD |
74 | |
75 | static | |
e7723462 | 76 | int handle_message(int sock, struct lttcomm_ust_msg *lum) |
d9e99d10 | 77 | { |
9eb62b9c MD |
78 | ssize_t len; |
79 | int ret; | |
80 | ||
e7723462 | 81 | switch (lum->cmd_type) { |
1ece9766 | 82 | case LTTNG_UST_CREATE_SESSION: |
9eb62b9c | 83 | { |
e7723462 | 84 | struct lttcomm_ust_reply lur; |
d9e99d10 | 85 | |
9eb62b9c MD |
86 | DBG("Handling create session message"); |
87 | memset(&lur, 0, sizeof(lur)); | |
1ece9766 | 88 | lur.cmd_type = LTTNG_UST_CREATE_SESSION; |
9eb62b9c MD |
89 | |
90 | /* ... */ | |
91 | ret = 0; | |
92 | ||
93 | if (!ret) | |
94 | lur.ret_code = LTTCOMM_OK; | |
95 | else | |
96 | lur.ret_code = LTTCOMM_SESSION_FAIL; | |
1ece9766 | 97 | lur.ret_val = 42; |
9eb62b9c MD |
98 | len = lttcomm_send_unix_sock(sock, &lur, sizeof(lur)); |
99 | switch (len) { | |
100 | case sizeof(lur): | |
101 | printf("message successfully sent\n"); | |
102 | break; | |
103 | case -1: | |
104 | if (errno == ECONNRESET) { | |
105 | printf("remote end closed connection\n"); | |
106 | return 0; | |
107 | } | |
108 | return -1; | |
109 | default: | |
110 | printf("incorrect message size: %zd\n", len); | |
111 | return -1; | |
112 | } | |
d9e99d10 | 113 | break; |
9eb62b9c | 114 | } |
d9e99d10 | 115 | default: |
e7723462 | 116 | ERR("Unimplemented command %d", (int) lum->cmd_type); |
d9e99d10 MD |
117 | return -1; |
118 | } | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static | |
123 | void *ust_listener_thread(void *arg) | |
124 | { | |
9eb62b9c MD |
125 | const char *sock_path = (const char *) arg; |
126 | int sock; | |
d9e99d10 MD |
127 | int ret; |
128 | ||
9eb62b9c MD |
129 | /* Restart trying to connect to the session daemon */ |
130 | restart: | |
131 | ||
132 | /* Check for sessiond availability with pipe TODO */ | |
133 | ||
134 | /* Register */ | |
135 | ret = lttcomm_connect_unix_sock(sock_path); | |
136 | if (ret < 0) { | |
137 | ERR("Error connecting to global apps socket"); | |
138 | } | |
139 | sock = ret; | |
140 | ret = register_app_to_sessiond(sock); | |
141 | if (ret < 0) { | |
142 | ERR("Error registering app to local apps socket"); | |
143 | sleep(5); | |
144 | goto restart; | |
145 | } | |
d9e99d10 MD |
146 | for (;;) { |
147 | ssize_t len; | |
e7723462 | 148 | struct lttcomm_ust_msg lum; |
d9e99d10 MD |
149 | |
150 | /* Receive session handle */ | |
e7723462 | 151 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
152 | switch (len) { |
153 | case 0: /* orderly shutdown */ | |
154 | DBG("ltt-sessiond has performed an orderly shutdown\n"); | |
155 | goto end; | |
e7723462 | 156 | case sizeof(lum): |
d9e99d10 | 157 | DBG("message received\n"); |
e7723462 | 158 | ret = handle_message(sock, &lum); |
d9e99d10 | 159 | if (ret) { |
9eb62b9c | 160 | ERR("Error handling message\n"); |
d9e99d10 MD |
161 | } |
162 | continue; | |
163 | case -1: | |
164 | if (errno == ECONNRESET) { | |
165 | ERR("remote end closed connection\n"); | |
166 | goto end; | |
167 | } | |
168 | goto end; | |
169 | default: | |
170 | ERR("incorrect message size: %zd\n", len); | |
171 | continue; | |
172 | } | |
173 | ||
174 | } | |
175 | end: | |
9eb62b9c MD |
176 | ret = close(sock); |
177 | if (ret) { | |
178 | ERR("Error closing local apps socket"); | |
179 | } | |
180 | goto restart; /* try to reconnect */ | |
d9e99d10 MD |
181 | return NULL; |
182 | } | |
183 | ||
184 | ||
2691221a MD |
185 | /* |
186 | * sessiond monitoring thread: monitor presence of global and per-user | |
187 | * sessiond by polling the application common named pipe. | |
188 | */ | |
189 | /* TODO */ | |
190 | ||
191 | void __attribute__((constructor)) lttng_ust_comm_init(void) | |
192 | { | |
193 | int ret; | |
194 | ||
195 | init_usterr(); | |
196 | ||
2691221a | 197 | /* Connect to the per-user (local) sessiond apps socket */ |
9eb62b9c | 198 | ret = setup_local_apps_socket(); |
2691221a | 199 | if (ret) { |
9eb62b9c | 200 | ERR("Error setting up to local apps socket"); |
2691221a | 201 | } |
9eb62b9c MD |
202 | #if 0 |
203 | ret = pthread_create(&global_ust_listener, NULL, | |
204 | ust_listener_thread, global_apps_sock_path); | |
205 | #endif //0 | |
206 | ret = pthread_create(&local_ust_listener, NULL, | |
207 | ust_listener_thread, local_apps_sock_path); | |
2691221a MD |
208 | } |
209 | ||
210 | void __attribute__((destructor)) lttng_ust_comm_exit(void) | |
211 | { | |
212 | int ret; | |
213 | ||
9eb62b9c MD |
214 | /* |
215 | * Using pthread_cancel here because: | |
216 | * A) we don't want to hang application teardown. | |
217 | * B) the thread is not allocating any resource. | |
218 | */ | |
219 | ret = pthread_cancel(global_ust_listener); | |
220 | if (ret) { | |
221 | ERR("Error cancelling global ust listener thread"); | |
2691221a | 222 | } |
9eb62b9c MD |
223 | ret = pthread_cancel(local_ust_listener); |
224 | if (ret) { | |
225 | ERR("Error cancelling local ust listener thread"); | |
2691221a MD |
226 | } |
227 | } |