fc4e4472e09806d4b698e2c613c6dec8022b1fa2
[ltt-control.git] / ltt / branches / poly / lttd / lttd.c
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>
21 #include <fcntl.h>
22 #include <sys/poll.h>
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;
44 static int append_mode = 0;
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).
51 * -a Trace append mode.
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");
61 printf("-a Append to an possibly existing trace.\n");
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
84 while(argn < argc) {
85
86 switch(argv[argn][0]) {
87 case '-':
88 switch(argv[argn][1]) {
89 case 't':
90 if(argn+1 < argc) {
91 trace_name = argv[argn+1];
92 argn++;
93 }
94 break;
95 case 'c':
96 if(argn+1 < argc) {
97 channel_name = argv[argn+1];
98 argn++;
99 }
100 break;
101 case 'd':
102 daemon_mode = 1;
103 break;
104 case 'a':
105 append_mode = 1;
106 break;
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) {
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 }
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;
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 }
249 }
250 }
251
252 closedir(channel_dir);
253
254 return 0;
255 }
256
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 */
272
273 int read_channels(struct channel_trace_fd *fd_pairs)
274 {
275 struct pollfd *pollfd;
276 int i;
277 int num_rdy;
278
279 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
280
281 for(i=0;i<fd_pairs->num_pairs;i++) {
282 pollfd[i].fd = fd_pairs->pair[i].channel;
283 pollfd[i].events = POLLIN|POLLPRI;
284 }
285
286 while(1) {
287
288 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
289 if(num_rdy == -1) {
290 perror("Poll error");
291 return -1;
292 }
293
294 printf("Data received\n");
295
296 for(i=0;i<fd_pairs->num_pairs;i++) {
297 switch(pollfd[i].revents) {
298 case POLLERR:
299 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
300 break;
301 case POLLHUP:
302 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
303 break;
304 case POLLNVAL:
305 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
306 break;
307 case POLLPRI:
308 /* Take care of high priority channels first. */
309 break;
310 default:
311 }
312
313 for(i=0;i<fd_pairs->num_pairs;i++) {
314 switch(pollfd[i].revents) {
315 case POLLIN:
316 /* Take care of low priority channels. */
317 break;
318 default:
319 }
320
321 }
322
323 free(pollfd);
324
325 return 0;
326 }
327
328
329 void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
330 {
331 int i;
332 int ret;
333
334 for(i=0;i<fd_pairs->num_pairs;i++) {
335 ret = close(fd_pairs->pair[i].channel);
336 if(ret == -1) perror("Close error on channel");
337 ret = close(fd_pairs->pair[i].trace);
338 if(ret == -1) perror("Close error on trace");
339 }
340 free(fd_pairs->pair);
341 }
342
343 int main(int argc, char ** argv)
344 {
345 int ret;
346 pid_t pid;
347 struct channel_trace_fd fd_pairs = { NULL, 0 };
348
349 ret = parse_arguments(argc, argv);
350
351 if(ret != 0) show_arguments();
352 if(ret < 0) return EINVAL;
353 if(ret > 0) return 0;
354
355 show_info();
356
357 if(daemon_mode) {
358 pid = fork();
359
360 if(pid > 0) {
361 /* parent */
362 return 0;
363 } else if(pid < 0) {
364 /* error */
365 printf("An error occured while forking.\n");
366 return -1;
367 }
368 /* else, we are the child, continue... */
369 }
370
371 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
372 goto close_channel;
373
374 ret = read_channels(&fd_pairs);
375
376 close_channel:
377 close_channel_trace_pairs(&fd_pairs);
378
379 return ret;
380 }
This page took 0.094129 seconds and 3 git commands to generate.