| 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 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/socket.h> |
| 25 | |
| 26 | #include <lttng/ust-config.h> |
| 27 | #include <lttng/ust-ctl.h> |
| 28 | #include <lttng/ust-abi.h> |
| 29 | #include <lttng/ust-events.h> |
| 30 | #include <lttng/ust-endian.h> |
| 31 | #include <usterr-signal-safe.h> |
| 32 | #include <ust-comm.h> |
| 33 | #include <helper.h> |
| 34 | |
| 35 | #include "../libringbuffer/backend.h" |
| 36 | #include "../libringbuffer/frontend.h" |
| 37 | #include "../liblttng-ust/wait.h" |
| 38 | #include "../liblttng-ust/lttng-rb-clients.h" |
| 39 | #include "../liblttng-ust/clock.h" |
| 40 | #include "../liblttng-ust/getenv.h" |
| 41 | #include "../liblttng-ust/lttng-tracer-core.h" |
| 42 | |
| 43 | #include "../libcounter/shm.h" |
| 44 | #include "../libcounter/smp.h" |
| 45 | #include "../libcounter/counter.h" |
| 46 | |
| 47 | /* |
| 48 | * Number of milliseconds to retry before failing metadata writes on |
| 49 | * buffer full condition. (10 seconds) |
| 50 | */ |
| 51 | #define LTTNG_METADATA_TIMEOUT_MSEC 10000 |
| 52 | |
| 53 | /* |
| 54 | * Channel representation within consumer. |
| 55 | */ |
| 56 | struct ustctl_consumer_channel { |
| 57 | struct lttng_channel *chan; /* lttng channel buffers */ |
| 58 | |
| 59 | /* initial attributes */ |
| 60 | struct ustctl_consumer_channel_attr attr; |
| 61 | int wait_fd; /* monitor close() */ |
| 62 | int wakeup_fd; /* monitor close() */ |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * Stream representation within consumer. |
| 67 | */ |
| 68 | struct ustctl_consumer_stream { |
| 69 | struct lttng_ust_shm_handle *handle; /* shared-memory handle */ |
| 70 | struct lttng_ust_lib_ring_buffer *buf; |
| 71 | struct ustctl_consumer_channel *chan; |
| 72 | int shm_fd, wait_fd, wakeup_fd; |
| 73 | int cpu; |
| 74 | uint64_t memory_map_size; |
| 75 | }; |
| 76 | |
| 77 | #define USTCTL_COUNTER_ATTR_DIMENSION_MAX 8 |
| 78 | struct ustctl_counter_attr { |
| 79 | enum ustctl_counter_arithmetic arithmetic; |
| 80 | enum ustctl_counter_bitness bitness; |
| 81 | uint32_t nr_dimensions; |
| 82 | int64_t global_sum_step; |
| 83 | struct ustctl_counter_dimension dimensions[USTCTL_COUNTER_ATTR_DIMENSION_MAX]; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Counter representation within daemon. |
| 88 | */ |
| 89 | struct ustctl_daemon_counter { |
| 90 | struct lib_counter *counter; |
| 91 | const struct lttng_counter_ops *ops; |
| 92 | struct ustctl_counter_attr *attr; /* initial attributes */ |
| 93 | }; |
| 94 | |
| 95 | extern void lttng_ring_buffer_client_overwrite_init(void); |
| 96 | extern void lttng_ring_buffer_client_overwrite_rt_init(void); |
| 97 | extern void lttng_ring_buffer_client_discard_init(void); |
| 98 | extern void lttng_ring_buffer_client_discard_rt_init(void); |
| 99 | extern void lttng_ring_buffer_metadata_client_init(void); |
| 100 | extern void lttng_ring_buffer_client_overwrite_exit(void); |
| 101 | extern void lttng_ring_buffer_client_overwrite_rt_exit(void); |
| 102 | extern void lttng_ring_buffer_client_discard_exit(void); |
| 103 | extern void lttng_ring_buffer_client_discard_rt_exit(void); |
| 104 | extern void lttng_ring_buffer_metadata_client_exit(void); |
| 105 | LTTNG_HIDDEN |
| 106 | extern void lttng_counter_client_percpu_32_modular_init(void); |
| 107 | LTTNG_HIDDEN |
| 108 | extern void lttng_counter_client_percpu_32_modular_exit(void); |
| 109 | LTTNG_HIDDEN |
| 110 | extern void lttng_counter_client_percpu_64_modular_init(void); |
| 111 | LTTNG_HIDDEN |
| 112 | extern void lttng_counter_client_percpu_64_modular_exit(void); |
| 113 | |
| 114 | int ustctl_release_handle(int sock, int handle) |
| 115 | { |
| 116 | struct ustcomm_ust_msg lum; |
| 117 | struct ustcomm_ust_reply lur; |
| 118 | |
| 119 | if (sock < 0 || handle < 0) |
| 120 | return 0; |
| 121 | memset(&lum, 0, sizeof(lum)); |
| 122 | lum.handle = handle; |
| 123 | lum.cmd = LTTNG_UST_RELEASE; |
| 124 | return ustcomm_send_app_cmd(sock, &lum, &lur); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * If sock is negative, it means we don't have to notify the other side |
| 129 | * (e.g. application has already vanished). |
| 130 | */ |
| 131 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
| 132 | { |
| 133 | int ret; |
| 134 | |
| 135 | if (!data) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | switch (data->type) { |
| 139 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: |
| 140 | if (data->u.channel.wakeup_fd >= 0) { |
| 141 | ret = close(data->u.channel.wakeup_fd); |
| 142 | if (ret < 0) { |
| 143 | ret = -errno; |
| 144 | return ret; |
| 145 | } |
| 146 | data->u.channel.wakeup_fd = -1; |
| 147 | } |
| 148 | free(data->u.channel.data); |
| 149 | data->u.channel.data = NULL; |
| 150 | break; |
| 151 | case LTTNG_UST_OBJECT_TYPE_STREAM: |
| 152 | if (data->u.stream.shm_fd >= 0) { |
| 153 | ret = close(data->u.stream.shm_fd); |
| 154 | if (ret < 0) { |
| 155 | ret = -errno; |
| 156 | return ret; |
| 157 | } |
| 158 | data->u.stream.shm_fd = -1; |
| 159 | } |
| 160 | if (data->u.stream.wakeup_fd >= 0) { |
| 161 | ret = close(data->u.stream.wakeup_fd); |
| 162 | if (ret < 0) { |
| 163 | ret = -errno; |
| 164 | return ret; |
| 165 | } |
| 166 | data->u.stream.wakeup_fd = -1; |
| 167 | } |
| 168 | break; |
| 169 | case LTTNG_UST_OBJECT_TYPE_EVENT: |
| 170 | case LTTNG_UST_OBJECT_TYPE_CONTEXT: |
| 171 | case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP: |
| 172 | case LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER: |
| 173 | break; |
| 174 | case LTTNG_UST_OBJECT_TYPE_COUNTER: |
| 175 | free(data->u.counter.data); |
| 176 | data->u.counter.data = NULL; |
| 177 | break; |
| 178 | case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL: |
| 179 | if (data->u.counter_global.shm_fd >= 0) { |
| 180 | ret = close(data->u.counter_global.shm_fd); |
| 181 | if (ret < 0) { |
| 182 | ret = -errno; |
| 183 | return ret; |
| 184 | } |
| 185 | data->u.counter_global.shm_fd = -1; |
| 186 | } |
| 187 | break; |
| 188 | case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU: |
| 189 | if (data->u.counter_cpu.shm_fd >= 0) { |
| 190 | ret = close(data->u.counter_cpu.shm_fd); |
| 191 | if (ret < 0) { |
| 192 | ret = -errno; |
| 193 | return ret; |
| 194 | } |
| 195 | data->u.counter_cpu.shm_fd = -1; |
| 196 | } |
| 197 | break; |
| 198 | default: |
| 199 | assert(0); |
| 200 | } |
| 201 | return ustctl_release_handle(sock, data->handle); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Send registration done packet to the application. |
| 206 | */ |
| 207 | int ustctl_register_done(int sock) |
| 208 | { |
| 209 | struct ustcomm_ust_msg lum; |
| 210 | struct ustcomm_ust_reply lur; |
| 211 | int ret; |
| 212 | |
| 213 | DBG("Sending register done command to %d", sock); |
| 214 | memset(&lum, 0, sizeof(lum)); |
| 215 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 216 | lum.cmd = LTTNG_UST_REGISTER_DONE; |
| 217 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * returns session handle. |
| 225 | */ |
| 226 | int ustctl_create_session(int sock) |
| 227 | { |
| 228 | struct ustcomm_ust_msg lum; |
| 229 | struct ustcomm_ust_reply lur; |
| 230 | int ret, session_handle; |
| 231 | |
| 232 | /* Create session */ |
| 233 | memset(&lum, 0, sizeof(lum)); |
| 234 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 235 | lum.cmd = LTTNG_UST_SESSION; |
| 236 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | session_handle = lur.ret_val; |
| 240 | DBG("received session handle %u", session_handle); |
| 241 | return session_handle; |
| 242 | } |
| 243 | |
| 244 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, |
| 245 | struct lttng_ust_object_data *channel_data, |
| 246 | struct lttng_ust_object_data **_event_data) |
| 247 | { |
| 248 | struct ustcomm_ust_msg lum; |
| 249 | struct ustcomm_ust_reply lur; |
| 250 | struct lttng_ust_object_data *event_data; |
| 251 | int ret; |
| 252 | |
| 253 | if (!channel_data || !_event_data) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | event_data = zmalloc(sizeof(*event_data)); |
| 257 | if (!event_data) |
| 258 | return -ENOMEM; |
| 259 | event_data->type = LTTNG_UST_OBJECT_TYPE_EVENT; |
| 260 | memset(&lum, 0, sizeof(lum)); |
| 261 | lum.handle = channel_data->handle; |
| 262 | lum.cmd = LTTNG_UST_EVENT; |
| 263 | strncpy(lum.u.event.name, ev->name, |
| 264 | LTTNG_UST_SYM_NAME_LEN); |
| 265 | lum.u.event.instrumentation = ev->instrumentation; |
| 266 | lum.u.event.loglevel_type = ev->loglevel_type; |
| 267 | lum.u.event.loglevel = ev->loglevel; |
| 268 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 269 | if (ret) { |
| 270 | free(event_data); |
| 271 | return ret; |
| 272 | } |
| 273 | event_data->handle = lur.ret_val; |
| 274 | DBG("received event handle %u", event_data->handle); |
| 275 | *_event_data = event_data; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int ustctl_add_context(int sock, struct lttng_ust_context_attr *ctx, |
| 280 | struct lttng_ust_object_data *obj_data, |
| 281 | struct lttng_ust_object_data **_context_data) |
| 282 | { |
| 283 | struct ustcomm_ust_msg lum; |
| 284 | struct ustcomm_ust_reply lur; |
| 285 | struct lttng_ust_object_data *context_data = NULL; |
| 286 | char *buf = NULL; |
| 287 | size_t len; |
| 288 | int ret; |
| 289 | |
| 290 | if (!obj_data || !_context_data) { |
| 291 | ret = -EINVAL; |
| 292 | goto end; |
| 293 | } |
| 294 | |
| 295 | context_data = zmalloc(sizeof(*context_data)); |
| 296 | if (!context_data) { |
| 297 | ret = -ENOMEM; |
| 298 | goto end; |
| 299 | } |
| 300 | context_data->type = LTTNG_UST_OBJECT_TYPE_CONTEXT; |
| 301 | memset(&lum, 0, sizeof(lum)); |
| 302 | lum.handle = obj_data->handle; |
| 303 | lum.cmd = LTTNG_UST_CONTEXT; |
| 304 | |
| 305 | lum.u.context.ctx = ctx->ctx; |
| 306 | switch (ctx->ctx) { |
| 307 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: |
| 308 | lum.u.context.u.perf_counter = ctx->u.perf_counter; |
| 309 | break; |
| 310 | case LTTNG_UST_CONTEXT_APP_CONTEXT: |
| 311 | { |
| 312 | size_t provider_name_len = strlen( |
| 313 | ctx->u.app_ctx.provider_name) + 1; |
| 314 | size_t ctx_name_len = strlen(ctx->u.app_ctx.ctx_name) + 1; |
| 315 | |
| 316 | lum.u.context.u.app_ctx.provider_name_len = provider_name_len; |
| 317 | lum.u.context.u.app_ctx.ctx_name_len = ctx_name_len; |
| 318 | |
| 319 | len = provider_name_len + ctx_name_len; |
| 320 | buf = zmalloc(len); |
| 321 | if (!buf) { |
| 322 | ret = -ENOMEM; |
| 323 | goto end; |
| 324 | } |
| 325 | memcpy(buf, ctx->u.app_ctx.provider_name, |
| 326 | provider_name_len); |
| 327 | memcpy(buf + provider_name_len, ctx->u.app_ctx.ctx_name, |
| 328 | ctx_name_len); |
| 329 | break; |
| 330 | } |
| 331 | default: |
| 332 | break; |
| 333 | } |
| 334 | ret = ustcomm_send_app_msg(sock, &lum); |
| 335 | if (ret) |
| 336 | goto end; |
| 337 | if (buf) { |
| 338 | /* send var len ctx_name */ |
| 339 | ret = ustcomm_send_unix_sock(sock, buf, len); |
| 340 | if (ret < 0) { |
| 341 | goto end; |
| 342 | } |
| 343 | if (ret != len) { |
| 344 | ret = -EINVAL; |
| 345 | goto end; |
| 346 | } |
| 347 | } |
| 348 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 349 | if (ret < 0) { |
| 350 | goto end; |
| 351 | } |
| 352 | context_data->handle = -1; |
| 353 | DBG("Context created successfully"); |
| 354 | *_context_data = context_data; |
| 355 | context_data = NULL; |
| 356 | end: |
| 357 | free(context_data); |
| 358 | free(buf); |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | int ustctl_set_filter(int sock, struct lttng_ust_filter_bytecode *bytecode, |
| 363 | struct lttng_ust_object_data *obj_data) |
| 364 | { |
| 365 | struct ustcomm_ust_msg lum; |
| 366 | struct ustcomm_ust_reply lur; |
| 367 | int ret; |
| 368 | |
| 369 | if (!obj_data) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | memset(&lum, 0, sizeof(lum)); |
| 373 | lum.handle = obj_data->handle; |
| 374 | lum.cmd = LTTNG_UST_FILTER; |
| 375 | lum.u.filter.data_size = bytecode->len; |
| 376 | lum.u.filter.reloc_offset = bytecode->reloc_offset; |
| 377 | lum.u.filter.seqnum = bytecode->seqnum; |
| 378 | |
| 379 | ret = ustcomm_send_app_msg(sock, &lum); |
| 380 | if (ret) |
| 381 | return ret; |
| 382 | /* send var len bytecode */ |
| 383 | ret = ustcomm_send_unix_sock(sock, bytecode->data, |
| 384 | bytecode->len); |
| 385 | if (ret < 0) { |
| 386 | return ret; |
| 387 | } |
| 388 | if (ret != bytecode->len) |
| 389 | return -EINVAL; |
| 390 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 391 | } |
| 392 | |
| 393 | int ustctl_set_capture(int sock, struct lttng_ust_capture_bytecode *bytecode, |
| 394 | struct lttng_ust_object_data *obj_data) |
| 395 | { |
| 396 | struct ustcomm_ust_msg lum; |
| 397 | struct ustcomm_ust_reply lur; |
| 398 | int ret; |
| 399 | |
| 400 | if (!obj_data) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | memset(&lum, 0, sizeof(lum)); |
| 404 | lum.handle = obj_data->handle; |
| 405 | lum.cmd = LTTNG_UST_CAPTURE; |
| 406 | lum.u.capture.data_size = bytecode->len; |
| 407 | lum.u.capture.reloc_offset = bytecode->reloc_offset; |
| 408 | lum.u.capture.seqnum = bytecode->seqnum; |
| 409 | |
| 410 | ret = ustcomm_send_app_msg(sock, &lum); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | /* send var len bytecode */ |
| 414 | ret = ustcomm_send_unix_sock(sock, bytecode->data, |
| 415 | bytecode->len); |
| 416 | if (ret < 0) { |
| 417 | return ret; |
| 418 | } |
| 419 | if (ret != bytecode->len) |
| 420 | return -EINVAL; |
| 421 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 422 | } |
| 423 | |
| 424 | int ustctl_set_exclusion(int sock, struct lttng_ust_event_exclusion *exclusion, |
| 425 | struct lttng_ust_object_data *obj_data) |
| 426 | { |
| 427 | struct ustcomm_ust_msg lum; |
| 428 | struct ustcomm_ust_reply lur; |
| 429 | int ret; |
| 430 | |
| 431 | if (!obj_data) { |
| 432 | return -EINVAL; |
| 433 | } |
| 434 | |
| 435 | memset(&lum, 0, sizeof(lum)); |
| 436 | lum.handle = obj_data->handle; |
| 437 | lum.cmd = LTTNG_UST_EXCLUSION; |
| 438 | lum.u.exclusion.count = exclusion->count; |
| 439 | |
| 440 | ret = ustcomm_send_app_msg(sock, &lum); |
| 441 | if (ret) { |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | /* send var len exclusion names */ |
| 446 | ret = ustcomm_send_unix_sock(sock, |
| 447 | exclusion->names, |
| 448 | exclusion->count * LTTNG_UST_SYM_NAME_LEN); |
| 449 | if (ret < 0) { |
| 450 | return ret; |
| 451 | } |
| 452 | if (ret != exclusion->count * LTTNG_UST_SYM_NAME_LEN) { |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 456 | } |
| 457 | |
| 458 | /* Enable event, channel and session ioctl */ |
| 459 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
| 460 | { |
| 461 | struct ustcomm_ust_msg lum; |
| 462 | struct ustcomm_ust_reply lur; |
| 463 | int ret; |
| 464 | |
| 465 | if (!object) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | memset(&lum, 0, sizeof(lum)); |
| 469 | lum.handle = object->handle; |
| 470 | lum.cmd = LTTNG_UST_ENABLE; |
| 471 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | DBG("enabled handle %u", object->handle); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* Disable event, channel and session ioctl */ |
| 479 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
| 480 | { |
| 481 | struct ustcomm_ust_msg lum; |
| 482 | struct ustcomm_ust_reply lur; |
| 483 | int ret; |
| 484 | |
| 485 | if (!object) |
| 486 | return -EINVAL; |
| 487 | |
| 488 | memset(&lum, 0, sizeof(lum)); |
| 489 | lum.handle = object->handle; |
| 490 | lum.cmd = LTTNG_UST_DISABLE; |
| 491 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 492 | if (ret) |
| 493 | return ret; |
| 494 | DBG("disable handle %u", object->handle); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | int ustctl_start_session(int sock, int handle) |
| 499 | { |
| 500 | struct lttng_ust_object_data obj; |
| 501 | |
| 502 | obj.handle = handle; |
| 503 | return ustctl_enable(sock, &obj); |
| 504 | } |
| 505 | |
| 506 | int ustctl_stop_session(int sock, int handle) |
| 507 | { |
| 508 | struct lttng_ust_object_data obj; |
| 509 | |
| 510 | obj.handle = handle; |
| 511 | return ustctl_disable(sock, &obj); |
| 512 | } |
| 513 | |
| 514 | int ustctl_create_event_notifier_group(int sock, int pipe_fd, |
| 515 | struct lttng_ust_object_data **_event_notifier_group_data) |
| 516 | { |
| 517 | struct lttng_ust_object_data *event_notifier_group_data; |
| 518 | struct ustcomm_ust_msg lum; |
| 519 | struct ustcomm_ust_reply lur; |
| 520 | ssize_t len; |
| 521 | int ret; |
| 522 | |
| 523 | if (!_event_notifier_group_data) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | event_notifier_group_data = zmalloc(sizeof(*event_notifier_group_data)); |
| 527 | if (!event_notifier_group_data) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | event_notifier_group_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER_GROUP; |
| 531 | |
| 532 | memset(&lum, 0, sizeof(lum)); |
| 533 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 534 | lum.cmd = LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE; |
| 535 | |
| 536 | ret = ustcomm_send_app_msg(sock, &lum); |
| 537 | if (ret) |
| 538 | goto error; |
| 539 | |
| 540 | /* Send event_notifier notification pipe. */ |
| 541 | len = ustcomm_send_fds_unix_sock(sock, &pipe_fd, 1); |
| 542 | if (len <= 0) { |
| 543 | ret = len; |
| 544 | goto error; |
| 545 | } |
| 546 | |
| 547 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 548 | if (ret) |
| 549 | goto error; |
| 550 | |
| 551 | event_notifier_group_data->handle = lur.ret_val; |
| 552 | DBG("received event_notifier group handle %d", event_notifier_group_data->handle); |
| 553 | |
| 554 | *_event_notifier_group_data = event_notifier_group_data; |
| 555 | |
| 556 | ret = 0; |
| 557 | goto end; |
| 558 | error: |
| 559 | free(event_notifier_group_data); |
| 560 | |
| 561 | end: |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | int ustctl_create_event_notifier(int sock, struct lttng_ust_event_notifier *event_notifier, |
| 566 | struct lttng_ust_object_data *event_notifier_group, |
| 567 | struct lttng_ust_object_data **_event_notifier_data) |
| 568 | { |
| 569 | struct ustcomm_ust_msg lum; |
| 570 | struct ustcomm_ust_reply lur; |
| 571 | struct lttng_ust_object_data *event_notifier_data; |
| 572 | ssize_t len; |
| 573 | int ret; |
| 574 | |
| 575 | if (!event_notifier_group || !_event_notifier_data) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | event_notifier_data = zmalloc(sizeof(*event_notifier_data)); |
| 579 | if (!event_notifier_data) |
| 580 | return -ENOMEM; |
| 581 | |
| 582 | event_notifier_data->type = LTTNG_UST_OBJECT_TYPE_EVENT_NOTIFIER; |
| 583 | |
| 584 | memset(&lum, 0, sizeof(lum)); |
| 585 | lum.handle = event_notifier_group->handle; |
| 586 | lum.cmd = LTTNG_UST_EVENT_NOTIFIER_CREATE; |
| 587 | lum.u.event_notifier.len = sizeof(*event_notifier); |
| 588 | |
| 589 | ret = ustcomm_send_app_msg(sock, &lum); |
| 590 | if (ret) { |
| 591 | free(event_notifier_data); |
| 592 | return ret; |
| 593 | } |
| 594 | /* Send struct lttng_ust_event_notifier */ |
| 595 | len = ustcomm_send_unix_sock(sock, event_notifier, sizeof(*event_notifier)); |
| 596 | if (len != sizeof(*event_notifier)) { |
| 597 | if (len < 0) |
| 598 | return len; |
| 599 | else |
| 600 | return -EIO; |
| 601 | } |
| 602 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 603 | if (ret) { |
| 604 | free(event_notifier_data); |
| 605 | return ret; |
| 606 | } |
| 607 | event_notifier_data->handle = lur.ret_val; |
| 608 | DBG("received event_notifier handle %u", event_notifier_data->handle); |
| 609 | *_event_notifier_data = event_notifier_data; |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | int ustctl_tracepoint_list(int sock) |
| 615 | { |
| 616 | struct ustcomm_ust_msg lum; |
| 617 | struct ustcomm_ust_reply lur; |
| 618 | int ret, tp_list_handle; |
| 619 | |
| 620 | memset(&lum, 0, sizeof(lum)); |
| 621 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 622 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; |
| 623 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | tp_list_handle = lur.ret_val; |
| 627 | DBG("received tracepoint list handle %u", tp_list_handle); |
| 628 | return tp_list_handle; |
| 629 | } |
| 630 | |
| 631 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, |
| 632 | struct lttng_ust_tracepoint_iter *iter) |
| 633 | { |
| 634 | struct ustcomm_ust_msg lum; |
| 635 | struct ustcomm_ust_reply lur; |
| 636 | int ret; |
| 637 | |
| 638 | if (!iter) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | memset(&lum, 0, sizeof(lum)); |
| 642 | lum.handle = tp_list_handle; |
| 643 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; |
| 644 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 645 | if (ret) |
| 646 | return ret; |
| 647 | DBG("received tracepoint list entry name %s loglevel %d", |
| 648 | lur.u.tracepoint.name, |
| 649 | lur.u.tracepoint.loglevel); |
| 650 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | int ustctl_tracepoint_field_list(int sock) |
| 655 | { |
| 656 | struct ustcomm_ust_msg lum; |
| 657 | struct ustcomm_ust_reply lur; |
| 658 | int ret, tp_field_list_handle; |
| 659 | |
| 660 | memset(&lum, 0, sizeof(lum)); |
| 661 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 662 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST; |
| 663 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | tp_field_list_handle = lur.ret_val; |
| 667 | DBG("received tracepoint field list handle %u", tp_field_list_handle); |
| 668 | return tp_field_list_handle; |
| 669 | } |
| 670 | |
| 671 | int ustctl_tracepoint_field_list_get(int sock, int tp_field_list_handle, |
| 672 | struct lttng_ust_field_iter *iter) |
| 673 | { |
| 674 | struct ustcomm_ust_msg lum; |
| 675 | struct ustcomm_ust_reply lur; |
| 676 | int ret; |
| 677 | ssize_t len; |
| 678 | |
| 679 | if (!iter) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | memset(&lum, 0, sizeof(lum)); |
| 683 | lum.handle = tp_field_list_handle; |
| 684 | lum.cmd = LTTNG_UST_TRACEPOINT_FIELD_LIST_GET; |
| 685 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | len = ustcomm_recv_unix_sock(sock, iter, sizeof(*iter)); |
| 689 | if (len != sizeof(*iter)) { |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | DBG("received tracepoint field list entry event_name %s event_loglevel %d field_name %s field_type %d", |
| 693 | iter->event_name, |
| 694 | iter->loglevel, |
| 695 | iter->field_name, |
| 696 | iter->type); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) |
| 701 | { |
| 702 | struct ustcomm_ust_msg lum; |
| 703 | struct ustcomm_ust_reply lur; |
| 704 | int ret; |
| 705 | |
| 706 | if (!v) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | memset(&lum, 0, sizeof(lum)); |
| 710 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 711 | lum.cmd = LTTNG_UST_TRACER_VERSION; |
| 712 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 713 | if (ret) |
| 714 | return ret; |
| 715 | memcpy(v, &lur.u.version, sizeof(*v)); |
| 716 | DBG("received tracer version"); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | int ustctl_wait_quiescent(int sock) |
| 721 | { |
| 722 | struct ustcomm_ust_msg lum; |
| 723 | struct ustcomm_ust_reply lur; |
| 724 | int ret; |
| 725 | |
| 726 | memset(&lum, 0, sizeof(lum)); |
| 727 | lum.handle = LTTNG_UST_ROOT_HANDLE; |
| 728 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; |
| 729 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 730 | if (ret) |
| 731 | return ret; |
| 732 | DBG("waited for quiescent state"); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) |
| 737 | { |
| 738 | if (!calibrate) |
| 739 | return -EINVAL; |
| 740 | |
| 741 | return -ENOSYS; |
| 742 | } |
| 743 | |
| 744 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
| 745 | { |
| 746 | struct ustcomm_ust_msg lum; |
| 747 | struct ustcomm_ust_reply lur; |
| 748 | int ret; |
| 749 | |
| 750 | if (!object) |
| 751 | return -EINVAL; |
| 752 | |
| 753 | memset(&lum, 0, sizeof(lum)); |
| 754 | lum.handle = object->handle; |
| 755 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; |
| 756 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 757 | if (ret) |
| 758 | return ret; |
| 759 | DBG("flushed buffer handle %u", object->handle); |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static |
| 764 | int ustctl_send_channel(int sock, |
| 765 | enum lttng_ust_chan_type type, |
| 766 | void *data, |
| 767 | uint64_t size, |
| 768 | int wakeup_fd, |
| 769 | int send_fd_only) |
| 770 | { |
| 771 | ssize_t len; |
| 772 | |
| 773 | if (!send_fd_only) { |
| 774 | /* Send mmap size */ |
| 775 | len = ustcomm_send_unix_sock(sock, &size, sizeof(size)); |
| 776 | if (len != sizeof(size)) { |
| 777 | if (len < 0) |
| 778 | return len; |
| 779 | else |
| 780 | return -EIO; |
| 781 | } |
| 782 | |
| 783 | /* Send channel type */ |
| 784 | len = ustcomm_send_unix_sock(sock, &type, sizeof(type)); |
| 785 | if (len != sizeof(type)) { |
| 786 | if (len < 0) |
| 787 | return len; |
| 788 | else |
| 789 | return -EIO; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* Send channel data */ |
| 794 | len = ustcomm_send_unix_sock(sock, data, size); |
| 795 | if (len != size) { |
| 796 | if (len < 0) |
| 797 | return len; |
| 798 | else |
| 799 | return -EIO; |
| 800 | } |
| 801 | |
| 802 | /* Send wakeup fd */ |
| 803 | len = ustcomm_send_fds_unix_sock(sock, &wakeup_fd, 1); |
| 804 | if (len <= 0) { |
| 805 | if (len < 0) |
| 806 | return len; |
| 807 | else |
| 808 | return -EIO; |
| 809 | } |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | static |
| 814 | int ustctl_send_stream(int sock, |
| 815 | uint32_t stream_nr, |
| 816 | uint64_t memory_map_size, |
| 817 | int shm_fd, int wakeup_fd, |
| 818 | int send_fd_only) |
| 819 | { |
| 820 | ssize_t len; |
| 821 | int fds[2]; |
| 822 | |
| 823 | if (!send_fd_only) { |
| 824 | if (shm_fd < 0) { |
| 825 | /* finish iteration */ |
| 826 | uint64_t v = -1; |
| 827 | |
| 828 | len = ustcomm_send_unix_sock(sock, &v, sizeof(v)); |
| 829 | if (len != sizeof(v)) { |
| 830 | if (len < 0) |
| 831 | return len; |
| 832 | else |
| 833 | return -EIO; |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | /* Send mmap size */ |
| 839 | len = ustcomm_send_unix_sock(sock, &memory_map_size, |
| 840 | sizeof(memory_map_size)); |
| 841 | if (len != sizeof(memory_map_size)) { |
| 842 | if (len < 0) |
| 843 | return len; |
| 844 | else |
| 845 | return -EIO; |
| 846 | } |
| 847 | |
| 848 | /* Send stream nr */ |
| 849 | len = ustcomm_send_unix_sock(sock, &stream_nr, |
| 850 | sizeof(stream_nr)); |
| 851 | if (len != sizeof(stream_nr)) { |
| 852 | if (len < 0) |
| 853 | return len; |
| 854 | else |
| 855 | return -EIO; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* Send shm fd and wakeup fd */ |
| 860 | fds[0] = shm_fd; |
| 861 | fds[1] = wakeup_fd; |
| 862 | len = ustcomm_send_fds_unix_sock(sock, fds, 2); |
| 863 | if (len <= 0) { |
| 864 | if (len < 0) |
| 865 | return len; |
| 866 | else |
| 867 | return -EIO; |
| 868 | } |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | int ustctl_recv_channel_from_consumer(int sock, |
| 873 | struct lttng_ust_object_data **_channel_data) |
| 874 | { |
| 875 | struct lttng_ust_object_data *channel_data; |
| 876 | ssize_t len; |
| 877 | int wakeup_fd; |
| 878 | int ret; |
| 879 | |
| 880 | channel_data = zmalloc(sizeof(*channel_data)); |
| 881 | if (!channel_data) { |
| 882 | ret = -ENOMEM; |
| 883 | goto error_alloc; |
| 884 | } |
| 885 | channel_data->type = LTTNG_UST_OBJECT_TYPE_CHANNEL; |
| 886 | channel_data->handle = -1; |
| 887 | |
| 888 | /* recv mmap size */ |
| 889 | len = ustcomm_recv_unix_sock(sock, &channel_data->size, |
| 890 | sizeof(channel_data->size)); |
| 891 | if (len != sizeof(channel_data->size)) { |
| 892 | if (len < 0) |
| 893 | ret = len; |
| 894 | else |
| 895 | ret = -EINVAL; |
| 896 | goto error; |
| 897 | } |
| 898 | |
| 899 | /* recv channel type */ |
| 900 | len = ustcomm_recv_unix_sock(sock, &channel_data->u.channel.type, |
| 901 | sizeof(channel_data->u.channel.type)); |
| 902 | if (len != sizeof(channel_data->u.channel.type)) { |
| 903 | if (len < 0) |
| 904 | ret = len; |
| 905 | else |
| 906 | ret = -EINVAL; |
| 907 | goto error; |
| 908 | } |
| 909 | |
| 910 | /* recv channel data */ |
| 911 | channel_data->u.channel.data = zmalloc(channel_data->size); |
| 912 | if (!channel_data->u.channel.data) { |
| 913 | ret = -ENOMEM; |
| 914 | goto error; |
| 915 | } |
| 916 | len = ustcomm_recv_unix_sock(sock, channel_data->u.channel.data, |
| 917 | channel_data->size); |
| 918 | if (len != channel_data->size) { |
| 919 | if (len < 0) |
| 920 | ret = len; |
| 921 | else |
| 922 | ret = -EINVAL; |
| 923 | goto error_recv_data; |
| 924 | } |
| 925 | /* recv wakeup fd */ |
| 926 | len = ustcomm_recv_fds_unix_sock(sock, &wakeup_fd, 1); |
| 927 | if (len <= 0) { |
| 928 | if (len < 0) { |
| 929 | ret = len; |
| 930 | goto error_recv_data; |
| 931 | } else { |
| 932 | ret = -EIO; |
| 933 | goto error_recv_data; |
| 934 | } |
| 935 | } |
| 936 | channel_data->u.channel.wakeup_fd = wakeup_fd; |
| 937 | *_channel_data = channel_data; |
| 938 | return 0; |
| 939 | |
| 940 | error_recv_data: |
| 941 | free(channel_data->u.channel.data); |
| 942 | error: |
| 943 | free(channel_data); |
| 944 | error_alloc: |
| 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | int ustctl_recv_stream_from_consumer(int sock, |
| 949 | struct lttng_ust_object_data **_stream_data) |
| 950 | { |
| 951 | struct lttng_ust_object_data *stream_data; |
| 952 | ssize_t len; |
| 953 | int ret; |
| 954 | int fds[2]; |
| 955 | |
| 956 | stream_data = zmalloc(sizeof(*stream_data)); |
| 957 | if (!stream_data) { |
| 958 | ret = -ENOMEM; |
| 959 | goto error_alloc; |
| 960 | } |
| 961 | |
| 962 | stream_data->type = LTTNG_UST_OBJECT_TYPE_STREAM; |
| 963 | stream_data->handle = -1; |
| 964 | |
| 965 | /* recv mmap size */ |
| 966 | len = ustcomm_recv_unix_sock(sock, &stream_data->size, |
| 967 | sizeof(stream_data->size)); |
| 968 | if (len != sizeof(stream_data->size)) { |
| 969 | if (len < 0) |
| 970 | ret = len; |
| 971 | else |
| 972 | ret = -EINVAL; |
| 973 | goto error; |
| 974 | } |
| 975 | if (stream_data->size == -1) { |
| 976 | ret = -LTTNG_UST_ERR_NOENT; |
| 977 | goto error; |
| 978 | } |
| 979 | |
| 980 | /* recv stream nr */ |
| 981 | len = ustcomm_recv_unix_sock(sock, &stream_data->u.stream.stream_nr, |
| 982 | sizeof(stream_data->u.stream.stream_nr)); |
| 983 | if (len != sizeof(stream_data->u.stream.stream_nr)) { |
| 984 | if (len < 0) |
| 985 | ret = len; |
| 986 | else |
| 987 | ret = -EINVAL; |
| 988 | goto error; |
| 989 | } |
| 990 | |
| 991 | /* recv shm fd and wakeup fd */ |
| 992 | len = ustcomm_recv_fds_unix_sock(sock, fds, 2); |
| 993 | if (len <= 0) { |
| 994 | if (len < 0) { |
| 995 | ret = len; |
| 996 | goto error; |
| 997 | } else { |
| 998 | ret = -EIO; |
| 999 | goto error; |
| 1000 | } |
| 1001 | } |
| 1002 | stream_data->u.stream.shm_fd = fds[0]; |
| 1003 | stream_data->u.stream.wakeup_fd = fds[1]; |
| 1004 | *_stream_data = stream_data; |
| 1005 | return 0; |
| 1006 | |
| 1007 | error: |
| 1008 | free(stream_data); |
| 1009 | error_alloc: |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | int ustctl_send_channel_to_ust(int sock, int session_handle, |
| 1014 | struct lttng_ust_object_data *channel_data) |
| 1015 | { |
| 1016 | struct ustcomm_ust_msg lum; |
| 1017 | struct ustcomm_ust_reply lur; |
| 1018 | int ret; |
| 1019 | |
| 1020 | if (!channel_data) |
| 1021 | return -EINVAL; |
| 1022 | |
| 1023 | memset(&lum, 0, sizeof(lum)); |
| 1024 | lum.handle = session_handle; |
| 1025 | lum.cmd = LTTNG_UST_CHANNEL; |
| 1026 | lum.u.channel.len = channel_data->size; |
| 1027 | lum.u.channel.type = channel_data->u.channel.type; |
| 1028 | ret = ustcomm_send_app_msg(sock, &lum); |
| 1029 | if (ret) |
| 1030 | return ret; |
| 1031 | |
| 1032 | ret = ustctl_send_channel(sock, |
| 1033 | channel_data->u.channel.type, |
| 1034 | channel_data->u.channel.data, |
| 1035 | channel_data->size, |
| 1036 | channel_data->u.channel.wakeup_fd, |
| 1037 | 1); |
| 1038 | if (ret) |
| 1039 | return ret; |
| 1040 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 1041 | if (!ret) { |
| 1042 | channel_data->handle = lur.ret_val; |
| 1043 | } |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | int ustctl_send_stream_to_ust(int sock, |
| 1048 | struct lttng_ust_object_data *channel_data, |
| 1049 | struct lttng_ust_object_data *stream_data) |
| 1050 | { |
| 1051 | struct ustcomm_ust_msg lum; |
| 1052 | struct ustcomm_ust_reply lur; |
| 1053 | int ret; |
| 1054 | |
| 1055 | memset(&lum, 0, sizeof(lum)); |
| 1056 | lum.handle = channel_data->handle; |
| 1057 | lum.cmd = LTTNG_UST_STREAM; |
| 1058 | lum.u.stream.len = stream_data->size; |
| 1059 | lum.u.stream.stream_nr = stream_data->u.stream.stream_nr; |
| 1060 | ret = ustcomm_send_app_msg(sock, &lum); |
| 1061 | if (ret) |
| 1062 | return ret; |
| 1063 | |
| 1064 | assert(stream_data); |
| 1065 | assert(stream_data->type == LTTNG_UST_OBJECT_TYPE_STREAM); |
| 1066 | |
| 1067 | ret = ustctl_send_stream(sock, |
| 1068 | stream_data->u.stream.stream_nr, |
| 1069 | stream_data->size, |
| 1070 | stream_data->u.stream.shm_fd, |
| 1071 | stream_data->u.stream.wakeup_fd, 1); |
| 1072 | if (ret) |
| 1073 | return ret; |
| 1074 | return ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 1075 | } |
| 1076 | |
| 1077 | int ustctl_duplicate_ust_object_data(struct lttng_ust_object_data **dest, |
| 1078 | struct lttng_ust_object_data *src) |
| 1079 | { |
| 1080 | struct lttng_ust_object_data *obj; |
| 1081 | int ret; |
| 1082 | |
| 1083 | if (src->handle != -1) { |
| 1084 | ret = -EINVAL; |
| 1085 | goto error; |
| 1086 | } |
| 1087 | |
| 1088 | obj = zmalloc(sizeof(*obj)); |
| 1089 | if (!obj) { |
| 1090 | ret = -ENOMEM; |
| 1091 | goto error; |
| 1092 | } |
| 1093 | |
| 1094 | obj->type = src->type; |
| 1095 | obj->handle = src->handle; |
| 1096 | obj->size = src->size; |
| 1097 | |
| 1098 | switch (obj->type) { |
| 1099 | case LTTNG_UST_OBJECT_TYPE_CHANNEL: |
| 1100 | { |
| 1101 | obj->u.channel.type = src->u.channel.type; |
| 1102 | if (src->u.channel.wakeup_fd >= 0) { |
| 1103 | obj->u.channel.wakeup_fd = |
| 1104 | dup(src->u.channel.wakeup_fd); |
| 1105 | if (obj->u.channel.wakeup_fd < 0) { |
| 1106 | ret = errno; |
| 1107 | goto chan_error_wakeup_fd; |
| 1108 | } |
| 1109 | } else { |
| 1110 | obj->u.channel.wakeup_fd = |
| 1111 | src->u.channel.wakeup_fd; |
| 1112 | } |
| 1113 | obj->u.channel.data = zmalloc(obj->size); |
| 1114 | if (!obj->u.channel.data) { |
| 1115 | ret = -ENOMEM; |
| 1116 | goto chan_error_alloc; |
| 1117 | } |
| 1118 | memcpy(obj->u.channel.data, src->u.channel.data, obj->size); |
| 1119 | break; |
| 1120 | |
| 1121 | chan_error_alloc: |
| 1122 | if (src->u.channel.wakeup_fd >= 0) { |
| 1123 | int closeret; |
| 1124 | |
| 1125 | closeret = close(obj->u.channel.wakeup_fd); |
| 1126 | if (closeret) { |
| 1127 | PERROR("close"); |
| 1128 | } |
| 1129 | } |
| 1130 | chan_error_wakeup_fd: |
| 1131 | goto error_type; |
| 1132 | |
| 1133 | } |
| 1134 | |
| 1135 | case LTTNG_UST_OBJECT_TYPE_STREAM: |
| 1136 | { |
| 1137 | obj->u.stream.stream_nr = src->u.stream.stream_nr; |
| 1138 | if (src->u.stream.wakeup_fd >= 0) { |
| 1139 | obj->u.stream.wakeup_fd = |
| 1140 | dup(src->u.stream.wakeup_fd); |
| 1141 | if (obj->u.stream.wakeup_fd < 0) { |
| 1142 | ret = errno; |
| 1143 | goto stream_error_wakeup_fd; |
| 1144 | } |
| 1145 | } else { |
| 1146 | obj->u.stream.wakeup_fd = |
| 1147 | src->u.stream.wakeup_fd; |
| 1148 | } |
| 1149 | |
| 1150 | if (src->u.stream.shm_fd >= 0) { |
| 1151 | obj->u.stream.shm_fd = |
| 1152 | dup(src->u.stream.shm_fd); |
| 1153 | if (obj->u.stream.shm_fd < 0) { |
| 1154 | ret = errno; |
| 1155 | goto stream_error_shm_fd; |
| 1156 | } |
| 1157 | } else { |
| 1158 | obj->u.stream.shm_fd = |
| 1159 | src->u.stream.shm_fd; |
| 1160 | } |
| 1161 | break; |
| 1162 | |
| 1163 | stream_error_shm_fd: |
| 1164 | if (src->u.stream.wakeup_fd >= 0) { |
| 1165 | int closeret; |
| 1166 | |
| 1167 | closeret = close(obj->u.stream.wakeup_fd); |
| 1168 | if (closeret) { |
| 1169 | PERROR("close"); |
| 1170 | } |
| 1171 | } |
| 1172 | stream_error_wakeup_fd: |
| 1173 | goto error_type; |
| 1174 | } |
| 1175 | |
| 1176 | case LTTNG_UST_OBJECT_TYPE_COUNTER: |
| 1177 | { |
| 1178 | obj->u.counter.data = zmalloc(obj->size); |
| 1179 | if (!obj->u.counter.data) { |
| 1180 | ret = -ENOMEM; |
| 1181 | goto error_type; |
| 1182 | } |
| 1183 | memcpy(obj->u.counter.data, src->u.counter.data, obj->size); |
| 1184 | break; |
| 1185 | } |
| 1186 | |
| 1187 | case LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL: |
| 1188 | { |
| 1189 | if (src->u.counter_global.shm_fd >= 0) { |
| 1190 | obj->u.counter_global.shm_fd = |
| 1191 | dup(src->u.counter_global.shm_fd); |
| 1192 | if (obj->u.counter_global.shm_fd < 0) { |
| 1193 | ret = errno; |
| 1194 | goto error_type; |
| 1195 | } |
| 1196 | } |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | case LTTNG_UST_OBJECT_TYPE_COUNTER_CPU: |
| 1201 | { |
| 1202 | obj->u.counter_cpu.cpu_nr = src->u.counter_cpu.cpu_nr; |
| 1203 | if (src->u.counter_cpu.shm_fd >= 0) { |
| 1204 | obj->u.counter_cpu.shm_fd = |
| 1205 | dup(src->u.counter_cpu.shm_fd); |
| 1206 | if (obj->u.counter_cpu.shm_fd < 0) { |
| 1207 | ret = errno; |
| 1208 | goto error_type; |
| 1209 | } |
| 1210 | } |
| 1211 | break; |
| 1212 | } |
| 1213 | |
| 1214 | default: |
| 1215 | ret = -EINVAL; |
| 1216 | goto error_type; |
| 1217 | } |
| 1218 | |
| 1219 | *dest = obj; |
| 1220 | return 0; |
| 1221 | |
| 1222 | error_type: |
| 1223 | free(obj); |
| 1224 | error: |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | /* Buffer operations */ |
| 1230 | |
| 1231 | int ustctl_get_nr_stream_per_channel(void) |
| 1232 | { |
| 1233 | return num_possible_cpus(); |
| 1234 | } |
| 1235 | |
| 1236 | struct ustctl_consumer_channel * |
| 1237 | ustctl_create_channel(struct ustctl_consumer_channel_attr *attr, |
| 1238 | const int *stream_fds, int nr_stream_fds) |
| 1239 | { |
| 1240 | struct ustctl_consumer_channel *chan; |
| 1241 | const char *transport_name; |
| 1242 | struct lttng_transport *transport; |
| 1243 | |
| 1244 | switch (attr->type) { |
| 1245 | case LTTNG_UST_CHAN_PER_CPU: |
| 1246 | if (attr->output == LTTNG_UST_MMAP) { |
| 1247 | if (attr->overwrite) { |
| 1248 | if (attr->read_timer_interval == 0) { |
| 1249 | transport_name = "relay-overwrite-mmap"; |
| 1250 | } else { |
| 1251 | transport_name = "relay-overwrite-rt-mmap"; |
| 1252 | } |
| 1253 | } else { |
| 1254 | if (attr->read_timer_interval == 0) { |
| 1255 | transport_name = "relay-discard-mmap"; |
| 1256 | } else { |
| 1257 | transport_name = "relay-discard-rt-mmap"; |
| 1258 | } |
| 1259 | } |
| 1260 | } else { |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | break; |
| 1264 | case LTTNG_UST_CHAN_METADATA: |
| 1265 | if (attr->output == LTTNG_UST_MMAP) |
| 1266 | transport_name = "relay-metadata-mmap"; |
| 1267 | else |
| 1268 | return NULL; |
| 1269 | break; |
| 1270 | default: |
| 1271 | transport_name = "<unknown>"; |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | |
| 1275 | transport = lttng_transport_find(transport_name); |
| 1276 | if (!transport) { |
| 1277 | DBG("LTTng transport %s not found\n", |
| 1278 | transport_name); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | chan = zmalloc(sizeof(*chan)); |
| 1283 | if (!chan) |
| 1284 | return NULL; |
| 1285 | |
| 1286 | chan->chan = transport->ops.channel_create(transport_name, NULL, |
| 1287 | attr->subbuf_size, attr->num_subbuf, |
| 1288 | attr->switch_timer_interval, |
| 1289 | attr->read_timer_interval, |
| 1290 | attr->uuid, attr->chan_id, |
| 1291 | stream_fds, nr_stream_fds, |
| 1292 | attr->blocking_timeout); |
| 1293 | if (!chan->chan) { |
| 1294 | goto chan_error; |
| 1295 | } |
| 1296 | chan->chan->ops = &transport->ops; |
| 1297 | memcpy(&chan->attr, attr, sizeof(chan->attr)); |
| 1298 | chan->wait_fd = ustctl_channel_get_wait_fd(chan); |
| 1299 | chan->wakeup_fd = ustctl_channel_get_wakeup_fd(chan); |
| 1300 | return chan; |
| 1301 | |
| 1302 | chan_error: |
| 1303 | free(chan); |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | void ustctl_destroy_channel(struct ustctl_consumer_channel *chan) |
| 1308 | { |
| 1309 | (void) ustctl_channel_close_wait_fd(chan); |
| 1310 | (void) ustctl_channel_close_wakeup_fd(chan); |
| 1311 | chan->chan->ops->channel_destroy(chan->chan); |
| 1312 | free(chan); |
| 1313 | } |
| 1314 | |
| 1315 | int ustctl_send_channel_to_sessiond(int sock, |
| 1316 | struct ustctl_consumer_channel *channel) |
| 1317 | { |
| 1318 | struct shm_object_table *table; |
| 1319 | |
| 1320 | table = channel->chan->handle->table; |
| 1321 | if (table->size <= 0) |
| 1322 | return -EINVAL; |
| 1323 | return ustctl_send_channel(sock, |
| 1324 | channel->attr.type, |
| 1325 | table->objects[0].memory_map, |
| 1326 | table->objects[0].memory_map_size, |
| 1327 | channel->wakeup_fd, |
| 1328 | 0); |
| 1329 | } |
| 1330 | |
| 1331 | int ustctl_send_stream_to_sessiond(int sock, |
| 1332 | struct ustctl_consumer_stream *stream) |
| 1333 | { |
| 1334 | if (!stream) |
| 1335 | return ustctl_send_stream(sock, -1U, -1U, -1, -1, 0); |
| 1336 | |
| 1337 | return ustctl_send_stream(sock, |
| 1338 | stream->cpu, |
| 1339 | stream->memory_map_size, |
| 1340 | stream->shm_fd, stream->wakeup_fd, |
| 1341 | 0); |
| 1342 | } |
| 1343 | |
| 1344 | int ustctl_write_metadata_to_channel( |
| 1345 | struct ustctl_consumer_channel *channel, |
| 1346 | const char *metadata_str, /* NOT null-terminated */ |
| 1347 | size_t len) /* metadata length */ |
| 1348 | { |
| 1349 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 1350 | struct lttng_channel *chan = channel->chan; |
| 1351 | const char *str = metadata_str; |
| 1352 | int ret = 0, waitret; |
| 1353 | size_t reserve_len, pos; |
| 1354 | |
| 1355 | for (pos = 0; pos < len; pos += reserve_len) { |
| 1356 | reserve_len = min_t(size_t, |
| 1357 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 1358 | len - pos); |
| 1359 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 1360 | sizeof(char), -1, chan->handle, NULL); |
| 1361 | /* |
| 1362 | * We don't care about metadata buffer's records lost |
| 1363 | * count, because we always retry here. Report error if |
| 1364 | * we need to bail out after timeout or being |
| 1365 | * interrupted. |
| 1366 | */ |
| 1367 | waitret = wait_cond_interruptible_timeout( |
| 1368 | ({ |
| 1369 | ret = chan->ops->event_reserve(&ctx, 0); |
| 1370 | ret != -ENOBUFS || !ret; |
| 1371 | }), |
| 1372 | LTTNG_METADATA_TIMEOUT_MSEC); |
| 1373 | if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) { |
| 1374 | DBG("LTTng: Failure to write metadata to buffers (%s)\n", |
| 1375 | waitret == -EINTR ? "interrupted" : |
| 1376 | (ret == -ENOBUFS ? "timeout" : "I/O error")); |
| 1377 | if (waitret == -EINTR) |
| 1378 | ret = waitret; |
| 1379 | goto end; |
| 1380 | } |
| 1381 | chan->ops->event_write(&ctx, &str[pos], reserve_len); |
| 1382 | chan->ops->event_commit(&ctx); |
| 1383 | } |
| 1384 | end: |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Write at most one packet in the channel. |
| 1390 | * Returns the number of bytes written on success, < 0 on error. |
| 1391 | */ |
| 1392 | ssize_t ustctl_write_one_packet_to_channel( |
| 1393 | struct ustctl_consumer_channel *channel, |
| 1394 | const char *metadata_str, /* NOT null-terminated */ |
| 1395 | size_t len) /* metadata length */ |
| 1396 | { |
| 1397 | struct lttng_ust_lib_ring_buffer_ctx ctx; |
| 1398 | struct lttng_channel *chan = channel->chan; |
| 1399 | const char *str = metadata_str; |
| 1400 | ssize_t reserve_len; |
| 1401 | int ret; |
| 1402 | |
| 1403 | reserve_len = min_t(ssize_t, |
| 1404 | chan->ops->packet_avail_size(chan->chan, chan->handle), |
| 1405 | len); |
| 1406 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, |
| 1407 | sizeof(char), -1, chan->handle, NULL); |
| 1408 | ret = chan->ops->event_reserve(&ctx, 0); |
| 1409 | if (ret != 0) { |
| 1410 | DBG("LTTng: event reservation failed"); |
| 1411 | assert(ret < 0); |
| 1412 | reserve_len = ret; |
| 1413 | goto end; |
| 1414 | } |
| 1415 | chan->ops->event_write(&ctx, str, reserve_len); |
| 1416 | chan->ops->event_commit(&ctx); |
| 1417 | |
| 1418 | end: |
| 1419 | return reserve_len; |
| 1420 | } |
| 1421 | |
| 1422 | int ustctl_channel_close_wait_fd(struct ustctl_consumer_channel *consumer_chan) |
| 1423 | { |
| 1424 | struct channel *chan; |
| 1425 | int ret; |
| 1426 | |
| 1427 | chan = consumer_chan->chan->chan; |
| 1428 | ret = ring_buffer_channel_close_wait_fd(&chan->backend.config, |
| 1429 | chan, chan->handle); |
| 1430 | if (!ret) |
| 1431 | consumer_chan->wait_fd = -1; |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | int ustctl_channel_close_wakeup_fd(struct ustctl_consumer_channel *consumer_chan) |
| 1436 | { |
| 1437 | struct channel *chan; |
| 1438 | int ret; |
| 1439 | |
| 1440 | chan = consumer_chan->chan->chan; |
| 1441 | ret = ring_buffer_channel_close_wakeup_fd(&chan->backend.config, |
| 1442 | chan, chan->handle); |
| 1443 | if (!ret) |
| 1444 | consumer_chan->wakeup_fd = -1; |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
| 1448 | int ustctl_stream_close_wait_fd(struct ustctl_consumer_stream *stream) |
| 1449 | { |
| 1450 | struct channel *chan; |
| 1451 | |
| 1452 | chan = stream->chan->chan->chan; |
| 1453 | return ring_buffer_stream_close_wait_fd(&chan->backend.config, |
| 1454 | chan, stream->handle, stream->cpu); |
| 1455 | } |
| 1456 | |
| 1457 | int ustctl_stream_close_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 1458 | { |
| 1459 | struct channel *chan; |
| 1460 | |
| 1461 | chan = stream->chan->chan->chan; |
| 1462 | return ring_buffer_stream_close_wakeup_fd(&chan->backend.config, |
| 1463 | chan, stream->handle, stream->cpu); |
| 1464 | } |
| 1465 | |
| 1466 | struct ustctl_consumer_stream * |
| 1467 | ustctl_create_stream(struct ustctl_consumer_channel *channel, |
| 1468 | int cpu) |
| 1469 | { |
| 1470 | struct ustctl_consumer_stream *stream; |
| 1471 | struct lttng_ust_shm_handle *handle; |
| 1472 | struct channel *chan; |
| 1473 | int shm_fd, wait_fd, wakeup_fd; |
| 1474 | uint64_t memory_map_size; |
| 1475 | struct lttng_ust_lib_ring_buffer *buf; |
| 1476 | int ret; |
| 1477 | |
| 1478 | if (!channel) |
| 1479 | return NULL; |
| 1480 | handle = channel->chan->handle; |
| 1481 | if (!handle) |
| 1482 | return NULL; |
| 1483 | |
| 1484 | chan = channel->chan->chan; |
| 1485 | buf = channel_get_ring_buffer(&chan->backend.config, |
| 1486 | chan, cpu, handle, &shm_fd, &wait_fd, |
| 1487 | &wakeup_fd, &memory_map_size); |
| 1488 | if (!buf) |
| 1489 | return NULL; |
| 1490 | ret = lib_ring_buffer_open_read(buf, handle); |
| 1491 | if (ret) |
| 1492 | return NULL; |
| 1493 | |
| 1494 | stream = zmalloc(sizeof(*stream)); |
| 1495 | if (!stream) |
| 1496 | goto alloc_error; |
| 1497 | stream->handle = handle; |
| 1498 | stream->buf = buf; |
| 1499 | stream->chan = channel; |
| 1500 | stream->shm_fd = shm_fd; |
| 1501 | stream->wait_fd = wait_fd; |
| 1502 | stream->wakeup_fd = wakeup_fd; |
| 1503 | stream->memory_map_size = memory_map_size; |
| 1504 | stream->cpu = cpu; |
| 1505 | return stream; |
| 1506 | |
| 1507 | alloc_error: |
| 1508 | return NULL; |
| 1509 | } |
| 1510 | |
| 1511 | void ustctl_destroy_stream(struct ustctl_consumer_stream *stream) |
| 1512 | { |
| 1513 | struct lttng_ust_lib_ring_buffer *buf; |
| 1514 | struct ustctl_consumer_channel *consumer_chan; |
| 1515 | |
| 1516 | assert(stream); |
| 1517 | buf = stream->buf; |
| 1518 | consumer_chan = stream->chan; |
| 1519 | (void) ustctl_stream_close_wait_fd(stream); |
| 1520 | (void) ustctl_stream_close_wakeup_fd(stream); |
| 1521 | lib_ring_buffer_release_read(buf, consumer_chan->chan->handle); |
| 1522 | free(stream); |
| 1523 | } |
| 1524 | |
| 1525 | int ustctl_channel_get_wait_fd(struct ustctl_consumer_channel *chan) |
| 1526 | { |
| 1527 | if (!chan) |
| 1528 | return -EINVAL; |
| 1529 | return shm_get_wait_fd(chan->chan->handle, |
| 1530 | &chan->chan->handle->chan._ref); |
| 1531 | } |
| 1532 | |
| 1533 | int ustctl_channel_get_wakeup_fd(struct ustctl_consumer_channel *chan) |
| 1534 | { |
| 1535 | if (!chan) |
| 1536 | return -EINVAL; |
| 1537 | return shm_get_wakeup_fd(chan->chan->handle, |
| 1538 | &chan->chan->handle->chan._ref); |
| 1539 | } |
| 1540 | |
| 1541 | int ustctl_stream_get_wait_fd(struct ustctl_consumer_stream *stream) |
| 1542 | { |
| 1543 | struct lttng_ust_lib_ring_buffer *buf; |
| 1544 | struct ustctl_consumer_channel *consumer_chan; |
| 1545 | |
| 1546 | if (!stream) |
| 1547 | return -EINVAL; |
| 1548 | buf = stream->buf; |
| 1549 | consumer_chan = stream->chan; |
| 1550 | return shm_get_wait_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 1551 | } |
| 1552 | |
| 1553 | int ustctl_stream_get_wakeup_fd(struct ustctl_consumer_stream *stream) |
| 1554 | { |
| 1555 | struct lttng_ust_lib_ring_buffer *buf; |
| 1556 | struct ustctl_consumer_channel *consumer_chan; |
| 1557 | |
| 1558 | if (!stream) |
| 1559 | return -EINVAL; |
| 1560 | buf = stream->buf; |
| 1561 | consumer_chan = stream->chan; |
| 1562 | return shm_get_wakeup_fd(consumer_chan->chan->handle, &buf->self._ref); |
| 1563 | } |
| 1564 | |
| 1565 | /* For mmap mode, readable without "get" operation */ |
| 1566 | |
| 1567 | void *ustctl_get_mmap_base(struct ustctl_consumer_stream *stream) |
| 1568 | { |
| 1569 | struct lttng_ust_lib_ring_buffer *buf; |
| 1570 | struct ustctl_consumer_channel *consumer_chan; |
| 1571 | |
| 1572 | if (!stream) |
| 1573 | return NULL; |
| 1574 | buf = stream->buf; |
| 1575 | consumer_chan = stream->chan; |
| 1576 | return shmp(consumer_chan->chan->handle, buf->backend.memory_map); |
| 1577 | } |
| 1578 | |
| 1579 | /* returns the length to mmap. */ |
| 1580 | int ustctl_get_mmap_len(struct ustctl_consumer_stream *stream, |
| 1581 | unsigned long *len) |
| 1582 | { |
| 1583 | struct ustctl_consumer_channel *consumer_chan; |
| 1584 | unsigned long mmap_buf_len; |
| 1585 | struct channel *chan; |
| 1586 | |
| 1587 | if (!stream) |
| 1588 | return -EINVAL; |
| 1589 | consumer_chan = stream->chan; |
| 1590 | chan = consumer_chan->chan->chan; |
| 1591 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1592 | return -EINVAL; |
| 1593 | mmap_buf_len = chan->backend.buf_size; |
| 1594 | if (chan->backend.extra_reader_sb) |
| 1595 | mmap_buf_len += chan->backend.subbuf_size; |
| 1596 | if (mmap_buf_len > INT_MAX) |
| 1597 | return -EFBIG; |
| 1598 | *len = mmap_buf_len; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | /* returns the maximum size for sub-buffers. */ |
| 1603 | int ustctl_get_max_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1604 | unsigned long *len) |
| 1605 | { |
| 1606 | struct ustctl_consumer_channel *consumer_chan; |
| 1607 | struct channel *chan; |
| 1608 | |
| 1609 | if (!stream) |
| 1610 | return -EINVAL; |
| 1611 | consumer_chan = stream->chan; |
| 1612 | chan = consumer_chan->chan->chan; |
| 1613 | *len = chan->backend.subbuf_size; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * For mmap mode, operate on the current packet (between get/put or |
| 1619 | * get_next/put_next). |
| 1620 | */ |
| 1621 | |
| 1622 | /* returns the offset of the subbuffer belonging to the mmap reader. */ |
| 1623 | int ustctl_get_mmap_read_offset(struct ustctl_consumer_stream *stream, |
| 1624 | unsigned long *off) |
| 1625 | { |
| 1626 | struct channel *chan; |
| 1627 | unsigned long sb_bindex; |
| 1628 | struct lttng_ust_lib_ring_buffer *buf; |
| 1629 | struct ustctl_consumer_channel *consumer_chan; |
| 1630 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *barray_idx; |
| 1631 | struct lttng_ust_lib_ring_buffer_backend_pages *pages; |
| 1632 | |
| 1633 | if (!stream) |
| 1634 | return -EINVAL; |
| 1635 | buf = stream->buf; |
| 1636 | consumer_chan = stream->chan; |
| 1637 | chan = consumer_chan->chan->chan; |
| 1638 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
| 1639 | return -EINVAL; |
| 1640 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, |
| 1641 | buf->backend.buf_rsb.id); |
| 1642 | barray_idx = shmp_index(consumer_chan->chan->handle, buf->backend.array, |
| 1643 | sb_bindex); |
| 1644 | if (!barray_idx) |
| 1645 | return -EINVAL; |
| 1646 | pages = shmp(consumer_chan->chan->handle, barray_idx->shmp); |
| 1647 | if (!pages) |
| 1648 | return -EINVAL; |
| 1649 | *off = pages->mmap_offset; |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1654 | int ustctl_get_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1655 | unsigned long *len) |
| 1656 | { |
| 1657 | struct ustctl_consumer_channel *consumer_chan; |
| 1658 | struct channel *chan; |
| 1659 | struct lttng_ust_lib_ring_buffer *buf; |
| 1660 | |
| 1661 | if (!stream) |
| 1662 | return -EINVAL; |
| 1663 | |
| 1664 | buf = stream->buf; |
| 1665 | consumer_chan = stream->chan; |
| 1666 | chan = consumer_chan->chan->chan; |
| 1667 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1668 | consumer_chan->chan->handle); |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | /* returns the size of the current sub-buffer, without padding (for mmap). */ |
| 1673 | int ustctl_get_padded_subbuf_size(struct ustctl_consumer_stream *stream, |
| 1674 | unsigned long *len) |
| 1675 | { |
| 1676 | struct ustctl_consumer_channel *consumer_chan; |
| 1677 | struct channel *chan; |
| 1678 | struct lttng_ust_lib_ring_buffer *buf; |
| 1679 | |
| 1680 | if (!stream) |
| 1681 | return -EINVAL; |
| 1682 | buf = stream->buf; |
| 1683 | consumer_chan = stream->chan; |
| 1684 | chan = consumer_chan->chan->chan; |
| 1685 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
| 1686 | consumer_chan->chan->handle); |
| 1687 | *len = LTTNG_UST_PAGE_ALIGN(*len); |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* Get exclusive read access to the next sub-buffer that can be read. */ |
| 1692 | int ustctl_get_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1693 | { |
| 1694 | struct lttng_ust_lib_ring_buffer *buf; |
| 1695 | struct ustctl_consumer_channel *consumer_chan; |
| 1696 | |
| 1697 | if (!stream) |
| 1698 | return -EINVAL; |
| 1699 | buf = stream->buf; |
| 1700 | consumer_chan = stream->chan; |
| 1701 | return lib_ring_buffer_get_next_subbuf(buf, |
| 1702 | consumer_chan->chan->handle); |
| 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | /* Release exclusive sub-buffer access, move consumer forward. */ |
| 1707 | int ustctl_put_next_subbuf(struct ustctl_consumer_stream *stream) |
| 1708 | { |
| 1709 | struct lttng_ust_lib_ring_buffer *buf; |
| 1710 | struct ustctl_consumer_channel *consumer_chan; |
| 1711 | |
| 1712 | if (!stream) |
| 1713 | return -EINVAL; |
| 1714 | buf = stream->buf; |
| 1715 | consumer_chan = stream->chan; |
| 1716 | lib_ring_buffer_put_next_subbuf(buf, consumer_chan->chan->handle); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | /* snapshot */ |
| 1721 | |
| 1722 | /* Get a snapshot of the current ring buffer producer and consumer positions */ |
| 1723 | int ustctl_snapshot(struct ustctl_consumer_stream *stream) |
| 1724 | { |
| 1725 | struct lttng_ust_lib_ring_buffer *buf; |
| 1726 | struct ustctl_consumer_channel *consumer_chan; |
| 1727 | |
| 1728 | if (!stream) |
| 1729 | return -EINVAL; |
| 1730 | buf = stream->buf; |
| 1731 | consumer_chan = stream->chan; |
| 1732 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 1733 | &buf->prod_snapshot, consumer_chan->chan->handle); |
| 1734 | } |
| 1735 | |
| 1736 | /* |
| 1737 | * Get a snapshot of the current ring buffer producer and consumer positions |
| 1738 | * even if the consumed and produced positions are contained within the same |
| 1739 | * subbuffer. |
| 1740 | */ |
| 1741 | int ustctl_snapshot_sample_positions(struct ustctl_consumer_stream *stream) |
| 1742 | { |
| 1743 | struct lttng_ust_lib_ring_buffer *buf; |
| 1744 | struct ustctl_consumer_channel *consumer_chan; |
| 1745 | |
| 1746 | if (!stream) |
| 1747 | return -EINVAL; |
| 1748 | buf = stream->buf; |
| 1749 | consumer_chan = stream->chan; |
| 1750 | return lib_ring_buffer_snapshot_sample_positions(buf, |
| 1751 | &buf->cons_snapshot, &buf->prod_snapshot, |
| 1752 | consumer_chan->chan->handle); |
| 1753 | } |
| 1754 | |
| 1755 | /* Get the consumer position (iteration start) */ |
| 1756 | int ustctl_snapshot_get_consumed(struct ustctl_consumer_stream *stream, |
| 1757 | unsigned long *pos) |
| 1758 | { |
| 1759 | struct lttng_ust_lib_ring_buffer *buf; |
| 1760 | |
| 1761 | if (!stream) |
| 1762 | return -EINVAL; |
| 1763 | buf = stream->buf; |
| 1764 | *pos = buf->cons_snapshot; |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
| 1768 | /* Get the producer position (iteration end) */ |
| 1769 | int ustctl_snapshot_get_produced(struct ustctl_consumer_stream *stream, |
| 1770 | unsigned long *pos) |
| 1771 | { |
| 1772 | struct lttng_ust_lib_ring_buffer *buf; |
| 1773 | |
| 1774 | if (!stream) |
| 1775 | return -EINVAL; |
| 1776 | buf = stream->buf; |
| 1777 | *pos = buf->prod_snapshot; |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | /* Get exclusive read access to the specified sub-buffer position */ |
| 1782 | int ustctl_get_subbuf(struct ustctl_consumer_stream *stream, |
| 1783 | unsigned long *pos) |
| 1784 | { |
| 1785 | struct lttng_ust_lib_ring_buffer *buf; |
| 1786 | struct ustctl_consumer_channel *consumer_chan; |
| 1787 | |
| 1788 | if (!stream) |
| 1789 | return -EINVAL; |
| 1790 | buf = stream->buf; |
| 1791 | consumer_chan = stream->chan; |
| 1792 | return lib_ring_buffer_get_subbuf(buf, *pos, |
| 1793 | consumer_chan->chan->handle); |
| 1794 | } |
| 1795 | |
| 1796 | /* Release exclusive sub-buffer access */ |
| 1797 | int ustctl_put_subbuf(struct ustctl_consumer_stream *stream) |
| 1798 | { |
| 1799 | struct lttng_ust_lib_ring_buffer *buf; |
| 1800 | struct ustctl_consumer_channel *consumer_chan; |
| 1801 | |
| 1802 | if (!stream) |
| 1803 | return -EINVAL; |
| 1804 | buf = stream->buf; |
| 1805 | consumer_chan = stream->chan; |
| 1806 | lib_ring_buffer_put_subbuf(buf, consumer_chan->chan->handle); |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | void ustctl_flush_buffer(struct ustctl_consumer_stream *stream, |
| 1811 | int producer_active) |
| 1812 | { |
| 1813 | struct lttng_ust_lib_ring_buffer *buf; |
| 1814 | struct ustctl_consumer_channel *consumer_chan; |
| 1815 | |
| 1816 | assert(stream); |
| 1817 | buf = stream->buf; |
| 1818 | consumer_chan = stream->chan; |
| 1819 | lib_ring_buffer_switch_slow(buf, |
| 1820 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, |
| 1821 | consumer_chan->chan->handle); |
| 1822 | } |
| 1823 | |
| 1824 | void ustctl_clear_buffer(struct ustctl_consumer_stream *stream) |
| 1825 | { |
| 1826 | struct lttng_ust_lib_ring_buffer *buf; |
| 1827 | struct ustctl_consumer_channel *consumer_chan; |
| 1828 | |
| 1829 | assert(stream); |
| 1830 | buf = stream->buf; |
| 1831 | consumer_chan = stream->chan; |
| 1832 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, |
| 1833 | consumer_chan->chan->handle); |
| 1834 | lib_ring_buffer_clear_reader(buf, consumer_chan->chan->handle); |
| 1835 | } |
| 1836 | |
| 1837 | static |
| 1838 | struct lttng_ust_client_lib_ring_buffer_client_cb *get_client_cb( |
| 1839 | struct lttng_ust_lib_ring_buffer *buf, |
| 1840 | struct lttng_ust_shm_handle *handle) |
| 1841 | { |
| 1842 | struct channel *chan; |
| 1843 | const struct lttng_ust_lib_ring_buffer_config *config; |
| 1844 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1845 | |
| 1846 | chan = shmp(handle, buf->backend.chan); |
| 1847 | if (!chan) |
| 1848 | return NULL; |
| 1849 | config = &chan->backend.config; |
| 1850 | if (!config->cb_ptr) |
| 1851 | return NULL; |
| 1852 | client_cb = caa_container_of(config->cb_ptr, |
| 1853 | struct lttng_ust_client_lib_ring_buffer_client_cb, |
| 1854 | parent); |
| 1855 | return client_cb; |
| 1856 | } |
| 1857 | |
| 1858 | int ustctl_get_timestamp_begin(struct ustctl_consumer_stream *stream, |
| 1859 | uint64_t *timestamp_begin) |
| 1860 | { |
| 1861 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1862 | struct lttng_ust_lib_ring_buffer *buf; |
| 1863 | struct lttng_ust_shm_handle *handle; |
| 1864 | |
| 1865 | if (!stream || !timestamp_begin) |
| 1866 | return -EINVAL; |
| 1867 | buf = stream->buf; |
| 1868 | handle = stream->chan->chan->handle; |
| 1869 | client_cb = get_client_cb(buf, handle); |
| 1870 | if (!client_cb) |
| 1871 | return -ENOSYS; |
| 1872 | return client_cb->timestamp_begin(buf, handle, timestamp_begin); |
| 1873 | } |
| 1874 | |
| 1875 | int ustctl_get_timestamp_end(struct ustctl_consumer_stream *stream, |
| 1876 | uint64_t *timestamp_end) |
| 1877 | { |
| 1878 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1879 | struct lttng_ust_lib_ring_buffer *buf; |
| 1880 | struct lttng_ust_shm_handle *handle; |
| 1881 | |
| 1882 | if (!stream || !timestamp_end) |
| 1883 | return -EINVAL; |
| 1884 | buf = stream->buf; |
| 1885 | handle = stream->chan->chan->handle; |
| 1886 | client_cb = get_client_cb(buf, handle); |
| 1887 | if (!client_cb) |
| 1888 | return -ENOSYS; |
| 1889 | return client_cb->timestamp_end(buf, handle, timestamp_end); |
| 1890 | } |
| 1891 | |
| 1892 | int ustctl_get_events_discarded(struct ustctl_consumer_stream *stream, |
| 1893 | uint64_t *events_discarded) |
| 1894 | { |
| 1895 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1896 | struct lttng_ust_lib_ring_buffer *buf; |
| 1897 | struct lttng_ust_shm_handle *handle; |
| 1898 | |
| 1899 | if (!stream || !events_discarded) |
| 1900 | return -EINVAL; |
| 1901 | buf = stream->buf; |
| 1902 | handle = stream->chan->chan->handle; |
| 1903 | client_cb = get_client_cb(buf, handle); |
| 1904 | if (!client_cb) |
| 1905 | return -ENOSYS; |
| 1906 | return client_cb->events_discarded(buf, handle, events_discarded); |
| 1907 | } |
| 1908 | |
| 1909 | int ustctl_get_content_size(struct ustctl_consumer_stream *stream, |
| 1910 | uint64_t *content_size) |
| 1911 | { |
| 1912 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1913 | struct lttng_ust_lib_ring_buffer *buf; |
| 1914 | struct lttng_ust_shm_handle *handle; |
| 1915 | |
| 1916 | if (!stream || !content_size) |
| 1917 | return -EINVAL; |
| 1918 | buf = stream->buf; |
| 1919 | handle = stream->chan->chan->handle; |
| 1920 | client_cb = get_client_cb(buf, handle); |
| 1921 | if (!client_cb) |
| 1922 | return -ENOSYS; |
| 1923 | return client_cb->content_size(buf, handle, content_size); |
| 1924 | } |
| 1925 | |
| 1926 | int ustctl_get_packet_size(struct ustctl_consumer_stream *stream, |
| 1927 | uint64_t *packet_size) |
| 1928 | { |
| 1929 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1930 | struct lttng_ust_lib_ring_buffer *buf; |
| 1931 | struct lttng_ust_shm_handle *handle; |
| 1932 | |
| 1933 | if (!stream || !packet_size) |
| 1934 | return -EINVAL; |
| 1935 | buf = stream->buf; |
| 1936 | handle = stream->chan->chan->handle; |
| 1937 | client_cb = get_client_cb(buf, handle); |
| 1938 | if (!client_cb) |
| 1939 | return -ENOSYS; |
| 1940 | return client_cb->packet_size(buf, handle, packet_size); |
| 1941 | } |
| 1942 | |
| 1943 | int ustctl_get_stream_id(struct ustctl_consumer_stream *stream, |
| 1944 | uint64_t *stream_id) |
| 1945 | { |
| 1946 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1947 | struct lttng_ust_lib_ring_buffer *buf; |
| 1948 | struct lttng_ust_shm_handle *handle; |
| 1949 | |
| 1950 | if (!stream || !stream_id) |
| 1951 | return -EINVAL; |
| 1952 | buf = stream->buf; |
| 1953 | handle = stream->chan->chan->handle; |
| 1954 | client_cb = get_client_cb(buf, handle); |
| 1955 | if (!client_cb) |
| 1956 | return -ENOSYS; |
| 1957 | return client_cb->stream_id(buf, handle, stream_id); |
| 1958 | } |
| 1959 | |
| 1960 | int ustctl_get_current_timestamp(struct ustctl_consumer_stream *stream, |
| 1961 | uint64_t *ts) |
| 1962 | { |
| 1963 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1964 | struct lttng_ust_lib_ring_buffer *buf; |
| 1965 | struct lttng_ust_shm_handle *handle; |
| 1966 | |
| 1967 | if (!stream || !ts) |
| 1968 | return -EINVAL; |
| 1969 | buf = stream->buf; |
| 1970 | handle = stream->chan->chan->handle; |
| 1971 | client_cb = get_client_cb(buf, handle); |
| 1972 | if (!client_cb || !client_cb->current_timestamp) |
| 1973 | return -ENOSYS; |
| 1974 | return client_cb->current_timestamp(buf, handle, ts); |
| 1975 | } |
| 1976 | |
| 1977 | int ustctl_get_sequence_number(struct ustctl_consumer_stream *stream, |
| 1978 | uint64_t *seq) |
| 1979 | { |
| 1980 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1981 | struct lttng_ust_lib_ring_buffer *buf; |
| 1982 | struct lttng_ust_shm_handle *handle; |
| 1983 | |
| 1984 | if (!stream || !seq) |
| 1985 | return -EINVAL; |
| 1986 | buf = stream->buf; |
| 1987 | handle = stream->chan->chan->handle; |
| 1988 | client_cb = get_client_cb(buf, handle); |
| 1989 | if (!client_cb || !client_cb->sequence_number) |
| 1990 | return -ENOSYS; |
| 1991 | return client_cb->sequence_number(buf, handle, seq); |
| 1992 | } |
| 1993 | |
| 1994 | int ustctl_get_instance_id(struct ustctl_consumer_stream *stream, |
| 1995 | uint64_t *id) |
| 1996 | { |
| 1997 | struct lttng_ust_client_lib_ring_buffer_client_cb *client_cb; |
| 1998 | struct lttng_ust_lib_ring_buffer *buf; |
| 1999 | struct lttng_ust_shm_handle *handle; |
| 2000 | |
| 2001 | if (!stream || !id) |
| 2002 | return -EINVAL; |
| 2003 | buf = stream->buf; |
| 2004 | handle = stream->chan->chan->handle; |
| 2005 | client_cb = get_client_cb(buf, handle); |
| 2006 | if (!client_cb) |
| 2007 | return -ENOSYS; |
| 2008 | return client_cb->instance_id(buf, handle, id); |
| 2009 | } |
| 2010 | |
| 2011 | #ifdef LTTNG_UST_HAVE_PERF_EVENT |
| 2012 | |
| 2013 | int ustctl_has_perf_counters(void) |
| 2014 | { |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
| 2018 | #else |
| 2019 | |
| 2020 | int ustctl_has_perf_counters(void) |
| 2021 | { |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
| 2025 | #endif |
| 2026 | |
| 2027 | #ifdef __linux__ |
| 2028 | /* |
| 2029 | * Override application pid/uid/gid with unix socket credentials. If |
| 2030 | * the application announced a pid matching our view, it means it is |
| 2031 | * within the same pid namespace, so expose the ppid provided by the |
| 2032 | * application. |
| 2033 | */ |
| 2034 | static |
| 2035 | int get_cred(int sock, |
| 2036 | const struct ustctl_reg_msg *reg_msg, |
| 2037 | uint32_t *pid, |
| 2038 | uint32_t *ppid, |
| 2039 | uint32_t *uid, |
| 2040 | uint32_t *gid) |
| 2041 | { |
| 2042 | struct ucred ucred; |
| 2043 | socklen_t ucred_len = sizeof(struct ucred); |
| 2044 | int ret; |
| 2045 | |
| 2046 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &ucred_len); |
| 2047 | if (ret) { |
| 2048 | return -LTTNG_UST_ERR_PEERCRED; |
| 2049 | } |
| 2050 | DBG("Unix socket peercred [ pid: %u, uid: %u, gid: %u ], " |
| 2051 | "application registered claiming [ pid: %u, ppid: %u, uid: %u, gid: %u ]", |
| 2052 | ucred.pid, ucred.uid, ucred.gid, |
| 2053 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); |
| 2054 | if (!ucred.pid) { |
| 2055 | ERR("Unix socket credential pid=0. Refusing application in distinct, non-nested pid namespace."); |
| 2056 | return -LTTNG_UST_ERR_PEERCRED_PID; |
| 2057 | } |
| 2058 | *pid = ucred.pid; |
| 2059 | *uid = ucred.uid; |
| 2060 | *gid = ucred.gid; |
| 2061 | if (ucred.pid == reg_msg->pid) { |
| 2062 | *ppid = reg_msg->ppid; |
| 2063 | } else { |
| 2064 | *ppid = 0; |
| 2065 | } |
| 2066 | return 0; |
| 2067 | } |
| 2068 | #elif defined(__FreeBSD__) |
| 2069 | #include <sys/ucred.h> |
| 2070 | #include <sys/un.h> |
| 2071 | |
| 2072 | /* |
| 2073 | * Override application uid/gid with unix socket credentials. Use the |
| 2074 | * first group of the cr_groups. |
| 2075 | * Use the pid and ppid provided by the application on registration. |
| 2076 | */ |
| 2077 | static |
| 2078 | int get_cred(int sock, |
| 2079 | const struct ustctl_reg_msg *reg_msg, |
| 2080 | uint32_t *pid, |
| 2081 | uint32_t *ppid, |
| 2082 | uint32_t *uid, |
| 2083 | uint32_t *gid) |
| 2084 | { |
| 2085 | struct xucred xucred; |
| 2086 | socklen_t xucred_len = sizeof(struct xucred); |
| 2087 | int ret; |
| 2088 | |
| 2089 | ret = getsockopt(sock, SOL_SOCKET, LOCAL_PEERCRED, &xucred, &xucred_len); |
| 2090 | if (ret) { |
| 2091 | return -LTTNG_UST_ERR_PEERCRED; |
| 2092 | } |
| 2093 | if (xucred.cr_version != XUCRED_VERSION || xucred.cr_ngroups < 1) { |
| 2094 | return -LTTNG_UST_ERR_PEERCRED; |
| 2095 | } |
| 2096 | DBG("Unix socket peercred [ uid: %u, gid: %u ], " |
| 2097 | "application registered claiming [ pid: %d, ppid: %d, uid: %u, gid: %u ]", |
| 2098 | xucred.cr_uid, xucred.cr_groups[0], |
| 2099 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); |
| 2100 | *pid = reg_msg->pid; |
| 2101 | *ppid = reg_msg->ppid; |
| 2102 | *uid = xucred.cr_uid; |
| 2103 | *gid = xucred.cr_groups[0]; |
| 2104 | return 0; |
| 2105 | } |
| 2106 | #else |
| 2107 | #warning "Using insecure fallback: trusting user id provided by registered applications. Please consider implementing use of unix socket credentials on your platform." |
| 2108 | static |
| 2109 | int get_cred(int sock, |
| 2110 | const struct ustctl_reg_msg *reg_msg, |
| 2111 | uint32_t *pid, |
| 2112 | uint32_t *ppid, |
| 2113 | uint32_t *uid, |
| 2114 | uint32_t *gid) |
| 2115 | { |
| 2116 | DBG("Application registered claiming [ pid: %u, ppid: %d, uid: %u, gid: %u ]", |
| 2117 | reg_msg->pid, reg_msg->ppid, reg_msg->uid, reg_msg->gid); |
| 2118 | *pid = reg_msg->pid; |
| 2119 | *ppid = reg_msg->ppid; |
| 2120 | *uid = reg_msg->uid; |
| 2121 | *gid = reg_msg->gid; |
| 2122 | return 0; |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
| 2126 | /* |
| 2127 | * Returns 0 on success, negative error value on error. |
| 2128 | */ |
| 2129 | int ustctl_recv_reg_msg(int sock, |
| 2130 | enum ustctl_socket_type *type, |
| 2131 | uint32_t *major, |
| 2132 | uint32_t *minor, |
| 2133 | uint32_t *pid, |
| 2134 | uint32_t *ppid, |
| 2135 | uint32_t *uid, |
| 2136 | uint32_t *gid, |
| 2137 | uint32_t *bits_per_long, |
| 2138 | uint32_t *uint8_t_alignment, |
| 2139 | uint32_t *uint16_t_alignment, |
| 2140 | uint32_t *uint32_t_alignment, |
| 2141 | uint32_t *uint64_t_alignment, |
| 2142 | uint32_t *long_alignment, |
| 2143 | int *byte_order, |
| 2144 | char *name) |
| 2145 | { |
| 2146 | ssize_t len; |
| 2147 | struct ustctl_reg_msg reg_msg; |
| 2148 | |
| 2149 | len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); |
| 2150 | if (len > 0 && len != sizeof(reg_msg)) |
| 2151 | return -EIO; |
| 2152 | if (len == 0) |
| 2153 | return -EPIPE; |
| 2154 | if (len < 0) |
| 2155 | return len; |
| 2156 | |
| 2157 | if (reg_msg.magic == LTTNG_UST_COMM_MAGIC) { |
| 2158 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 2159 | BIG_ENDIAN : LITTLE_ENDIAN; |
| 2160 | } else if (reg_msg.magic == bswap_32(LTTNG_UST_COMM_MAGIC)) { |
| 2161 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 2162 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 2163 | } else { |
| 2164 | return -LTTNG_UST_ERR_INVAL_MAGIC; |
| 2165 | } |
| 2166 | switch (reg_msg.socket_type) { |
| 2167 | case 0: *type = USTCTL_SOCKET_CMD; |
| 2168 | break; |
| 2169 | case 1: *type = USTCTL_SOCKET_NOTIFY; |
| 2170 | break; |
| 2171 | default: |
| 2172 | return -LTTNG_UST_ERR_INVAL_SOCKET_TYPE; |
| 2173 | } |
| 2174 | *major = reg_msg.major; |
| 2175 | *minor = reg_msg.minor; |
| 2176 | *bits_per_long = reg_msg.bits_per_long; |
| 2177 | *uint8_t_alignment = reg_msg.uint8_t_alignment; |
| 2178 | *uint16_t_alignment = reg_msg.uint16_t_alignment; |
| 2179 | *uint32_t_alignment = reg_msg.uint32_t_alignment; |
| 2180 | *uint64_t_alignment = reg_msg.uint64_t_alignment; |
| 2181 | *long_alignment = reg_msg.long_alignment; |
| 2182 | memcpy(name, reg_msg.name, LTTNG_UST_ABI_PROCNAME_LEN); |
| 2183 | if (reg_msg.major < LTTNG_UST_ABI_MAJOR_VERSION_OLDEST_COMPATIBLE || |
| 2184 | reg_msg.major > LTTNG_UST_ABI_MAJOR_VERSION) { |
| 2185 | return -LTTNG_UST_ERR_UNSUP_MAJOR; |
| 2186 | } |
| 2187 | return get_cred(sock, ®_msg, pid, ppid, uid, gid); |
| 2188 | } |
| 2189 | |
| 2190 | int ustctl_recv_notify(int sock, enum ustctl_notify_cmd *notify_cmd) |
| 2191 | { |
| 2192 | struct ustcomm_notify_hdr header; |
| 2193 | ssize_t len; |
| 2194 | |
| 2195 | len = ustcomm_recv_unix_sock(sock, &header, sizeof(header)); |
| 2196 | if (len > 0 && len != sizeof(header)) |
| 2197 | return -EIO; |
| 2198 | if (len == 0) |
| 2199 | return -EPIPE; |
| 2200 | if (len < 0) |
| 2201 | return len; |
| 2202 | switch (header.notify_cmd) { |
| 2203 | case 0: |
| 2204 | *notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 2205 | break; |
| 2206 | case 1: |
| 2207 | *notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 2208 | break; |
| 2209 | case 2: |
| 2210 | *notify_cmd = USTCTL_NOTIFY_CMD_ENUM; |
| 2211 | break; |
| 2212 | default: |
| 2213 | return -EINVAL; |
| 2214 | } |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Returns 0 on success, negative error value on error. |
| 2220 | */ |
| 2221 | int ustctl_recv_register_event(int sock, |
| 2222 | int *session_objd, |
| 2223 | int *channel_objd, |
| 2224 | char *event_name, |
| 2225 | int *loglevel, |
| 2226 | char **signature, |
| 2227 | size_t *nr_fields, |
| 2228 | struct ustctl_field **fields, |
| 2229 | char **model_emf_uri) |
| 2230 | { |
| 2231 | ssize_t len; |
| 2232 | struct ustcomm_notify_event_msg msg; |
| 2233 | size_t signature_len, fields_len, model_emf_uri_len; |
| 2234 | char *a_sign = NULL, *a_model_emf_uri = NULL; |
| 2235 | struct ustctl_field *a_fields = NULL; |
| 2236 | |
| 2237 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 2238 | if (len > 0 && len != sizeof(msg)) |
| 2239 | return -EIO; |
| 2240 | if (len == 0) |
| 2241 | return -EPIPE; |
| 2242 | if (len < 0) |
| 2243 | return len; |
| 2244 | |
| 2245 | *session_objd = msg.session_objd; |
| 2246 | *channel_objd = msg.channel_objd; |
| 2247 | strncpy(event_name, msg.event_name, LTTNG_UST_SYM_NAME_LEN); |
| 2248 | event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 2249 | *loglevel = msg.loglevel; |
| 2250 | signature_len = msg.signature_len; |
| 2251 | fields_len = msg.fields_len; |
| 2252 | |
| 2253 | if (fields_len % sizeof(*a_fields) != 0) { |
| 2254 | return -EINVAL; |
| 2255 | } |
| 2256 | |
| 2257 | model_emf_uri_len = msg.model_emf_uri_len; |
| 2258 | |
| 2259 | /* recv signature. contains at least \0. */ |
| 2260 | a_sign = zmalloc(signature_len); |
| 2261 | if (!a_sign) |
| 2262 | return -ENOMEM; |
| 2263 | len = ustcomm_recv_unix_sock(sock, a_sign, signature_len); |
| 2264 | if (len > 0 && len != signature_len) { |
| 2265 | len = -EIO; |
| 2266 | goto signature_error; |
| 2267 | } |
| 2268 | if (len == 0) { |
| 2269 | len = -EPIPE; |
| 2270 | goto signature_error; |
| 2271 | } |
| 2272 | if (len < 0) { |
| 2273 | goto signature_error; |
| 2274 | } |
| 2275 | /* Enforce end of string */ |
| 2276 | a_sign[signature_len - 1] = '\0'; |
| 2277 | |
| 2278 | /* recv fields */ |
| 2279 | if (fields_len) { |
| 2280 | a_fields = zmalloc(fields_len); |
| 2281 | if (!a_fields) { |
| 2282 | len = -ENOMEM; |
| 2283 | goto signature_error; |
| 2284 | } |
| 2285 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 2286 | if (len > 0 && len != fields_len) { |
| 2287 | len = -EIO; |
| 2288 | goto fields_error; |
| 2289 | } |
| 2290 | if (len == 0) { |
| 2291 | len = -EPIPE; |
| 2292 | goto fields_error; |
| 2293 | } |
| 2294 | if (len < 0) { |
| 2295 | goto fields_error; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | if (model_emf_uri_len) { |
| 2300 | /* recv model_emf_uri_len */ |
| 2301 | a_model_emf_uri = zmalloc(model_emf_uri_len); |
| 2302 | if (!a_model_emf_uri) { |
| 2303 | len = -ENOMEM; |
| 2304 | goto fields_error; |
| 2305 | } |
| 2306 | len = ustcomm_recv_unix_sock(sock, a_model_emf_uri, |
| 2307 | model_emf_uri_len); |
| 2308 | if (len > 0 && len != model_emf_uri_len) { |
| 2309 | len = -EIO; |
| 2310 | goto model_error; |
| 2311 | } |
| 2312 | if (len == 0) { |
| 2313 | len = -EPIPE; |
| 2314 | goto model_error; |
| 2315 | } |
| 2316 | if (len < 0) { |
| 2317 | goto model_error; |
| 2318 | } |
| 2319 | /* Enforce end of string */ |
| 2320 | a_model_emf_uri[model_emf_uri_len - 1] = '\0'; |
| 2321 | } |
| 2322 | |
| 2323 | *signature = a_sign; |
| 2324 | *nr_fields = fields_len / sizeof(*a_fields); |
| 2325 | *fields = a_fields; |
| 2326 | *model_emf_uri = a_model_emf_uri; |
| 2327 | |
| 2328 | return 0; |
| 2329 | |
| 2330 | model_error: |
| 2331 | free(a_model_emf_uri); |
| 2332 | fields_error: |
| 2333 | free(a_fields); |
| 2334 | signature_error: |
| 2335 | free(a_sign); |
| 2336 | return len; |
| 2337 | } |
| 2338 | |
| 2339 | /* |
| 2340 | * Returns 0 on success, negative error value on error. |
| 2341 | */ |
| 2342 | int ustctl_reply_register_event(int sock, |
| 2343 | uint32_t id, |
| 2344 | int ret_code) |
| 2345 | { |
| 2346 | ssize_t len; |
| 2347 | struct { |
| 2348 | struct ustcomm_notify_hdr header; |
| 2349 | struct ustcomm_notify_event_reply r; |
| 2350 | } reply; |
| 2351 | |
| 2352 | memset(&reply, 0, sizeof(reply)); |
| 2353 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_EVENT; |
| 2354 | reply.r.ret_code = ret_code; |
| 2355 | reply.r.event_id = id; |
| 2356 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 2357 | if (len > 0 && len != sizeof(reply)) |
| 2358 | return -EIO; |
| 2359 | if (len < 0) |
| 2360 | return len; |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | /* |
| 2365 | * Returns 0 on success, negative UST or system error value on error. |
| 2366 | */ |
| 2367 | int ustctl_recv_register_enum(int sock, |
| 2368 | int *session_objd, |
| 2369 | char *enum_name, |
| 2370 | struct ustctl_enum_entry **entries, |
| 2371 | size_t *nr_entries) |
| 2372 | { |
| 2373 | ssize_t len; |
| 2374 | struct ustcomm_notify_enum_msg msg; |
| 2375 | size_t entries_len; |
| 2376 | struct ustctl_enum_entry *a_entries = NULL; |
| 2377 | |
| 2378 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 2379 | if (len > 0 && len != sizeof(msg)) |
| 2380 | return -EIO; |
| 2381 | if (len == 0) |
| 2382 | return -EPIPE; |
| 2383 | if (len < 0) |
| 2384 | return len; |
| 2385 | |
| 2386 | *session_objd = msg.session_objd; |
| 2387 | strncpy(enum_name, msg.enum_name, LTTNG_UST_SYM_NAME_LEN); |
| 2388 | enum_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
| 2389 | entries_len = msg.entries_len; |
| 2390 | |
| 2391 | if (entries_len % sizeof(*a_entries) != 0) { |
| 2392 | return -EINVAL; |
| 2393 | } |
| 2394 | |
| 2395 | /* recv entries */ |
| 2396 | if (entries_len) { |
| 2397 | a_entries = zmalloc(entries_len); |
| 2398 | if (!a_entries) |
| 2399 | return -ENOMEM; |
| 2400 | len = ustcomm_recv_unix_sock(sock, a_entries, entries_len); |
| 2401 | if (len > 0 && len != entries_len) { |
| 2402 | len = -EIO; |
| 2403 | goto entries_error; |
| 2404 | } |
| 2405 | if (len == 0) { |
| 2406 | len = -EPIPE; |
| 2407 | goto entries_error; |
| 2408 | } |
| 2409 | if (len < 0) { |
| 2410 | goto entries_error; |
| 2411 | } |
| 2412 | } |
| 2413 | *nr_entries = entries_len / sizeof(*a_entries); |
| 2414 | *entries = a_entries; |
| 2415 | |
| 2416 | return 0; |
| 2417 | |
| 2418 | entries_error: |
| 2419 | free(a_entries); |
| 2420 | return len; |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * Returns 0 on success, negative error value on error. |
| 2425 | */ |
| 2426 | int ustctl_reply_register_enum(int sock, |
| 2427 | uint64_t id, |
| 2428 | int ret_code) |
| 2429 | { |
| 2430 | ssize_t len; |
| 2431 | struct { |
| 2432 | struct ustcomm_notify_hdr header; |
| 2433 | struct ustcomm_notify_enum_reply r; |
| 2434 | } reply; |
| 2435 | |
| 2436 | memset(&reply, 0, sizeof(reply)); |
| 2437 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_ENUM; |
| 2438 | reply.r.ret_code = ret_code; |
| 2439 | reply.r.enum_id = id; |
| 2440 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 2441 | if (len > 0 && len != sizeof(reply)) |
| 2442 | return -EIO; |
| 2443 | if (len < 0) |
| 2444 | return len; |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Returns 0 on success, negative UST or system error value on error. |
| 2450 | */ |
| 2451 | int ustctl_recv_register_channel(int sock, |
| 2452 | int *session_objd, /* session descriptor (output) */ |
| 2453 | int *channel_objd, /* channel descriptor (output) */ |
| 2454 | size_t *nr_fields, |
| 2455 | struct ustctl_field **fields) |
| 2456 | { |
| 2457 | ssize_t len; |
| 2458 | struct ustcomm_notify_channel_msg msg; |
| 2459 | size_t fields_len; |
| 2460 | struct ustctl_field *a_fields; |
| 2461 | |
| 2462 | len = ustcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
| 2463 | if (len > 0 && len != sizeof(msg)) |
| 2464 | return -EIO; |
| 2465 | if (len == 0) |
| 2466 | return -EPIPE; |
| 2467 | if (len < 0) |
| 2468 | return len; |
| 2469 | |
| 2470 | *session_objd = msg.session_objd; |
| 2471 | *channel_objd = msg.channel_objd; |
| 2472 | fields_len = msg.ctx_fields_len; |
| 2473 | |
| 2474 | if (fields_len % sizeof(*a_fields) != 0) { |
| 2475 | return -EINVAL; |
| 2476 | } |
| 2477 | |
| 2478 | /* recv fields */ |
| 2479 | if (fields_len) { |
| 2480 | a_fields = zmalloc(fields_len); |
| 2481 | if (!a_fields) { |
| 2482 | len = -ENOMEM; |
| 2483 | goto alloc_error; |
| 2484 | } |
| 2485 | len = ustcomm_recv_unix_sock(sock, a_fields, fields_len); |
| 2486 | if (len > 0 && len != fields_len) { |
| 2487 | len = -EIO; |
| 2488 | goto fields_error; |
| 2489 | } |
| 2490 | if (len == 0) { |
| 2491 | len = -EPIPE; |
| 2492 | goto fields_error; |
| 2493 | } |
| 2494 | if (len < 0) { |
| 2495 | goto fields_error; |
| 2496 | } |
| 2497 | *fields = a_fields; |
| 2498 | } else { |
| 2499 | *fields = NULL; |
| 2500 | } |
| 2501 | *nr_fields = fields_len / sizeof(*a_fields); |
| 2502 | return 0; |
| 2503 | |
| 2504 | fields_error: |
| 2505 | free(a_fields); |
| 2506 | alloc_error: |
| 2507 | return len; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * Returns 0 on success, negative error value on error. |
| 2512 | */ |
| 2513 | int ustctl_reply_register_channel(int sock, |
| 2514 | uint32_t chan_id, |
| 2515 | enum ustctl_channel_header header_type, |
| 2516 | int ret_code) |
| 2517 | { |
| 2518 | ssize_t len; |
| 2519 | struct { |
| 2520 | struct ustcomm_notify_hdr header; |
| 2521 | struct ustcomm_notify_channel_reply r; |
| 2522 | } reply; |
| 2523 | |
| 2524 | memset(&reply, 0, sizeof(reply)); |
| 2525 | reply.header.notify_cmd = USTCTL_NOTIFY_CMD_CHANNEL; |
| 2526 | reply.r.ret_code = ret_code; |
| 2527 | reply.r.chan_id = chan_id; |
| 2528 | switch (header_type) { |
| 2529 | case USTCTL_CHANNEL_HEADER_COMPACT: |
| 2530 | reply.r.header_type = 1; |
| 2531 | break; |
| 2532 | case USTCTL_CHANNEL_HEADER_LARGE: |
| 2533 | reply.r.header_type = 2; |
| 2534 | break; |
| 2535 | default: |
| 2536 | reply.r.header_type = 0; |
| 2537 | break; |
| 2538 | } |
| 2539 | len = ustcomm_send_unix_sock(sock, &reply, sizeof(reply)); |
| 2540 | if (len > 0 && len != sizeof(reply)) |
| 2541 | return -EIO; |
| 2542 | if (len < 0) |
| 2543 | return len; |
| 2544 | return 0; |
| 2545 | } |
| 2546 | |
| 2547 | /* Regenerate the statedump. */ |
| 2548 | int ustctl_regenerate_statedump(int sock, int handle) |
| 2549 | { |
| 2550 | struct ustcomm_ust_msg lum; |
| 2551 | struct ustcomm_ust_reply lur; |
| 2552 | int ret; |
| 2553 | |
| 2554 | memset(&lum, 0, sizeof(lum)); |
| 2555 | lum.handle = handle; |
| 2556 | lum.cmd = LTTNG_UST_SESSION_STATEDUMP; |
| 2557 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
| 2558 | if (ret) |
| 2559 | return ret; |
| 2560 | DBG("Regenerated statedump for handle %u", handle); |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | /* counter operations */ |
| 2565 | |
| 2566 | int ustctl_get_nr_cpu_per_counter(void) |
| 2567 | { |
| 2568 | return lttng_counter_num_possible_cpus(); |
| 2569 | } |
| 2570 | |
| 2571 | struct ustctl_daemon_counter * |
| 2572 | ustctl_create_counter(size_t nr_dimensions, |
| 2573 | const struct ustctl_counter_dimension *dimensions, |
| 2574 | int64_t global_sum_step, |
| 2575 | int global_counter_fd, |
| 2576 | int nr_counter_cpu_fds, |
| 2577 | const int *counter_cpu_fds, |
| 2578 | enum ustctl_counter_bitness bitness, |
| 2579 | enum ustctl_counter_arithmetic arithmetic, |
| 2580 | uint32_t alloc_flags) |
| 2581 | { |
| 2582 | const char *transport_name; |
| 2583 | struct ustctl_daemon_counter *counter; |
| 2584 | struct lttng_counter_transport *transport; |
| 2585 | struct lttng_counter_dimension ust_dim[LTTNG_COUNTER_DIMENSION_MAX]; |
| 2586 | size_t i; |
| 2587 | |
| 2588 | if (nr_dimensions > LTTNG_COUNTER_DIMENSION_MAX) |
| 2589 | return NULL; |
| 2590 | /* Currently, only per-cpu allocation is supported. */ |
| 2591 | switch (alloc_flags) { |
| 2592 | case USTCTL_COUNTER_ALLOC_PER_CPU: |
| 2593 | break; |
| 2594 | |
| 2595 | case USTCTL_COUNTER_ALLOC_PER_CPU | USTCTL_COUNTER_ALLOC_GLOBAL: |
| 2596 | case USTCTL_COUNTER_ALLOC_GLOBAL: |
| 2597 | default: |
| 2598 | return NULL; |
| 2599 | } |
| 2600 | switch (bitness) { |
| 2601 | case USTCTL_COUNTER_BITNESS_32: |
| 2602 | switch (arithmetic) { |
| 2603 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: |
| 2604 | transport_name = "counter-per-cpu-32-modular"; |
| 2605 | break; |
| 2606 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: |
| 2607 | transport_name = "counter-per-cpu-32-saturation"; |
| 2608 | break; |
| 2609 | default: |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | break; |
| 2613 | case USTCTL_COUNTER_BITNESS_64: |
| 2614 | switch (arithmetic) { |
| 2615 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: |
| 2616 | transport_name = "counter-per-cpu-64-modular"; |
| 2617 | break; |
| 2618 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: |
| 2619 | transport_name = "counter-per-cpu-64-saturation"; |
| 2620 | break; |
| 2621 | default: |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | break; |
| 2625 | default: |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | |
| 2629 | transport = lttng_counter_transport_find(transport_name); |
| 2630 | if (!transport) { |
| 2631 | DBG("LTTng transport %s not found\n", |
| 2632 | transport_name); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | |
| 2636 | counter = zmalloc(sizeof(*counter)); |
| 2637 | if (!counter) |
| 2638 | return NULL; |
| 2639 | counter->attr = zmalloc(sizeof(*counter->attr)); |
| 2640 | if (!counter->attr) |
| 2641 | goto free_counter; |
| 2642 | counter->attr->bitness = bitness; |
| 2643 | counter->attr->arithmetic = arithmetic; |
| 2644 | counter->attr->nr_dimensions = nr_dimensions; |
| 2645 | counter->attr->global_sum_step = global_sum_step; |
| 2646 | for (i = 0; i < nr_dimensions; i++) |
| 2647 | counter->attr->dimensions[i] = dimensions[i]; |
| 2648 | |
| 2649 | for (i = 0; i < nr_dimensions; i++) { |
| 2650 | ust_dim[i].size = dimensions[i].size; |
| 2651 | ust_dim[i].underflow_index = dimensions[i].underflow_index; |
| 2652 | ust_dim[i].overflow_index = dimensions[i].overflow_index; |
| 2653 | ust_dim[i].has_underflow = dimensions[i].has_underflow; |
| 2654 | ust_dim[i].has_overflow = dimensions[i].has_overflow; |
| 2655 | } |
| 2656 | counter->counter = transport->ops.counter_create(nr_dimensions, |
| 2657 | ust_dim, global_sum_step, global_counter_fd, |
| 2658 | nr_counter_cpu_fds, counter_cpu_fds, true); |
| 2659 | if (!counter->counter) |
| 2660 | goto free_attr; |
| 2661 | counter->ops = &transport->ops; |
| 2662 | return counter; |
| 2663 | |
| 2664 | free_attr: |
| 2665 | free(counter->attr); |
| 2666 | free_counter: |
| 2667 | free(counter); |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | |
| 2671 | int ustctl_create_counter_data(struct ustctl_daemon_counter *counter, |
| 2672 | struct lttng_ust_object_data **_counter_data) |
| 2673 | { |
| 2674 | struct lttng_ust_object_data *counter_data; |
| 2675 | struct lttng_ust_counter_conf counter_conf = {0}; |
| 2676 | size_t i; |
| 2677 | int ret; |
| 2678 | |
| 2679 | switch (counter->attr->arithmetic) { |
| 2680 | case USTCTL_COUNTER_ARITHMETIC_MODULAR: |
| 2681 | counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_MODULAR; |
| 2682 | break; |
| 2683 | case USTCTL_COUNTER_ARITHMETIC_SATURATION: |
| 2684 | counter_conf.arithmetic = LTTNG_UST_COUNTER_ARITHMETIC_SATURATION; |
| 2685 | break; |
| 2686 | default: |
| 2687 | return -EINVAL; |
| 2688 | } |
| 2689 | switch (counter->attr->bitness) { |
| 2690 | case USTCTL_COUNTER_BITNESS_32: |
| 2691 | counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_32; |
| 2692 | break; |
| 2693 | case USTCTL_COUNTER_BITNESS_64: |
| 2694 | counter_conf.bitness = LTTNG_UST_COUNTER_BITNESS_64; |
| 2695 | break; |
| 2696 | default: |
| 2697 | return -EINVAL; |
| 2698 | } |
| 2699 | counter_conf.number_dimensions = counter->attr->nr_dimensions; |
| 2700 | counter_conf.global_sum_step = counter->attr->global_sum_step; |
| 2701 | for (i = 0; i < counter->attr->nr_dimensions; i++) { |
| 2702 | counter_conf.dimensions[i].size = counter->attr->dimensions[i].size; |
| 2703 | counter_conf.dimensions[i].underflow_index = counter->attr->dimensions[i].underflow_index; |
| 2704 | counter_conf.dimensions[i].overflow_index = counter->attr->dimensions[i].overflow_index; |
| 2705 | counter_conf.dimensions[i].has_underflow = counter->attr->dimensions[i].has_underflow; |
| 2706 | counter_conf.dimensions[i].has_overflow = counter->attr->dimensions[i].has_overflow; |
| 2707 | } |
| 2708 | |
| 2709 | counter_data = zmalloc(sizeof(*counter_data)); |
| 2710 | if (!counter_data) { |
| 2711 | ret = -ENOMEM; |
| 2712 | goto error_alloc; |
| 2713 | } |
| 2714 | counter_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER; |
| 2715 | counter_data->handle = -1; |
| 2716 | |
| 2717 | counter_data->size = sizeof(counter_conf); |
| 2718 | counter_data->u.counter.data = zmalloc(sizeof(counter_conf)); |
| 2719 | if (!counter_data->u.counter.data) { |
| 2720 | ret = -ENOMEM; |
| 2721 | goto error_alloc_data; |
| 2722 | } |
| 2723 | |
| 2724 | memcpy(counter_data->u.counter.data, &counter_conf, sizeof(counter_conf)); |
| 2725 | *_counter_data = counter_data; |
| 2726 | |
| 2727 | return 0; |
| 2728 | |
| 2729 | error_alloc_data: |
| 2730 | free(counter_data); |
| 2731 | error_alloc: |
| 2732 | return ret; |
| 2733 | } |
| 2734 | |
| 2735 | int ustctl_create_counter_global_data(struct ustctl_daemon_counter *counter, |
| 2736 | struct lttng_ust_object_data **_counter_global_data) |
| 2737 | { |
| 2738 | struct lttng_ust_object_data *counter_global_data; |
| 2739 | int ret, fd; |
| 2740 | size_t len; |
| 2741 | |
| 2742 | if (lttng_counter_get_global_shm(counter->counter, &fd, &len)) |
| 2743 | return -EINVAL; |
| 2744 | counter_global_data = zmalloc(sizeof(*counter_global_data)); |
| 2745 | if (!counter_global_data) { |
| 2746 | ret = -ENOMEM; |
| 2747 | goto error_alloc; |
| 2748 | } |
| 2749 | counter_global_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_GLOBAL; |
| 2750 | counter_global_data->handle = -1; |
| 2751 | counter_global_data->size = len; |
| 2752 | counter_global_data->u.counter_global.shm_fd = fd; |
| 2753 | *_counter_global_data = counter_global_data; |
| 2754 | return 0; |
| 2755 | |
| 2756 | error_alloc: |
| 2757 | return ret; |
| 2758 | } |
| 2759 | |
| 2760 | int ustctl_create_counter_cpu_data(struct ustctl_daemon_counter *counter, int cpu, |
| 2761 | struct lttng_ust_object_data **_counter_cpu_data) |
| 2762 | { |
| 2763 | struct lttng_ust_object_data *counter_cpu_data; |
| 2764 | int ret, fd; |
| 2765 | size_t len; |
| 2766 | |
| 2767 | if (lttng_counter_get_cpu_shm(counter->counter, cpu, &fd, &len)) |
| 2768 | return -EINVAL; |
| 2769 | counter_cpu_data = zmalloc(sizeof(*counter_cpu_data)); |
| 2770 | if (!counter_cpu_data) { |
| 2771 | ret = -ENOMEM; |
| 2772 | goto error_alloc; |
| 2773 | } |
| 2774 | counter_cpu_data->type = LTTNG_UST_OBJECT_TYPE_COUNTER_CPU; |
| 2775 | counter_cpu_data->handle = -1; |
| 2776 | counter_cpu_data->size = len; |
| 2777 | counter_cpu_data->u.counter_cpu.shm_fd = fd; |
| 2778 | counter_cpu_data->u.counter_cpu.cpu_nr = cpu; |
| 2779 | *_counter_cpu_data = counter_cpu_data; |
| 2780 | return 0; |
| 2781 | |
| 2782 | error_alloc: |
| 2783 | return ret; |
| 2784 | } |
| 2785 | |
| 2786 | void ustctl_destroy_counter(struct ustctl_daemon_counter *counter) |
| 2787 | { |
| 2788 | counter->ops->counter_destroy(counter->counter); |
| 2789 | free(counter->attr); |
| 2790 | free(counter); |
| 2791 | } |
| 2792 | |
| 2793 | int ustctl_send_counter_data_to_ust(int sock, int parent_handle, |
| 2794 | struct lttng_ust_object_data *counter_data) |
| 2795 | { |
| 2796 | struct ustcomm_ust_msg lum; |
| 2797 | struct ustcomm_ust_reply lur; |
| 2798 | int ret; |
| 2799 | size_t size; |
| 2800 | ssize_t len; |
| 2801 | |
| 2802 | if (!counter_data) |
| 2803 | return -EINVAL; |
| 2804 | |
| 2805 | size = counter_data->size; |
| 2806 | memset(&lum, 0, sizeof(lum)); |
| 2807 | lum.handle = parent_handle; |
| 2808 | lum.cmd = LTTNG_UST_COUNTER; |
| 2809 | lum.u.counter.len = size; |
| 2810 | ret = ustcomm_send_app_msg(sock, &lum); |
| 2811 | if (ret) |
| 2812 | return ret; |
| 2813 | |
| 2814 | /* Send counter data */ |
| 2815 | len = ustcomm_send_unix_sock(sock, counter_data->u.counter.data, size); |
| 2816 | if (len != size) { |
| 2817 | if (len < 0) |
| 2818 | return len; |
| 2819 | else |
| 2820 | return -EIO; |
| 2821 | } |
| 2822 | |
| 2823 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 2824 | if (!ret) { |
| 2825 | counter_data->handle = lur.ret_val; |
| 2826 | } |
| 2827 | return ret; |
| 2828 | } |
| 2829 | |
| 2830 | int ustctl_send_counter_global_data_to_ust(int sock, |
| 2831 | struct lttng_ust_object_data *counter_data, |
| 2832 | struct lttng_ust_object_data *counter_global_data) |
| 2833 | { |
| 2834 | struct ustcomm_ust_msg lum; |
| 2835 | struct ustcomm_ust_reply lur; |
| 2836 | int ret, shm_fd[1]; |
| 2837 | size_t size; |
| 2838 | ssize_t len; |
| 2839 | |
| 2840 | if (!counter_data || !counter_global_data) |
| 2841 | return -EINVAL; |
| 2842 | |
| 2843 | size = counter_global_data->size; |
| 2844 | memset(&lum, 0, sizeof(lum)); |
| 2845 | lum.handle = counter_data->handle; /* parent handle */ |
| 2846 | lum.cmd = LTTNG_UST_COUNTER_GLOBAL; |
| 2847 | lum.u.counter_global.len = size; |
| 2848 | ret = ustcomm_send_app_msg(sock, &lum); |
| 2849 | if (ret) |
| 2850 | return ret; |
| 2851 | |
| 2852 | shm_fd[0] = counter_global_data->u.counter_global.shm_fd; |
| 2853 | len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1); |
| 2854 | if (len <= 0) { |
| 2855 | if (len < 0) |
| 2856 | return len; |
| 2857 | else |
| 2858 | return -EIO; |
| 2859 | } |
| 2860 | |
| 2861 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 2862 | if (!ret) { |
| 2863 | counter_global_data->handle = lur.ret_val; |
| 2864 | } |
| 2865 | return ret; |
| 2866 | } |
| 2867 | |
| 2868 | int ustctl_send_counter_cpu_data_to_ust(int sock, |
| 2869 | struct lttng_ust_object_data *counter_data, |
| 2870 | struct lttng_ust_object_data *counter_cpu_data) |
| 2871 | { |
| 2872 | struct ustcomm_ust_msg lum; |
| 2873 | struct ustcomm_ust_reply lur; |
| 2874 | int ret, shm_fd[1]; |
| 2875 | size_t size; |
| 2876 | ssize_t len; |
| 2877 | |
| 2878 | if (!counter_data || !counter_cpu_data) |
| 2879 | return -EINVAL; |
| 2880 | |
| 2881 | size = counter_cpu_data->size; |
| 2882 | memset(&lum, 0, sizeof(lum)); |
| 2883 | lum.handle = counter_data->handle; /* parent handle */ |
| 2884 | lum.cmd = LTTNG_UST_COUNTER_CPU; |
| 2885 | lum.u.counter_cpu.len = size; |
| 2886 | lum.u.counter_cpu.cpu_nr = counter_cpu_data->u.counter_cpu.cpu_nr; |
| 2887 | ret = ustcomm_send_app_msg(sock, &lum); |
| 2888 | if (ret) |
| 2889 | return ret; |
| 2890 | |
| 2891 | shm_fd[0] = counter_cpu_data->u.counter_global.shm_fd; |
| 2892 | len = ustcomm_send_fds_unix_sock(sock, shm_fd, 1); |
| 2893 | if (len <= 0) { |
| 2894 | if (len < 0) |
| 2895 | return len; |
| 2896 | else |
| 2897 | return -EIO; |
| 2898 | } |
| 2899 | |
| 2900 | ret = ustcomm_recv_app_reply(sock, &lur, lum.handle, lum.cmd); |
| 2901 | if (!ret) { |
| 2902 | counter_cpu_data->handle = lur.ret_val; |
| 2903 | } |
| 2904 | return ret; |
| 2905 | } |
| 2906 | |
| 2907 | int ustctl_counter_read(struct ustctl_daemon_counter *counter, |
| 2908 | const size_t *dimension_indexes, |
| 2909 | int cpu, int64_t *value, |
| 2910 | bool *overflow, bool *underflow) |
| 2911 | { |
| 2912 | return counter->ops->counter_read(counter->counter, dimension_indexes, cpu, |
| 2913 | value, overflow, underflow); |
| 2914 | } |
| 2915 | |
| 2916 | int ustctl_counter_aggregate(struct ustctl_daemon_counter *counter, |
| 2917 | const size_t *dimension_indexes, |
| 2918 | int64_t *value, |
| 2919 | bool *overflow, bool *underflow) |
| 2920 | { |
| 2921 | return counter->ops->counter_aggregate(counter->counter, dimension_indexes, |
| 2922 | value, overflow, underflow); |
| 2923 | } |
| 2924 | |
| 2925 | int ustctl_counter_clear(struct ustctl_daemon_counter *counter, |
| 2926 | const size_t *dimension_indexes) |
| 2927 | { |
| 2928 | return counter->ops->counter_clear(counter->counter, dimension_indexes); |
| 2929 | } |
| 2930 | |
| 2931 | static __attribute__((constructor)) |
| 2932 | void ustctl_init(void) |
| 2933 | { |
| 2934 | init_usterr(); |
| 2935 | lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */ |
| 2936 | lttng_ust_clock_init(); |
| 2937 | lttng_ring_buffer_metadata_client_init(); |
| 2938 | lttng_ring_buffer_client_overwrite_init(); |
| 2939 | lttng_ring_buffer_client_overwrite_rt_init(); |
| 2940 | lttng_ring_buffer_client_discard_init(); |
| 2941 | lttng_ring_buffer_client_discard_rt_init(); |
| 2942 | lttng_counter_client_percpu_32_modular_init(); |
| 2943 | lttng_counter_client_percpu_64_modular_init(); |
| 2944 | lib_ringbuffer_signal_init(); |
| 2945 | } |
| 2946 | |
| 2947 | static __attribute__((destructor)) |
| 2948 | void ustctl_exit(void) |
| 2949 | { |
| 2950 | lttng_ring_buffer_client_discard_rt_exit(); |
| 2951 | lttng_ring_buffer_client_discard_exit(); |
| 2952 | lttng_ring_buffer_client_overwrite_rt_exit(); |
| 2953 | lttng_ring_buffer_client_overwrite_exit(); |
| 2954 | lttng_ring_buffer_metadata_client_exit(); |
| 2955 | lttng_counter_client_percpu_32_modular_exit(); |
| 2956 | lttng_counter_client_percpu_64_modular_exit(); |
| 2957 | } |