Update documentation following babeltrace UI change
[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/* Init ust traceable application's list */
32static struct ust_app_list ust_app_list = {
33 .head = CDS_LIST_HEAD_INIT(ust_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_app_to_list(struct ust_app *lta)
42{
43 cds_list_add(&lta->list, &ust_app_list.head);
44 ust_app_list.count++;
45}
46
47/*
48 * Delete a traceable application structure from the global list.
49 */
50static void del_app_from_list(struct ust_app *lta)
51{
52 struct ltt_ust_channel *chan;
53
54 cds_list_del(&lta->list);
55 /* Sanity check */
56 if (ust_app_list.count > 0) {
57 ust_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 * Iterate over the traceable apps list and return a pointer or NULL if not
67 * found.
68 */
69static struct ust_app *find_app_by_sock(int sock)
70{
71 struct ust_app *iter;
72
73 cds_list_for_each_entry(iter, &ust_app_list.head, list) {
74 if (iter->sock == sock) {
75 /* Found */
76 return iter;
77 }
78 }
79
80 return NULL;
81}
82
83/*
84 * Return pointer to traceable apps list.
85 */
86struct ust_app_list *ust_app_get_list(void)
87{
88 return &ust_app_list;
89}
90
91/*
92 * Acquire traceable apps list lock.
93 */
94void ust_app_lock_list(void)
95{
96 pthread_mutex_lock(&ust_app_list.lock);
97}
98
99/*
100 * Release traceable apps list lock.
101 */
102void ust_app_unlock_list(void)
103{
104 pthread_mutex_unlock(&ust_app_list.lock);
105}
106
107/*
108 * Iterate over the traceable apps list and return a pointer or NULL if not
109 * found.
110 */
111struct ust_app *ust_app_get_by_pid(pid_t pid)
112{
113 struct ust_app *iter;
114
115 cds_list_for_each_entry(iter, &ust_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 ust_app struct and
130 * add it to the global traceable app list.
131 *
132 * On success, return 0, else return malloc ENOMEM.
133 */
134int ust_app_register(struct ust_register_msg *msg, int sock)
135{
136 struct ust_app *lta;
137
138 lta = malloc(sizeof(struct ust_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 ust_app_lock_list();
156 add_app_to_list(lta);
157 ust_app_unlock_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 ust_app_unregister(int sock)
173{
174 struct ust_app *lta;
175
176 ust_app_lock_list();
177 lta = find_app_by_sock(sock);
178 if (lta) {
179 DBG("PID %d unregistered with sock %d", lta->pid, sock);
180 del_app_from_list(lta);
181 close(lta->sock);
182 free(lta);
183 }
184 ust_app_unlock_list();
185}
186
187/*
188 * Return traceable_app_count
189 */
190unsigned int ust_app_list_count(void)
191{
192 unsigned int count;
193
194 ust_app_lock_list();
195 count = ust_app_list.count;
196 ust_app_unlock_list();
197
198 return count;
199}
200
201/*
202 * Free and clean all traceable apps of the global list.
203 */
204void ust_app_clean_list(void)
205{
206 struct ust_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, &ust_app_list.head, list) {
213 del_app_from_list(iter);
214 close(iter->sock);
215 free(iter);
216 }
217}
This page took 0.022988 seconds and 4 git commands to generate.