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 | ||
56fff090 | 29 | #include "ust-app.h" |
91d76f53 | 30 | |
56fff090 DG |
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), | |
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 | 40 | */ |
56fff090 | 41 | static void add_app_to_list(struct ust_app *lta) |
91d76f53 | 42 | { |
56fff090 DG |
43 | cds_list_add(<a->list, &ust_app_list.head); |
44 | ust_app_list.count++; | |
91d76f53 DG |
45 | } |
46 | ||
47 | /* | |
099e26bd | 48 | * Delete a traceable application structure from the global list. |
91d76f53 | 49 | */ |
56fff090 | 50 | static void del_app_from_list(struct ust_app *lta) |
91d76f53 | 51 | { |
44d3bd01 DG |
52 | struct ltt_ust_channel *chan; |
53 | ||
91d76f53 DG |
54 | cds_list_del(<a->list); |
55 | /* Sanity check */ | |
56fff090 DG |
56 | if (ust_app_list.count > 0) { |
57 | ust_app_list.count--; | |
099e26bd | 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 | /* | |
56fff090 DG |
66 | * Iterate over the traceable apps list and return a pointer or NULL if not |
67 | * found. | |
099e26bd | 68 | */ |
56fff090 | 69 | static struct ust_app *find_app_by_sock(int sock) |
099e26bd | 70 | { |
56fff090 DG |
71 | struct ust_app *iter; |
72 | ||
73 | cds_list_for_each_entry(iter, &ust_app_list.head, list) { | |
74 | if (iter->sock == sock) { | |
75 | /* Found */ | |
76 | return iter; | |
77 | } | |
78 | } | |
79 | ||
80 | return NULL; | |
099e26bd DG |
81 | } |
82 | ||
83 | /* | |
56fff090 | 84 | * Return pointer to traceable apps list. |
099e26bd | 85 | */ |
56fff090 | 86 | struct ust_app_list *ust_app_get_list(void) |
099e26bd | 87 | { |
56fff090 | 88 | return &ust_app_list; |
099e26bd DG |
89 | } |
90 | ||
91 | /* | |
56fff090 | 92 | * Acquire traceable apps list lock. |
099e26bd | 93 | */ |
56fff090 | 94 | void ust_app_lock_list(void) |
099e26bd | 95 | { |
56fff090 | 96 | pthread_mutex_lock(&ust_app_list.lock); |
099e26bd DG |
97 | } |
98 | ||
99 | /* | |
56fff090 | 100 | * Release traceable apps list lock. |
099e26bd | 101 | */ |
56fff090 | 102 | void ust_app_unlock_list(void) |
099e26bd | 103 | { |
56fff090 | 104 | pthread_mutex_unlock(&ust_app_list.lock); |
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 | */ | |
56fff090 | 111 | struct ust_app *ust_app_get_by_pid(pid_t pid) |
0177d773 | 112 | { |
56fff090 | 113 | struct ust_app *iter; |
0177d773 | 114 | |
56fff090 | 115 | cds_list_for_each_entry(iter, &ust_app_list.head, list) { |
0177d773 DG |
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 | /* |
56fff090 | 129 | * Using pid and uid (of the app), allocate a new ust_app struct and |
050349bb | 130 | * add it to the global traceable app list. |
91d76f53 | 131 | * |
050349bb | 132 | * On success, return 0, else return malloc ENOMEM. |
91d76f53 | 133 | */ |
56fff090 | 134 | int ust_app_register(struct ust_register_msg *msg, int sock) |
91d76f53 | 135 | { |
56fff090 | 136 | struct ust_app *lta; |
91d76f53 | 137 | |
56fff090 | 138 | lta = malloc(sizeof(struct ust_app)); |
91d76f53 DG |
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 | 154 | |
56fff090 DG |
155 | ust_app_lock_list(); |
156 | add_app_to_list(lta); | |
157 | ust_app_unlock_list(); | |
099e26bd DG |
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 | */ |
56fff090 | 172 | void ust_app_unregister(int sock) |
91d76f53 | 173 | { |
56fff090 | 174 | struct ust_app *lta; |
91d76f53 | 175 | |
56fff090 | 176 | ust_app_lock_list(); |
099e26bd DG |
177 | lta = find_app_by_sock(sock); |
178 | if (lta) { | |
179 | DBG("PID %d unregistered with sock %d", lta->pid, sock); | |
56fff090 | 180 | del_app_from_list(lta); |
44d3bd01 | 181 | close(lta->sock); |
91d76f53 | 182 | free(lta); |
91d76f53 | 183 | } |
56fff090 | 184 | ust_app_unlock_list(); |
91d76f53 DG |
185 | } |
186 | ||
187 | /* | |
050349bb | 188 | * Return traceable_app_count |
91d76f53 | 189 | */ |
56fff090 | 190 | unsigned int ust_app_list_count(void) |
91d76f53 | 191 | { |
099e26bd | 192 | unsigned int count; |
91d76f53 | 193 | |
56fff090 DG |
194 | ust_app_lock_list(); |
195 | count = ust_app_list.count; | |
196 | ust_app_unlock_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 | */ |
56fff090 | 204 | void ust_app_clean_list(void) |
91d76f53 | 205 | { |
56fff090 | 206 | struct ust_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 | */ |
56fff090 DG |
212 | cds_list_for_each_entry_safe(iter, tmp, &ust_app_list.head, list) { |
213 | del_app_from_list(iter); | |
099e26bd DG |
214 | close(iter->sock); |
215 | free(iter); | |
91d76f53 | 216 | } |
91d76f53 | 217 | } |