| 1 | /* |
| 2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> |
| 3 | * Copyright (C) 2011-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License only. |
| 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 along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <string.h> |
| 21 | #include <lttng/ust-ctl.h> |
| 22 | #include <lttng/ust-abi.h> |
| 23 | #include <lttng/ust-events.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <byteswap.h> |
| 26 | |
| 27 | #include <usterr-signal-safe.h> |
| 28 | #include <ust-comm.h> |
| 29 | #include <helper.h> |
| 30 | |
| 31 | #include "../libringbuffer/backend.h" |
| 32 | #include "../libringbuffer/frontend.h" |
| 33 | #include "../liblttng-ust/wait.h" |
| 34 | #include "../liblttng-ust/lttng-rb-clients.h" |
| 35 | #include "../liblttng-ust/clock.h" |
| 36 | |
| 37 | /* |
| 38 | * Number of milliseconds to retry before failing metadata writes on |
| 39 | * buffer full condition. (10 seconds) |
| 40 | */ |
| 41 | #define LTTNG_METADATA_TIMEOUT_MSEC 10000 |
| 42 | |
| 43 | /* |
| 44 | * Channel representation within consumer. |
| 45 | */ |
| 46 | struct ustctl_consumer_channel { |
| 47 | struct lttng_channel *chan; /* lttng channel buffers */ |
| 48 | |
| 49 | /* initial attributes */ |
| 50 | struct ustctl_consumer_channel_attr attr; |
| 51 | int wait_fd; /* monitor close() */ |
| 52 | int wakeup_fd; /* monitor close() */ |
| 53 | }; |
| 54 | |
| 55 | /* |
| 56 | * Stream representation within consumer. |
| 57 | */ |
| 58 | struct ustctl_consumer_stream { |
| 59 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
| 60 | struct lttng_ust_lib_ring_buffer *buf; |
| 61 | struct ustctl_consumer_channel *chan; |
| 62 | int shm_fd, wait_fd, wakeup_fd; |
| 63 | int cpu; |
| 64 | uint64_t memory_map_size; |
| 65 | }; |
| 66 | |
| 67 | extern void lttng_ring_buffer_client_overwrite_init(void); |
| 68 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
| 69 | extern void lttng_ring_buffer_client_discard_init(void); |
| 70 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
| 71 | extern void lttng_ring_buffer_metadata_client_init(void); |
| 72 | extern void lttng_ring_buffer_client_overwrite_exit(void); |
| 73 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
| 74 | extern void lttng_ring_buffer_client_discard_exit(void); |
| 75 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
| 76 | extern void lttng_ring_buffer_metadata_client_exit(void); |
| 77 | |
| 78 | volatile enum ust_loglevel ust_loglevel; |
| 79 | |
| 80 | int ustctl_release_handle(int sock, int handle) |
| 81 | { |
| 82 | struct ustcomm_ust_msg lum; |
| 83 | struct ustcomm_ust_reply lur; |
| 84 | |
| 85 | if (sock < 0 || handle < 0) |
| 86 | return 0; |
| 87 | memset(&lum, 0, sizeof(lum)); |
| 88 | lum.handle = handle; |
| 89 | lum.cmd = LTTNG_UST_RELEASE; |
| 90 | return ustcomm_send_app_cmd(sock, &lum, &lur); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * If sock is negative, it means we don't have to notify the other side |
| 95 | * (e.g. application has already vanished). |
| 96 | */ |
| 97 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
| 98 | { |
| 99 | int ret; |
| 100 | |
| 101 | if (!data) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | switch (data->type) { |
| 105 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: |
| 106 | if (data->u.channel.wakeup_fd >= 0) { |
| 107 | ret = close(data->u.channel.wakeup_fd); |
| 108 | if (ret < 0) { |
| 109 | ret = -errno; |
| 110 | return ret; |
| 111 | } |
| 112 | } |
| 113 | free(data->u.channel.data); |
| 114 | break; |
| 115 | case LTTNG_UST_OBJECT_TYPE_STREAM: |
| 116 | if (data->u.stream.shm_fd >= 0) { |
| 117 | ret = close(data->u.stream.shm_fd); |
| 118 | if (ret < 0) { |
| 119 | ret = -errno; |
| 120 | return ret; |
| 121 | } |
| 122 | } |
| 123 | if (data->u.stream.wakeup_fd >= 0) { |
| 124 | ret = close(data->u.stream.wakeup_fd); |
| 125 | if (ret < 0) { |
| 126 | ret = -errno; |
| 127 | return ret; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | case LTTNG_UST_OBJECT_TYPE_EVENT: |
| 132 | case LTTNG_UST_OBJECT_TYPE_CONTEXT: |
| 133 | break; |
| 134 | default: |
| 135 | assert(0); |
| 136 | } |
| 137 | return ustctl_release_handle(sock, data->handle); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Send registration done packet to the application. |
| 142 | */ |
| 143 | int ustctl_register_done(int sock) |
| 144 | { |
| 145 | struct ustcomm_ust_msg lum; |
| 146 | struct ustcomm_ust_reply lur; |
| 147 | int ret; |
| 148 | |
| 149 | DBG("Sending register done command to %d", sock); |
| 150 | memset(&lum, 0, sizeof(lum)); |
| 151 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 152 | lum.cmd = LTTNG_UST_REGISTER_DONE; |
| 153 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * returns session handle. |
| 161 | */ |
| 162 | int ustctl_create_session(int sock) |
| 163 | { |
| 164 | struct ustcomm_ust_msg lum; |
| 165 | struct ustcomm_ust_reply lur; |
| 166 | int ret, session_handle; |
| 167 | |
| 168 | /* Create session */ |
| 169 | memset(&lum, 0, sizeof(lum)); |
| 170 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 171 | lum.cmd = LTTNG_UST_SESSION; |
| 172 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | session_handle = lur.ret_val; |
| 176 | DBG("received session handle %u", session_handle); |
| 177 | return session_handle; |
| 178 | } |
| 179 | |
| 180 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
| 181 | struct lttng_ust_object_data *channel_data, |
| 182 | struct lttng_ust_object_data **_event_data) |
| 183 | { |
| 184 | struct ustcomm_ust_msg lum; |
| 185 | struct ustcomm_ust_reply lur; |
| 186 | struct lttng_ust_object_data *event_data; |
| 187 | int ret; |
| 188 | |
| 189 | if (!channel_data || !_event_data) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | event_data = zmalloc(sizeof(*event_data)); |
| 193 | if (!event_data) |
| 194 | return -ENOMEM; |
| 195 | event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT; |
| 196 | memset(&lum, 0, sizeof(lum)); |
| 197 | lum.handle = channel_data->handle; |
| 198 | lum.cmd = LTTNG_UST_EVENT; |
| 199 | strncpy(lum.u.event.name, ev->name, |
| 200 | LTTNG_UST_SYM_NAME_LEN); |
| 201 | lum.u.event.instrumentation = ev->instrumentation; |
| 202 | lum.u.event.loglevel_type = ev->loglevel_type; |
| 203 | lum.u.event.loglevel = ev->loglevel; |
| 204 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 205 | if (ret) { |
| 206 | free(event_data); |
| 207 | return ret; |
| 208 | } |
| 209 | event_data->handle = lur.ret_val; |
| 210 | DBG("received event handle %u", event_data->handle); |
| 211 | *_event_data = event_data; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int ustctl_add_context(int sock, struct lttng_ust_context *ctx, |
| 216 | struct lttng_ust_object_data *obj_data, |
| 217 | struct lttng_ust_object_data **_context_data) |
| 218 | { |
| 219 | struct ustcomm_ust_msg lum; |
| 220 | struct ustcomm_ust_reply lur; |
| 221 | struct lttng_ust_object_data *context_data; |
| 222 | int ret; |
| 223 | |
| 224 | if (!obj_data || !_context_data) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | context_data = zmalloc(sizeof(*context_data)); |
| 228 | if (!context_data) |
| 229 | return -ENOMEM; |
| 230 | context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT; |
| 231 | memset(&lum, 0, sizeof(lum)); |
| 232 | lum.handle = obj_data->handle; |
| 233 | lum.cmd = LTTNG_UST_CONTEXT; |
| 234 | lum.u.context = *ctx; |
| 235 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 236 | if (ret) { |
| 237 | free(context_data); |
| 238 | return ret; |
| 239 | } |
| 240 | context_data->handle = -1; |
| 241 | DBG("Context created successfully"); |
| 242 | *_context_data = context_data; |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode, |
| 247 | struct lttng_ust_object_data *obj_data) |
| 248 | { |
| 249 | struct ustcomm_ust_msg lum; |
| 250 | struct ustcomm_ust_reply lur; |
| 251 | int ret; |
| 252 | |
| 253 | if (!obj_data) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | memset(&lum, 0, sizeof(lum)); |
| 257 | lum.handle = obj_data->handle; |
| 258 | lum.cmd = LTTNG_UST_FILTER; |
| 259 | lum.u.filter.data_size = bytecode->len; |
| 260 | lum.u.filter.reloc_offset = bytecode->reloc_offset; |
| 261 | lum.u.filter.seqnum = bytecode->seqnum; |
| 262 | |
| 263 | ret = ustcomm_send_app_msg(sock, &lum); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | /* send var len bytecode */ |
| 267 | ret = ustcomm_send_unix_sock(sock, bytecode->data, |
| 268 | bytecode->len); |
| 269 | if (ret < 0) { |
| 270 | return ret; |
| 271 | } |
| 272 | if (ret != bytecode->len) |
| 273 | return -EINVAL; |
| 274 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 275 | } |
| 276 | |
| 277 | int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion, |
| 278 | struct lttng_ust_object_data *obj_data) |
| 279 | { |
| 280 | struct ustcomm_ust_msg lum; |
| 281 | struct ustcomm_ust_reply lur; |
| 282 | int ret; |
| 283 | |
| 284 | if (!obj_data) { |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | memset(&lum, 0, sizeof(lum)); |
| 289 | lum.handle = obj_data->handle; |
| 290 | lum.cmd = LTTNG_UST_EXCLUSION; |
| 291 | lum.u.exclusion.count = exclusion->count; |
| 292 | |
| 293 | ret = ustcomm_send_app_msg(sock, &lum); |
| 294 | if (ret) { |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | /* send var len exclusion names */ |
| 299 | ret = ustcomm_send_unix_sock(sock, |
| 300 | exclusion->names, |
| 301 | exclusion->count * LTTNG_UST_SYM_NAME_LEN); |
| 302 | if (ret < 0) { |
| 303 | return ret; |
| 304 | } |
| 305 | if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) { |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 309 | } |
| 310 | |
| 311 | /* Enable event, channel and session ioctl */ |
| 312 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
| 313 | { |
| 314 | struct ustcomm_ust_msg lum; |
| 315 | struct ustcomm_ust_reply lur; |
| 316 | int ret; |
| 317 | |
| 318 | if (!object) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | memset(&lum, 0, sizeof(lum)); |
| 322 | lum.handle = object->handle; |
| 323 | lum.cmd = LTTNG_UST_ENABLE; |
| 324 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 325 | if (ret) |
| 326 | return ret; |
| 327 | DBG("enabled handle %u", object->handle); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* Disable event, channel and session ioctl */ |
| 332 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
| 333 | { |
| 334 | struct ustcomm_ust_msg lum; |
| 335 | struct ustcomm_ust_reply lur; |
| 336 | int ret; |
| 337 | |
| 338 | if (!object) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | memset(&lum, 0, sizeof(lum)); |
| 342 | lum.handle = object->handle; |
| 343 | lum.cmd = LTTNG_UST_DISABLE; |
| 344 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | DBG("disable handle %u", object->handle); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int ustctl_start_session(int sock, int handle) |
| 352 | { |
| 353 | struct lttng_ust_object_data obj; |
| 354 | |
| 355 | obj.handle = handle; |
| 356 | return ustctl_enable(sock, &obj); |
| 357 | } |
| 358 | |
| 359 | int ustctl_stop_session(int sock, int handle) |
| 360 | { |
| 361 | struct lttng_ust_object_data obj; |
| 362 | |
| 363 | obj.handle = handle; |
| 364 | return ustctl_disable(sock, &obj); |
| 365 | } |
| 366 | |
| 367 | int ustctl_tracepoint_list(int sock) |
| 368 | { |
| 369 | struct ustcomm_ust_msg lum; |
| 370 | struct ustcomm_ust_reply lur; |
| 371 | int ret, tp_list_handle; |
| 372 | |
| 373 | memset(&lum, 0, sizeof(lum)); |
| 374 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 375 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; |
| 376 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | tp_list_handle = lur.ret_val; |
| 380 | DBG("received tracepoint list handle %u", tp_list_handle); |
| 381 | return tp_list_handle; |
| 382 | } |
| 383 | |
| 384 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, |
| 385 | struct lttng_ust_tracepoint_iter *iter) |
| 386 | { |
| 387 | struct ustcomm_ust_msg lum; |
| 388 | struct ustcomm_ust_reply lur; |
| 389 | int ret; |
| 390 | |
| 391 | if (!iter) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | memset(&lum, 0, sizeof(lum)); |
| 395 | lum.handle = tp_list_handle; |
| 396 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; |
| 397 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 398 | if (ret) |
| 399 | return ret; |
| 400 | DBG("received tracepoint list entry name %s loglevel %d", |
| 401 | lur.u.tracepoint.name, |
| 402 | lur.u.tracepoint.loglevel); |
| 403 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | int ustctl_tracepoint_field_list(int sock) |
| 408 | { |
| 409 | struct ustcomm_ust_msg lum; |
| 410 | struct ustcomm_ust_reply lur; |
| 411 | int ret, tp_field_list_handle; |
| 412 | |
| 413 | memset(&lum, 0, sizeof(lum)); |
| 414 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 415 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST; |
| 416 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 417 | if (ret) |
| 418 | return ret; |
| 419 | tp_field_list_handle = lur.ret_val; |
| 420 | DBG("received tracepoint field list handle %u", tp_field_list_handle); |
| 421 | return tp_field_list_handle; |
| 422 | } |
| 423 | |
| 424 | int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle, |
| 425 | struct lttng_ust_field_iter *iter) |
| 426 | { |
| 427 | struct ustcomm_ust_msg lum; |
| 428 | struct ustcomm_ust_reply lur; |
| 429 | int ret; |
| 430 | ssize_t len; |
| 431 | |
| 432 | if (!iter) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | memset(&lum, 0, sizeof(lum)); |
| 436 | lum.handle = tp_field_list_handle; |
| 437 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET; |
| 438 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 439 | if (ret) |
| 440 | return ret; |
| 441 | len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter)); |
| 442 | if (len != sizeof(*iter)) { |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d", |
| 446 | iter->event_name, |
| 447 | iter->loglevel, |
| 448 | iter->field_name, |
| 449 | iter->type); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
| 454 | { |
| 455 | struct ustcomm_ust_msg lum; |
| 456 | struct ustcomm_ust_reply lur; |
| 457 | int ret; |
| 458 | |
| 459 | if (!v) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | memset(&lum, 0, sizeof(lum)); |
| 463 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 464 | lum.cmd = LTTNG_UST_TRACER_VERSION; |
| 465 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | memcpy(v, &lur.u.version, sizeof(*v)); |
| 469 | DBG("received tracer version"); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | int ustctl_wait_quiescent(int sock) |
| 474 | { |
| 475 | struct ustcomm_ust_msg lum; |
| 476 | struct ustcomm_ust_reply lur; |
| 477 | int ret; |
| 478 | |
| 479 | memset(&lum, 0, sizeof(lum)); |
| 480 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 481 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; |
| 482 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | DBG("waited for quiescent state"); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) |
| 490 | { |
| 491 | if (!calibrate) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | return -ENOSYS; |
| 495 | } |
| 496 | |
| 497 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
| 498 | { |
| 499 | struct ustcomm_ust_msg lum; |
| 500 | struct ustcomm_ust_reply lur; |
| 501 | int ret; |
| 502 | |
| 503 | if (!object) |
| 504 | return -EINVAL; |
| 505 | |
| 506 | memset(&lum, 0, sizeof(lum)); |
| 507 | lum.handle = object->handle; |
| 508 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; |
| 509 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 510 | if (ret) |
| 511 | return ret; |
| 512 | DBG("flushed buffer handle %u", object->handle); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static |
| 517 | int ustctl_send_channel(int sock, |
| 518 | enum lttng_ust_chan_type type, |
| 519 | void *data, |
| 520 | uint64_t size, |
| 521 | int wakeup_fd, |
| 522 | int send_fd_only) |
| 523 | { |
| 524 | ssize_t len; |
| 525 | |
| 526 | if (!send_fd_only) { |
| 527 | /* Send mmap size */ |
| 528 | len = ustcomm_send_unix_sock(sock, &size, sizeof(size)); |
| 529 | if (len != sizeof(size)) { |
| 530 | if (len < 0) |
| 531 | return len; |
| 532 | else |
| 533 | return -EIO; |
| 534 | } |
| 535 | |
| 536 | /* Send channel type */ |
| 537 | len = ustcomm_send_unix_sock(sock, &type, sizeof(type)); |
| 538 | if (len != sizeof(type)) { |
| 539 | if (len < 0) |
| 540 | return len; |
| 541 | else |
| 542 | return -EIO; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* Send channel data */ |
| 547 | len = ustcomm_send_unix_sock(sock, data, size); |
| 548 | if (len != size) { |
| 549 | if (len < 0) |
| 550 | return len; |
| 551 | else |
| 552 | return -EIO; |
| 553 | } |
| 554 | |
| 555 | /* Send wakeup fd */ |
| 556 | len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1); |
| 557 | if (len <= 0) { |
| 558 | if (len < 0) |
| 559 | return len; |
| 560 | else |
| 561 | return -EIO; |
| 562 | } |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static |
| 567 | int ustctl_send_stream(int sock, |
| 568 | uint32_t stream_nr, |
| 569 | uint64_t memory_map_size, |
| 570 | int shm_fd, int wakeup_fd, |
| 571 | int send_fd_only) |
| 572 | { |
| 573 | ssize_t len; |
| 574 | int fds[2]; |
| 575 | |
| 576 | if (!send_fd_only) { |
| 577 | if (shm_fd < 0) { |
| 578 | /* finish iteration */ |
| 579 | uint64_t v = -1; |
| 580 | |
| 581 | len = ustcomm_send_unix_sock(sock, &v, sizeof(v)); |
| 582 | if (len != sizeof(v)) { |
| 583 | if (len < 0) |
| 584 | return len; |
| 585 | else |
| 586 | return -EIO; |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /* Send mmap size */ |
| 592 | len = ustcomm_send_unix_sock(sock, &memory_map_size, |
| 593 | sizeof(memory_map_size)); |
| 594 | if (len != sizeof(memory_map_size)) { |
| 595 | if (len < 0) |
| 596 | return len; |
| 597 | else |
| 598 | return -EIO; |
| 599 | } |
| 600 | |
| 601 | /* Send stream nr */ |
| 602 | len = ustcomm_send_unix_sock(sock, &stream_nr, |
| 603 | sizeof(stream_nr)); |
| 604 | if (len != sizeof(stream_nr)) { |
| 605 | if (len < 0) |
| 606 | return len; |
| 607 | else |
| 608 | return -EIO; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* Send shm fd and wakeup fd */ |
| 613 | fds[0] = shm_fd; |
| 614 | fds[1] = wakeup_fd; |
| 615 | len = ustcomm_send_fds_unix_sock(sock, fds, 2); |
| 616 | if (len <= 0) { |
| 617 | if (len < 0) |
| 618 | return len; |
| 619 | else |
| 620 | return -EIO; |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | int ustctl_recv_channel_from_consumer(int sock, |
| 626 | struct lttng_ust_object_data **_channel_data) |
| 627 | { |
| 628 | struct lttng_ust_object_data *channel_data; |
| 629 | ssize_t len; |
| 630 | int wakeup_fd; |
| 631 | int ret; |
| 632 | |
| 633 | channel_data = zmalloc(sizeof(*channel_data)); |
| 634 | if (!channel_data) { |
| 635 | ret = -ENOMEM; |
| 636 | goto error_alloc; |
| 637 | } |
| 638 | channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL; |
| 639 | channel_data->handle = -1; |
| 640 | |
| 641 | /* recv mmap size */ |
| 642 | len = ustcomm_recv_unix_sock(sock, &channel_data->size, |
| 643 | sizeof(channel_data->size)); |
| 644 | if (len != sizeof(channel_data->size)) { |
| 645 | if (len < 0) |
| 646 | ret = len; |
| 647 | else |
| 648 | ret = -EINVAL; |
| 649 | goto error; |
| 650 | } |
| 651 | |
| 652 | /* recv channel type */ |
| 653 | len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type, |
| 654 | sizeof(channel_data->u.channel.type)); |
| 655 | if (len != sizeof(channel_data->u.channel.type)) { |
| 656 | if (len < 0) |
| 657 | ret = len; |
| 658 | else |
| 659 | ret = -EINVAL; |
| 660 | goto error; |
| 661 | } |
| 662 | |
| 663 | /* recv channel data */ |
| 664 | channel_data->u.channel.data = zmalloc(channel_data->size); |
| 665 | if (!channel_data->u.channel.data) { |
| 666 | ret = -ENOMEM; |
| 667 | goto error; |
| 668 | } |
| 669 | len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data, |
| 670 | channel_data->size); |
| 671 | if (len != channel_data->size) { |
| 672 | if (len < 0) |
| 673 | ret = len; |
| 674 | else |
| 675 | ret = -EINVAL; |
| 676 | goto error_recv_data; |
| 677 | } |
| 678 | /* recv wakeup fd */ |
| 679 | len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); |
| 680 | if (len <= 0) { |
| 681 | if (len < 0) { |
| 682 | ret = len; |
| 683 | goto error_recv_data; |
| 684 | } else { |
| 685 | ret = -EIO; |
| 686 | goto error_recv_data; |
| 687 | } |
| 688 | } |
| 689 | channel_data->u.channel.wakeup_fd = wakeup_fd; |
| 690 | *_channel_data = channel_data; |
| 691 | return 0; |
| 692 | |
| 693 | error_recv_data: |
| 694 | free(channel_data->u.channel.data); |
| 695 | error: |
| 696 | free(channel_data); |
| 697 | error_alloc: |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | int ustctl_recv_stream_from_consumer(int sock, |
| 702 | struct lttng_ust_object_data **_stream_data) |
| 703 | { |
| 704 | struct lttng_ust_object_data *stream_data; |
| 705 | ssize_t len; |
| 706 | int ret; |
| 707 | int fds[2]; |
| 708 | |
| 709 | stream_data = zmalloc(sizeof(*stream_data)); |
| 710 | if (!stream_data) { |
| 711 | ret = -ENOMEM; |
| 712 | goto error_alloc; |
| 713 | } |
| 714 | |
| 715 | stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM; |
| 716 | stream_data->handle = -1; |
| 717 | |
| 718 | /* recv mmap size */ |
| 719 | len = ustcomm_recv_unix_sock(sock, &stream_data->size, |
| 720 | sizeof(stream_data->size)); |
| 721 | if (len != sizeof(stream_data->size)) { |
| 722 | if (len < 0) |
| 723 | ret = len; |
| 724 | else |
| 725 | ret = -EINVAL; |
| 726 | goto error; |
| 727 | } |
| 728 | if (stream_data->size == -1) { |
| 729 | ret = -LTTNG_UST_ERR_NOENT; |
| 730 | goto error; |
| 731 | } |
| 732 | |
| 733 | /* recv stream nr */ |
| 734 | len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr, |
| 735 | sizeof(stream_data->u.stream.stream_nr)); |
| 736 | if (len != sizeof(stream_data->u.stream.stream_nr)) { |
| 737 | if (len < 0) |
| 738 | ret = len; |
| 739 | else |
| 740 | ret = -EINVAL; |
| 741 | goto error; |
| 742 | } |
| 743 | |
| 744 | /* recv shm fd and wakeup fd */ |
| 745 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
| 746 | if (len <= 0) { |
| 747 | if (len < 0) { |
| 748 | ret = len; |
| 749 | goto error; |
| 750 | } else { |
| 751 | ret = -EIO; |
| 752 | goto error; |
| 753 | } |
| 754 | } |
| 755 | stream_data->u.stream.shm_fd = fds[0]; |
| 756 | stream_data->u.stream.wakeup_fd = fds[1]; |
| 757 | *_stream_data = stream_data; |
| 758 | return 0; |
| 759 | |
| 760 | error: |
| 761 | free(stream_data); |
| 762 | error_alloc: |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | int ustctl_send_channel_to_ust(int sock, int session_handle, |
| 767 | struct lttng_ust_object_data *channel_data) |
| 768 | { |
| 769 | struct ustcomm_ust_msg lum; |
| 770 | struct ustcomm_ust_reply lur; |
| 771 | int ret; |
| 772 | |
| 773 | if (!channel_data) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | memset(&lum, 0, sizeof(lum)); |
| 777 | lum.handle = session_handle; |
| 778 | lum.cmd = LTTNG_UST_CHANNEL; |
| 779 | lum.u.channel.len = channel_data->size; |
| 780 | lum.u.channel.type = channel_data->u.channel.type; |
| 781 | ret = ustcomm_send_app_msg(sock, &lum); |
| 782 | if (ret) |
| 783 | return ret; |
| 784 | |
| 785 | ret = ustctl_send_channel(sock, |
| 786 | channel_data->u.channel.type, |
| 787 | channel_data->u.channel.data, |
| 788 | channel_data->size, |
| 789 | channel_data->u.channel.wakeup_fd, |
| 790 | 1); |
| 791 | if (ret) |
| 792 | return ret; |
| 793 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 794 | if (!ret) { |
| 795 | channel_data->handle = lur.ret_val; |
| 796 | } |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | int ustctl_send_stream_to_ust(int sock, |
| 801 | struct lttng_ust_object_data *channel_data, |
| 802 | struct lttng_ust_object_data *stream_data) |
| 803 | { |
| 804 | struct ustcomm_ust_msg lum; |
| 805 | struct ustcomm_ust_reply lur; |
| 806 | int ret; |
| 807 | |
| 808 | memset(&lum, 0, sizeof(lum)); |
| 809 | lum.handle = channel_data->handle; |
| 810 | lum.cmd = LTTNG_UST_STREAM; |
| 811 | lum.u.stream.len = stream_data->size; |
| 812 | lum.u.stream.stream_nr = stream_data->u.stream.stream_nr; |
| 813 | ret = ustcomm_send_app_msg(sock, &lum); |
| 814 | if (ret) |
| 815 | return ret; |
| 816 | |
| 817 | assert(stream_data); |
| 818 | assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM); |
| 819 | |
| 820 | ret = ustctl_send_stream(sock, |
| 821 | stream_data->u.stream.stream_nr, |
| 822 | stream_data->size, |
| 823 | stream_data->u.stream.shm_fd, |
| 824 | stream_data->u.stream.wakeup_fd, 1); |
| 825 | if (ret) |
| 826 | return ret; |
| 827 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 828 | } |
| 829 | |
| 830 | int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest, |
| 831 | struct lttng_ust_object_data *src) |
| 832 | { |
| 833 | struct lttng_ust_object_data *obj; |
| 834 | int ret; |
| 835 | |
| 836 | if (src->handle != -1) { |
| 837 | ret = -EINVAL; |
| 838 | goto error; |
| 839 | } |
| 840 | |
| 841 | obj = zmalloc(sizeof(*obj)); |
| 842 | if (!obj) { |
| 843 | ret = -ENOMEM; |
| 844 | goto error; |
| 845 | } |
| 846 | |
| 847 | obj->type = src->type; |
| 848 | obj->handle = src->handle; |
| 849 | obj->size = src->size; |
| 850 | |
| 851 | switch (obj->type) { |
| 852 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: |
| 853 | { |
| 854 | obj->u.channel.type = src->u.channel.type; |
| 855 | if (src->u.channel.wakeup_fd >= 0) { |
| 856 | obj->u.channel.wakeup_fd = |
| 857 | dup(src->u.channel.wakeup_fd); |
| 858 | if (obj->u.channel.wakeup_fd < 0) { |
| 859 | ret = errno; |
| 860 | goto chan_error_wakeup_fd; |
| 861 | } |
| 862 | } else { |
| 863 | obj->u.channel.wakeup_fd = |
| 864 | src->u.channel.wakeup_fd; |
| 865 | } |
| 866 | obj->u.channel.data = zmalloc(obj->size); |
| 867 | if (!obj->u.channel.data) { |
| 868 | ret = -ENOMEM; |
| 869 | goto chan_error_alloc; |
| 870 | } |
| 871 | memcpy(obj->u.channel.data, src->u.channel.data, obj->size); |
| 872 | break; |
| 873 | |
| 874 | chan_error_alloc: |
| 875 | if (src->u.channel.wakeup_fd >= 0) { |
| 876 | int closeret; |
| 877 | |
| 878 | closeret = close(obj->u.channel.wakeup_fd); |
| 879 | if (closeret) { |
| 880 | PERROR("close"); |
| 881 | } |
| 882 | } |
| 883 | chan_error_wakeup_fd: |
| 884 | goto error_type; |
| 885 | |
| 886 | } |
| 887 | |
| 888 | case LTTNG_UST_OBJECT_TYPE_STREAM: |
| 889 | { |
| 890 | obj->u.stream.stream_nr = src->u.stream.stream_nr; |
| 891 | if (src->u.stream.wakeup_fd >= 0) { |
| 892 | obj->u.stream.wakeup_fd = |
| 893 | dup(src->u.stream.wakeup_fd); |
| 894 | if (obj->u.stream.wakeup_fd < 0) { |
| 895 | ret = errno; |
| 896 | goto stream_error_wakeup_fd; |
| 897 | } |
| 898 | } else { |
| 899 | obj->u.stream.wakeup_fd = |
| 900 | src->u.stream.wakeup_fd; |
| 901 | } |
| 902 | |
| 903 | if (src->u.stream.shm_fd >= 0) { |
| 904 | obj->u.stream.shm_fd = |
| 905 | dup(src->u.stream.shm_fd); |
| 906 | if (obj->u.stream.shm_fd < 0) { |
| 907 | ret = errno; |
| 908 | goto stream_error_shm_fd; |
| 909 | } |
| 910 | } else { |
| 911 | obj->u.stream.shm_fd = |
| 912 | src->u.stream.shm_fd; |
| 913 | } |
| 914 | break; |
| 915 | |
| 916 | stream_error_shm_fd: |
| 917 | if (src->u.stream.wakeup_fd >= 0) { |
| 918 | int closeret; |
| 919 | |
| 920 | closeret = close(obj->u.stream.wakeup_fd); |
| 921 | if (closeret) { |
| 922 | PERROR("close"); |
| 923 | } |
| 924 | } |
| 925 | stream_error_wakeup_fd: |
| 926 | goto error_type; |
| 927 | } |
| 928 | |
| 929 | default: |
| 930 | ret = -EINVAL; |
| 931 | goto error_type; |
| 932 | } |
| 933 | |
| 934 | *dest = obj; |
| 935 | return 0; |
| 936 | |
| 937 | error_type: |
| 938 | free(obj); |
| 939 | error: |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | |
| 944 | /* Buffer operations */ |
| 945 | |
| 946 | int ustctl_get_nr_stream_per_channel(void) |
| 947 | { |
| 948 | return num_possible_cpus(); |
| 949 | } |
| 950 | |
| 951 | struct ustctl_consumer_channel * |
| 952 | ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, |
| 953 | const int *stream_fds, int nr_stream_fds) |
| 954 | { |
| 955 | struct ustctl_consumer_channel *chan; |
| 956 | const char *transport_name; |
| 957 | struct lttng_transport *transport; |
| 958 | |
| 959 | switch (attr->type) { |
| 960 | case LTTNG_UST_CHAN_PER_CPU: |
| 961 | if (attr->output == LTTNG_UST_MMAP) { |
| 962 | if (attr->overwrite) { |
| 963 | if (attr->read_timer_interval == 0) { |
| 964 | transport_name = "relay-overwrite-mmap"; |
| 965 | } else { |
| 966 | transport_name = "relay-overwrite-rt-mmap"; |
| 967 | } |
| 968 | } else { |
| 969 | if (attr->read_timer_interval == 0) { |
| 970 | transport_name = "relay-discard-mmap"; |
| 971 | } else { |
| 972 | transport_name = "relay-discard-rt-mmap"; |
| 973 | } |
| 974 | } |
| 975 | } else { |
| 976 | return NULL; |
| 977 | } |
| 978 | break; |
| 979 | case LTTNG_UST_CHAN_METADATA: |
| 980 | if (attr->output == LTTNG_UST_MMAP) |
| 981 | transport_name = "relay-metadata-mmap"; |
| 982 | else |
| 983 | return NULL; |
| 984 | break; |
| 985 | default: |
| 986 | transport_name = "<unknown>"; |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | transport = lttng_transport_find(transport_name); |
| 991 | if (!transport) { |
| 992 | DBG("LTTng transport %s not found\n", |
| 993 | transport_name); |
| 994 | return NULL; |
| 995 | } |
| 996 | |
| 997 | chan = zmalloc(sizeof(*chan)); |
| 998 | if (!chan) |
| 999 | return NULL; |
| 1000 | |
| 1001 | chan->chan = transport->ops.channel_create(transport_name, NULL, |
| 1002 | attr->subbuf_size, attr->num_subbuf, |
| 1003 | attr->switch_timer_interval, |
| 1004 | attr->read_timer_interval, |
| 1005 | attr->uuid, attr->chan_id, |
| 1006 | stream_fds, nr_stream_fds); |
| 1007 | if (!chan->chan) { |
| 1008 | goto chan_error; |
| 1009 | } |
| 1010 | chan->chan->ops = &transport->ops; |
| 1011 | memcpy(&chan->attr, attr, sizeof(chan->attr)); |
| 1012 | chan->wait_fd = ustctl_channel_get_wait_fd(chan); |
| 1013 | chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan); |
| 1014 | return chan; |
| 1015 | |
| 1016 | chan_error: |
| 1017 | free(chan); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
| 1021 | void ustctl_destroy_channel(struct ustctl_consumer_channel *chan) |
| 1022 | { |
| 1023 | (void) ustctl_channel_close_wait_fd(chan); |
| 1024 | (void) ustctl_channel_close_wakeup_fd(chan); |
| 1025 | chan->chan->ops->channel_destroy(chan->chan); |
| 1026 | free(chan); |
| 1027 | } |
| 1028 | |
| 1029 | int ustctl_send_channel_to_sessiond(int sock, |
| 1030 | struct ustctl_consumer_channel *channel) |
| 1031 | { |
| 1032 | struct shm_object_table *table; |
| 1033 | |
| 1034 | table = channel->chan->handle->table; |
| 1035 | if (table->size <= 0) |
| 1036 | return -EINVAL; |
| 1037 | return ustctl_send_channel(sock, |
| 1038 | channel->attr.type, |
| 1039 | table->objects[0].memory_map, |
| 1040 | table->objects[0].memory_map_size, |
| 1041 | channel->wakeup_fd, |
| 1042 | 0); |
| 1043 | } |
| 1044 | |
| 1045 | int ustctl_send_stream_to_sessiond(int sock, |
| 1046 | struct ustctl_consumer_stream *stream) |
| 1047 | { |
| 1048 | if (!stream) |
| 1049 | return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0); |
| 1050 | |
| 1051 | return ustctl_send_stream(sock, |
| 1052 | stream->cpu, |
| 1053 | stream->memory_map_size, |
| 1054 | stream->shm_fd, stream->wakeup_fd, |
| 1055 | 0); |
| 1056 | } |
| 1057 | |
| 1058 | int ustctl_write_metadata_to_channel( |
| 1059 | struct ustctl_consumer_channel *channel, |
| 1060 | const char *metadata_str, /* NOT null-terminated */ |
| 1061 | size_t len) /* metadata length */ |
| 1062 | { |
| 1063 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 1064 | struct lttng_channel *chan = channel->chan; |
| 1065 | const char *str = metadata_str; |
| 1066 | int ret = 0, waitret; |
| 1067 | size_t reserve_len, pos; |
| 1068 | |
| 1069 | for (pos = 0; pos < len; pos += reserve_len) { |
| 1070 | reserve_len = min_t(size_t, |
| 1071 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 1072 | len - pos); |
| 1073 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 1074 | sizeof(char), -1, chan->handle); |
| 1075 | /* |
| 1076 | * We don't care about metadata buffer's records lost |
| 1077 | * count, because we always retry here. Report error if |
| 1078 | * we need to bail out after timeout or being |
| 1079 | * interrupted. |
| 1080 | */ |
| 1081 | waitret = wait_cond_interruptible_timeout( |
| 1082 | ({ |
| 1083 | ret = chan->ops->event_reserve(&ctx, 0); |
| 1084 | ret != -ENOBUFS || !ret; |
| 1085 | }), |
| 1086 | LTTNG_METADATA_TIMEOUT_MSEC); |
| 1087 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
| 1088 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
| 1089 | waitret == -EINTR ? "interrupted" : |
| 1090 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 1091 | if (waitret == -EINTR) |
| 1092 | ret = waitret; |
| 1093 | goto end; |
| 1094 | } |
| 1095 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
| 1096 | chan->ops->event_commit(&ctx); |
| 1097 | } |
| 1098 | end: |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Write at most one packet in the channel. |
| 1104 | * Returns the number of bytes written on success, < 0 on error. |
| 1105 | */ |
| 1106 | ssize_t ustctl_write_one_packet_to_channel( |
| 1107 | struct ustctl_consumer_channel *channel, |
| 1108 | const char *metadata_str, /* NOT null-terminated */ |
| 1109 | size_t len) /* metadata length */ |
| 1110 | { |
| 1111 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 1112 | struct lttng_channel *chan = channel->chan; |
| 1113 | const char *str = metadata_str; |
| 1114 | ssize_t reserve_len; |
| 1115 | int ret; |
| 1116 | |
| 1117 | reserve_len = min_t(ssize_t, |
| 1118 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 1119 | len); |
| 1120 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 1121 | sizeof(char), -1, chan->handle); |
| 1122 | ret = chan->ops->event_reserve(&ctx, 0); |
| 1123 | if (ret != 0) { |
| 1124 | DBG("LTTng: event reservation failed"); |
| 1125 | assert(ret < 0); |
| 1126 | reserve_len = ret; |
| 1127 | goto end; |
| 1128 | } |
| 1129 | chan->ops->event_write(&ctx, str, reserve_len); |
| 1130 | chan->ops->event_commit(&ctx); |
| 1131 | |
| 1132 | end: |
| 1133 | return reserve_len; |
| 1134 | } |
| 1135 | |
| 1136 | int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan) |
| 1137 | { |
| 1138 | struct channel *chan; |
| 1139 | int ret; |
| 1140 | |
| 1141 | chan = consumer_chan->chan->chan; |
| 1142 | ret = ring_buffer_channel_close_wait_fd(&chan->backend.config, |
| 1143 | chan, chan->handle); |
| 1144 | if (!ret) |
| 1145 | consumer_chan->wait_fd = -1; |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan) |
| 1150 | { |
| 1151 | struct channel *chan; |
| 1152 | int ret; |
| 1153 | |
| 1154 | chan = consumer_chan->chan->chan; |
| 1155 | ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config, |
| 1156 | chan, chan->handle); |
| 1157 | if (!ret) |
| 1158 | consumer_chan->wakeup_fd = -1; |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream) |
| 1163 | { |
| 1164 | struct channel *chan; |
| 1165 | |
| 1166 | chan = stream->chan->chan->chan; |
| 1167 | return ring_buffer_stream_close_wait_fd(&chan->backend.config, |
| 1168 | chan, stream->handle, stream->cpu); |
| 1169 | } |
| 1170 | |
| 1171 | int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 1172 | { |
| 1173 | struct channel *chan; |
| 1174 | |
| 1175 | chan = stream->chan->chan->chan; |
| 1176 | return ring_buffer_stream_close_wakeup_fd(&chan->backend.config, |
| 1177 | chan, stream->handle, stream->cpu); |
| 1178 | } |
| 1179 | |
| 1180 | struct ustctl_consumer_stream * |
| 1181 | ustctl_create_stream(struct ustctl_consumer_channel *channel, |
| 1182 | int cpu) |
| 1183 | { |
| 1184 | struct ustctl_consumer_stream *stream; |
| 1185 | struct lttng_ust_shm_handle *handle; |
| 1186 | struct channel *chan; |
| 1187 | int shm_fd, wait_fd, wakeup_fd; |
| 1188 | uint64_t memory_map_size; |
| 1189 | struct lttng_ust_lib_ring_buffer *buf; |
| 1190 | int ret; |
| 1191 | |
| 1192 | if (!channel) |
| 1193 | return NULL; |
| 1194 | handle = channel->chan->handle; |
| 1195 | if (!handle) |
| 1196 | return NULL; |
| 1197 | |
| 1198 | chan = channel->chan->chan; |
| 1199 | buf = channel_get_ring_buffer(&chan->backend.config, |
| 1200 | chan, cpu, handle, &shm_fd, &wait_fd, |
| 1201 | &wakeup_fd, &memory_map_size); |
| 1202 | if (!buf) |
| 1203 | return NULL; |
| 1204 | ret = lib_ring_buffer_open_read(buf, handle); |
| 1205 | if (ret) |
| 1206 | return NULL; |
| 1207 | |
| 1208 | stream = zmalloc(sizeof(*stream)); |
| 1209 | if (!stream) |
| 1210 | goto alloc_error; |
| 1211 | stream->handle = handle; |
| 1212 | stream->buf = buf; |
| 1213 | stream->chan = channel; |
| 1214 | stream->shm_fd = shm_fd; |
| 1215 | stream->wait_fd = wait_fd; |
| 1216 | stream->wakeup_fd = wakeup_fd; |
| 1217 | stream->memory_map_size = memory_map_size; |
| 1218 | stream->cpu = cpu; |
| 1219 | return stream; |
| 1220 | |
| 1221 | alloc_error: |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | |
| 1225 | void ustctl_destroy_stream(struct ustctl_consumer_stream *stream) |
| 1226 | { |
| 1227 | struct lttng_ust_lib_ring_buffer *buf; |
| 1228 | struct ustctl_consumer_channel *consumer_chan; |
| 1229 | |
| 1230 | assert(stream); |
| 1231 | buf = stream->buf; |
| 1232 | consumer_chan = stream->chan; |
| 1233 | (void) ustctl_stream_close_wait_fd(stream); |
| 1234 | (void) ustctl_stream_close_wakeup_fd(stream); |
| 1235 | lib_ring_buffer_release_read(buf, consumer_chan->chan->handle); |
| 1236 | free(stream); |
| 1237 | } |
| 1238 | |
| 1239 | int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan) |
| 1240 | { |
| 1241 | if (!chan) |
| 1242 | return -EINVAL; |
| 1243 | return shm_get_wait_fd(chan->chan->handle, |
| 1244 | &chan->chan->handle->chan._ref); |
| 1245 | } |
| 1246 | |
| 1247 | int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan) |
| 1248 | { |
| 1249 | if (!chan) |
| 1250 | return -EINVAL; |
| 1251 | return shm_get_wakeup_fd(chan->chan->handle, |
| 1252 | &chan->chan->handle->chan._ref); |
| 1253 | } |
| 1254 | |
| 1255 | int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream) |
| 1256 | { |
| 1257 | struct lttng_ust_lib_ring_buffer *buf; |
| 1258 | struct ustctl_consumer_channel *consumer_chan; |
| 1259 | |
| 1260 | if (!stream) |
| 1261 | return -EINVAL; |
| 1262 | buf = stream->buf; |
| 1263 | consumer_chan = stream->chan; |
| 1264 | return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 1265 | } |
| 1266 | |
| 1267 | int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 1268 | { |
| 1269 | struct lttng_ust_lib_ring_buffer *buf; |
| 1270 | struct ustctl_consumer_channel *consumer_chan; |
| 1271 | |
| 1272 | if (!stream) |
| 1273 | return -EINVAL; |
| 1274 | buf = stream->buf; |
| 1275 | consumer_chan = stream->chan; |
| 1276 | return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 1277 | } |
| 1278 | |
| 1279 | /* For mmap mode, readable without "get" operation */ |
| 1280 | |
| 1281 | void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream) |
| 1282 | { |
| 1283 | struct lttng_ust_lib_ring_buffer *buf; |
| 1284 | struct ustctl_consumer_channel *consumer_chan; |
| 1285 | |
| 1286 | if (!stream) |
| 1287 | return NULL; |
| 1288 | buf = stream->buf; |
| 1289 | consumer_chan = stream->chan; |
| 1290 | return shmp(consumer_chan->chan->handle, buf->backend.memory_map); |
| 1291 | } |
| 1292 | |
| 1293 | /* returns the length to mmap. */ |
| 1294 | int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream, |
| 1295 | unsigned long *len) |
| 1296 | { |
| 1297 | struct ustctl_consumer_channel *consumer_chan; |
| 1298 | unsigned long mmap_buf_len; |
| 1299 | struct channel *chan; |
| 1300 | |
| 1301 | if (!stream) |
| 1302 | return -EINVAL; |
| 1303 | consumer_chan = stream->chan; |
| 1304 | chan = consumer_chan->chan->chan; |
| 1305 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1306 | return -EINVAL; |
| 1307 | mmap_buf_len = chan->backend.buf_size; |
| 1308 | if (chan->backend.extra_reader_sb) |
| 1309 | mmap_buf_len += chan->backend.subbuf_size; |
| 1310 | if (mmap_buf_len > INT_MAX) |
| 1311 | return -EFBIG; |
| 1312 | *len = mmap_buf_len; |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | /* returns the maximum size for sub-buffers. */ |
| 1317 | int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1318 | unsigned long *len) |
| 1319 | { |
| 1320 | struct ustctl_consumer_channel *consumer_chan; |
| 1321 | struct channel *chan; |
| 1322 | |
| 1323 | if (!stream) |
| 1324 | return -EINVAL; |
| 1325 | consumer_chan = stream->chan; |
| 1326 | chan = consumer_chan->chan->chan; |
| 1327 | *len = chan->backend.subbuf_size; |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * For mmap mode, operate on the current packet (between get/put or |
| 1333 | * get_next/put_next). |
| 1334 | */ |
| 1335 | |
| 1336 | /* returns the offset of the subbuffer belonging to the mmap reader. */ |
| 1337 | int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream, |
| 1338 | unsigned long *off) |
| 1339 | { |
| 1340 | struct channel *chan; |
| 1341 | unsigned long sb_bindex; |
| 1342 | struct lttng_ust_lib_ring_buffer *buf; |
| 1343 | struct ustctl_consumer_channel *consumer_chan; |
| 1344 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx; |
| 1345 | struct lttng_ust_lib_ring_buffer_backend_pages *pages; |
| 1346 | |
| 1347 | if (!stream) |
| 1348 | return -EINVAL; |
| 1349 | buf = stream->buf; |
| 1350 | consumer_chan = stream->chan; |
| 1351 | chan = consumer_chan->chan->chan; |
| 1352 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1353 | return -EINVAL; |
| 1354 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, |
| 1355 | buf->backend.buf_rsb.id); |
| 1356 | barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array, |
| 1357 | sb_bindex); |
| 1358 | if (!barray_idx) |
| 1359 | return -EINVAL; |
| 1360 | pages = shmp(consumer_chan->chan->handle, barray_idx->shmp); |
| 1361 | if (!pages) |
| 1362 | return -EINVAL; |
| 1363 | *off = pages->mmap_offset; |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1368 | int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1369 | unsigned long *len) |
| 1370 | { |
| 1371 | struct ustctl_consumer_channel *consumer_chan; |
| 1372 | struct channel *chan; |
| 1373 | struct lttng_ust_lib_ring_buffer *buf; |
| 1374 | |
| 1375 | if (!stream) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | buf = stream->buf; |
| 1379 | consumer_chan = stream->chan; |
| 1380 | chan = consumer_chan->chan->chan; |
| 1381 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1382 | consumer_chan->chan->handle); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1387 | int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1388 | unsigned long *len) |
| 1389 | { |
| 1390 | struct ustctl_consumer_channel *consumer_chan; |
| 1391 | struct channel *chan; |
| 1392 | struct lttng_ust_lib_ring_buffer *buf; |
| 1393 | |
| 1394 | if (!stream) |
| 1395 | return -EINVAL; |
| 1396 | buf = stream->buf; |
| 1397 | consumer_chan = stream->chan; |
| 1398 | chan = consumer_chan->chan->chan; |
| 1399 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1400 | consumer_chan->chan->handle); |
| 1401 | *len = PAGE_ALIGN(*len); |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* Get exclusive read access to the next sub-buffer that can be read. */ |
| 1406 | int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1407 | { |
| 1408 | struct lttng_ust_lib_ring_buffer *buf; |
| 1409 | struct ustctl_consumer_channel *consumer_chan; |
| 1410 | |
| 1411 | if (!stream) |
| 1412 | return -EINVAL; |
| 1413 | buf = stream->buf; |
| 1414 | consumer_chan = stream->chan; |
| 1415 | return lib_ring_buffer_get_next_subbuf(buf, |
| 1416 | consumer_chan->chan->handle); |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /* Release exclusive sub-buffer access, move consumer forward. */ |
| 1421 | int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1422 | { |
| 1423 | struct lttng_ust_lib_ring_buffer *buf; |
| 1424 | struct ustctl_consumer_channel *consumer_chan; |
| 1425 | |
| 1426 | if (!stream) |
| 1427 | return -EINVAL; |
| 1428 | buf = stream->buf; |
| 1429 | consumer_chan = stream->chan; |
| 1430 | lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | /* snapshot */ |
| 1435 | |
| 1436 | /* Get a snapshot of the current ring buffer producer and consumer positions */ |
| 1437 | int ustctl_snapshot(struct ustctl_consumer_stream *stream) |
| 1438 | { |
| 1439 | struct lttng_ust_lib_ring_buffer *buf; |
| 1440 | struct ustctl_consumer_channel *consumer_chan; |
| 1441 | |
| 1442 | if (!stream) |
| 1443 | return -EINVAL; |
| 1444 | buf = stream->buf; |
| 1445 | consumer_chan = stream->chan; |
| 1446 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 1447 | &buf->prod_snapshot, consumer_chan->chan->handle); |
| 1448 | } |
| 1449 | |
| 1450 | /* Get the consumer position (iteration start) */ |
| 1451 | int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream, |
| 1452 | unsigned long *pos) |
| 1453 | { |
| 1454 | struct lttng_ust_lib_ring_buffer *buf; |
| 1455 | |
| 1456 | if (!stream) |
| 1457 | return -EINVAL; |
| 1458 | buf = stream->buf; |
| 1459 | *pos = buf->cons_snapshot; |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | /* Get the producer position (iteration end) */ |
| 1464 | int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream, |
| 1465 | unsigned long *pos) |
| 1466 | { |
| 1467 | struct lttng_ust_lib_ring_buffer *buf; |
| 1468 | |
| 1469 | if (!stream) |
| 1470 | return -EINVAL; |
| 1471 | buf = stream->buf; |
| 1472 | *pos = buf->prod_snapshot; |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | /* Get exclusive read access to the specified sub-buffer position */ |
| 1477 | int ustctl_get_subbuf(struct ustctl_consumer_stream *stream, |
| 1478 | unsigned long *pos) |
| 1479 | { |
| 1480 | struct lttng_ust_lib_ring_buffer *buf; |
| 1481 | struct ustctl_consumer_channel *consumer_chan; |
| 1482 | |
| 1483 | if (!stream) |
| 1484 | return -EINVAL; |
| 1485 | buf = stream->buf; |
| 1486 | consumer_chan = stream->chan; |
| 1487 | return lib_ring_buffer_get_subbuf(buf, *pos, |
| 1488 | consumer_chan->chan->handle); |
| 1489 | } |
| 1490 | |
| 1491 | /* Release exclusive sub-buffer access */ |
| 1492 | int ustctl_put_subbuf(struct ustctl_consumer_stream *stream) |
| 1493 | { |
| 1494 | struct lttng_ust_lib_ring_buffer *buf; |
| 1495 | struct ustctl_consumer_channel *consumer_chan; |
| 1496 | |
| 1497 | if (!stream) |
| 1498 | return -EINVAL; |
| 1499 | buf = stream->buf; |
| 1500 | consumer_chan = stream->chan; |
| 1501 | lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle); |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | void ustctl_flush_buffer(struct ustctl_consumer_stream *stream, |
| 1506 | int producer_active) |
| 1507 | { |
| 1508 | struct lttng_ust_lib_ring_buffer *buf; |
| 1509 | struct ustctl_consumer_channel *consumer_chan; |
| 1510 | |
| 1511 | assert(stream); |
| 1512 | buf = stream->buf; |
| 1513 | consumer_chan = stream->chan; |
| 1514 | lib_ring_buffer_switch_slow(buf, |
| 1515 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, |
| 1516 | consumer_chan->chan->handle); |
| 1517 | } |
| 1518 | |
| 1519 | static |
| 1520 | struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb( |
| 1521 | struct lttng_ust_lib_ring_buffer *buf, |
| 1522 | struct lttng_ust_shm_handle *handle) |
| 1523 | { |
| 1524 | struct channel *chan; |
| 1525 | const struct lttng_ust_lib_ring_buffer_config *config; |
| 1526 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1527 | |
| 1528 | chan = shmp(handle, buf->backend.chan); |
| 1529 | if (!chan) |
| 1530 | return NULL; |
| 1531 | config = &chan->backend.config; |
| 1532 | if (!config->cb_ptr) |
| 1533 | return NULL; |
| 1534 | client_cb = caa_container_of(config->cb_ptr, |
| 1535 | struct lttng_ust_client_lib_ring_buffer_client_cb, |
| 1536 | parent); |
| 1537 | return client_cb; |
| 1538 | } |
| 1539 | |
| 1540 | int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream, |
| 1541 | uint64_t *timestamp_begin) |
| 1542 | { |
| 1543 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1544 | struct lttng_ust_lib_ring_buffer *buf; |
| 1545 | struct lttng_ust_shm_handle *handle; |
| 1546 | |
| 1547 | if (!stream || !timestamp_begin) |
| 1548 | return -EINVAL; |
| 1549 | buf = stream->buf; |
| 1550 | handle = stream->chan->chan->handle; |
| 1551 | client_cb = get_client_cb(buf, handle); |
| 1552 | if (!client_cb) |
| 1553 | return -ENOSYS; |
| 1554 | return client_cb->timestamp_begin(buf, handle, timestamp_begin); |
| 1555 | } |
| 1556 | |
| 1557 | int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream, |
| 1558 | uint64_t *timestamp_end) |
| 1559 | { |
| 1560 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1561 | struct lttng_ust_lib_ring_buffer *buf; |
| 1562 | struct lttng_ust_shm_handle *handle; |
| 1563 | |
| 1564 | if (!stream || !timestamp_end) |
| 1565 | return -EINVAL; |
| 1566 | buf = stream->buf; |
| 1567 | handle = stream->chan->chan->handle; |
| 1568 | client_cb = get_client_cb(buf, handle); |
| 1569 | if (!client_cb) |
| 1570 | return -ENOSYS; |
| 1571 | return client_cb->timestamp_end(buf, handle, timestamp_end); |
| 1572 | } |
| 1573 | |
| 1574 | int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream, |
| 1575 | uint64_t *events_discarded) |
| 1576 | { |
| 1577 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1578 | struct lttng_ust_lib_ring_buffer *buf; |
| 1579 | struct lttng_ust_shm_handle *handle; |
| 1580 | |
| 1581 | if (!stream || !events_discarded) |
| 1582 | return -EINVAL; |
| 1583 | buf = stream->buf; |
| 1584 | handle = stream->chan->chan->handle; |
| 1585 | client_cb = get_client_cb(buf, handle); |
| 1586 | if (!client_cb) |
| 1587 | return -ENOSYS; |
| 1588 | return client_cb->events_discarded(buf, handle, events_discarded); |
| 1589 | } |
| 1590 | |
| 1591 | int ustctl_get_content_size(struct ustctl_consumer_stream *stream, |
| 1592 | uint64_t *content_size) |
| 1593 | { |
| 1594 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1595 | struct lttng_ust_lib_ring_buffer *buf; |
| 1596 | struct lttng_ust_shm_handle *handle; |
| 1597 | |
| 1598 | if (!stream || !content_size) |
| 1599 | return -EINVAL; |
| 1600 | buf = stream->buf; |
| 1601 | handle = stream->chan->chan->handle; |
| 1602 | client_cb = get_client_cb(buf, handle); |
| 1603 | if (!client_cb) |
| 1604 | return -ENOSYS; |
| 1605 | return client_cb->content_size(buf, handle, content_size); |
| 1606 | } |
| 1607 | |
| 1608 | int ustctl_get_packet_size(struct ustctl_consumer_stream *stream, |
| 1609 | uint64_t *packet_size) |
| 1610 | { |
| 1611 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1612 | struct lttng_ust_lib_ring_buffer *buf; |
| 1613 | struct lttng_ust_shm_handle *handle; |
| 1614 | |
| 1615 | if (!stream || !packet_size) |
| 1616 | return -EINVAL; |
| 1617 | buf = stream->buf; |
| 1618 | handle = stream->chan->chan->handle; |
| 1619 | client_cb = get_client_cb(buf, handle); |
| 1620 | if (!client_cb) |
| 1621 | return -ENOSYS; |
| 1622 | return client_cb->packet_size(buf, handle, packet_size); |
| 1623 | } |
| 1624 | |
| 1625 | int ustctl_get_stream_id(struct ustctl_consumer_stream *stream, |
| 1626 | uint64_t *stream_id) |
| 1627 | { |
| 1628 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1629 | struct lttng_ust_lib_ring_buffer *buf; |
| 1630 | struct lttng_ust_shm_handle *handle; |
| 1631 | |
| 1632 | if (!stream || !stream_id) |
| 1633 | return -EINVAL; |
| 1634 | buf = stream->buf; |
| 1635 | handle = stream->chan->chan->handle; |
| 1636 | client_cb = get_client_cb(buf, handle); |
| 1637 | if (!client_cb) |
| 1638 | return -ENOSYS; |
| 1639 | return client_cb->stream_id(buf, handle, stream_id); |
| 1640 | } |
| 1641 | |
| 1642 | int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream, |
| 1643 | uint64_t *ts) |
| 1644 | { |
| 1645 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1646 | struct lttng_ust_lib_ring_buffer *buf; |
| 1647 | struct lttng_ust_shm_handle *handle; |
| 1648 | |
| 1649 | if (!stream || !ts) |
| 1650 | return -EINVAL; |
| 1651 | buf = stream->buf; |
| 1652 | handle = stream->chan->chan->handle; |
| 1653 | client_cb = get_client_cb(buf, handle); |
| 1654 | if (!client_cb || !client_cb->current_timestamp) |
| 1655 | return -ENOSYS; |
| 1656 | return client_cb->current_timestamp(buf, handle, ts); |
| 1657 | } |
| 1658 | |
| 1659 | #if defined(__x86_64__) || defined(__i386__) |
| 1660 | |
| 1661 | int ustctl_has_perf_counters(void) |
| 1662 | { |
| 1663 | return 1; |
| 1664 | } |
| 1665 | |
| 1666 | #else |
| 1667 | |
| 1668 | int ustctl_has_perf_counters(void) |
| 1669 | { |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | #endif |
| 1674 | |
| 1675 | /* |
| 1676 | * Returns 0 on success, negative error value on error. |
| 1677 | */ |
| 1678 | int ustctl_recv_reg_msg(int sock, |
| 1679 | enum ustctl_socket_type *type, |
| 1680 | uint32_t *major, |
| 1681 | uint32_t *minor, |
| 1682 | uint32_t *pid, |
| 1683 | uint32_t *ppid, |
| 1684 | uint32_t *uid, |
| 1685 | uint32_t *gid, |
| 1686 | uint32_t *bits_per_long, |
| 1687 | uint32_t *uint8_t_alignment, |
| 1688 | uint32_t *uint16_t_alignment, |
| 1689 | uint32_t *uint32_t_alignment, |
| 1690 | uint32_t *uint64_t_alignment, |
| 1691 | uint32_t *long_alignment, |
| 1692 | int *byte_order, |
| 1693 | char *name) |
| 1694 | { |
| 1695 | ssize_t len; |
| 1696 | struct ustctl_reg_msg reg_msg; |
| 1697 | |
| 1698 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
| 1699 | if (len > 0 && len != sizeof(reg_msg)) |
| 1700 | return -EIO; |
| 1701 | if (len == 0) |
| 1702 | return -EPIPE; |
| 1703 | if (len < 0) |
| 1704 | return len; |
| 1705 | |
| 1706 | if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) { |
| 1707 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 1708 | BIG_ENDIAN : LITTLE_ENDIAN; |
| 1709 | } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) { |
| 1710 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 1711 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 1712 | } else { |
| 1713 | return -LTTNG_UST_ERR_INVAL_MAGIC; |
| 1714 | } |
| 1715 | switch (reg_msg.socket_type) { |
| 1716 | case 0: *type = USTCTL_SOCKET_CMD; |
| 1717 | break; |
| 1718 | case 1: *type = USTCTL_SOCKET_NOTIFY; |
| 1719 | break; |
| 1720 | default: |
| 1721 | return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE; |
| 1722 | } |
| 1723 | *major = reg_msg.major; |
| 1724 | *minor = reg_msg.minor; |
| 1725 | *pid = reg_msg.pid; |
| 1726 | *ppid = reg_msg.ppid; |
| 1727 | *uid = reg_msg.uid; |
| 1728 | *gid = reg_msg.gid; |
| 1729 | *bits_per_long = reg_msg.bits_per_long; |
| 1730 | *uint8_t_alignment = reg_msg.uint8_t_alignment; |
| 1731 | *uint16_t_alignment = reg_msg.uint16_t_alignment; |
| 1732 | *uint32_t_alignment = reg_msg.uint32_t_alignment; |
| 1733 | *uint64_t_alignment = reg_msg.uint64_t_alignment; |
| 1734 | *long_alignment = reg_msg.long_alignment; |
| 1735 | memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN); |
| 1736 | if (reg_msg.major != LTTNG_UST_ABI_MAJOR_VERSION) { |
| 1737 | return -LTTNG_UST_ERR_UNSUP_MAJOR; |
| 1738 | } |
| 1739 | |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd) |
| 1744 | { |
| 1745 | struct ustcomm_notify_hdr header; |
| 1746 | ssize_t len; |
| 1747 | |
| 1748 | len = ustcomm_recv_unix_sock(sock, &header, sizeof(header)); |
| 1749 | if (len > 0 && len != sizeof(header)) |
| 1750 | return -EIO; |
| 1751 | if (len == 0) |
| 1752 | return -EPIPE; |
| 1753 | if (len < 0) |
| 1754 | return len; |
| 1755 | switch (header.notify_cmd) { |
| 1756 | case 0: |
| 1757 | *notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 1758 | break; |
| 1759 | case 1: |
| 1760 | *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1761 | break; |
| 1762 | default: |
| 1763 | return -EINVAL; |
| 1764 | } |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | /* |
| 1769 | * Returns 0 on success, negative error value on error. |
| 1770 | */ |
| 1771 | int ustctl_recv_register_event(int sock, |
| 1772 | int *session_objd, |
| 1773 | int *channel_objd, |
| 1774 | char *event_name, |
| 1775 | int *loglevel, |
| 1776 | char **signature, |
| 1777 | size_t *nr_fields, |
| 1778 | struct ustctl_field **fields, |
| 1779 | char **model_emf_uri) |
| 1780 | { |
| 1781 | ssize_t len; |
| 1782 | struct ustcomm_notify_event_msg msg; |
| 1783 | size_t signature_len, fields_len, model_emf_uri_len; |
| 1784 | char *a_sign = NULL, *a_model_emf_uri = NULL; |
| 1785 | struct ustctl_field *a_fields = NULL; |
| 1786 | |
| 1787 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 1788 | if (len > 0 && len != sizeof(msg)) |
| 1789 | return -EIO; |
| 1790 | if (len == 0) |
| 1791 | return -EPIPE; |
| 1792 | if (len < 0) |
| 1793 | return len; |
| 1794 | |
| 1795 | *session_objd = msg.session_objd; |
| 1796 | *channel_objd = msg.channel_objd; |
| 1797 | strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 1798 | event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 1799 | *loglevel = msg.loglevel; |
| 1800 | signature_len = msg.signature_len; |
| 1801 | fields_len = msg.fields_len; |
| 1802 | |
| 1803 | if (fields_len % sizeof(*a_fields) != 0) { |
| 1804 | return -EINVAL; |
| 1805 | } |
| 1806 | |
| 1807 | model_emf_uri_len = msg.model_emf_uri_len; |
| 1808 | |
| 1809 | /* recv signature. contains at least \0. */ |
| 1810 | a_sign = zmalloc(signature_len); |
| 1811 | if (!a_sign) |
| 1812 | return -ENOMEM; |
| 1813 | len = ustcomm_recv_unix_sock(sock, a_sign, signature_len); |
| 1814 | if (len > 0 && len != signature_len) { |
| 1815 | len = -EIO; |
| 1816 | goto signature_error; |
| 1817 | } |
| 1818 | if (len == 0) { |
| 1819 | len = -EPIPE; |
| 1820 | goto signature_error; |
| 1821 | } |
| 1822 | if (len < 0) { |
| 1823 | goto signature_error; |
| 1824 | } |
| 1825 | /* Enforce end of string */ |
| 1826 | a_sign[signature_len - 1] = '\0'; |
| 1827 | |
| 1828 | /* recv fields */ |
| 1829 | if (fields_len) { |
| 1830 | a_fields = zmalloc(fields_len); |
| 1831 | if (!a_fields) { |
| 1832 | len = -ENOMEM; |
| 1833 | goto signature_error; |
| 1834 | } |
| 1835 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 1836 | if (len > 0 && len != fields_len) { |
| 1837 | len = -EIO; |
| 1838 | goto fields_error; |
| 1839 | } |
| 1840 | if (len == 0) { |
| 1841 | len = -EPIPE; |
| 1842 | goto fields_error; |
| 1843 | } |
| 1844 | if (len < 0) { |
| 1845 | goto fields_error; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | if (model_emf_uri_len) { |
| 1850 | /* recv model_emf_uri_len */ |
| 1851 | a_model_emf_uri = zmalloc(model_emf_uri_len); |
| 1852 | if (!a_model_emf_uri) { |
| 1853 | len = -ENOMEM; |
| 1854 | goto fields_error; |
| 1855 | } |
| 1856 | len = ustcomm_recv_unix_sock(sock, a_model_emf_uri, |
| 1857 | model_emf_uri_len); |
| 1858 | if (len > 0 && len != model_emf_uri_len) { |
| 1859 | len = -EIO; |
| 1860 | goto model_error; |
| 1861 | } |
| 1862 | if (len == 0) { |
| 1863 | len = -EPIPE; |
| 1864 | goto model_error; |
| 1865 | } |
| 1866 | if (len < 0) { |
| 1867 | goto model_error; |
| 1868 | } |
| 1869 | /* Enforce end of string */ |
| 1870 | a_model_emf_uri[model_emf_uri_len - 1] = '\0'; |
| 1871 | } |
| 1872 | |
| 1873 | *signature = a_sign; |
| 1874 | *nr_fields = fields_len / sizeof(*a_fields); |
| 1875 | *fields = a_fields; |
| 1876 | *model_emf_uri = a_model_emf_uri; |
| 1877 | |
| 1878 | return 0; |
| 1879 | |
| 1880 | model_error: |
| 1881 | free(a_model_emf_uri); |
| 1882 | fields_error: |
| 1883 | free(a_fields); |
| 1884 | signature_error: |
| 1885 | free(a_sign); |
| 1886 | return len; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Returns 0 on success, negative error value on error. |
| 1891 | */ |
| 1892 | int ustctl_reply_register_event(int sock, |
| 1893 | uint32_t id, |
| 1894 | int ret_code) |
| 1895 | { |
| 1896 | ssize_t len; |
| 1897 | struct { |
| 1898 | struct ustcomm_notify_hdr header; |
| 1899 | struct ustcomm_notify_event_reply r; |
| 1900 | } reply; |
| 1901 | |
| 1902 | memset(&reply, 0, sizeof(reply)); |
| 1903 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 1904 | reply.r.ret_code = ret_code; |
| 1905 | reply.r.event_id = id; |
| 1906 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 1907 | if (len > 0 && len != sizeof(reply)) |
| 1908 | return -EIO; |
| 1909 | if (len < 0) |
| 1910 | return len; |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * Returns 0 on success, negative UST or system error value on error. |
| 1916 | */ |
| 1917 | int ustctl_recv_register_channel(int sock, |
| 1918 | int *session_objd, /* session descriptor (output) */ |
| 1919 | int *channel_objd, /* channel descriptor (output) */ |
| 1920 | size_t *nr_fields, |
| 1921 | struct ustctl_field **fields) |
| 1922 | { |
| 1923 | ssize_t len; |
| 1924 | struct ustcomm_notify_channel_msg msg; |
| 1925 | size_t fields_len; |
| 1926 | struct ustctl_field *a_fields; |
| 1927 | |
| 1928 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 1929 | if (len > 0 && len != sizeof(msg)) |
| 1930 | return -EIO; |
| 1931 | if (len == 0) |
| 1932 | return -EPIPE; |
| 1933 | if (len < 0) |
| 1934 | return len; |
| 1935 | |
| 1936 | *session_objd = msg.session_objd; |
| 1937 | *channel_objd = msg.channel_objd; |
| 1938 | fields_len = msg.ctx_fields_len; |
| 1939 | |
| 1940 | if (fields_len % sizeof(*a_fields) != 0) { |
| 1941 | return -EINVAL; |
| 1942 | } |
| 1943 | |
| 1944 | /* recv fields */ |
| 1945 | if (fields_len) { |
| 1946 | a_fields = zmalloc(fields_len); |
| 1947 | if (!a_fields) { |
| 1948 | len = -ENOMEM; |
| 1949 | goto alloc_error; |
| 1950 | } |
| 1951 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 1952 | if (len > 0 && len != fields_len) { |
| 1953 | len = -EIO; |
| 1954 | goto fields_error; |
| 1955 | } |
| 1956 | if (len == 0) { |
| 1957 | len = -EPIPE; |
| 1958 | goto fields_error; |
| 1959 | } |
| 1960 | if (len < 0) { |
| 1961 | goto fields_error; |
| 1962 | } |
| 1963 | *fields = a_fields; |
| 1964 | } else { |
| 1965 | *fields = NULL; |
| 1966 | } |
| 1967 | *nr_fields = fields_len / sizeof(*a_fields); |
| 1968 | return 0; |
| 1969 | |
| 1970 | fields_error: |
| 1971 | free(a_fields); |
| 1972 | alloc_error: |
| 1973 | return len; |
| 1974 | } |
| 1975 | |
| 1976 | /* |
| 1977 | * Returns 0 on success, negative error value on error. |
| 1978 | */ |
| 1979 | int ustctl_reply_register_channel(int sock, |
| 1980 | uint32_t chan_id, |
| 1981 | enum ustctl_channel_header header_type, |
| 1982 | int ret_code) |
| 1983 | { |
| 1984 | ssize_t len; |
| 1985 | struct { |
| 1986 | struct ustcomm_notify_hdr header; |
| 1987 | struct ustcomm_notify_channel_reply r; |
| 1988 | } reply; |
| 1989 | |
| 1990 | memset(&reply, 0, sizeof(reply)); |
| 1991 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 1992 | reply.r.ret_code = ret_code; |
| 1993 | reply.r.chan_id = chan_id; |
| 1994 | switch (header_type) { |
| 1995 | case USTCTL_CHANNEL_HEADER_COMPACT: |
| 1996 | reply.r.header_type = 1; |
| 1997 | break; |
| 1998 | case USTCTL_CHANNEL_HEADER_LARGE: |
| 1999 | reply.r.header_type = 2; |
| 2000 | break; |
| 2001 | default: |
| 2002 | reply.r.header_type = 0; |
| 2003 | break; |
| 2004 | } |
| 2005 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 2006 | if (len > 0 && len != sizeof(reply)) |
| 2007 | return -EIO; |
| 2008 | if (len < 0) |
| 2009 | return len; |
| 2010 | return 0; |
| 2011 | } |
| 2012 | |
| 2013 | static __attribute__((constructor)) |
| 2014 | void ustctl_init(void) |
| 2015 | { |
| 2016 | init_usterr(); |
| 2017 | lttng_ust_clock_init(); |
| 2018 | lttng_ring_buffer_metadata_client_init(); |
| 2019 | lttng_ring_buffer_client_overwrite_init(); |
| 2020 | lttng_ring_buffer_client_overwrite_rt_init(); |
| 2021 | lttng_ring_buffer_client_discard_init(); |
| 2022 | lttng_ring_buffer_client_discard_rt_init(); |
| 2023 | lib_ringbuffer_signal_init(); |
| 2024 | } |
| 2025 | |
| 2026 | static __attribute__((destructor)) |
| 2027 | void ustctl_exit(void) |
| 2028 | { |
| 2029 | lttng_ring_buffer_client_discard_rt_exit(); |
| 2030 | lttng_ring_buffer_client_discard_exit(); |
| 2031 | lttng_ring_buffer_client_overwrite_rt_exit(); |
| 2032 | lttng_ring_buffer_client_overwrite_exit(); |
| 2033 | lttng_ring_buffer_metadata_client_exit(); |
| 2034 | } |