9a540f0a6d0772ff2c8fadfc61a377b0582b7640
[lttng-tools.git] / liblttng-sessiond-comm / lttng-sessiond-comm.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29 #include <assert.h>
30
31 #include <lttng-sessiond-comm.h>
32
33 /*
34 * Human readable error message.
35 */
36 static const char *lttcomm_readable_code[] = {
37 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
38 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
39 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
40 [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
41 [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
42 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
43 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
44 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
46 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
47 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
48 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed",
49 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
50 [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
51 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
52 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
53 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
54 [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
55 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
56 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
57 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
58 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
59 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
60 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
61 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
62 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
63 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
64 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
65 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
66 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed",
67 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed",
68 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed",
69 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed",
70 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed",
71 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist",
72 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
73 [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
74 [ LTTCOMM_ERR_INDEX(KCONSUMERD_COMMAND_SOCK_READY) ] = "Kconsumerd command socket ready",
75 [ LTTCOMM_ERR_INDEX(KCONSUMERD_SUCCESS_RECV_FD) ] = "Kconsumerd success on receiving fds",
76 [ LTTCOMM_ERR_INDEX(KCONSUMERD_ERROR_RECV_FD) ] = "Kconsumerd error on receiving fds",
77 [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_ERROR) ] = "Kconsumerd error in polling thread",
78 [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_NVAL) ] = "Kconsumerd polling on closed fd",
79 [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_HUP) ] = "Kconsumerd all fd hung up",
80 [ LTTCOMM_ERR_INDEX(KCONSUMERD_EXIT_SUCCESS) ] = "Kconsumerd exiting normally",
81 [ LTTCOMM_ERR_INDEX(KCONSUMERD_EXIT_FAILURE) ] = "Kconsumerd exiting on error",
82 [ LTTCOMM_ERR_INDEX(KCONSUMERD_OUTFD_ERROR) ] = "Kconsumerd error opening the tracefile",
83 [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_EBADF) ] = "Kconsumerd splice EBADF",
84 [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_EINVAL) ] = "Kconsumerd splice EINVAL",
85 [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_ENOMEM) ] = "Kconsumerd splice ENOMEM",
86 [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_ESPIPE) ] = "Kconsumerd splice ESPIPE",
87 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found",
88 };
89
90 /*
91 * lttcom_get_readable_code
92 *
93 * Return ptr to string representing a human readable
94 * error code from the lttcomm_return_code enum.
95 *
96 * These code MUST be negative in other to treat that
97 * as an error value.
98 */
99 const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
100 {
101 int tmp_code = -code;
102
103 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
104 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
105 }
106
107 return "Unknown error code";
108 }
109
110 /*
111 * lttcomm_connect_unix_sock
112 *
113 * Connect to unix socket using the path name.
114 */
115 int lttcomm_connect_unix_sock(const char *pathname)
116 {
117 struct sockaddr_un sun;
118 int fd;
119 int ret;
120
121 fd = socket(PF_UNIX, SOCK_STREAM, 0);
122 if (fd < 0) {
123 perror("socket");
124 ret = fd;
125 goto error;
126 }
127
128 memset(&sun, 0, sizeof(sun));
129 sun.sun_family = AF_UNIX;
130 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
131 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
132
133 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
134 if (ret < 0) {
135 /*
136 * Don't print message on connect error, because connect
137 * is used in normal execution to detect if sessiond is
138 * alive.
139 */
140 goto error_connect;
141 }
142
143 return fd;
144
145 error_connect:
146 close(fd);
147 error:
148 return ret;
149 }
150
151 /*
152 * lttcomm_accept_unix_sock
153 *
154 * Do an accept(2) on the sock and return the
155 * new file descriptor. The socket MUST be bind(2) before.
156 */
157 int lttcomm_accept_unix_sock(int sock)
158 {
159 int new_fd;
160 struct sockaddr_un sun;
161 socklen_t len = 0;
162
163 /* Blocking call */
164 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
165 if (new_fd < 0) {
166 perror("accept");
167 goto error;
168 }
169
170 return new_fd;
171
172 error:
173 return -1;
174 }
175
176 /*
177 * lttcomm_create_unix_sock
178 *
179 * Creates a AF_UNIX local socket using pathname
180 * bind the socket upon creation and return the fd.
181 */
182 int lttcomm_create_unix_sock(const char *pathname)
183 {
184 struct sockaddr_un sun;
185 int fd;
186 int ret = -1;
187
188 /* Create server socket */
189 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
190 perror("socket");
191 goto error;
192 }
193
194 memset(&sun, 0, sizeof(sun));
195 sun.sun_family = AF_UNIX;
196 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
197 sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
198
199 /* Unlink the old file if present */
200 (void) unlink(pathname);
201 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
202 if (ret < 0) {
203 perror("bind");
204 goto error;
205 }
206
207 return fd;
208
209 error:
210 return ret;
211 }
212
213 /*
214 * lttcomm_listen_unix_sock
215 *
216 * Make the socket listen using MAX_LISTEN.
217 */
218 int lttcomm_listen_unix_sock(int sock)
219 {
220 int ret;
221
222 ret = listen(sock, MAX_LISTEN);
223 if (ret < 0) {
224 perror("listen");
225 }
226
227 return ret;
228 }
229
230 /*
231 * lttcomm_recv_unix_sock
232 *
233 * Receive data of size len in put that data into
234 * the buf param. Using recvmsg API.
235 * Return the size of received data.
236 */
237 ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
238 {
239 struct msghdr msg = { 0 };
240 struct iovec iov[1];
241 ssize_t ret = -1;
242
243 iov[0].iov_base = buf;
244 iov[0].iov_len = len;
245 msg.msg_iov = iov;
246 msg.msg_iovlen = 1;
247
248 ret = recvmsg(sock, &msg, 0);
249 if (ret < 0) {
250 perror("recvmsg");
251 }
252
253 return ret;
254 }
255
256 /*
257 * lttcomm_send_unix_sock
258 *
259 * Send buf data of size len. Using sendmsg API.
260 * Return the size of sent data.
261 */
262 ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
263 {
264 struct msghdr msg = { 0 };
265 struct iovec iov[1];
266 ssize_t ret = -1;
267
268 iov[0].iov_base = buf;
269 iov[0].iov_len = len;
270 msg.msg_iov = iov;
271 msg.msg_iovlen = 1;
272
273 ret = sendmsg(sock, &msg, 0);
274 if (ret < 0) {
275 perror("sendmsg");
276 }
277
278 return ret;
279 }
280
281 /*
282 * lttcomm_close_unix_sock
283 *
284 * Shutdown cleanly a unix socket.
285 */
286 int lttcomm_close_unix_sock(int sock)
287 {
288 int ret;
289
290 /* Shutdown receptions and transmissions */
291 ret = shutdown(sock, SHUT_RDWR);
292 if (ret < 0) {
293 perror("shutdown");
294 }
295
296 return ret;
297 }
298
299 /*
300 * lttcomm_send_fds_unix_sock
301 *
302 * Send multiple fds on a unix socket.
303 */
304 ssize_t lttcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd, size_t len)
305 {
306 struct msghdr msg = { 0 };
307 struct cmsghdr *cmptr;
308 struct iovec iov[1];
309 ssize_t ret = -1;
310 unsigned int sizeof_fds = nb_fd * sizeof(int);
311 char tmp[CMSG_SPACE(sizeof_fds)];
312
313 /*
314 * Note: the consumerd receiver only supports receiving one FD per
315 * message.
316 */
317 assert(nb_fd == 1);
318
319 msg.msg_control = (caddr_t)tmp;
320 msg.msg_controllen = CMSG_LEN(sizeof_fds);
321
322 cmptr = CMSG_FIRSTHDR(&msg);
323 cmptr->cmsg_level = SOL_SOCKET;
324 cmptr->cmsg_type = SCM_RIGHTS;
325 cmptr->cmsg_len = CMSG_LEN(sizeof_fds);
326 memcpy(CMSG_DATA(cmptr), fds, sizeof_fds);
327 /* Sum of the length of all control messages in the buffer: */
328 msg.msg_controllen = cmptr->cmsg_len;
329
330 iov[0].iov_base = buf;
331 iov[0].iov_len = len;
332 msg.msg_iov = iov;
333 msg.msg_iovlen = 1;
334
335 ret = sendmsg(sock, &msg, 0);
336 if (ret < 0) {
337 perror("sendmsg");
338 }
339
340 return ret;
341 }
This page took 0.035758 seconds and 3 git commands to generate.