| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; only version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | */ |
| 17 | |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <lttng/lttng.h> |
| 21 | #include <lttng-sessiond-comm.h> |
| 22 | #include <lttngerr.h> |
| 23 | |
| 24 | #include "channel.h" |
| 25 | #include "kernel-ctl.h" |
| 26 | #include "utils.h" |
| 27 | |
| 28 | /* |
| 29 | * Return allocated channel attributes. |
| 30 | */ |
| 31 | static struct lttng_channel *init_default_attr(int dom, char *name) |
| 32 | { |
| 33 | struct lttng_channel *chan; |
| 34 | |
| 35 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 36 | if (chan == NULL) { |
| 37 | perror("malloc channel init"); |
| 38 | goto error_alloc; |
| 39 | } |
| 40 | |
| 41 | if (snprintf(chan->name, sizeof(chan->name), "%s", name) < 0) { |
| 42 | perror("snprintf channel name"); |
| 43 | goto error; |
| 44 | } |
| 45 | |
| 46 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 47 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 48 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 49 | |
| 50 | switch (dom) { |
| 51 | case LTTNG_DOMAIN_KERNEL: |
| 52 | chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; |
| 53 | chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 54 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 55 | break; |
| 56 | /* TODO: add UST */ |
| 57 | default: |
| 58 | goto error; /* Not implemented */ |
| 59 | } |
| 60 | |
| 61 | return chan; |
| 62 | |
| 63 | error: |
| 64 | free(chan); |
| 65 | error_alloc: |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Disable kernel channel of the kernel session. |
| 71 | */ |
| 72 | int channel_kernel_disable(struct ltt_kernel_session *ksession, |
| 73 | char *channel_name) |
| 74 | { |
| 75 | int ret; |
| 76 | struct ltt_kernel_channel *kchan; |
| 77 | |
| 78 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 79 | if (kchan == NULL) { |
| 80 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 81 | goto error; |
| 82 | } else if (kchan->enabled == 1) { |
| 83 | ret = kernel_disable_channel(kchan); |
| 84 | if (ret < 0) { |
| 85 | if (ret != EEXIST) { |
| 86 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; |
| 87 | } |
| 88 | goto error; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ret = LTTCOMM_OK; |
| 93 | |
| 94 | error: |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Enable kernel channel of the kernel session. |
| 100 | */ |
| 101 | int channel_kernel_enable(struct ltt_kernel_session *ksession, |
| 102 | struct ltt_kernel_channel *kchan) |
| 103 | { |
| 104 | int ret; |
| 105 | |
| 106 | if (kchan->enabled == 0) { |
| 107 | ret = kernel_enable_channel(kchan); |
| 108 | if (ret < 0) { |
| 109 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; |
| 110 | goto error; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | ret = LTTCOMM_OK; |
| 115 | |
| 116 | error: |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Create kernel channel of the kernel session and notify kernel thread. |
| 122 | */ |
| 123 | int channel_kernel_create(struct ltt_kernel_session *ksession, |
| 124 | char *channel_name, struct lttng_channel *chan, int kernel_pipe) |
| 125 | { |
| 126 | int ret; |
| 127 | struct lttng_channel *attr = chan; |
| 128 | |
| 129 | /* Creating channel attributes if needed */ |
| 130 | if (attr == NULL) { |
| 131 | attr = init_default_attr(LTTNG_DOMAIN_KERNEL, channel_name); |
| 132 | if (attr == NULL) { |
| 133 | ret = LTTCOMM_FATAL; |
| 134 | goto error; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Channel not found, creating it */ |
| 139 | ret = kernel_create_channel(ksession, attr, ksession->trace_path); |
| 140 | if (ret < 0) { |
| 141 | ret = LTTCOMM_KERN_CHAN_FAIL; |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | /* Notify kernel thread that there is a new channel */ |
| 146 | ret = notify_thread_pipe(kernel_pipe); |
| 147 | if (ret < 0) { |
| 148 | ret = LTTCOMM_FATAL; |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | ret = LTTCOMM_OK; |
| 153 | |
| 154 | error: |
| 155 | return ret; |
| 156 | } |