Add jul.c/.h to sessiond code
[lttng-tools.git] / src / bin / lttng-sessiond / jul.c
CommitLineData
0475c50c
DG
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@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#define _GNU_SOURCE
19#include <assert.h>
20
21#include <common/common.h>
22
23#include "jul.h"
24#include "utils.h"
25
26/*
27 * URCU intermediate call to complete destroy a JUL event.
28 */
29static void destroy_event_jul_rcu(struct rcu_head *head)
30{
31 struct lttng_ht_node_str *node =
32 caa_container_of(head, struct lttng_ht_node_str, head);
33 struct jul_event *event =
34 caa_container_of(node, struct jul_event, node);
35
36 free(event);
37}
38
39/*
40 * Initialize an already allocated JUL domain object.
41 *
42 * Return 0 on success or else a negative errno value.
43 */
44int jul_init_domain(struct jul_domain *dom)
45{
46 int ret;
47
48 assert(dom);
49
50 dom->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
51 if (!dom->events) {
52 ret = -ENOMEM;
53 goto error;
54 }
55
56 return 0;
57
58error:
59 return ret;
60}
61
62/*
63 * Create a newly allocated JUL event data structure. If name is valid, it's
64 * copied into the created event.
65 *
66 * Return a new object else NULL on error.
67 */
68struct jul_event *jul_create_event(const char *name)
69{
70 struct jul_event *event;
71
72 DBG3("JUL create new event with name %s", name);
73
74 event = zmalloc(sizeof(*event));
75 if (!event) {
76 goto error;
77 }
78
79 if (name) {
80 strncpy(event->name, name, sizeof(event->name));
81 event->name[sizeof(event->name) - 1] = '\0';
82 }
83
84error:
85 return event;
86}
87
88/*
89 * Unique add of a JUL event to a given domain.
90 */
91void jul_add_event(struct jul_event *event, struct jul_domain *dom)
92{
93 assert(event);
94 assert(dom);
95 assert(dom->events);
96
97 DBG3("JUL adding event %s to domain", event->name);
98
99 lttng_ht_add_unique_str(dom->events, &event->node);
100}
101
102/*
103 * Find a JUL event in the given domain using name.
104 *
105 * RCU read side lock MUST be acquired.
106 *
107 * Return object if found else NULL.
108 */
109struct jul_event *jul_find_by_name(const char *name, struct jul_domain *dom)
110{
111 struct lttng_ht_node_str *node;
112 struct lttng_ht_iter iter;
113
114 assert(name);
115 assert(dom);
116 assert(dom->events);
117
118 lttng_ht_lookup(dom->events, (void *)name, &iter);
119 node = lttng_ht_iter_get_node_str(&iter);
120 if (node == NULL) {
121 goto error;
122 }
123
124 DBG3("JUL found by name %s in domain.", name);
125 return caa_container_of(node, struct jul_event, node);
126
127error:
128 DBG3("JUL NOT found by name %s in domain.", name);
129 return NULL;
130}
131
132/*
133 * Delete JUL event from given domain. Events hash table MUST be initialized.
134 */
135void jul_delete_event(struct jul_event *event, struct jul_domain *dom)
136{
137 int ret;
138 struct lttng_ht_iter iter;
139
140 assert(event);
141 assert(dom);
142 assert(dom->events);
143
144 DBG3("JUL deleting event %s from domain", event->name);
145
146 iter.iter.node = &event->node.node;
147 rcu_read_lock();
148 ret = lttng_ht_del(dom->events, &iter);
149 rcu_read_unlock();
150 assert(!ret);
151}
152
153/*
154 * Free given JUl event. After this call, the pointer is not usable anymore.
155 */
156void jul_destroy_event(struct jul_event *event)
157{
158 assert(event);
159
160 free(event);
161}
162
163/*
164 * Destroy a JUL domain completely. Note that the given pointer is NOT freed
165 * thus a reference can be passed to this function.
166 */
167void jul_destroy_domain(struct jul_domain *dom)
168{
169 struct lttng_ht_node_str *node;
170 struct lttng_ht_iter iter;
171
172 assert(dom);
173
174 DBG3("JUL destroy domain");
175
176 /*
177 * Just ignore if no events hash table exists. This is possible if for
178 * instance a JUL domain object was allocated but not initialized.
179 */
180 if (!dom->events) {
181 return;
182 }
183
184 rcu_read_lock();
185 cds_lfht_for_each_entry(dom->events->ht, &iter.iter, node, node) {
186 int ret;
187
188 ret = lttng_ht_del(dom->events, &iter);
189 assert(!ret);
190 call_rcu(&node->head, destroy_event_jul_rcu);
191 }
192 rcu_read_unlock();
193
194 ht_cleanup_push(dom->events);
195}
This page took 0.041946 seconds and 4 git commands to generate.