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