summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jussi.pakkanen@canonical.com>2011-06-29 12:27:32 +0300
committerJussi Pakkanen <jussi.pakkanen@canonical.com>2011-06-29 12:27:32 +0300
commit86bf36db6e4edcbd306b919b5b25206c9187797d (patch)
treeaa53a02addc7fb3f0f674a0ffbf0b4606fdc08e7
parentceb61a1bf2b683c2ea059bac1e66ba4a134de8c0 (diff)
parentd2f570cc2d844667233039cf8759329d67053a68 (diff)
Merged robustness branch.
-rw-r--r--src/evemu.c1
-rw-r--r--tools/evemu-record.c39
2 files changed, 38 insertions, 2 deletions
diff --git a/src/evemu.c b/src/evemu.c
index 21187af..f1a9eea 100644
--- a/src/evemu.c
+++ b/src/evemu.c
@@ -379,6 +379,7 @@ int evemu_record(FILE *fp, int fd, int ms)
return ret;
if (ret == sizeof(ev))
evemu_write_event(fp, &ev);
+ fflush(fp);
}
return 0;
diff --git a/tools/evemu-record.c b/tools/evemu-record.c
index afdd400..7cf1161 100644
--- a/tools/evemu-record.c
+++ b/tools/evemu-record.c
@@ -45,14 +45,24 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
+#include <signal.h>
#define WAIT_MS 10000
+FILE *output;
+
+static void handler (int sig)
+{
+ fflush(output);
+ if (output != stdout)
+ fclose(output);
+}
+
int main(int argc, char *argv[])
{
int fd;
if (argc < 2) {
- fprintf(stderr, "Usage: %s <device>\n", argv[0]);
+ fprintf(stderr, "Usage: %s <device> [output file]\n", argv[0]);
return -1;
}
fd = open(argv[1], O_RDONLY | O_NONBLOCK);
@@ -60,9 +70,34 @@ int main(int argc, char *argv[])
fprintf(stderr, "error: could not open device\n");
return -1;
}
- if (evemu_record(stdout, fd, WAIT_MS)) {
+
+ struct sigaction act;
+ memset (&act, '\0', sizeof(act));
+ act.sa_handler = &handler;
+
+ if (sigaction(SIGTERM, &act, NULL) < 0) {
+ fprintf (stderr, "Could not attach TERM signal handler.\n");
+ return 1;
+ }
+ if (sigaction(SIGINT, &act, NULL) < 0) {
+ fprintf (stderr, "Could not attach INT signal handler.\n");
+ return 1;
+ }
+
+ if (argc < 3)
+ output = stdout;
+ else {
+ output = fopen(argv[2], "w");
+ if (!output) {
+ fprintf(stderr, "error: could not open output file");
+ }
+ }
+
+ if (evemu_record(output, fd, WAIT_MS)) {
fprintf(stderr, "error: could not describe device\n");
}
close(fd);
+ if (output != stdout)
+ fclose(output);
return 0;
}