2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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
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.
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.
31 /* Init ust traceable application's list */
32 static struct ust_app_list ust_app_list
= {
33 .head
= CDS_LIST_HEAD_INIT(ust_app_list
.head
),
34 .lock
= PTHREAD_MUTEX_INITIALIZER
,
39 * Add a traceable application structure to the global list.
41 static void add_app_to_list(struct ust_app
*lta
)
43 cds_list_add(<a
->list
, &ust_app_list
.head
);
48 * Delete a traceable application structure from the global list.
50 static void del_app_from_list(struct ust_app
*lta
)
52 struct ltt_ust_channel
*chan
;
54 cds_list_del(<a
->list
);
56 if (ust_app_list
.count
> 0) {
60 cds_list_for_each_entry(chan
, <a
->channels
.head
, list
) {
61 trace_ust_destroy_channel(chan
);
66 * Iterate over the traceable apps list and return a pointer or NULL if not
69 static struct ust_app
*find_app_by_sock(int sock
)
73 cds_list_for_each_entry(iter
, &ust_app_list
.head
, list
) {
74 if (iter
->sock
== sock
) {
84 * Return pointer to traceable apps list.
86 struct ust_app_list
*ust_app_get_list(void)
92 * Acquire traceable apps list lock.
94 void ust_app_lock_list(void)
96 pthread_mutex_lock(&ust_app_list
.lock
);
100 * Release traceable apps list lock.
102 void ust_app_unlock_list(void)
104 pthread_mutex_unlock(&ust_app_list
.lock
);
108 * Iterate over the traceable apps list and return a pointer or NULL if not
111 struct ust_app
*ust_app_get_by_pid(pid_t pid
)
113 struct ust_app
*iter
;
115 cds_list_for_each_entry(iter
, &ust_app_list
.head
, list
) {
116 if (iter
->pid
== pid
) {
118 DBG2("Found traceable app by pid %d", pid
);
123 DBG2("Traceable app with pid %d not found", pid
);
129 * Using pid and uid (of the app), allocate a new ust_app struct and
130 * add it to the global traceable app list.
132 * On success, return 0, else return malloc ENOMEM.
134 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
138 lta
= malloc(sizeof(struct ust_app
));
147 lta
->ppid
= msg
->ppid
;
148 lta
->v_major
= msg
->major
;
149 lta
->v_minor
= msg
->minor
;
151 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
152 lta
->name
[16] = '\0';
153 CDS_INIT_LIST_HEAD(<a
->channels
.head
);
156 add_app_to_list(lta
);
157 ust_app_unlock_list();
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
);
167 * Unregister app by removing it from the global traceable app list and freeing
170 * The socket is already closed at this point so no close to sock.
172 void ust_app_unregister(int sock
)
177 lta
= find_app_by_sock(sock
);
179 DBG("PID %d unregistered with sock %d", lta
->pid
, sock
);
180 del_app_from_list(lta
);
184 ust_app_unlock_list();
188 * Return traceable_app_count
190 unsigned int ust_app_list_count(void)
195 count
= ust_app_list
.count
;
196 ust_app_unlock_list();
202 * Free and clean all traceable apps of the global list.
204 void ust_app_clean_list(void)
206 struct ust_app
*iter
, *tmp
;
209 * Don't acquire list lock here. This function should be called from
210 * cleanup() functions meaning that the program will exit.
212 cds_list_for_each_entry_safe(iter
, tmp
, &ust_app_list
.head
, list
) {
213 del_app_from_list(iter
);