d9fdf01588465a3e826bbc53b3a0e6cb35d29705
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-commands.c
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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, version 2 only, as
6 * published by the Free Software Foundation.
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., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <lttng/trigger/trigger.h>
19 #include <lttng/lttng-error.h>
20 #include "notification-thread.h"
21 #include "notification-thread-commands.h"
22 #include <common/error.h>
23 #include <unistd.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26
27 static
28 void init_notification_thread_command(struct notification_thread_command *cmd)
29 {
30 memset(cmd, 0, sizeof(*cmd));
31 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
32 lttng_waiter_init(&cmd->reply_waiter);
33 }
34
35 static
36 int run_command_wait(struct notification_thread_handle *handle,
37 struct notification_thread_command *cmd)
38 {
39 int ret;
40 uint64_t notification_counter = 1;
41
42 pthread_mutex_lock(&handle->cmd_queue.lock);
43 /* Add to queue. */
44 cds_list_add_tail(&cmd->cmd_list_node,
45 &handle->cmd_queue.list);
46 /* Wake-up thread. */
47 ret = write(handle->cmd_queue.event_fd,
48 &notification_counter, sizeof(notification_counter));
49 if (ret < 0) {
50 PERROR("write to notification thread's queue event fd");
51 /*
52 * Remove the command from the list so the notification
53 * thread does not process it.
54 */
55 cds_list_del(&cmd->cmd_list_node);
56 goto error_unlock_queue;
57 }
58 pthread_mutex_unlock(&handle->cmd_queue.lock);
59
60 lttng_waiter_wait(&cmd->reply_waiter);
61 return 0;
62 error_unlock_queue:
63 pthread_mutex_unlock(&handle->cmd_queue.lock);
64 return -1;
65 }
66
67 enum lttng_error_code notification_thread_command_register_trigger(
68 struct notification_thread_handle *handle,
69 struct lttng_trigger *trigger)
70 {
71 int ret;
72 enum lttng_error_code ret_code;
73 struct notification_thread_command cmd;
74
75 init_notification_thread_command(&cmd);
76
77 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
78 cmd.parameters.trigger = trigger;
79
80 ret = run_command_wait(handle, &cmd);
81 if (ret) {
82 ret_code = LTTNG_ERR_UNK;
83 goto end;
84 }
85 ret_code = cmd.reply_code;
86 end:
87 return ret_code;
88 }
89
90 enum lttng_error_code notification_thread_command_unregister_trigger(
91 struct notification_thread_handle *handle,
92 struct lttng_trigger *trigger)
93 {
94 int ret;
95 enum lttng_error_code ret_code;
96 struct notification_thread_command cmd;
97
98 init_notification_thread_command(&cmd);
99
100 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
101 cmd.parameters.trigger = trigger;
102
103 ret = run_command_wait(handle, &cmd);
104 if (ret) {
105 ret_code = LTTNG_ERR_UNK;
106 goto end;
107 }
108 ret_code = cmd.reply_code;
109 end:
110 return ret_code;
111 }
112
113 enum lttng_error_code notification_thread_command_add_channel(
114 struct notification_thread_handle *handle,
115 char *session_name, uid_t uid, gid_t gid,
116 char *channel_name, uint64_t key,
117 enum lttng_domain_type domain, uint64_t capacity)
118 {
119 int ret;
120 enum lttng_error_code ret_code;
121 struct notification_thread_command cmd;
122
123 init_notification_thread_command(&cmd);
124
125 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
126 cmd.parameters.add_channel.session_name = session_name;
127 cmd.parameters.add_channel.uid = uid;
128 cmd.parameters.add_channel.gid = gid;
129 cmd.parameters.add_channel.channel_name = channel_name;
130 cmd.parameters.add_channel.key.key = key;
131 cmd.parameters.add_channel.key.domain = domain;
132 cmd.parameters.add_channel.capacity = capacity;
133
134 ret = run_command_wait(handle, &cmd);
135 if (ret) {
136 ret_code = LTTNG_ERR_UNK;
137 goto end;
138 }
139 ret_code = cmd.reply_code;
140 end:
141 return ret_code;
142 }
143
144 enum lttng_error_code notification_thread_command_remove_channel(
145 struct notification_thread_handle *handle,
146 uint64_t key, enum lttng_domain_type domain)
147 {
148 int ret;
149 enum lttng_error_code ret_code;
150 struct notification_thread_command cmd;
151
152 init_notification_thread_command(&cmd);
153
154 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
155 cmd.parameters.remove_channel.key = key;
156 cmd.parameters.remove_channel.domain = domain;
157
158 ret = run_command_wait(handle, &cmd);
159 if (ret) {
160 ret_code = LTTNG_ERR_UNK;
161 goto end;
162 }
163 ret_code = cmd.reply_code;
164 end:
165 return ret_code;
166 }
167
168 void notification_thread_command_quit(
169 struct notification_thread_handle *handle)
170 {
171 int ret;
172 struct notification_thread_command cmd;
173
174 init_notification_thread_command(&cmd);
175
176 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
177 ret = run_command_wait(handle, &cmd);
178 assert(!ret && cmd.reply_code == LTTNG_OK);
179 }
This page took 0.031911 seconds and 3 git commands to generate.