| 1 | /* |
| 2 | * lttng-abi.c |
| 3 | * |
| 4 | * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng ABI |
| 7 | * |
| 8 | * Mimic system calls for: |
| 9 | * - session creation, returns a file descriptor or failure. |
| 10 | * - channel creation, returns a file descriptor or failure. |
| 11 | * - Operates on a session file descriptor |
| 12 | * - Takes all channel options as parameters. |
| 13 | * - stream get, returns a file descriptor or failure. |
| 14 | * - Operates on a channel file descriptor. |
| 15 | * - stream notifier get, returns a file descriptor or failure. |
| 16 | * - Operates on a channel file descriptor. |
| 17 | * - event creation, returns a file descriptor or failure. |
| 18 | * - Operates on a channel file descriptor |
| 19 | * - Takes an event name as parameter |
| 20 | * - Takes an instrumentation source as parameter |
| 21 | * - e.g. tracepoints, dynamic_probes... |
| 22 | * - Takes instrumentation source specific arguments. |
| 23 | * |
| 24 | * Dual LGPL v2.1/GPL v2 license. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/proc_fs.h> |
| 29 | #include <linux/anon_inodes.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 34 | #include "wrapper/ringbuffer/vfs.h" |
| 35 | #include "wrapper/poll.h" |
| 36 | #include "lttng-abi.h" |
| 37 | #include "lttng-events.h" |
| 38 | #include "lttng-tracer.h" |
| 39 | |
| 40 | /* |
| 41 | * This is LTTng's own personal way to create a system call as an external |
| 42 | * module. We use ioctl() on /proc/lttng. |
| 43 | */ |
| 44 | |
| 45 | static struct proc_dir_entry *lttng_proc_dentry; |
| 46 | static const struct file_operations lttng_fops; |
| 47 | static const struct file_operations lttng_session_fops; |
| 48 | static const struct file_operations lttng_channel_fops; |
| 49 | static const struct file_operations lttng_metadata_fops; |
| 50 | static const struct file_operations lttng_event_fops; |
| 51 | |
| 52 | /* |
| 53 | * Teardown management: opened file descriptors keep a refcount on the module, |
| 54 | * so it can only exit when all file descriptors are closed. |
| 55 | */ |
| 56 | |
| 57 | enum channel_type { |
| 58 | PER_CPU_CHANNEL, |
| 59 | METADATA_CHANNEL, |
| 60 | }; |
| 61 | |
| 62 | static |
| 63 | int lttng_abi_create_session(void) |
| 64 | { |
| 65 | struct lttng_session *session; |
| 66 | struct file *session_file; |
| 67 | int session_fd, ret; |
| 68 | |
| 69 | session = lttng_session_create(); |
| 70 | if (!session) |
| 71 | return -ENOMEM; |
| 72 | session_fd = get_unused_fd(); |
| 73 | if (session_fd < 0) { |
| 74 | ret = session_fd; |
| 75 | goto fd_error; |
| 76 | } |
| 77 | session_file = anon_inode_getfile("[lttng_session]", |
| 78 | <tng_session_fops, |
| 79 | session, O_RDWR); |
| 80 | if (IS_ERR(session_file)) { |
| 81 | ret = PTR_ERR(session_file); |
| 82 | goto file_error; |
| 83 | } |
| 84 | session->file = session_file; |
| 85 | fd_install(session_fd, session_file); |
| 86 | return session_fd; |
| 87 | |
| 88 | file_error: |
| 89 | put_unused_fd(session_fd); |
| 90 | fd_error: |
| 91 | lttng_session_destroy(session); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static |
| 96 | int lttng_abi_tracepoint_list(void) |
| 97 | { |
| 98 | struct file *tracepoint_list_file; |
| 99 | int file_fd, ret; |
| 100 | |
| 101 | file_fd = get_unused_fd(); |
| 102 | if (file_fd < 0) { |
| 103 | ret = file_fd; |
| 104 | goto fd_error; |
| 105 | } |
| 106 | |
| 107 | tracepoint_list_file = anon_inode_getfile("[lttng_session]", |
| 108 | <tng_tracepoint_list_fops, |
| 109 | NULL, O_RDWR); |
| 110 | if (IS_ERR(tracepoint_list_file)) { |
| 111 | ret = PTR_ERR(tracepoint_list_file); |
| 112 | goto file_error; |
| 113 | } |
| 114 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
| 115 | if (ret < 0) |
| 116 | goto open_error; |
| 117 | fd_install(file_fd, tracepoint_list_file); |
| 118 | if (file_fd < 0) { |
| 119 | ret = file_fd; |
| 120 | goto fd_error; |
| 121 | } |
| 122 | return file_fd; |
| 123 | |
| 124 | open_error: |
| 125 | fput(tracepoint_list_file); |
| 126 | file_error: |
| 127 | put_unused_fd(file_fd); |
| 128 | fd_error: |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static |
| 133 | long lttng_abi_tracer_version(struct file *file, |
| 134 | struct lttng_kernel_tracer_version __user *uversion_param) |
| 135 | { |
| 136 | struct lttng_kernel_tracer_version v; |
| 137 | |
| 138 | v.version = LTTNG_VERSION; |
| 139 | v.patchlevel = LTTNG_PATCHLEVEL; |
| 140 | v.sublevel = LTTNG_SUBLEVEL; |
| 141 | |
| 142 | if (copy_to_user(uversion_param, &v, sizeof(v))) |
| 143 | return -EFAULT; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static |
| 148 | long lttng_abi_add_context(struct file *file, |
| 149 | struct lttng_kernel_context __user *ucontext_param, |
| 150 | struct lttng_ctx **ctx, struct lttng_session *session) |
| 151 | { |
| 152 | struct lttng_kernel_context context_param; |
| 153 | |
| 154 | if (session->been_active) |
| 155 | return -EPERM; |
| 156 | |
| 157 | if (copy_from_user(&context_param, ucontext_param, sizeof(context_param))) |
| 158 | return -EFAULT; |
| 159 | |
| 160 | switch (context_param.ctx) { |
| 161 | case LTTNG_KERNEL_CONTEXT_PID: |
| 162 | return lttng_add_pid_to_ctx(ctx); |
| 163 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 164 | return lttng_add_prio_to_ctx(ctx); |
| 165 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 166 | return lttng_add_nice_to_ctx(ctx); |
| 167 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 168 | return lttng_add_vpid_to_ctx(ctx); |
| 169 | case LTTNG_KERNEL_CONTEXT_TID: |
| 170 | return lttng_add_tid_to_ctx(ctx); |
| 171 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 172 | return lttng_add_vtid_to_ctx(ctx); |
| 173 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 174 | return lttng_add_ppid_to_ctx(ctx); |
| 175 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 176 | return lttng_add_vppid_to_ctx(ctx); |
| 177 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
| 178 | context_param.u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 179 | return lttng_add_perf_counter_to_ctx(context_param.u.perf_counter.type, |
| 180 | context_param.u.perf_counter.config, |
| 181 | context_param.u.perf_counter.name, |
| 182 | ctx); |
| 183 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 184 | return lttng_add_procname_to_ctx(ctx); |
| 185 | default: |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * lttng_ioctl - lttng syscall through ioctl |
| 192 | * |
| 193 | * @file: the file |
| 194 | * @cmd: the command |
| 195 | * @arg: command arg |
| 196 | * |
| 197 | * This ioctl implements lttng commands: |
| 198 | * LTTNG_KERNEL_SESSION |
| 199 | * Returns a LTTng trace session file descriptor |
| 200 | * LTTNG_KERNEL_TRACER_VERSION |
| 201 | * Returns the LTTng kernel tracer version |
| 202 | * LTTNG_KERNEL_TRACEPOINT_LIST |
| 203 | * Returns a file descriptor listing available tracepoints |
| 204 | * LTTNG_KERNEL_WAIT_QUIESCENT |
| 205 | * Returns after all previously running probes have completed |
| 206 | * |
| 207 | * The returned session will be deleted when its file descriptor is closed. |
| 208 | */ |
| 209 | static |
| 210 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 211 | { |
| 212 | switch (cmd) { |
| 213 | case LTTNG_KERNEL_SESSION: |
| 214 | return lttng_abi_create_session(); |
| 215 | case LTTNG_KERNEL_TRACER_VERSION: |
| 216 | return lttng_abi_tracer_version(file, |
| 217 | (struct lttng_kernel_tracer_version __user *) arg); |
| 218 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
| 219 | return lttng_abi_tracepoint_list(); |
| 220 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
| 221 | synchronize_trace(); |
| 222 | return 0; |
| 223 | case LTTNG_KERNEL_CALIBRATE: |
| 224 | { |
| 225 | struct lttng_kernel_calibrate __user *ucalibrate = |
| 226 | (struct lttng_kernel_calibrate __user *) arg; |
| 227 | struct lttng_kernel_calibrate calibrate; |
| 228 | int ret; |
| 229 | |
| 230 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) |
| 231 | return -EFAULT; |
| 232 | ret = lttng_calibrate(&calibrate); |
| 233 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) |
| 234 | return -EFAULT; |
| 235 | return ret; |
| 236 | } |
| 237 | default: |
| 238 | return -ENOIOCTLCMD; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static const struct file_operations lttng_fops = { |
| 243 | .owner = THIS_MODULE, |
| 244 | .unlocked_ioctl = lttng_ioctl, |
| 245 | #ifdef CONFIG_COMPAT |
| 246 | .compat_ioctl = lttng_ioctl, |
| 247 | #endif |
| 248 | }; |
| 249 | |
| 250 | /* |
| 251 | * We tolerate no failure in this function (if one happens, we print a dmesg |
| 252 | * error, but cannot return any error, because the channel information is |
| 253 | * invariant. |
| 254 | */ |
| 255 | static |
| 256 | void lttng_metadata_create_events(struct file *channel_file) |
| 257 | { |
| 258 | struct lttng_channel *channel = channel_file->private_data; |
| 259 | static struct lttng_kernel_event metadata_params = { |
| 260 | .instrumentation = LTTNG_KERNEL_TRACEPOINT, |
| 261 | .name = "lttng_metadata", |
| 262 | }; |
| 263 | struct lttng_event *event; |
| 264 | |
| 265 | /* |
| 266 | * We tolerate no failure path after event creation. It will stay |
| 267 | * invariant for the rest of the session. |
| 268 | */ |
| 269 | event = lttng_event_create(channel, &metadata_params, NULL, NULL); |
| 270 | if (!event) { |
| 271 | goto create_error; |
| 272 | } |
| 273 | return; |
| 274 | |
| 275 | create_error: |
| 276 | WARN_ON(1); |
| 277 | return; /* not allowed to return error */ |
| 278 | } |
| 279 | |
| 280 | static |
| 281 | int lttng_abi_create_channel(struct file *session_file, |
| 282 | struct lttng_kernel_channel __user *uchan_param, |
| 283 | enum channel_type channel_type) |
| 284 | { |
| 285 | struct lttng_session *session = session_file->private_data; |
| 286 | const struct file_operations *fops = NULL; |
| 287 | const char *transport_name; |
| 288 | struct lttng_channel *chan; |
| 289 | struct file *chan_file; |
| 290 | struct lttng_kernel_channel chan_param; |
| 291 | int chan_fd; |
| 292 | int ret = 0; |
| 293 | |
| 294 | if (copy_from_user(&chan_param, uchan_param, sizeof(chan_param))) |
| 295 | return -EFAULT; |
| 296 | chan_fd = get_unused_fd(); |
| 297 | if (chan_fd < 0) { |
| 298 | ret = chan_fd; |
| 299 | goto fd_error; |
| 300 | } |
| 301 | switch (channel_type) { |
| 302 | case PER_CPU_CHANNEL: |
| 303 | fops = <tng_channel_fops; |
| 304 | break; |
| 305 | case METADATA_CHANNEL: |
| 306 | fops = <tng_metadata_fops; |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | chan_file = anon_inode_getfile("[lttng_channel]", |
| 311 | fops, |
| 312 | NULL, O_RDWR); |
| 313 | if (IS_ERR(chan_file)) { |
| 314 | ret = PTR_ERR(chan_file); |
| 315 | goto file_error; |
| 316 | } |
| 317 | switch (channel_type) { |
| 318 | case PER_CPU_CHANNEL: |
| 319 | if (chan_param.output == LTTNG_KERNEL_SPLICE) { |
| 320 | transport_name = chan_param.overwrite ? |
| 321 | "relay-overwrite" : "relay-discard"; |
| 322 | } else if (chan_param.output == LTTNG_KERNEL_MMAP) { |
| 323 | transport_name = chan_param.overwrite ? |
| 324 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
| 325 | } else { |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | break; |
| 329 | case METADATA_CHANNEL: |
| 330 | if (chan_param.output == LTTNG_KERNEL_SPLICE) |
| 331 | transport_name = "relay-metadata"; |
| 332 | else if (chan_param.output == LTTNG_KERNEL_MMAP) |
| 333 | transport_name = "relay-metadata-mmap"; |
| 334 | else |
| 335 | return -EINVAL; |
| 336 | break; |
| 337 | default: |
| 338 | transport_name = "<unknown>"; |
| 339 | break; |
| 340 | } |
| 341 | /* |
| 342 | * We tolerate no failure path after channel creation. It will stay |
| 343 | * invariant for the rest of the session. |
| 344 | */ |
| 345 | chan = lttng_channel_create(session, transport_name, NULL, |
| 346 | chan_param.subbuf_size, |
| 347 | chan_param.num_subbuf, |
| 348 | chan_param.switch_timer_interval, |
| 349 | chan_param.read_timer_interval); |
| 350 | if (!chan) { |
| 351 | ret = -EINVAL; |
| 352 | goto chan_error; |
| 353 | } |
| 354 | chan->file = chan_file; |
| 355 | chan_file->private_data = chan; |
| 356 | fd_install(chan_fd, chan_file); |
| 357 | if (channel_type == METADATA_CHANNEL) { |
| 358 | session->metadata = chan; |
| 359 | lttng_metadata_create_events(chan_file); |
| 360 | } |
| 361 | |
| 362 | /* The channel created holds a reference on the session */ |
| 363 | atomic_long_inc(&session_file->f_count); |
| 364 | |
| 365 | return chan_fd; |
| 366 | |
| 367 | chan_error: |
| 368 | fput(chan_file); |
| 369 | file_error: |
| 370 | put_unused_fd(chan_fd); |
| 371 | fd_error: |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * lttng_session_ioctl - lttng session fd ioctl |
| 377 | * |
| 378 | * @file: the file |
| 379 | * @cmd: the command |
| 380 | * @arg: command arg |
| 381 | * |
| 382 | * This ioctl implements lttng commands: |
| 383 | * LTTNG_KERNEL_CHANNEL |
| 384 | * Returns a LTTng channel file descriptor |
| 385 | * LTTNG_KERNEL_ENABLE |
| 386 | * Enables tracing for a session (weak enable) |
| 387 | * LTTNG_KERNEL_DISABLE |
| 388 | * Disables tracing for a session (strong disable) |
| 389 | * LTTNG_KERNEL_METADATA |
| 390 | * Returns a LTTng metadata file descriptor |
| 391 | * |
| 392 | * The returned channel will be deleted when its file descriptor is closed. |
| 393 | */ |
| 394 | static |
| 395 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 396 | { |
| 397 | struct lttng_session *session = file->private_data; |
| 398 | |
| 399 | switch (cmd) { |
| 400 | case LTTNG_KERNEL_CHANNEL: |
| 401 | return lttng_abi_create_channel(file, |
| 402 | (struct lttng_kernel_channel __user *) arg, |
| 403 | PER_CPU_CHANNEL); |
| 404 | case LTTNG_KERNEL_SESSION_START: |
| 405 | case LTTNG_KERNEL_ENABLE: |
| 406 | return lttng_session_enable(session); |
| 407 | case LTTNG_KERNEL_SESSION_STOP: |
| 408 | case LTTNG_KERNEL_DISABLE: |
| 409 | return lttng_session_disable(session); |
| 410 | case LTTNG_KERNEL_METADATA: |
| 411 | return lttng_abi_create_channel(file, |
| 412 | (struct lttng_kernel_channel __user *) arg, |
| 413 | METADATA_CHANNEL); |
| 414 | default: |
| 415 | return -ENOIOCTLCMD; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Called when the last file reference is dropped. |
| 421 | * |
| 422 | * Big fat note: channels and events are invariant for the whole session after |
| 423 | * their creation. So this session destruction also destroys all channel and |
| 424 | * event structures specific to this session (they are not destroyed when their |
| 425 | * individual file is released). |
| 426 | */ |
| 427 | static |
| 428 | int lttng_session_release(struct inode *inode, struct file *file) |
| 429 | { |
| 430 | struct lttng_session *session = file->private_data; |
| 431 | |
| 432 | if (session) |
| 433 | lttng_session_destroy(session); |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static const struct file_operations lttng_session_fops = { |
| 438 | .owner = THIS_MODULE, |
| 439 | .release = lttng_session_release, |
| 440 | .unlocked_ioctl = lttng_session_ioctl, |
| 441 | #ifdef CONFIG_COMPAT |
| 442 | .compat_ioctl = lttng_session_ioctl, |
| 443 | #endif |
| 444 | }; |
| 445 | |
| 446 | static |
| 447 | int lttng_abi_open_stream(struct file *channel_file) |
| 448 | { |
| 449 | struct lttng_channel *channel = channel_file->private_data; |
| 450 | struct lib_ring_buffer *buf; |
| 451 | int stream_fd, ret; |
| 452 | struct file *stream_file; |
| 453 | |
| 454 | buf = channel->ops->buffer_read_open(channel->chan); |
| 455 | if (!buf) |
| 456 | return -ENOENT; |
| 457 | |
| 458 | stream_fd = get_unused_fd(); |
| 459 | if (stream_fd < 0) { |
| 460 | ret = stream_fd; |
| 461 | goto fd_error; |
| 462 | } |
| 463 | stream_file = anon_inode_getfile("[lttng_stream]", |
| 464 | &lib_ring_buffer_file_operations, |
| 465 | buf, O_RDWR); |
| 466 | if (IS_ERR(stream_file)) { |
| 467 | ret = PTR_ERR(stream_file); |
| 468 | goto file_error; |
| 469 | } |
| 470 | /* |
| 471 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor |
| 472 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this |
| 473 | * file descriptor, so we set FMODE_PREAD here. |
| 474 | */ |
| 475 | stream_file->f_mode |= FMODE_PREAD; |
| 476 | fd_install(stream_fd, stream_file); |
| 477 | /* |
| 478 | * The stream holds a reference to the channel within the generic ring |
| 479 | * buffer library, so no need to hold a refcount on the channel and |
| 480 | * session files here. |
| 481 | */ |
| 482 | return stream_fd; |
| 483 | |
| 484 | file_error: |
| 485 | put_unused_fd(stream_fd); |
| 486 | fd_error: |
| 487 | channel->ops->buffer_read_close(buf); |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | static |
| 492 | int lttng_abi_create_event(struct file *channel_file, |
| 493 | struct lttng_kernel_event __user *uevent_param) |
| 494 | { |
| 495 | struct lttng_channel *channel = channel_file->private_data; |
| 496 | struct lttng_event *event; |
| 497 | struct lttng_kernel_event event_param; |
| 498 | int event_fd, ret; |
| 499 | struct file *event_file; |
| 500 | |
| 501 | if (copy_from_user(&event_param, uevent_param, sizeof(event_param))) |
| 502 | return -EFAULT; |
| 503 | event_param.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 504 | switch (event_param.instrumentation) { |
| 505 | case LTTNG_KERNEL_KRETPROBE: |
| 506 | event_param.u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 507 | break; |
| 508 | case LTTNG_KERNEL_KPROBE: |
| 509 | event_param.u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 510 | break; |
| 511 | case LTTNG_KERNEL_FUNCTION: |
| 512 | event_param.u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
| 513 | break; |
| 514 | default: |
| 515 | break; |
| 516 | } |
| 517 | switch (event_param.instrumentation) { |
| 518 | default: |
| 519 | event_fd = get_unused_fd(); |
| 520 | if (event_fd < 0) { |
| 521 | ret = event_fd; |
| 522 | goto fd_error; |
| 523 | } |
| 524 | event_file = anon_inode_getfile("[lttng_event]", |
| 525 | <tng_event_fops, |
| 526 | NULL, O_RDWR); |
| 527 | if (IS_ERR(event_file)) { |
| 528 | ret = PTR_ERR(event_file); |
| 529 | goto file_error; |
| 530 | } |
| 531 | /* |
| 532 | * We tolerate no failure path after event creation. It |
| 533 | * will stay invariant for the rest of the session. |
| 534 | */ |
| 535 | event = lttng_event_create(channel, &event_param, NULL, NULL); |
| 536 | if (!event) { |
| 537 | ret = -EINVAL; |
| 538 | goto event_error; |
| 539 | } |
| 540 | event_file->private_data = event; |
| 541 | fd_install(event_fd, event_file); |
| 542 | /* The event holds a reference on the channel */ |
| 543 | atomic_long_inc(&channel_file->f_count); |
| 544 | break; |
| 545 | case LTTNG_KERNEL_SYSCALL: |
| 546 | /* |
| 547 | * Only all-syscall tracing supported for now. |
| 548 | */ |
| 549 | if (event_param.name[0] != '\0') |
| 550 | return -EINVAL; |
| 551 | ret = lttng_syscalls_register(channel, NULL); |
| 552 | if (ret) |
| 553 | goto fd_error; |
| 554 | event_fd = 0; |
| 555 | break; |
| 556 | } |
| 557 | return event_fd; |
| 558 | |
| 559 | event_error: |
| 560 | fput(event_file); |
| 561 | file_error: |
| 562 | put_unused_fd(event_fd); |
| 563 | fd_error: |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * lttng_channel_ioctl - lttng syscall through ioctl |
| 569 | * |
| 570 | * @file: the file |
| 571 | * @cmd: the command |
| 572 | * @arg: command arg |
| 573 | * |
| 574 | * This ioctl implements lttng commands: |
| 575 | * LTTNG_KERNEL_STREAM |
| 576 | * Returns an event stream file descriptor or failure. |
| 577 | * (typically, one event stream records events from one CPU) |
| 578 | * LTTNG_KERNEL_EVENT |
| 579 | * Returns an event file descriptor or failure. |
| 580 | * LTTNG_KERNEL_CONTEXT |
| 581 | * Prepend a context field to each event in the channel |
| 582 | * LTTNG_KERNEL_ENABLE |
| 583 | * Enable recording for events in this channel (weak enable) |
| 584 | * LTTNG_KERNEL_DISABLE |
| 585 | * Disable recording for events in this channel (strong disable) |
| 586 | * |
| 587 | * Channel and event file descriptors also hold a reference on the session. |
| 588 | */ |
| 589 | static |
| 590 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 591 | { |
| 592 | struct lttng_channel *channel = file->private_data; |
| 593 | |
| 594 | switch (cmd) { |
| 595 | case LTTNG_KERNEL_STREAM: |
| 596 | return lttng_abi_open_stream(file); |
| 597 | case LTTNG_KERNEL_EVENT: |
| 598 | return lttng_abi_create_event(file, (struct lttng_kernel_event __user *) arg); |
| 599 | case LTTNG_KERNEL_CONTEXT: |
| 600 | return lttng_abi_add_context(file, |
| 601 | (struct lttng_kernel_context __user *) arg, |
| 602 | &channel->ctx, channel->session); |
| 603 | case LTTNG_KERNEL_ENABLE: |
| 604 | return lttng_channel_enable(channel); |
| 605 | case LTTNG_KERNEL_DISABLE: |
| 606 | return lttng_channel_disable(channel); |
| 607 | default: |
| 608 | return -ENOIOCTLCMD; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * lttng_metadata_ioctl - lttng syscall through ioctl |
| 614 | * |
| 615 | * @file: the file |
| 616 | * @cmd: the command |
| 617 | * @arg: command arg |
| 618 | * |
| 619 | * This ioctl implements lttng commands: |
| 620 | * LTTNG_KERNEL_STREAM |
| 621 | * Returns an event stream file descriptor or failure. |
| 622 | * |
| 623 | * Channel and event file descriptors also hold a reference on the session. |
| 624 | */ |
| 625 | static |
| 626 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 627 | { |
| 628 | switch (cmd) { |
| 629 | case LTTNG_KERNEL_STREAM: |
| 630 | return lttng_abi_open_stream(file); |
| 631 | default: |
| 632 | return -ENOIOCTLCMD; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * lttng_channel_poll - lttng stream addition/removal monitoring |
| 638 | * |
| 639 | * @file: the file |
| 640 | * @wait: poll table |
| 641 | */ |
| 642 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
| 643 | { |
| 644 | struct lttng_channel *channel = file->private_data; |
| 645 | unsigned int mask = 0; |
| 646 | |
| 647 | if (file->f_mode & FMODE_READ) { |
| 648 | poll_wait_set_exclusive(wait); |
| 649 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
| 650 | wait); |
| 651 | |
| 652 | if (channel->ops->is_disabled(channel->chan)) |
| 653 | return POLLERR; |
| 654 | if (channel->ops->is_finalized(channel->chan)) |
| 655 | return POLLHUP; |
| 656 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
| 657 | return POLLIN | POLLRDNORM; |
| 658 | return 0; |
| 659 | } |
| 660 | return mask; |
| 661 | |
| 662 | } |
| 663 | |
| 664 | static |
| 665 | int lttng_channel_release(struct inode *inode, struct file *file) |
| 666 | { |
| 667 | struct lttng_channel *channel = file->private_data; |
| 668 | |
| 669 | if (channel) |
| 670 | fput(channel->session->file); |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static const struct file_operations lttng_channel_fops = { |
| 675 | .owner = THIS_MODULE, |
| 676 | .release = lttng_channel_release, |
| 677 | .poll = lttng_channel_poll, |
| 678 | .unlocked_ioctl = lttng_channel_ioctl, |
| 679 | #ifdef CONFIG_COMPAT |
| 680 | .compat_ioctl = lttng_channel_ioctl, |
| 681 | #endif |
| 682 | }; |
| 683 | |
| 684 | static const struct file_operations lttng_metadata_fops = { |
| 685 | .owner = THIS_MODULE, |
| 686 | .release = lttng_channel_release, |
| 687 | .unlocked_ioctl = lttng_metadata_ioctl, |
| 688 | #ifdef CONFIG_COMPAT |
| 689 | .compat_ioctl = lttng_metadata_ioctl, |
| 690 | #endif |
| 691 | }; |
| 692 | |
| 693 | /** |
| 694 | * lttng_event_ioctl - lttng syscall through ioctl |
| 695 | * |
| 696 | * @file: the file |
| 697 | * @cmd: the command |
| 698 | * @arg: command arg |
| 699 | * |
| 700 | * This ioctl implements lttng commands: |
| 701 | * LTTNG_KERNEL_CONTEXT |
| 702 | * Prepend a context field to each record of this event |
| 703 | * LTTNG_KERNEL_ENABLE |
| 704 | * Enable recording for this event (weak enable) |
| 705 | * LTTNG_KERNEL_DISABLE |
| 706 | * Disable recording for this event (strong disable) |
| 707 | */ |
| 708 | static |
| 709 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 710 | { |
| 711 | struct lttng_event *event = file->private_data; |
| 712 | |
| 713 | switch (cmd) { |
| 714 | case LTTNG_KERNEL_CONTEXT: |
| 715 | return lttng_abi_add_context(file, |
| 716 | (struct lttng_kernel_context __user *) arg, |
| 717 | &event->ctx, event->chan->session); |
| 718 | case LTTNG_KERNEL_ENABLE: |
| 719 | return lttng_event_enable(event); |
| 720 | case LTTNG_KERNEL_DISABLE: |
| 721 | return lttng_event_disable(event); |
| 722 | default: |
| 723 | return -ENOIOCTLCMD; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static |
| 728 | int lttng_event_release(struct inode *inode, struct file *file) |
| 729 | { |
| 730 | struct lttng_event *event = file->private_data; |
| 731 | |
| 732 | if (event) |
| 733 | fput(event->chan->file); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | /* TODO: filter control ioctl */ |
| 738 | static const struct file_operations lttng_event_fops = { |
| 739 | .owner = THIS_MODULE, |
| 740 | .release = lttng_event_release, |
| 741 | .unlocked_ioctl = lttng_event_ioctl, |
| 742 | #ifdef CONFIG_COMPAT |
| 743 | .compat_ioctl = lttng_event_ioctl, |
| 744 | #endif |
| 745 | }; |
| 746 | |
| 747 | int __init lttng_abi_init(void) |
| 748 | { |
| 749 | int ret = 0; |
| 750 | |
| 751 | wrapper_vmalloc_sync_all(); |
| 752 | lttng_proc_dentry = proc_create_data("lttng", S_IWUSR, NULL, |
| 753 | <tng_fops, NULL); |
| 754 | |
| 755 | if (!lttng_proc_dentry) { |
| 756 | printk(KERN_ERR "Error creating LTTng control file\n"); |
| 757 | ret = -ENOMEM; |
| 758 | goto error; |
| 759 | } |
| 760 | error: |
| 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | void __exit lttng_abi_exit(void) |
| 765 | { |
| 766 | if (lttng_proc_dentry) |
| 767 | remove_proc_entry("lttng", NULL); |
| 768 | } |