| 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 | #define _GNU_SOURCE |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <common/sessiond-comm/sessiond-comm.h> |
| 23 | #include <common/lttngerr.h> |
| 24 | #include <common/lttng-share.h> |
| 25 | |
| 26 | #include "channel.h" |
| 27 | #include "kernel.h" |
| 28 | #include "ust-ctl.h" |
| 29 | #include "utils.h" |
| 30 | #include "ust-app.h" |
| 31 | |
| 32 | /* |
| 33 | * Return allocated channel attributes. |
| 34 | */ |
| 35 | struct lttng_channel *channel_new_default_attr(int dom) |
| 36 | { |
| 37 | struct lttng_channel *chan; |
| 38 | |
| 39 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 40 | if (chan == NULL) { |
| 41 | PERROR("zmalloc channel init"); |
| 42 | goto error_alloc; |
| 43 | } |
| 44 | |
| 45 | if (snprintf(chan->name, sizeof(chan->name), "%s", |
| 46 | DEFAULT_CHANNEL_NAME) < 0) { |
| 47 | PERROR("snprintf default channel name"); |
| 48 | goto error; |
| 49 | } |
| 50 | |
| 51 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 52 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 53 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 54 | |
| 55 | switch (dom) { |
| 56 | case LTTNG_DOMAIN_KERNEL: |
| 57 | chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; |
| 58 | chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 59 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 60 | break; |
| 61 | case LTTNG_DOMAIN_UST: |
| 62 | case LTTNG_DOMAIN_UST_PID: |
| 63 | chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; |
| 64 | chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; |
| 65 | chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT; |
| 66 | break; |
| 67 | default: |
| 68 | goto error; /* Not implemented */ |
| 69 | } |
| 70 | |
| 71 | return chan; |
| 72 | |
| 73 | error: |
| 74 | free(chan); |
| 75 | error_alloc: |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Disable kernel channel of the kernel session. |
| 81 | */ |
| 82 | int channel_kernel_disable(struct ltt_kernel_session *ksession, |
| 83 | char *channel_name) |
| 84 | { |
| 85 | int ret; |
| 86 | struct ltt_kernel_channel *kchan; |
| 87 | |
| 88 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 89 | if (kchan == NULL) { |
| 90 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 91 | goto error; |
| 92 | } else if (kchan->enabled == 1) { |
| 93 | ret = kernel_disable_channel(kchan); |
| 94 | if (ret < 0 && ret != -EEXIST) { |
| 95 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; |
| 96 | goto error; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ret = LTTCOMM_OK; |
| 101 | |
| 102 | error: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Enable kernel channel of the kernel session. |
| 108 | */ |
| 109 | int channel_kernel_enable(struct ltt_kernel_session *ksession, |
| 110 | struct ltt_kernel_channel *kchan) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | if (kchan->enabled == 0) { |
| 115 | ret = kernel_enable_channel(kchan); |
| 116 | if (ret < 0) { |
| 117 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; |
| 118 | goto error; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | ret = LTTCOMM_OK; |
| 123 | |
| 124 | error: |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Create kernel channel of the kernel session and notify kernel thread. |
| 130 | */ |
| 131 | int channel_kernel_create(struct ltt_kernel_session *ksession, |
| 132 | struct lttng_channel *attr, int kernel_pipe) |
| 133 | { |
| 134 | int ret; |
| 135 | struct lttng_channel *defattr = NULL; |
| 136 | |
| 137 | /* Creating channel attributes if needed */ |
| 138 | if (attr == NULL) { |
| 139 | defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL); |
| 140 | if (defattr == NULL) { |
| 141 | ret = LTTCOMM_FATAL; |
| 142 | goto error; |
| 143 | } |
| 144 | attr = defattr; |
| 145 | } |
| 146 | |
| 147 | /* Channel not found, creating it */ |
| 148 | ret = kernel_create_channel(ksession, attr, ksession->trace_path); |
| 149 | if (ret < 0) { |
| 150 | ret = LTTCOMM_KERN_CHAN_FAIL; |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | /* Notify kernel thread that there is a new channel */ |
| 155 | ret = notify_thread_pipe(kernel_pipe); |
| 156 | if (ret < 0) { |
| 157 | ret = LTTCOMM_FATAL; |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | ret = LTTCOMM_OK; |
| 162 | error: |
| 163 | free(defattr); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Enable UST channel for session and domain. |
| 169 | */ |
| 170 | int channel_ust_enable(struct ltt_ust_session *usess, int domain, |
| 171 | struct ltt_ust_channel *uchan) |
| 172 | { |
| 173 | int ret = LTTCOMM_OK; |
| 174 | |
| 175 | /* If already enabled, everything is OK */ |
| 176 | if (uchan->enabled) { |
| 177 | DBG3("Channel %s already enabled. Skipping", uchan->name); |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | switch (domain) { |
| 182 | case LTTNG_DOMAIN_UST: |
| 183 | DBG2("Channel %s being enabled in UST global domain", uchan->name); |
| 184 | /* Enable channel for global domain */ |
| 185 | ret = ust_app_enable_channel_glb(usess, uchan); |
| 186 | break; |
| 187 | case LTTNG_DOMAIN_UST_PID: |
| 188 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 189 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 190 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | if (ret < 0) { |
| 195 | if (ret != -EEXIST) { |
| 196 | ret = LTTCOMM_UST_CHAN_ENABLE_FAIL; |
| 197 | goto error; |
| 198 | } else { |
| 199 | ret = LTTCOMM_OK; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | uchan->enabled = 1; |
| 204 | DBG2("Channel %s enabled successfully", uchan->name); |
| 205 | |
| 206 | end: |
| 207 | error: |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Create UST channel for session and domain. |
| 213 | */ |
| 214 | int channel_ust_create(struct ltt_ust_session *usess, int domain, |
| 215 | struct lttng_channel *attr) |
| 216 | { |
| 217 | int ret = LTTCOMM_OK; |
| 218 | struct ltt_ust_channel *uchan = NULL; |
| 219 | struct lttng_channel *defattr = NULL; |
| 220 | |
| 221 | /* Creating channel attributes if needed */ |
| 222 | if (attr == NULL) { |
| 223 | defattr = channel_new_default_attr(domain); |
| 224 | if (defattr == NULL) { |
| 225 | ret = LTTCOMM_FATAL; |
| 226 | goto error; |
| 227 | } |
| 228 | attr = defattr; |
| 229 | } |
| 230 | |
| 231 | /* Create UST channel */ |
| 232 | uchan = trace_ust_create_channel(attr, usess->pathname); |
| 233 | if (uchan == NULL) { |
| 234 | ret = LTTCOMM_FATAL; |
| 235 | goto error; |
| 236 | } |
| 237 | uchan->enabled = 1; |
| 238 | |
| 239 | switch (domain) { |
| 240 | case LTTNG_DOMAIN_UST: |
| 241 | DBG2("Channel %s being created in UST global domain", uchan->name); |
| 242 | |
| 243 | /* Enable channel for global domain */ |
| 244 | ret = ust_app_create_channel_glb(usess, uchan); |
| 245 | break; |
| 246 | case LTTNG_DOMAIN_UST_PID: |
| 247 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 248 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 249 | default: |
| 250 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 251 | goto error_free_chan; |
| 252 | } |
| 253 | |
| 254 | if (ret < 0 && ret != -EEXIST) { |
| 255 | ret = LTTCOMM_UST_CHAN_ENABLE_FAIL; |
| 256 | goto error_free_chan; |
| 257 | } |
| 258 | |
| 259 | /* Adding the channel to the channel hash table. */ |
| 260 | rcu_read_lock(); |
| 261 | lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node); |
| 262 | rcu_read_unlock(); |
| 263 | |
| 264 | DBG2("Channel %s created successfully", uchan->name); |
| 265 | |
| 266 | free(defattr); |
| 267 | return LTTCOMM_OK; |
| 268 | |
| 269 | error_free_chan: |
| 270 | /* |
| 271 | * No need to remove the channel from the hash table because at this point |
| 272 | * it was not added hence the direct call and no call_rcu(). |
| 273 | */ |
| 274 | trace_ust_destroy_channel(uchan); |
| 275 | error: |
| 276 | free(defattr); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Disable UST channel for session and domain. |
| 282 | */ |
| 283 | int channel_ust_disable(struct ltt_ust_session *usess, int domain, |
| 284 | struct ltt_ust_channel *uchan) |
| 285 | { |
| 286 | int ret = LTTCOMM_OK; |
| 287 | |
| 288 | /* Already disabled */ |
| 289 | if (uchan->enabled == 0) { |
| 290 | DBG2("Channel UST %s already disabled", uchan->name); |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | /* Get the right channel's hashtable */ |
| 295 | switch (domain) { |
| 296 | case LTTNG_DOMAIN_UST: |
| 297 | DBG2("Channel %s being disabled in UST global domain", uchan->name); |
| 298 | /* Disable channel for global domain */ |
| 299 | ret = ust_app_disable_channel_glb(usess, uchan); |
| 300 | break; |
| 301 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 302 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 303 | case LTTNG_DOMAIN_UST_PID: |
| 304 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | if (ret < 0 && ret != -EEXIST) { |
| 309 | ret = LTTCOMM_UST_DISABLE_FAIL; |
| 310 | goto error; |
| 311 | } |
| 312 | |
| 313 | uchan->enabled = 0; |
| 314 | |
| 315 | DBG2("Channel %s disabled successfully", uchan->name); |
| 316 | |
| 317 | return LTTCOMM_OK; |
| 318 | |
| 319 | end: |
| 320 | error: |
| 321 | return ret; |
| 322 | } |