Commit | Line | Data |
---|---|---|
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 | ||
91d76f53 DG |
29 | #include "traceable-app.h" |
30 | ||
91d76f53 | 31 | /* Init ust traceabl application's list */ |
099e26bd | 32 | static struct ltt_traceable_app_list ltt_traceable_app_list = { |
91d76f53 | 33 | .head = CDS_LIST_HEAD_INIT(ltt_traceable_app_list.head), |
099e26bd DG |
34 | .lock = PTHREAD_MUTEX_INITIALIZER, |
35 | .count = 0, | |
91d76f53 DG |
36 | }; |
37 | ||
91d76f53 | 38 | /* |
099e26bd | 39 | * Add a traceable application structure to the global list. |
91d76f53 DG |
40 | */ |
41 | static void add_traceable_app(struct ltt_traceable_app *lta) | |
42 | { | |
91d76f53 | 43 | cds_list_add(<a->list, <t_traceable_app_list.head); |
099e26bd | 44 | ltt_traceable_app_list.count++; |
91d76f53 DG |
45 | } |
46 | ||
47 | /* | |
099e26bd | 48 | * Delete a traceable application structure from the global list. |
91d76f53 DG |
49 | */ |
50 | static void del_traceable_app(struct ltt_traceable_app *lta) | |
51 | { | |
44d3bd01 DG |
52 | struct ltt_ust_channel *chan; |
53 | ||
91d76f53 DG |
54 | cds_list_del(<a->list); |
55 | /* Sanity check */ | |
099e26bd DG |
56 | if (ltt_traceable_app_list.count > 0) { |
57 | ltt_traceable_app_list.count--; | |
58 | } | |
44d3bd01 DG |
59 | |
60 | cds_list_for_each_entry(chan, <a->channels.head, list) { | |
61 | trace_ust_destroy_channel(chan); | |
62 | } | |
099e26bd DG |
63 | } |
64 | ||
65 | /* | |
66 | * Return pointer to traceable apps list. | |
67 | */ | |
68 | struct ltt_traceable_app_list *get_traceable_apps_list(void) | |
69 | { | |
70 | return <t_traceable_app_list; | |
71 | } | |
72 | ||
73 | /* | |
74 | * Acquire traceable apps list lock. | |
75 | */ | |
76 | void lock_apps_list(void) | |
77 | { | |
78 | pthread_mutex_lock(<t_traceable_app_list.lock); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Release traceable apps list lock. | |
83 | */ | |
84 | void unlock_apps_list(void) | |
85 | { | |
86 | pthread_mutex_unlock(<t_traceable_app_list.lock); | |
87 | } | |
88 | ||
89 | /* | |
90 | * Iterate over the traceable apps list and return a pointer or NULL if not | |
91 | * found. | |
92 | */ | |
93 | static struct ltt_traceable_app *find_app_by_sock(int sock) | |
94 | { | |
95 | struct ltt_traceable_app *iter; | |
96 | ||
97 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { | |
98 | if (iter->sock == sock) { | |
99 | /* Found */ | |
100 | return iter; | |
101 | } | |
91d76f53 | 102 | } |
099e26bd DG |
103 | |
104 | return NULL; | |
91d76f53 DG |
105 | } |
106 | ||
0177d773 DG |
107 | /* |
108 | * Iterate over the traceable apps list and return a pointer or NULL if not | |
109 | * found. | |
110 | */ | |
111 | struct ltt_traceable_app *traceable_app_get_by_pid(pid_t pid) | |
112 | { | |
113 | struct ltt_traceable_app *iter; | |
114 | ||
115 | cds_list_for_each_entry(iter, <t_traceable_app_list.head, list) { | |
116 | if (iter->pid == pid) { | |
117 | /* Found */ | |
44d3bd01 | 118 | DBG2("Found traceable app by pid %d", pid); |
0177d773 DG |
119 | return iter; |
120 | } | |
121 | } | |
122 | ||
44d3bd01 DG |
123 | DBG2("Traceable app with pid %d not found", pid); |
124 | ||
0177d773 DG |
125 | return NULL; |
126 | } | |
127 | ||
91d76f53 | 128 | /* |
050349bb DG |
129 | * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and |
130 | * add it to the global traceable app list. | |
91d76f53 | 131 | * |
050349bb | 132 | * On success, return 0, else return malloc ENOMEM. |
91d76f53 | 133 | */ |
099e26bd | 134 | int register_traceable_app(struct ust_register_msg *msg, int sock) |
91d76f53 DG |
135 | { |
136 | struct ltt_traceable_app *lta; | |
137 | ||
138 | lta = malloc(sizeof(struct ltt_traceable_app)); | |
139 | if (lta == NULL) { | |
140 | perror("malloc"); | |
141 | return -ENOMEM; | |
142 | } | |
143 | ||
099e26bd DG |
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'; | |
44d3bd01 | 153 | CDS_INIT_LIST_HEAD(<a->channels.head); |
099e26bd DG |
154 | |
155 | lock_apps_list(); | |
91d76f53 | 156 | add_traceable_app(lta); |
099e26bd DG |
157 | unlock_apps_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); | |
91d76f53 DG |
162 | |
163 | return 0; | |
164 | } | |
165 | ||
166 | /* | |
050349bb DG |
167 | * Unregister app by removing it from the global traceable app list and freeing |
168 | * the data struct. | |
099e26bd DG |
169 | * |
170 | * The socket is already closed at this point so no close to sock. | |
91d76f53 | 171 | */ |
099e26bd | 172 | void unregister_traceable_app(int sock) |
91d76f53 DG |
173 | { |
174 | struct ltt_traceable_app *lta; | |
175 | ||
099e26bd DG |
176 | lock_apps_list(); |
177 | lta = find_app_by_sock(sock); | |
178 | if (lta) { | |
179 | DBG("PID %d unregistered with sock %d", lta->pid, sock); | |
91d76f53 | 180 | del_traceable_app(lta); |
44d3bd01 | 181 | close(lta->sock); |
91d76f53 | 182 | free(lta); |
91d76f53 | 183 | } |
099e26bd | 184 | unlock_apps_list(); |
91d76f53 DG |
185 | } |
186 | ||
187 | /* | |
050349bb | 188 | * Return traceable_app_count |
91d76f53 DG |
189 | */ |
190 | unsigned int get_app_count(void) | |
191 | { | |
099e26bd | 192 | unsigned int count; |
91d76f53 | 193 | |
099e26bd DG |
194 | lock_apps_list(); |
195 | count = ltt_traceable_app_list.count; | |
196 | unlock_apps_list(); | |
91d76f53 | 197 | |
099e26bd | 198 | return count; |
91d76f53 DG |
199 | } |
200 | ||
201 | /* | |
099e26bd | 202 | * Free and clean all traceable apps of the global list. |
91d76f53 | 203 | */ |
099e26bd | 204 | void clean_traceable_apps_list(void) |
91d76f53 | 205 | { |
099e26bd | 206 | struct ltt_traceable_app *iter, *tmp; |
91d76f53 | 207 | |
099e26bd DG |
208 | /* |
209 | * Don't acquire list lock here. This function should be called from | |
210 | * cleanup() functions meaning that the program will exit. | |
91d76f53 | 211 | */ |
099e26bd | 212 | cds_list_for_each_entry_safe(iter, tmp, <t_traceable_app_list.head, list) { |
44d3bd01 | 213 | del_traceable_app(iter); |
099e26bd DG |
214 | close(iter->sock); |
215 | free(iter); | |
91d76f53 | 216 | } |
91d76f53 | 217 | } |