| 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 "liblttsessiondcomm.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_KERN_NA) ] = "Kernel tracer not available", |
| 55 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists", |
| 56 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", |
| 57 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", |
| 58 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", |
| 59 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed", |
| 60 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", |
| 61 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed", |
| 62 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", |
| 63 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", |
| 64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", |
| 65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed", |
| 66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed", |
| 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed", |
| 68 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed", |
| 69 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed", |
| 70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist", |
| 71 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found", |
| 72 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed", |
| 73 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_COMMAND_SOCK_READY) ] = "Kconsumerd command socket ready", |
| 74 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_SUCCESS_RECV_FD) ] = "Kconsumerd success on receiving fds", |
| 75 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_ERROR_RECV_FD) ] = "Kconsumerd error on receiving fds", |
| 76 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_ERROR) ] = "Kconsumerd error in polling thread", |
| 77 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_NVAL) ] = "Kconsumerd polling on closed fd", |
| 78 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_POLL_HUP) ] = "Kconsumerd all fd hung up", |
| 79 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_EXIT_SUCCESS) ] = "Kconsumerd exiting normally", |
| 80 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_EXIT_FAILURE) ] = "Kconsumerd exiting on error", |
| 81 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_OUTFD_ERROR) ] = "Kconsumerd error opening the tracefile", |
| 82 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_EBADF) ] = "Kconsumerd splice EBADF", |
| 83 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_EINVAL) ] = "Kconsumerd splice EINVAL", |
| 84 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_ENOMEM) ] = "Kconsumerd splice ENOMEM", |
| 85 | [ LTTCOMM_ERR_INDEX(KCONSUMERD_SPLICE_ESPIPE) ] = "Kconsumerd splice ESPIPE", |
| 86 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
| 87 | }; |
| 88 | |
| 89 | /* |
| 90 | * lttcom_get_readable_code |
| 91 | * |
| 92 | * Return ptr to string representing a human readable |
| 93 | * error code from the lttcomm_return_code enum. |
| 94 | * |
| 95 | * These code MUST be negative in other to treat that |
| 96 | * as an error value. |
| 97 | */ |
| 98 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) |
| 99 | { |
| 100 | int tmp_code = -code; |
| 101 | |
| 102 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { |
| 103 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; |
| 104 | } |
| 105 | |
| 106 | return "Unknown error code"; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * lttcomm_connect_unix_sock |
| 111 | * |
| 112 | * Connect to unix socket using the path name. |
| 113 | */ |
| 114 | int lttcomm_connect_unix_sock(const char *pathname) |
| 115 | { |
| 116 | struct sockaddr_un sun; |
| 117 | int fd; |
| 118 | int ret = 1; |
| 119 | |
| 120 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 121 | if (fd < 0) { |
| 122 | perror("socket"); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | memset(&sun, 0, sizeof(sun)); |
| 127 | sun.sun_family = AF_UNIX; |
| 128 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
| 129 | |
| 130 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 131 | if (ret < 0) { |
| 132 | perror("connect"); |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | return fd; |
| 137 | |
| 138 | error: |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * lttcomm_accept_unix_sock |
| 144 | * |
| 145 | * Do an accept(2) on the sock and return the |
| 146 | * new file descriptor. The socket MUST be bind(2) before. |
| 147 | */ |
| 148 | int lttcomm_accept_unix_sock(int sock) |
| 149 | { |
| 150 | int new_fd; |
| 151 | struct sockaddr_un sun; |
| 152 | socklen_t len = 0; |
| 153 | |
| 154 | /* Blocking call */ |
| 155 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); |
| 156 | if (new_fd < 0) { |
| 157 | perror("accept"); |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | return new_fd; |
| 162 | |
| 163 | error: |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * lttcomm_create_unix_sock |
| 169 | * |
| 170 | * Creates a AF_UNIX local socket using pathname |
| 171 | * bind the socket upon creation and return the fd. |
| 172 | */ |
| 173 | int lttcomm_create_unix_sock(const char *pathname) |
| 174 | { |
| 175 | struct sockaddr_un sun; |
| 176 | int fd; |
| 177 | int ret = -1; |
| 178 | |
| 179 | /* Create server socket */ |
| 180 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 181 | perror("socket"); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | memset(&sun, 0, sizeof(sun)); |
| 186 | sun.sun_family = AF_UNIX; |
| 187 | strncpy(sun.sun_path, pathname, strlen(pathname)); |
| 188 | |
| 189 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
| 190 | if (ret < 0) { |
| 191 | perror("bind"); |
| 192 | goto error; |
| 193 | } |
| 194 | |
| 195 | return fd; |
| 196 | |
| 197 | error: |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * lttcomm_listen_unix_sock |
| 203 | * |
| 204 | * Make the socket listen using MAX_LISTEN. |
| 205 | */ |
| 206 | int lttcomm_listen_unix_sock(int sock) |
| 207 | { |
| 208 | int ret; |
| 209 | |
| 210 | ret = listen(sock, MAX_LISTEN); |
| 211 | if (ret < 0) { |
| 212 | perror("listen"); |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * lttcomm_recv_unix_sock |
| 220 | * |
| 221 | * Receive data of size len in put that data into |
| 222 | * the buf param. Using recvmsg API. |
| 223 | * Return the size of received data. |
| 224 | */ |
| 225 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) |
| 226 | { |
| 227 | struct msghdr msg = { 0 }; |
| 228 | struct iovec iov[1]; |
| 229 | ssize_t ret = -1; |
| 230 | |
| 231 | iov[0].iov_base = buf; |
| 232 | iov[0].iov_len = len; |
| 233 | msg.msg_iov = iov; |
| 234 | msg.msg_iovlen = 1; |
| 235 | |
| 236 | ret = recvmsg(sock, &msg, 0); |
| 237 | if (ret < 0) { |
| 238 | perror("recvmsg"); |
| 239 | } |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * lttcomm_send_unix_sock |
| 246 | * |
| 247 | * Send buf data of size len. Using sendmsg API. |
| 248 | * Return the size of sent data. |
| 249 | */ |
| 250 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) |
| 251 | { |
| 252 | struct msghdr msg = { 0 }; |
| 253 | struct iovec iov[1]; |
| 254 | ssize_t ret = -1; |
| 255 | |
| 256 | iov[0].iov_base = buf; |
| 257 | iov[0].iov_len = len; |
| 258 | msg.msg_iov = iov; |
| 259 | msg.msg_iovlen = 1; |
| 260 | |
| 261 | ret = sendmsg(sock, &msg, 0); |
| 262 | if (ret < 0) { |
| 263 | perror("sendmsg"); |
| 264 | } |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * lttcomm_close_unix_sock |
| 271 | * |
| 272 | * Shutdown cleanly a unix socket. |
| 273 | */ |
| 274 | int lttcomm_close_unix_sock(int sock) |
| 275 | { |
| 276 | int ret; |
| 277 | |
| 278 | /* Shutdown receptions and transmissions */ |
| 279 | ret = shutdown(sock, SHUT_RDWR); |
| 280 | if (ret < 0) { |
| 281 | perror("shutdown"); |
| 282 | } |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * lttcomm_send_fds_unix_sock |
| 289 | * |
| 290 | * Send multiple fds on a unix socket. |
| 291 | */ |
| 292 | ssize_t lttcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd, size_t len) |
| 293 | { |
| 294 | struct msghdr msg = { 0 }; |
| 295 | struct cmsghdr *cmptr; |
| 296 | struct iovec iov[1]; |
| 297 | ssize_t ret = -1; |
| 298 | unsigned int sizeof_fds = nb_fd * sizeof(int); |
| 299 | char tmp[CMSG_SPACE(sizeof_fds)]; |
| 300 | |
| 301 | /* |
| 302 | * Note: the consumerd receiver only supports receiving one FD per |
| 303 | * message. |
| 304 | */ |
| 305 | assert(nb_fd == 1); |
| 306 | |
| 307 | msg.msg_control = (caddr_t)tmp; |
| 308 | msg.msg_controllen = CMSG_LEN(sizeof_fds); |
| 309 | |
| 310 | cmptr = CMSG_FIRSTHDR(&msg); |
| 311 | cmptr->cmsg_level = SOL_SOCKET; |
| 312 | cmptr->cmsg_type = SCM_RIGHTS; |
| 313 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
| 314 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
| 315 | /* Sum of the length of all control messages in the buffer: */ |
| 316 | msg.msg_controllen = cmptr->cmsg_len; |
| 317 | |
| 318 | iov[0].iov_base = buf; |
| 319 | iov[0].iov_len = len; |
| 320 | msg.msg_iov = iov; |
| 321 | msg.msg_iovlen = 1; |
| 322 | |
| 323 | ret = sendmsg(sock, &msg, 0); |
| 324 | if (ret < 0) { |
| 325 | perror("sendmsg"); |
| 326 | } |
| 327 | |
| 328 | return ret; |
| 329 | } |