Add the sessiond notification-handling subsystem
[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 <common/futex.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27
28 static
29 void init_notification_thread_command(struct notification_thread_command *cmd)
30 {
31 memset(cmd, 0, sizeof(*cmd));
32 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
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 futex_nto1_prepare(&cmd->reply_futex);
43
44 pthread_mutex_lock(&handle->cmd_queue.lock);
45 /* Add to queue. */
46 cds_list_add_tail(&cmd->cmd_list_node,
47 &handle->cmd_queue.list);
48 /* Wake-up thread. */
49 ret = write(handle->cmd_queue.event_fd,
50 &notification_counter, sizeof(notification_counter));
51 if (ret < 0) {
52 PERROR("write to notification thread's queue event fd");
53 /*
54 * Remove the command from the list so the notification
55 * thread does not process it.
56 */
57 cds_list_del(&cmd->cmd_list_node);
58 goto error_unlock_queue;
59 }
60 pthread_mutex_unlock(&handle->cmd_queue.lock);
61
62 futex_nto1_wait(&cmd->reply_futex);
63 return 0;
64 error_unlock_queue:
65 pthread_mutex_unlock(&handle->cmd_queue.lock);
66 return -1;
67 }
68
69 enum lttng_error_code notification_thread_command_register_trigger(
70 struct notification_thread_handle *handle,
71 struct lttng_trigger *trigger)
72 {
73 int ret;
74 enum lttng_error_code ret_code;
75 struct notification_thread_command cmd;
76
77 init_notification_thread_command(&cmd);
78
79 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
80 cmd.parameters.trigger = trigger;
81
82 ret = run_command_wait(handle, &cmd);
83 if (ret) {
84 ret_code = LTTNG_ERR_UNK;
85 goto end;
86 }
87 ret_code = cmd.reply_code;
88 end:
89 return ret_code;
90 }
91
92 enum lttng_error_code notification_thread_command_unregister_trigger(
93 struct notification_thread_handle *handle,
94 struct lttng_trigger *trigger)
95 {
96 int ret;
97 enum lttng_error_code ret_code;
98 struct notification_thread_command cmd;
99
100 init_notification_thread_command(&cmd);
101
102 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
103 cmd.parameters.trigger = trigger;
104
105 ret = run_command_wait(handle, &cmd);
106 if (ret) {
107 ret_code = LTTNG_ERR_UNK;
108 goto end;
109 }
110 ret_code = cmd.reply_code;
111 end:
112 return ret_code;
113 }
114
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)
120 {
121 int ret;
122 enum lttng_error_code ret_code;
123 struct notification_thread_command cmd;
124
125 init_notification_thread_command(&cmd);
126
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;
135
136 ret = run_command_wait(handle, &cmd);
137 if (ret) {
138 ret_code = LTTNG_ERR_UNK;
139 goto end;
140 }
141 ret_code = cmd.reply_code;
142 end:
143 return ret_code;
144 }
145
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)
149 {
150 int ret;
151 enum lttng_error_code ret_code;
152 struct notification_thread_command cmd;
153
154 init_notification_thread_command(&cmd);
155
156 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
157 cmd.parameters.remove_channel.key = key;
158 cmd.parameters.remove_channel.domain = domain;
159
160 ret = run_command_wait(handle, &cmd);
161 if (ret) {
162 ret_code = LTTNG_ERR_UNK;
163 goto end;
164 }
165 ret_code = cmd.reply_code;
166 end:
167 return ret_code;
168 }
169
170 void notification_thread_command_quit(
171 struct notification_thread_handle *handle)
172 {
173 int ret;
174 struct notification_thread_command cmd;
175
176 init_notification_thread_command(&cmd);
177
178 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
179 ret = run_command_wait(handle, &cmd);
180 assert(!ret && cmd.reply_code == LTTNG_OK);
181 }
This page took 0.054379 seconds and 5 git commands to generate.