Commit | Line | Data |
---|---|---|
ab33e65c PP |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
ef290fca | 18 | #define _GNU_SOURCE |
ab33e65c PP |
19 | #include <stdio.h> |
20 | #include <unistd.h> | |
21 | #include <getopt.h> | |
22 | #include <stdlib.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <dirent.h> | |
ab33e65c PP |
26 | |
27 | #include "ustcomm.h" | |
28 | #include "ustcmd.h" | |
29 | ||
08230db7 | 30 | pid_t *ustcmd_get_online_pids(void) |
772030fe | 31 | { |
08230db7 PMF |
32 | struct dirent *dirent; |
33 | DIR *dir; | |
ef290fca | 34 | unsigned int ret_size = 1 * sizeof(pid_t), i = 0; |
ab33e65c PP |
35 | |
36 | dir = opendir(SOCK_DIR); | |
37 | if (!dir) { | |
38 | return NULL; | |
39 | } | |
40 | ||
08230db7 | 41 | pid_t *ret = (pid_t *) malloc(ret_size); |
ab33e65c | 42 | |
772030fe | 43 | while ((dirent = readdir(dir))) { |
ab33e65c PP |
44 | if (!strcmp(dirent->d_name, ".") || |
45 | !strcmp(dirent->d_name, "..")) { | |
46 | ||
47 | continue; | |
48 | } | |
49 | ||
50 | if (dirent->d_type != DT_DIR && | |
51 | !!strcmp(dirent->d_name, "ustd")) { | |
52 | ||
08230db7 | 53 | sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]); |
ab33e65c PP |
54 | if (pid_is_online(ret[i])) { |
55 | ret_size += sizeof(pid_t); | |
08230db7 | 56 | ret = (pid_t *) realloc(ret, ret_size); |
ab33e65c PP |
57 | ++i; |
58 | } | |
59 | } | |
60 | } | |
61 | ||
77957c95 | 62 | ret[i] = 0; /* Array end */ |
ab33e65c | 63 | |
08230db7 PMF |
64 | if (ret[0] == 0) { |
65 | /* No PID at all */ | |
ab33e65c PP |
66 | free(ret); |
67 | return NULL; | |
68 | } | |
69 | ||
70 | closedir(dir); | |
71 | return ret; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF). | |
76 | * | |
77 | * @param mn Marker name | |
78 | * @param state Marker's new state | |
79 | * @param pid Traced process ID | |
80 | * @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG} | |
81 | */ | |
08230db7 | 82 | int ustcmd_set_marker_state(const char *mn, int state, pid_t pid) |
ef290fca | 83 | { |
08230db7 PMF |
84 | char *cmd_str [] = {"disable_marker", "enable_marker"}; |
85 | char *cmd; | |
ef290fca PMF |
86 | int result; |
87 | ||
ab33e65c PP |
88 | if (mn == NULL) { |
89 | return USTCMD_ERR_ARG; | |
90 | } | |
91 | ||
ab33e65c PP |
92 | asprintf(&cmd, "%s %s", cmd_str[state], mn); |
93 | ||
08230db7 | 94 | result = ustcmd_send_cmd(cmd, pid, NULL); |
ef290fca | 95 | if (result) { |
ab33e65c PP |
96 | free(cmd); |
97 | return USTCMD_ERR_GEN; | |
98 | } | |
99 | ||
100 | free(cmd); | |
101 | return 0; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Destroys an UST trace according to a PID. | |
106 | * | |
107 | * @param pid Traced process ID | |
108 | * @return 0 if successful, or error USTCMD_ERR_GEN | |
109 | */ | |
772030fe PMF |
110 | int ustcmd_destroy_trace(pid_t pid) |
111 | { | |
112 | int result; | |
ab33e65c | 113 | |
b5b073e2 | 114 | result = ustcmd_send_cmd("trace_destroy", pid, NULL); |
772030fe | 115 | if (result) { |
ab33e65c PP |
116 | return USTCMD_ERR_GEN; |
117 | } | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Starts an UST trace (and setups it) according to a PID. | |
124 | * | |
125 | * @param pid Traced process ID | |
126 | * @return 0 if successful, or error USTCMD_ERR_GEN | |
127 | */ | |
772030fe PMF |
128 | int ustcmd_setup_and_start(pid_t pid) |
129 | { | |
130 | int result; | |
ab33e65c | 131 | |
08230db7 | 132 | result = ustcmd_send_cmd("start", pid, NULL); |
772030fe | 133 | if (result) { |
ab33e65c PP |
134 | return USTCMD_ERR_GEN; |
135 | } | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
62ec620f PMF |
140 | /** |
141 | * Creates an UST trace according to a PID. | |
142 | * | |
143 | * @param pid Traced process ID | |
144 | * @return 0 if successful, or error USTCMD_ERR_GEN | |
145 | */ | |
146 | int ustcmd_create_trace(pid_t pid) | |
147 | { | |
148 | int result; | |
149 | ||
150 | result = ustcmd_send_cmd("trace_create", pid, NULL); | |
151 | if (result) { | |
152 | return USTCMD_ERR_GEN; | |
153 | } | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
ab33e65c PP |
158 | /** |
159 | * Starts an UST trace according to a PID. | |
160 | * | |
161 | * @param pid Traced process ID | |
162 | * @return 0 if successful, or error USTCMD_ERR_GEN | |
163 | */ | |
772030fe PMF |
164 | int ustcmd_start_trace(pid_t pid) |
165 | { | |
166 | int result; | |
ab33e65c | 167 | |
08230db7 | 168 | result = ustcmd_send_cmd("trace_start", pid, NULL); |
772030fe | 169 | if (result) { |
ab33e65c PP |
170 | return USTCMD_ERR_GEN; |
171 | } | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Stops an UST trace according to a PID. | |
178 | * | |
179 | * @param pid Traced process ID | |
180 | * @return 0 if successful, or error USTCMD_ERR_GEN | |
181 | */ | |
772030fe PMF |
182 | int ustcmd_stop_trace(pid_t pid) |
183 | { | |
184 | int result; | |
ab33e65c | 185 | |
08230db7 | 186 | result = ustcmd_send_cmd("trace_stop", pid, NULL); |
772030fe | 187 | if (result) { |
ab33e65c PP |
188 | return USTCMD_ERR_GEN; |
189 | } | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Counts newlines ('\n') in a string. | |
196 | * | |
197 | * @param str String to search in | |
198 | * @return Total newlines count | |
199 | */ | |
08230db7 | 200 | unsigned int ustcmd_count_nl(const char *str) |
772030fe | 201 | { |
ab33e65c PP |
202 | unsigned int i = 0, tot = 0; |
203 | ||
204 | while (str[i] != '\0') { | |
205 | if (str[i] == '\n') { | |
206 | ++tot; | |
207 | } | |
208 | ++i; | |
209 | } | |
210 | ||
211 | return tot; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Frees a CMSF array. | |
216 | * | |
217 | * @param cmsf CMSF array to free | |
218 | * @return 0 if successful, or error USTCMD_ERR_ARG | |
219 | */ | |
08230db7 | 220 | int ustcmd_free_cmsf(struct marker_status *cmsf) |
772030fe | 221 | { |
ab33e65c PP |
222 | if (cmsf == NULL) { |
223 | return USTCMD_ERR_ARG; | |
224 | } | |
225 | ||
226 | unsigned int i = 0; | |
227 | while (cmsf[i].channel != NULL) { | |
228 | free(cmsf[i].channel); | |
229 | free(cmsf[i].marker); | |
230 | free(cmsf[i].fs); | |
231 | ++i; | |
232 | } | |
233 | free(cmsf); | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Gets channel/marker/state/format string for a given PID. | |
240 | * | |
241 | * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller | |
242 | * frees with `ustcmd_free_cmsf') | |
243 | * @param pid Targeted PID | |
244 | * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN} | |
245 | */ | |
08230db7 | 246 | int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid) |
772030fe | 247 | { |
08230db7 | 248 | char *big_str = NULL; |
ef290fca | 249 | int result; |
08230db7 | 250 | struct marker_status *tmp_cmsf = NULL; |
ef290fca PMF |
251 | unsigned int i = 0, cmsf_ind = 0; |
252 | ||
ab33e65c PP |
253 | if (cmsf == NULL) { |
254 | return USTCMD_ERR_ARG; | |
255 | } | |
08230db7 | 256 | result = ustcmd_send_cmd("list_markers", pid, &big_str); |
ef290fca | 257 | if (result) { |
ab33e65c PP |
258 | return USTCMD_ERR_GEN; |
259 | } | |
260 | ||
261 | if (big_str == NULL) { | |
262 | fprintf(stderr, "ustcmd: error while getting markers list\n"); | |
263 | return USTCMD_ERR_GEN; | |
264 | } | |
265 | ||
08230db7 | 266 | tmp_cmsf = (struct marker_status *) malloc(sizeof(struct marker_status) * |
ab33e65c PP |
267 | (ustcmd_count_nl(big_str) + 1)); |
268 | if (tmp_cmsf == NULL) { | |
269 | return USTCMD_ERR_GEN; | |
270 | } | |
271 | ||
77957c95 | 272 | /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */ |
ab33e65c | 273 | while (big_str[i] != '\0') { |
ab33e65c | 274 | char state; |
ef290fca | 275 | |
264f6231 | 276 | sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]", |
ab33e65c PP |
277 | &tmp_cmsf[cmsf_ind].channel, |
278 | &tmp_cmsf[cmsf_ind].marker, | |
279 | &state, | |
280 | &tmp_cmsf[cmsf_ind].fs); | |
281 | tmp_cmsf[cmsf_ind].state = (state == USTCMD_MS_CHR_ON ? | |
77957c95 | 282 | USTCMD_MS_ON : USTCMD_MS_OFF); /* Marker state */ |
ab33e65c PP |
283 | |
284 | while (big_str[i] != '\n') { | |
77957c95 | 285 | ++i; /* Go to next '\n' */ |
ab33e65c | 286 | } |
77957c95 | 287 | ++i; /* Skip current pointed '\n' */ |
ab33e65c PP |
288 | ++cmsf_ind; |
289 | } | |
290 | tmp_cmsf[cmsf_ind].channel = NULL; | |
291 | tmp_cmsf[cmsf_ind].marker = NULL; | |
292 | tmp_cmsf[cmsf_ind].fs = NULL; | |
293 | ||
294 | *cmsf = tmp_cmsf; | |
295 | ||
296 | free(big_str); | |
297 | return 0; | |
298 | } | |
299 | ||
300 | /** | |
301 | * Shoots a given command using ustcomm. | |
302 | * | |
303 | * @param cmd Null-terminated command to shoot | |
304 | * @param pid Targeted PID | |
305 | * @param reply Pointer to string to be filled with a reply string (must | |
306 | * be NULL if no reply is needed for the given command). | |
307 | * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_CONN} | |
308 | */ | |
772030fe | 309 | |
08230db7 | 310 | int ustcmd_send_cmd(const char *cmd, const pid_t pid, char **reply) |
772030fe PMF |
311 | { |
312 | struct ustcomm_connection conn; | |
313 | ||
ab33e65c PP |
314 | if (cmd == NULL) { |
315 | return USTCMD_ERR_ARG; | |
316 | } | |
317 | ||
ab33e65c | 318 | if (ustcomm_connect_app(pid, &conn)) { |
08230db7 | 319 | fprintf(stderr, "ustcmd_send_cmd: could not connect to PID %u\n", |
ab33e65c PP |
320 | (unsigned int) pid); |
321 | return USTCMD_ERR_CONN; | |
322 | } | |
323 | ||
324 | ustcomm_send_request(&conn, cmd, reply); | |
325 | ||
326 | return 0; | |
327 | } |