2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
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>
28 void init_notification_thread_command(struct notification_thread_command
*cmd
)
30 memset(cmd
, 0, sizeof(*cmd
));
31 CDS_INIT_LIST_HEAD(&cmd
->cmd_list_node
);
32 lttng_waiter_init(&cmd
->reply_waiter
);
36 int run_command_wait(struct notification_thread_handle
*handle
,
37 struct notification_thread_command
*cmd
)
40 uint64_t notification_counter
= 1;
42 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
44 cds_list_add_tail(&cmd
->cmd_list_node
,
45 &handle
->cmd_queue
.list
);
47 ret
= lttng_write(lttng_pipe_get_writefd(handle
->cmd_queue
.event_pipe
),
48 ¬ification_counter
, sizeof(notification_counter
));
49 if (ret
!= sizeof(notification_counter
)) {
50 PERROR("write to notification thread's queue event fd");
52 * Remove the command from the list so the notification
53 * thread does not process it.
55 cds_list_del(&cmd
->cmd_list_node
);
56 goto error_unlock_queue
;
58 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
60 lttng_waiter_wait(&cmd
->reply_waiter
);
63 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
67 enum lttng_error_code
notification_thread_command_register_trigger(
68 struct notification_thread_handle
*handle
,
69 struct lttng_trigger
*trigger
)
72 enum lttng_error_code ret_code
;
73 struct notification_thread_command cmd
;
75 init_notification_thread_command(&cmd
);
77 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
78 cmd
.parameters
.trigger
= trigger
;
80 ret
= run_command_wait(handle
, &cmd
);
82 ret_code
= LTTNG_ERR_UNK
;
85 ret_code
= cmd
.reply_code
;
90 enum lttng_error_code
notification_thread_command_unregister_trigger(
91 struct notification_thread_handle
*handle
,
92 struct lttng_trigger
*trigger
)
95 enum lttng_error_code ret_code
;
96 struct notification_thread_command cmd
;
98 init_notification_thread_command(&cmd
);
100 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
101 cmd
.parameters
.trigger
= trigger
;
103 ret
= run_command_wait(handle
, &cmd
);
105 ret_code
= LTTNG_ERR_UNK
;
108 ret_code
= cmd
.reply_code
;
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
)
120 enum lttng_error_code ret_code
;
121 struct notification_thread_command cmd
;
123 init_notification_thread_command(&cmd
);
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
;
134 ret
= run_command_wait(handle
, &cmd
);
136 ret_code
= LTTNG_ERR_UNK
;
139 ret_code
= cmd
.reply_code
;
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
)
149 enum lttng_error_code ret_code
;
150 struct notification_thread_command cmd
;
152 init_notification_thread_command(&cmd
);
154 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
155 cmd
.parameters
.remove_channel
.key
= key
;
156 cmd
.parameters
.remove_channel
.domain
= domain
;
158 ret
= run_command_wait(handle
, &cmd
);
160 ret_code
= LTTNG_ERR_UNK
;
163 ret_code
= cmd
.reply_code
;
168 void notification_thread_command_quit(
169 struct notification_thread_handle
*handle
)
172 struct notification_thread_command cmd
;
174 init_notification_thread_command(&cmd
);
176 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
177 ret
= run_command_wait(handle
, &cmd
);
178 assert(!ret
&& cmd
.reply_code
== LTTNG_OK
);