6cecb9cf |
1 | /* lttd |
2 | * |
3 | * Linux Trace Toolkit Daemon |
4 | * |
5 | * This is a simple daemon that reads a few relayfs channels and save them in a |
6 | * trace. |
7 | * |
8 | * |
9 | * Copyright 2005 - |
10 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
11 | */ |
12 | |
13 | #include <stdio.h> |
14 | #include <errno.h> |
15 | #include <sys/types.h> |
16 | #include <sys/stat.h> |
17 | #include <unistd.h> |
18 | #include <stdlib.h> |
19 | #include <dirent.h> |
20 | #include <string.h> |
9e7dbe44 |
21 | #include <fcntl.h> |
22 | #include <sys/poll.h> |
6cecb9cf |
23 | |
24 | enum { |
25 | GET_SUBBUF, |
26 | PUT_SUBBUF, |
27 | GET_N_BUBBUFS, |
28 | GET_SUBBUF_SIZE |
29 | }; |
30 | |
31 | struct fd_pair { |
32 | int channel; |
33 | int trace; |
34 | }; |
35 | |
36 | struct channel_trace_fd { |
37 | struct fd_pair *pair; |
38 | int num_pairs; |
39 | }; |
40 | |
41 | static char *trace_name = NULL; |
42 | static char *channel_name = NULL; |
43 | static int daemon_mode = 0; |
9e7dbe44 |
44 | static int append_mode = 0; |
6cecb9cf |
45 | |
46 | /* Args : |
47 | * |
48 | * -t directory Directory name of the trace to write to. Will be created. |
49 | * -c directory Root directory of the relayfs trace channels. |
50 | * -d Run in background (daemon). |
9e7dbe44 |
51 | * -a Trace append mode. |
6cecb9cf |
52 | */ |
53 | void show_arguments(void) |
54 | { |
55 | printf("Please use the following arguments :\n"); |
56 | printf("\n"); |
57 | printf("-t directory Directory name of the trace to write to.\n" |
58 | " It will be created.\n"); |
59 | printf("-c directory Root directory of the relayfs trace channels.\n"); |
60 | printf("-d Run in background (daemon).\n"); |
9e7dbe44 |
61 | printf("-a Append to an possibly existing trace.\n"); |
6cecb9cf |
62 | printf("\n"); |
63 | } |
64 | |
65 | |
66 | /* parse_arguments |
67 | * |
68 | * Parses the command line arguments. |
69 | * |
70 | * Returns 1 if the arguments were correct, but doesn't ask for program |
71 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. |
72 | */ |
73 | int parse_arguments(int argc, char **argv) |
74 | { |
75 | int ret = 0; |
76 | int argn = 1; |
77 | |
78 | if(argc == 2) { |
79 | if(strcmp(argv[1], "-h") == 0) { |
80 | return 1; |
81 | } |
82 | } |
83 | |
9e7dbe44 |
84 | while(argn < argc) { |
6cecb9cf |
85 | |
86 | switch(argv[argn][0]) { |
87 | case '-': |
88 | switch(argv[argn][1]) { |
89 | case 't': |
9e7dbe44 |
90 | if(argn+1 < argc) { |
91 | trace_name = argv[argn+1]; |
92 | argn++; |
93 | } |
6cecb9cf |
94 | break; |
95 | case 'c': |
9e7dbe44 |
96 | if(argn+1 < argc) { |
97 | channel_name = argv[argn+1]; |
98 | argn++; |
99 | } |
6cecb9cf |
100 | break; |
101 | case 'd': |
102 | daemon_mode = 1; |
103 | break; |
9e7dbe44 |
104 | case 'a': |
105 | append_mode = 1; |
106 | break; |
6cecb9cf |
107 | default: |
108 | printf("Invalid argument '%s'.\n", argv[argn]); |
109 | printf("\n"); |
110 | ret = -1; |
111 | } |
112 | break; |
113 | default: |
114 | printf("Invalid argument '%s'.\n", argv[argn]); |
115 | printf("\n"); |
116 | ret = -1; |
117 | } |
118 | argn++; |
119 | } |
120 | |
121 | if(trace_name == NULL) { |
122 | printf("Please specify a trace name.\n"); |
123 | printf("\n"); |
124 | ret = -1; |
125 | } |
126 | |
127 | if(channel_name == NULL) { |
128 | printf("Please specify a channel name.\n"); |
129 | printf("\n"); |
130 | ret = -1; |
131 | } |
132 | |
133 | return ret; |
134 | } |
135 | |
136 | void show_info(void) |
137 | { |
138 | printf("Linux Trace Toolkit Trace Daemon\n"); |
139 | printf("\n"); |
140 | printf("Reading from relayfs directory : %s\n", channel_name); |
141 | printf("Writing to trace directory : %s\n", trace_name); |
142 | printf("\n"); |
143 | } |
144 | |
145 | |
146 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
147 | struct channel_trace_fd *fd_pairs) |
148 | { |
149 | DIR *channel_dir = opendir(subchannel_name); |
150 | struct dirent *entry; |
151 | struct stat stat_buf; |
152 | int ret; |
153 | char path_channel[PATH_MAX]; |
154 | int path_channel_len; |
155 | char *path_channel_ptr; |
156 | char path_trace[PATH_MAX]; |
157 | int path_trace_len; |
158 | char *path_trace_ptr; |
159 | |
160 | if(channel_dir == NULL) { |
161 | perror(subchannel_name); |
162 | return ENOENT; |
163 | } |
164 | |
165 | //FIXME : check if the directory already exist, and ask the user if he wants |
166 | //to append to the traces. |
167 | printf("Creating trace subdirectory %s\n", subtrace_name); |
168 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
169 | if(ret == -1) { |
9e7dbe44 |
170 | if(errno == EEXIST && append_mode) { |
171 | printf("Appending to directory %s as resquested\n", subtrace_name); |
172 | } else { |
173 | perror(subtrace_name); |
174 | return -1; |
175 | } |
6cecb9cf |
176 | } |
177 | |
178 | strncpy(path_channel, subchannel_name, PATH_MAX-1); |
179 | path_channel_len = strlen(path_channel); |
180 | path_channel[path_channel_len] = '/'; |
181 | path_channel_len++; |
182 | path_channel_ptr = path_channel + path_channel_len; |
183 | |
184 | strncpy(path_trace, subtrace_name, PATH_MAX-1); |
185 | path_trace_len = strlen(path_trace); |
186 | path_trace[path_trace_len] = '/'; |
187 | path_trace_len++; |
188 | path_trace_ptr = path_trace + path_trace_len; |
189 | |
190 | while((entry = readdir(channel_dir)) != NULL) { |
191 | |
192 | if(entry->d_name[0] == '.') continue; |
193 | |
194 | strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); |
195 | strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len); |
196 | |
197 | ret = stat(path_channel, &stat_buf); |
198 | if(ret == -1) { |
199 | perror(path_channel); |
200 | continue; |
201 | } |
202 | |
203 | printf("Channel file : %s\n", path_channel); |
204 | |
205 | if(S_ISDIR(stat_buf.st_mode)) { |
206 | |
207 | printf("Entering channel subdirectory...\n"); |
208 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs); |
209 | if(ret < 0) continue; |
9e7dbe44 |
210 | } else if(S_ISREG(stat_buf.st_mode)) { |
211 | printf("Opening file.\n"); |
212 | |
213 | fd_pairs->pair = realloc(fd_pairs->pair, |
214 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); |
215 | |
216 | /* Open the channel in read mode */ |
217 | fd_pairs->pair[fd_pairs->num_pairs-1].channel = |
218 | open(path_channel, O_RDONLY | O_NONBLOCK); |
219 | if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { |
220 | perror(path_channel); |
221 | } |
222 | /* Open the trace in write mode, only append if append_mode */ |
223 | ret = stat(path_trace, &stat_buf); |
224 | if(ret == 0) { |
225 | if(append_mode) { |
226 | printf("Appending to file %s as resquested\n", path_trace); |
227 | |
228 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
229 | open(path_trace, O_WRONLY|O_APPEND, |
230 | S_IRWXU|S_IRWXG|S_IRWXO); |
231 | |
232 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
233 | perror(path_trace); |
234 | } |
235 | } else { |
236 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
237 | return -1; |
238 | } |
239 | } else { |
240 | if(errno == ENOENT) { |
241 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
242 | open(path_trace, O_WRONLY|O_CREAT|O_EXCL, |
243 | S_IRWXU|S_IRWXG|S_IRWXO); |
244 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
245 | perror(path_trace); |
246 | } |
247 | } |
248 | } |
6cecb9cf |
249 | } |
6cecb9cf |
250 | } |
251 | |
252 | closedir(channel_dir); |
253 | |
254 | return 0; |
255 | } |
256 | |
9e7dbe44 |
257 | /* read_channels |
258 | * |
259 | * Read the realyfs channels and write them in the paired tracefiles. |
260 | * |
261 | * @fd_pairs : paired channels and trace files. |
262 | * |
263 | * returns 0 on success, -1 on error. |
264 | * |
265 | * Note that the high priority polled channels are consumed first. We then poll |
266 | * again to see if these channels are still in priority. Only when no |
267 | * high priority channel is left, we start reading low priority channels. |
268 | * |
269 | * Note that a channel is considered high priority when the buffer is almost |
270 | * full. |
271 | */ |
6cecb9cf |
272 | |
273 | int read_channels(struct channel_trace_fd *fd_pairs) |
274 | { |
9e7dbe44 |
275 | struct pollfd *pollfd; |
276 | int i; |
277 | int num_rdy; |
68047576 |
278 | int high_prio; |
9e7dbe44 |
279 | |
280 | pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); |
281 | |
282 | for(i=0;i<fd_pairs->num_pairs;i++) { |
283 | pollfd[i].fd = fd_pairs->pair[i].channel; |
284 | pollfd[i].events = POLLIN|POLLPRI; |
285 | } |
286 | |
287 | while(1) { |
68047576 |
288 | high_prio = 0; |
289 | |
9e7dbe44 |
290 | num_rdy = poll(pollfd, fd_pairs->num_pairs, -1); |
291 | if(num_rdy == -1) { |
292 | perror("Poll error"); |
293 | return -1; |
294 | } |
295 | |
296 | printf("Data received\n"); |
297 | |
298 | for(i=0;i<fd_pairs->num_pairs;i++) { |
299 | switch(pollfd[i].revents) { |
300 | case POLLERR: |
301 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
302 | break; |
303 | case POLLHUP: |
304 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
305 | break; |
306 | case POLLNVAL: |
307 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
308 | break; |
309 | case POLLPRI: |
310 | /* Take care of high priority channels first. */ |
68047576 |
311 | high_prio = 1; |
9e7dbe44 |
312 | break; |
68047576 |
313 | } |
9e7dbe44 |
314 | } |
315 | |
68047576 |
316 | if(!high_prio) { |
317 | for(i=0;i<fd_pairs->num_pairs;i++) { |
318 | switch(pollfd[i].revents) { |
319 | case POLLIN: |
320 | /* Take care of low priority channels. */ |
321 | break; |
322 | } |
323 | } |
9e7dbe44 |
324 | } |
325 | |
326 | } |
327 | |
328 | free(pollfd); |
329 | |
6cecb9cf |
330 | return 0; |
331 | } |
332 | |
333 | |
334 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) |
335 | { |
9e7dbe44 |
336 | int i; |
337 | int ret; |
6cecb9cf |
338 | |
9e7dbe44 |
339 | for(i=0;i<fd_pairs->num_pairs;i++) { |
340 | ret = close(fd_pairs->pair[i].channel); |
341 | if(ret == -1) perror("Close error on channel"); |
342 | ret = close(fd_pairs->pair[i].trace); |
343 | if(ret == -1) perror("Close error on trace"); |
344 | } |
345 | free(fd_pairs->pair); |
6cecb9cf |
346 | } |
347 | |
348 | int main(int argc, char ** argv) |
349 | { |
350 | int ret; |
351 | pid_t pid; |
352 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
353 | |
354 | ret = parse_arguments(argc, argv); |
355 | |
356 | if(ret != 0) show_arguments(); |
357 | if(ret < 0) return EINVAL; |
358 | if(ret > 0) return 0; |
359 | |
360 | show_info(); |
361 | |
362 | if(daemon_mode) { |
363 | pid = fork(); |
364 | |
365 | if(pid > 0) { |
366 | /* parent */ |
367 | return 0; |
368 | } else if(pid < 0) { |
369 | /* error */ |
370 | printf("An error occured while forking.\n"); |
371 | return -1; |
372 | } |
373 | /* else, we are the child, continue... */ |
374 | } |
375 | |
376 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) |
9e7dbe44 |
377 | goto close_channel; |
6cecb9cf |
378 | |
379 | ret = read_channels(&fd_pairs); |
380 | |
9e7dbe44 |
381 | close_channel: |
6cecb9cf |
382 | close_channel_trace_pairs(&fd_pairs); |
383 | |
6cecb9cf |
384 | return ret; |
385 | } |