| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 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 |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <lttng-sessiond-comm.h> |
| 26 | #include <lttngerr.h> |
| 27 | |
| 28 | #include "ust-comm.h" |
| 29 | #include "ust-ctl.h" |
| 30 | #include "../hashtable/hash.h" |
| 31 | |
| 32 | /* |
| 33 | * Init command for tracer with cmd type and correct handle. |
| 34 | */ |
| 35 | static void init_command(int cmd, int handle, struct lttcomm_ust_msg *command) |
| 36 | { |
| 37 | memset(command, 0, sizeof(struct lttcomm_ust_msg)); |
| 38 | |
| 39 | command->cmd = cmd; |
| 40 | command->handle = handle; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Generic send command to ust tracer. Caller must free reply. |
| 45 | */ |
| 46 | static struct lttcomm_ust_reply *send_command(int sock, |
| 47 | struct lttcomm_ust_msg *command) |
| 48 | { |
| 49 | struct lttcomm_ust_reply *reply; |
| 50 | |
| 51 | reply = ustcomm_send_command(sock, command); |
| 52 | if (reply == NULL) { |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | if (reply->ret_code != LTTCOMM_OK) { |
| 57 | ERR("Return code (%d): %s", reply->ret_code, |
| 58 | lttcomm_get_readable_code(reply->ret_code)); |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | return reply; |
| 63 | |
| 64 | error: |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Send registration done packet to the application. |
| 70 | */ |
| 71 | int ustctl_register_done(int sock) |
| 72 | { |
| 73 | struct lttcomm_ust_msg command; |
| 74 | struct lttcomm_ust_reply *reply; |
| 75 | |
| 76 | DBG("Sending register done command to %d", sock); |
| 77 | |
| 78 | command.cmd = LTTNG_UST_REGISTER_DONE; |
| 79 | command.handle = LTTNG_UST_ROOT_HANDLE; |
| 80 | |
| 81 | reply = ustcomm_send_command(sock, &command); |
| 82 | if (reply == NULL) { |
| 83 | goto error; |
| 84 | } |
| 85 | |
| 86 | if (reply->ret_code != LTTCOMM_OK) { |
| 87 | DBG("Return code: %s", lttcomm_get_readable_code(reply->ret_code)); |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | |
| 93 | error: |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Create an UST session on the tracer. |
| 99 | * |
| 100 | * Return handle if success else negative value. |
| 101 | */ |
| 102 | int ustctl_create_session(int sock, struct ltt_ust_session *session) |
| 103 | { |
| 104 | int ret; |
| 105 | struct lttcomm_ust_msg command; |
| 106 | struct lttcomm_ust_reply *reply = NULL; |
| 107 | |
| 108 | command.cmd = LTTNG_UST_SESSION; |
| 109 | command.handle = LTTNG_UST_ROOT_HANDLE; |
| 110 | |
| 111 | reply = ustcomm_send_command(sock, &command); |
| 112 | if (reply == NULL) { |
| 113 | goto error; |
| 114 | } |
| 115 | |
| 116 | /* Save session handle */ |
| 117 | ret = reply->ret_val; |
| 118 | free(reply); |
| 119 | |
| 120 | DBG2("ustctl create session command successful with handle %d", ret); |
| 121 | |
| 122 | return ret; |
| 123 | |
| 124 | error: |
| 125 | free(reply); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Create UST channel to the tracer. |
| 131 | * |
| 132 | * Return handle if success else negative value. |
| 133 | */ |
| 134 | int ustctl_create_channel(int sock, struct ltt_ust_session *session, |
| 135 | struct lttng_ust_channel *channel) |
| 136 | { |
| 137 | int ret; |
| 138 | struct lttcomm_ust_msg command; |
| 139 | struct lttcomm_ust_reply *reply = NULL; |
| 140 | |
| 141 | init_command(LTTNG_UST_CHANNEL, session->handle, &command); |
| 142 | /* Copy channel attributes to command */ |
| 143 | memcpy(&command.u.channel, channel, sizeof(command.u.channel)); |
| 144 | |
| 145 | /* Send command to tracer */ |
| 146 | reply = send_command(sock, &command); |
| 147 | if (reply == NULL) { |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | ret = reply->ret_val; |
| 152 | free(reply); |
| 153 | |
| 154 | return ret; |
| 155 | |
| 156 | error: |
| 157 | free(reply); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Enable UST channel. |
| 163 | */ |
| 164 | int ustctl_enable_channel(int sock, struct ltt_ust_session *session, |
| 165 | struct ltt_ust_channel *chan) |
| 166 | { |
| 167 | struct lttcomm_ust_msg command; |
| 168 | struct lttcomm_ust_reply *reply = NULL; |
| 169 | |
| 170 | init_command(LTTNG_UST_ENABLE, chan->handle, &command); |
| 171 | |
| 172 | reply = ustcomm_send_command(sock, &command); |
| 173 | if (reply == NULL) { |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | if (reply->handle != chan->handle) { |
| 178 | ERR("Receive wrong handle from UST reply on enable channel"); |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | chan->enabled = 1; |
| 183 | free(reply); |
| 184 | |
| 185 | DBG2("ustctl enable channel successful for sock %d", sock); |
| 186 | return 0; |
| 187 | |
| 188 | error: |
| 189 | free(reply); |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Disable UST channel. |
| 195 | */ |
| 196 | int ustctl_disable_channel(int sock, struct ltt_ust_session *session, |
| 197 | struct ltt_ust_channel *chan) |
| 198 | { |
| 199 | struct lttcomm_ust_msg command; |
| 200 | struct lttcomm_ust_reply *reply = NULL; |
| 201 | |
| 202 | memset(&command, 0, sizeof(command)); |
| 203 | |
| 204 | command.cmd = LTTNG_UST_DISABLE; |
| 205 | command.handle = chan->handle; |
| 206 | |
| 207 | reply = ustcomm_send_command(sock, &command); |
| 208 | if (reply == NULL) { |
| 209 | goto error; |
| 210 | } |
| 211 | |
| 212 | if (reply->handle != chan->handle) { |
| 213 | ERR("Receive wrong handle from UST reply on enable channel"); |
| 214 | goto error; |
| 215 | } |
| 216 | |
| 217 | chan->enabled = 1; |
| 218 | free(reply); |
| 219 | |
| 220 | DBG2("ustctl disable channel successful for sock %d", sock); |
| 221 | return 0; |
| 222 | |
| 223 | error: |
| 224 | free(reply); |
| 225 | return -1; |
| 226 | } |