Merge branch 'master' into benchmark
[lttng-tools.git] / ltt-sessiond / ust-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 "ust-app.h"
30
31#include "benchmark.h"
32
33/* Init ust traceable application's list */
34static struct ust_app_list ust_app_list = {
35 .head = CDS_LIST_HEAD_INIT(ust_app_list.head),
36 .lock = PTHREAD_MUTEX_INITIALIZER,
37 .count = 0,
38};
39
40/*
41 * Add a traceable application structure to the global list.
42 */
43static void add_app_to_list(struct ust_app *lta)
44{
45 cds_list_add(&lta->list, &ust_app_list.head);
46 ust_app_list.count++;
47}
48
49/*
50 * Delete a traceable application structure from the global list.
51 */
52static void del_app_from_list(struct ust_app *lta)
53{
54 struct ltt_ust_channel *chan;
55
56 cds_list_del(&lta->list);
57 /* Sanity check */
58 if (ust_app_list.count > 0) {
59 ust_app_list.count--;
60 }
61
62 cds_list_for_each_entry(chan, &lta->channels.head, list) {
63 trace_ust_destroy_channel(chan);
64 }
65}
66
67/*
68 * Iterate over the traceable apps list and return a pointer or NULL if not
69 * found.
70 */
71static struct ust_app *find_app_by_sock(int sock)
72{
73 struct ust_app *iter;
74
75 cds_list_for_each_entry(iter, &ust_app_list.head, list) {
76 if (iter->sock == sock) {
77 /* Found */
78 return iter;
79 }
80 }
81
82 return NULL;
83}
84
85/*
86 * Return pointer to traceable apps list.
87 */
88struct ust_app_list *ust_app_get_list(void)
89{
90 return &ust_app_list;
91}
92
93/*
94 * Acquire traceable apps list lock.
95 */
96void ust_app_lock_list(void)
97{
98 pthread_mutex_lock(&ust_app_list.lock);
99}
100
101/*
102 * Release traceable apps list lock.
103 */
104void ust_app_unlock_list(void)
105{
106 pthread_mutex_unlock(&ust_app_list.lock);
107}
108
109/*
110 * Iterate over the traceable apps list and return a pointer or NULL if not
111 * found.
112 */
113struct ust_app *ust_app_get_by_pid(pid_t pid)
114{
115 struct ust_app *iter;
116
117 cds_list_for_each_entry(iter, &ust_app_list.head, list) {
118 if (iter->pid == pid) {
119 /* Found */
120 DBG2("Found traceable app by pid %d", pid);
121 return iter;
122 }
123 }
124
125 DBG2("Traceable app with pid %d not found", pid);
126
127 return NULL;
128}
129
130/*
131 * Using pid and uid (of the app), allocate a new ust_app struct and
132 * add it to the global traceable app list.
133 *
134 * On success, return 0, else return malloc ENOMEM.
135 */
136int ust_app_register(struct ust_register_msg *msg, int sock)
137{
138 struct ust_app *lta;
139
140 lta = malloc(sizeof(struct ust_app));
141 if (lta == NULL) {
142 perror("malloc");
143 return -ENOMEM;
144 }
145
146 lta->uid = msg->uid;
147 lta->gid = msg->gid;
148 lta->pid = msg->pid;
149 lta->ppid = msg->ppid;
150 lta->v_major = msg->major;
151 lta->v_minor = msg->minor;
152 lta->sock = sock;
153 strncpy(lta->name, msg->name, sizeof(lta->name));
154 lta->name[16] = '\0';
155 CDS_INIT_LIST_HEAD(&lta->channels.head);
156
157 ust_app_lock_list();
158 add_app_to_list(lta);
159 ust_app_unlock_list();
160
161 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
162 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
163 lta->sock, lta->name, lta->v_major, lta->v_minor);
164
165 return 0;
166}
167
168/*
169 * Unregister app by removing it from the global traceable app list and freeing
170 * the data struct.
171 *
172 * The socket is already closed at this point so no close to sock.
173 */
174void ust_app_unregister(int sock)
175{
176 struct ust_app *lta;
177
178 tracepoint(ust_unregister_start);
179
180 ust_app_lock_list();
181 lta = find_app_by_sock(sock);
182 if (lta) {
183 DBG("PID %d unregistered with sock %d", lta->pid, sock);
184 del_app_from_list(lta);
185 close(lta->sock);
186 free(lta);
187 }
188 ust_app_unlock_list();
189
190 tracepoint(ust_unregister_stop);
191}
192
193/*
194 * Return traceable_app_count
195 */
196unsigned int ust_app_list_count(void)
197{
198 unsigned int count;
199
200 ust_app_lock_list();
201 count = ust_app_list.count;
202 ust_app_unlock_list();
203
204 return count;
205}
206
207/*
208 * Free and clean all traceable apps of the global list.
209 */
210void ust_app_clean_list(void)
211{
212 struct ust_app *iter, *tmp;
213
214 /*
215 * Don't acquire list lock here. This function should be called from
216 * cleanup() functions meaning that the program will exit.
217 */
218 cds_list_for_each_entry_safe(iter, tmp, &ust_app_list.head, list) {
219 del_app_from_list(iter);
220 close(iter->sock);
221 free(iter);
222 }
223}
This page took 0.023977 seconds and 4 git commands to generate.