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.
29 #include "traceable-app.h"
31 /* Init ust traceabl application's list */
32 static 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
,
39 * Add a traceable application structure to the global list.
41 static void add_traceable_app(struct ltt_traceable_app
*lta
)
43 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
44 ltt_traceable_app_list
.count
++;
48 * Delete a traceable application structure from the global list.
50 static void del_traceable_app(struct ltt_traceable_app
*lta
)
52 cds_list_del(<a
->list
);
54 if (ltt_traceable_app_list
.count
> 0) {
55 ltt_traceable_app_list
.count
--;
60 * Return pointer to traceable apps list.
62 struct ltt_traceable_app_list
*get_traceable_apps_list(void)
64 return <t_traceable_app_list
;
68 * Acquire traceable apps list lock.
70 void lock_apps_list(void)
72 pthread_mutex_lock(<t_traceable_app_list
.lock
);
76 * Release traceable apps list lock.
78 void unlock_apps_list(void)
80 pthread_mutex_unlock(<t_traceable_app_list
.lock
);
84 * Iterate over the traceable apps list and return a pointer or NULL if not
87 static struct ltt_traceable_app
*find_app_by_sock(int sock
)
89 struct ltt_traceable_app
*iter
;
91 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
92 if (iter
->sock
== sock
) {
102 * Iterate over the traceable apps list and return a pointer or NULL if not
105 struct ltt_traceable_app
*traceable_app_get_by_pid(pid_t pid
)
107 struct ltt_traceable_app
*iter
;
109 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
110 if (iter
->pid
== pid
) {
120 * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and
121 * add it to the global traceable app list.
123 * On success, return 0, else return malloc ENOMEM.
125 int register_traceable_app(struct ust_register_msg
*msg
, int sock
)
127 struct ltt_traceable_app
*lta
;
129 lta
= malloc(sizeof(struct ltt_traceable_app
));
138 lta
->ppid
= msg
->ppid
;
139 lta
->v_major
= msg
->major
;
140 lta
->v_minor
= msg
->minor
;
142 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
143 lta
->name
[16] = '\0';
146 add_traceable_app(lta
);
149 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
150 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
151 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
157 * Unregister app by removing it from the global traceable app list and freeing
160 * The socket is already closed at this point so no close to sock.
162 void unregister_traceable_app(int sock
)
164 struct ltt_traceable_app
*lta
;
167 lta
= find_app_by_sock(sock
);
169 DBG("PID %d unregistered with sock %d", lta
->pid
, sock
);
171 del_traceable_app(lta
);
179 * Return traceable_app_count
181 unsigned int get_app_count(void)
186 count
= ltt_traceable_app_list
.count
;
193 * Free and clean all traceable apps of the global list.
195 void clean_traceable_apps_list(void)
197 struct ltt_traceable_app
*iter
, *tmp
;
200 * Don't acquire list lock here. This function should be called from
201 * cleanup() functions meaning that the program will exit.
203 cds_list_for_each_entry_safe(iter
, tmp
, <t_traceable_app_list
.head
, list
) {