compile fix
[ltt-control.git] / ltt / branches / poly / lttd / lttd.c
CommitLineData
617de8e1 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>
90ccaa9a 21#include <fcntl.h>
22#include <sys/poll.h>
617de8e1 23
24enum {
25 GET_SUBBUF,
26 PUT_SUBBUF,
27 GET_N_BUBBUFS,
28 GET_SUBBUF_SIZE
29};
30
31struct fd_pair {
32 int channel;
33 int trace;
34};
35
36struct channel_trace_fd {
37 struct fd_pair *pair;
38 int num_pairs;
39};
40
41static char *trace_name = NULL;
42static char *channel_name = NULL;
43static int daemon_mode = 0;
90ccaa9a 44static int append_mode = 0;
617de8e1 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).
90ccaa9a 51 * -a Trace append mode.
617de8e1 52 */
53void 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");
90ccaa9a 61 printf("-a Append to an possibly existing trace.\n");
617de8e1 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 */
73int 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
90ccaa9a 84 while(argn < argc) {
617de8e1 85
86 switch(argv[argn][0]) {
87 case '-':
88 switch(argv[argn][1]) {
89 case 't':
90ccaa9a 90 if(argn+1 < argc) {
91 trace_name = argv[argn+1];
92 argn++;
93 }
617de8e1 94 break;
95 case 'c':
90ccaa9a 96 if(argn+1 < argc) {
97 channel_name = argv[argn+1];
98 argn++;
99 }
617de8e1 100 break;
101 case 'd':
102 daemon_mode = 1;
103 break;
90ccaa9a 104 case 'a':
105 append_mode = 1;
106 break;
617de8e1 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
136void 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
146int 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) {
90ccaa9a 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 }
617de8e1 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;
90ccaa9a 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 }
617de8e1 249 }
617de8e1 250 }
251
252 closedir(channel_dir);
253
254 return 0;
255}
256
90ccaa9a 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 */
617de8e1 272
273int read_channels(struct channel_trace_fd *fd_pairs)
274{
90ccaa9a 275 struct pollfd *pollfd;
276 int i;
277 int num_rdy;
4f45ea55 278 int high_prio;
90ccaa9a 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) {
4f45ea55 288 high_prio = 0;
289
90ccaa9a 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. */
4f45ea55 311 high_prio = 1;
90ccaa9a 312 break;
4f45ea55 313 }
90ccaa9a 314 }
315
4f45ea55 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 }
90ccaa9a 324 }
325
326 }
327
328 free(pollfd);
329
617de8e1 330 return 0;
331}
332
333
334void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
335{
90ccaa9a 336 int i;
337 int ret;
617de8e1 338
90ccaa9a 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);
617de8e1 346}
347
348int 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))
90ccaa9a 377 goto close_channel;
617de8e1 378
379 ret = read_channels(&fd_pairs);
380
90ccaa9a 381close_channel:
617de8e1 382 close_channel_trace_pairs(&fd_pairs);
383
617de8e1 384 return ret;
385}
This page took 0.050556 seconds and 4 git commands to generate.