Add enable channel support for UST
[lttng-tools.git] / ltt-sessiond / traceable-app.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27#include <lttngerr.h>
28
29#include "traceable-app.h"
30
31/* Init ust traceabl application's list */
32static struct ltt_traceable_app_list ltt_traceable_app_list = {
33 .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head),
34 .lock = PTHREAD_MUTEX_INITIALIZER,
35 .count = 0,
36};
37
38/*
39 * Add a traceable application structure to the global list.
40 */
41static void add_traceable_app(struct ltt_traceable_app *lta)
42{
43 cds_list_add(&lta->list, &ltt_traceable_app_list.head);
44 ltt_traceable_app_list.count++;
45}
46
47/*
48 * Delete a traceable application structure from the global list.
49 */
50static void del_traceable_app(struct ltt_traceable_app *lta)
51{
52 struct ltt_ust_channel *chan;
53
54 cds_list_del(&lta->list);
55 /* Sanity check */
56 if (ltt_traceable_app_list.count > 0) {
57 ltt_traceable_app_list.count--;
58 }
59
60 cds_list_for_each_entry(chan, &lta->channels.head, list) {
61 trace_ust_destroy_channel(chan);
62 }
63}
64
65/*
66 * Return pointer to traceable apps list.
67 */
68struct ltt_traceable_app_list *get_traceable_apps_list(void)
69{
70 return &ltt_traceable_app_list;
71}
72
73/*
74 * Acquire traceable apps list lock.
75 */
76void lock_apps_list(void)
77{
78 pthread_mutex_lock(&ltt_traceable_app_list.lock);
79}
80
81/*
82 * Release traceable apps list lock.
83 */
84void unlock_apps_list(void)
85{
86 pthread_mutex_unlock(&ltt_traceable_app_list.lock);
87}
88
89/*
90 * Iterate over the traceable apps list and return a pointer or NULL if not
91 * found.
92 */
93static struct ltt_traceable_app *find_app_by_sock(int sock)
94{
95 struct ltt_traceable_app *iter;
96
97 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
98 if (iter->sock == sock) {
99 /* Found */
100 return iter;
101 }
102 }
103
104 return NULL;
105}
106
107/*
108 * Iterate over the traceable apps list and return a pointer or NULL if not
109 * found.
110 */
111struct ltt_traceable_app *traceable_app_get_by_pid(pid_t pid)
112{
113 struct ltt_traceable_app *iter;
114
115 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
116 if (iter->pid == pid) {
117 /* Found */
118 DBG2("Found traceable app by pid %d", pid);
119 return iter;
120 }
121 }
122
123 DBG2("Traceable app with pid %d not found", pid);
124
125 return NULL;
126}
127
128/*
129 * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and
130 * add it to the global traceable app list.
131 *
132 * On success, return 0, else return malloc ENOMEM.
133 */
134int register_traceable_app(struct ust_register_msg *msg, int sock)
135{
136 struct ltt_traceable_app *lta;
137
138 lta = malloc(sizeof(struct ltt_traceable_app));
139 if (lta == NULL) {
140 perror("malloc");
141 return -ENOMEM;
142 }
143
144 lta->uid = msg->uid;
145 lta->gid = msg->gid;
146 lta->pid = msg->pid;
147 lta->ppid = msg->ppid;
148 lta->v_major = msg->major;
149 lta->v_minor = msg->minor;
150 lta->sock = sock;
151 strncpy(lta->name, msg->name, sizeof(lta->name));
152 lta->name[16] = '\0';
153 CDS_INIT_LIST_HEAD(&lta->channels.head);
154
155 lock_apps_list();
156 add_traceable_app(lta);
157 unlock_apps_list();
158
159 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
160 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
161 lta->sock, lta->name, lta->v_major, lta->v_minor);
162
163 return 0;
164}
165
166/*
167 * Unregister app by removing it from the global traceable app list and freeing
168 * the data struct.
169 *
170 * The socket is already closed at this point so no close to sock.
171 */
172void unregister_traceable_app(int sock)
173{
174 struct ltt_traceable_app *lta;
175
176 lock_apps_list();
177 lta = find_app_by_sock(sock);
178 if (lta) {
179 DBG("PID %d unregistered with sock %d", lta->pid, sock);
180 del_traceable_app(lta);
181 close(lta->sock);
182 free(lta);
183 }
184 unlock_apps_list();
185}
186
187/*
188 * Return traceable_app_count
189 */
190unsigned int get_app_count(void)
191{
192 unsigned int count;
193
194 lock_apps_list();
195 count = ltt_traceable_app_list.count;
196 unlock_apps_list();
197
198 return count;
199}
200
201/*
202 * Free and clean all traceable apps of the global list.
203 */
204void clean_traceable_apps_list(void)
205{
206 struct ltt_traceable_app *iter, *tmp;
207
208 /*
209 * Don't acquire list lock here. This function should be called from
210 * cleanup() functions meaning that the program will exit.
211 */
212 cds_list_for_each_entry_safe(iter, tmp, &ltt_traceable_app_list.head, list) {
213 del_traceable_app(iter);
214 close(iter->sock);
215 free(iter);
216 }
217}
This page took 0.022929 seconds and 4 git commands to generate.