Cleanup comments and bad indent
[lttng-tools.git] / ltt-sessiond / traceable-app.c
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 <urcu/list.h>
25
26 #include "lttngerr.h"
27 #include "traceable-app.h"
28
29 /* Number of element for the list below. */
30 static unsigned int traceable_app_count;
31
32 /* Init ust traceabl application's list */
33 struct ltt_traceable_app_list ltt_traceable_app_list = {
34 .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head),
35 };
36
37 /* List mutex */
38 pthread_mutex_t ltt_traceable_app_list_mutex;
39
40 /* Internal function */
41 static void add_traceable_app(struct ltt_traceable_app *lta);
42 static void del_traceable_app(struct ltt_traceable_app *lta);
43
44 /*
45 * Add a traceable application structure to the global list protected by a
46 * mutex.
47 */
48 static void add_traceable_app(struct ltt_traceable_app *lta)
49 {
50 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
51 cds_list_add(&lta->list, &ltt_traceable_app_list.head);
52 traceable_app_count++;
53 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
54 }
55
56 /*
57 * Delete a traceable application structure from the global list protected by a
58 * mutex.
59 */
60 static void del_traceable_app(struct ltt_traceable_app *lta)
61 {
62 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
63 cds_list_del(&lta->list);
64 /* Sanity check */
65 if (traceable_app_count != 0) {
66 traceable_app_count--;
67 }
68 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
69 }
70
71 /*
72 * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and
73 * add it to the global traceable app list.
74 *
75 * On success, return 0, else return malloc ENOMEM.
76 */
77 int register_traceable_app(pid_t pid, uid_t uid)
78 {
79 struct ltt_traceable_app *lta;
80
81 lta = malloc(sizeof(struct ltt_traceable_app));
82 if (lta == NULL) {
83 perror("malloc");
84 return -ENOMEM;
85 }
86
87 lta->uid = uid;
88 lta->pid = pid;
89 add_traceable_app(lta);
90 DBG("Application %d registered with UID %d", pid, uid);
91
92 return 0;
93 }
94
95 /*
96 * Unregister app by removing it from the global traceable app list and freeing
97 * the data struct.
98 */
99 void unregister_traceable_app(pid_t pid)
100 {
101 struct ltt_traceable_app *lta;
102
103 lta = find_app_by_pid(pid);
104 if (lta != NULL) {
105 del_traceable_app(lta);
106 free(lta);
107 DBG("PID %d unregistered", pid);
108 }
109 }
110
111 /*
112 * Return traceable_app_count
113 */
114 unsigned int get_app_count(void)
115 {
116 return traceable_app_count;
117 }
118
119 /*
120 * Iterate over the traceable apps list and return a pointer or NULL if not
121 * found.
122 */
123 struct ltt_traceable_app *find_app_by_pid(pid_t pid)
124 {
125 struct ltt_traceable_app *iter;
126
127 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
128 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
129 if (iter->pid == pid) {
130 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
131 /* Found */
132 return iter;
133 }
134 }
135 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
136
137 return NULL;
138 }
139
140 /*
141 * List traceable user-space application and fill an array of pids.
142 */
143 void get_app_list_pids(pid_t *pids)
144 {
145 int i = 0;
146 struct ltt_traceable_app *iter;
147
148 /* Protected by a mutex here because the threads manage_client
149 * and manage_apps can access this list.
150 */
151 pthread_mutex_lock(&ltt_traceable_app_list_mutex);
152 cds_list_for_each_entry(iter, &ltt_traceable_app_list.head, list) {
153 pids[i] = iter->pid;
154 i++;
155 }
156 pthread_mutex_unlock(&ltt_traceable_app_list_mutex);
157 }
This page took 0.046148 seconds and 4 git commands to generate.