summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jussi.pakkanen@canonical.com>2011-06-28 18:54:35 +0300
committerJussi Pakkanen <jussi.pakkanen@canonical.com>2011-06-28 18:54:35 +0300
commitd2ef7446d7243518d41b1fd0e6a1fd2481011beb (patch)
tree4451ef8c28a7c30da622d4b10fda70f0439f187d
parent4b99c44b90aba3800246ed145270b53282a14e55 (diff)
Add signal handler to flush data in case of abrupt exit.
-rw-r--r--tools/evemu-record.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/tools/evemu-record.c b/tools/evemu-record.c
index 1acb06f..25e7b05 100644
--- a/tools/evemu-record.c
+++ b/tools/evemu-record.c
@@ -45,13 +45,22 @@
#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;
- FILE *out;
if (argc < 2) {
fprintf(stderr, "Usage: %s <device> [output file]\n", argv[0]);
return -1;
@@ -62,20 +71,33 @@ int main(int argc, char *argv[])
return -1;
}
+ 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)
- out = stdout;
+ output = stdout;
else {
- out = fopen(argv[2], "w");
- if (!out) {
+ output = fopen(argv[2], "w");
+ if (!output) {
fprintf(stderr, "error: could not open output file");
}
}
- if (evemu_record(out, fd, WAIT_MS)) {
+ if (evemu_record(output, fd, WAIT_MS)) {
fprintf(stderr, "error: could not describe device\n");
}
close(fd);
- if (argc > 2)
- fclose(out);
+ if (output != stdout)
+ fclose(output);
return 0;
}