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>
23 #include <common/futex.h>
29 void init_notification_thread_command(struct notification_thread_command
*cmd
)
31 memset(cmd
, 0, sizeof(*cmd
));
32 CDS_INIT_LIST_HEAD(&cmd
->cmd_list_node
);
36 int run_command_wait(struct notification_thread_handle
*handle
,
37 struct notification_thread_command
*cmd
)
40 uint64_t notification_counter
= 1;
42 futex_nto1_prepare(&cmd
->reply_futex
);
44 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
46 cds_list_add_tail(&cmd
->cmd_list_node
,
47 &handle
->cmd_queue
.list
);
49 ret
= write(handle
->cmd_queue
.event_fd
,
50 ¬ification_counter
, sizeof(notification_counter
));
52 PERROR("write to notification thread's queue event fd");
54 * Remove the command from the list so the notification
55 * thread does not process it.
57 cds_list_del(&cmd
->cmd_list_node
);
58 goto error_unlock_queue
;
60 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
62 futex_nto1_wait(&cmd
->reply_futex
);
65 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
69 enum lttng_error_code
notification_thread_command_register_trigger(
70 struct notification_thread_handle
*handle
,
71 struct lttng_trigger
*trigger
)
74 enum lttng_error_code ret_code
;
75 struct notification_thread_command cmd
;
77 init_notification_thread_command(&cmd
);
79 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
80 cmd
.parameters
.trigger
= trigger
;
82 ret
= run_command_wait(handle
, &cmd
);
84 ret_code
= LTTNG_ERR_UNK
;
87 ret_code
= cmd
.reply_code
;
92 enum lttng_error_code
notification_thread_command_unregister_trigger(
93 struct notification_thread_handle
*handle
,
94 struct lttng_trigger
*trigger
)
97 enum lttng_error_code ret_code
;
98 struct notification_thread_command cmd
;
100 init_notification_thread_command(&cmd
);
102 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
103 cmd
.parameters
.trigger
= trigger
;
105 ret
= run_command_wait(handle
, &cmd
);
107 ret_code
= LTTNG_ERR_UNK
;
110 ret_code
= cmd
.reply_code
;
115 enum lttng_error_code
notification_thread_command_add_channel(
116 struct notification_thread_handle
*handle
,
117 char *session_name
, uid_t uid
, gid_t gid
,
118 char *channel_name
, uint64_t key
,
119 enum lttng_domain_type domain
, uint64_t capacity
)
122 enum lttng_error_code ret_code
;
123 struct notification_thread_command cmd
;
125 init_notification_thread_command(&cmd
);
127 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
128 cmd
.parameters
.add_channel
.session_name
= session_name
;
129 cmd
.parameters
.add_channel
.uid
= uid
;
130 cmd
.parameters
.add_channel
.gid
= gid
;
131 cmd
.parameters
.add_channel
.channel_name
= channel_name
;
132 cmd
.parameters
.add_channel
.key
.key
= key
;
133 cmd
.parameters
.add_channel
.key
.domain
= domain
;
134 cmd
.parameters
.add_channel
.capacity
= capacity
;
136 ret
= run_command_wait(handle
, &cmd
);
138 ret_code
= LTTNG_ERR_UNK
;
141 ret_code
= cmd
.reply_code
;
146 enum lttng_error_code
notification_thread_command_remove_channel(
147 struct notification_thread_handle
*handle
,
148 uint64_t key
, enum lttng_domain_type domain
)
151 enum lttng_error_code ret_code
;
152 struct notification_thread_command cmd
;
154 init_notification_thread_command(&cmd
);
156 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
157 cmd
.parameters
.remove_channel
.key
= key
;
158 cmd
.parameters
.remove_channel
.domain
= domain
;
160 ret
= run_command_wait(handle
, &cmd
);
162 ret_code
= LTTNG_ERR_UNK
;
165 ret_code
= cmd
.reply_code
;
170 void notification_thread_command_quit(
171 struct notification_thread_handle
*handle
)
174 struct notification_thread_command cmd
;
176 init_notification_thread_command(&cmd
);
178 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
179 ret
= run_command_wait(handle
, &cmd
);
180 assert(!ret
&& cmd
.reply_code
== LTTNG_OK
);