2753b6f5ab639b593692c62292a5740cd08c653f
[lttng-tools.git] / ltt-sessiond / ust-ctl.c
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
31 /*
32 * Send registration done packet to the application.
33 */
34 int ustctl_register_done(int sock)
35 {
36 struct lttcomm_ust_msg command;
37 struct lttcomm_ust_reply *reply;
38
39 DBG("Sending register done command to %d", sock);
40
41 command.cmd = LTTNG_UST_REGISTER_DONE;
42 command.handle = LTTNG_UST_ROOT_HANDLE;
43
44 reply = ustcomm_send_command(sock, &command);
45 if (reply == NULL) {
46 goto error;
47 }
48
49 if (reply->ret_code != LTTCOMM_OK) {
50 DBG("Return code: %s", lttcomm_get_readable_code(reply->ret_code));
51 goto error;
52 }
53
54 return 0;
55
56 error:
57 return -1;
58 }
59
60 /*
61 * Create an UST session on the tracer.
62 */
63 int ustctl_create_session(int sock, struct ltt_ust_session *session)
64 {
65 struct lttcomm_ust_msg command;
66 struct lttcomm_ust_reply *reply = NULL;
67
68 command.cmd = LTTNG_UST_SESSION;
69 command.handle = LTTNG_UST_ROOT_HANDLE;
70
71 reply = ustcomm_send_command(sock, &command);
72 if (reply == NULL) {
73 goto error;
74 }
75
76 if (reply->ret_code != LTTCOMM_OK) {
77 DBG("Return code: %s", lttcomm_get_readable_code(reply->ret_code));
78 goto error;
79 }
80
81 /* Save session handle */
82 session->handle = reply->ret_val;
83 free(reply);
84
85 DBG2("ustctl create session command successful");
86 return 0;
87
88 error:
89 free(reply);
90 return -1;
91 }
92
93 /*
94 * Create UST channel to the tracer.
95 */
96 int ustctl_create_channel(int sock, struct ltt_ust_session *session,
97 struct lttng_channel *channel)
98 {
99 struct lttcomm_ust_msg command;
100 struct lttcomm_ust_reply *reply = NULL;
101 struct ltt_ust_channel *uchan;
102
103 uchan = trace_ust_create_channel(channel, session->path);
104 if (uchan == NULL) {
105 goto error;
106 }
107
108 memset(&command, 0, sizeof(command));
109
110 command.cmd = LTTNG_UST_CHANNEL;
111 command.handle = session->handle;
112
113 /* Copy channel attributes to command */
114 memcpy(&command.u.channel, &uchan->attr, sizeof(command.u.channel));
115
116 reply = ustcomm_send_command(sock, &command);
117 if (reply == NULL) {
118 goto error;
119 }
120
121 if (reply->ret_code != LTTCOMM_OK) {
122 DBG("Return code (%d): %s", reply->ret_code,
123 lttcomm_get_readable_code(reply->ret_code));
124 goto error;
125 }
126
127 uchan->handle = reply->ret_val;
128
129 /* Add channel to session */
130 cds_list_add(&uchan->list, &session->channels.head);
131 session->channels.count++;
132
133 free(reply);
134
135 return 0;
136
137 error:
138 free(reply);
139 return -1;
140 }
141
142 /*
143 * Enable UST channel.
144 */
145 int ustctl_enable_channel(int sock, struct ltt_ust_session *session,
146 struct ltt_ust_channel *chan)
147 {
148 struct lttcomm_ust_msg command;
149 struct lttcomm_ust_reply *reply = NULL;
150
151 memset(&command, 0, sizeof(command));
152
153 command.cmd = LTTNG_UST_ENABLE;
154 command.handle = chan->handle;
155
156 reply = ustcomm_send_command(sock, &command);
157 if (reply == NULL) {
158 goto error;
159 }
160
161 if (reply->ret_code != LTTCOMM_OK) {
162 DBG("Return code (%d): %s", reply->ret_code,
163 lttcomm_get_readable_code(reply->ret_code));
164 goto error;
165 } else if (reply->handle != chan->handle) {
166 ERR("Receive wrong handle from UST reply on enable channel");
167 goto error;
168 }
169
170 chan->enabled = 1;
171 free(reply);
172
173 DBG2("ustctl enable channel successful for sock %d", sock);
174 return 0;
175
176 error:
177 free(reply);
178 return -1;
179 }
180
181 /*
182 * Disable UST channel.
183 */
184 int ustctl_disable_channel(int sock, struct ltt_ust_session *session,
185 struct ltt_ust_channel *chan)
186 {
187 struct lttcomm_ust_msg command;
188 struct lttcomm_ust_reply *reply = NULL;
189
190 memset(&command, 0, sizeof(command));
191
192 command.cmd = LTTNG_UST_DISABLE;
193 command.handle = chan->handle;
194
195 reply = ustcomm_send_command(sock, &command);
196 if (reply == NULL) {
197 goto error;
198 }
199
200 if (reply->ret_code != LTTCOMM_OK) {
201 DBG("Return code (%d): %s", reply->ret_code,
202 lttcomm_get_readable_code(reply->ret_code));
203 goto error;
204 } else if (reply->handle != chan->handle) {
205 ERR("Receive wrong handle from UST reply on enable channel");
206 goto error;
207 }
208
209 chan->enabled = 1;
210 free(reply);
211
212 DBG2("ustctl disable channel successful for sock %d", sock);
213 return 0;
214
215 error:
216 free(reply);
217 return -1;
218 }
This page took 0.032552 seconds and 3 git commands to generate.