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