[-]
[+]
|
Changed |
_service:tar_git:yamui-1.1.2.tar.gz/main.c
^
|
@@ -1,10 +1,12 @@
-#define _BSD_SOURCE 700
+#define _DEFAULT_SOURCE
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
+#include <signal.h>
+#include <sys/signalfd.h>
#include <sys/select.h>
#include "os-update.h"
@@ -24,6 +26,30 @@
/* ------------------------------------------------------------------------ */
+static int
+wait_signalfd(int sigfd, unsigned long long int msecs)
+{
+ int ret;
+ fd_set fdset;
+ struct timespec ts = {
+ .tv_sec = msecs / 1000,
+ .tv_nsec = (msecs % 1000) * 1000000
+ };
+
+ FD_ZERO(&fdset);
+ if (sigfd >= 0)
+ FD_SET(sigfd, &fdset);
+
+ ret = pselect(sigfd + 1, &fdset, NULL, NULL, msecs ? &ts : NULL, NULL);
+ if (ret > 0)
+ printf("Interrupted, bailing out\n");
+ else if (ret == -1)
+ printf("An error occured, bailing out\n");
+ return ret;
+}
+
+/* ------------------------------------------------------------------------ */
+
static void
short_help(void)
{
@@ -88,6 +114,10 @@
int image_count = 0;
int ret = 0;
int i = 0;
+ int sigfd = -1;
+ sigset_t mask;
+
+ setlinebuf(stdout);
while (1) {
c = getopt_long(argc, argv, "a:i:p:s:t:h", options,
@@ -134,6 +164,20 @@
if (osUpdateScreenInit())
return -1;
+ /* Allow SIGTERM and SIGINT to interrupt pselect() and move to cleanup */
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGTERM);
+ sigaddset(&mask, SIGINT);
+ sigfd = signalfd(-1, &mask, 0);
+ if (sigfd == -1) {
+ printf("Could not create signal fd\n");
+ goto cleanup;
+ }
+ if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
+ printf("Could not block signals\n");
+ goto cleanup;
+ }
+
/* In case there is text to add, add it to both sides of the "flip" */
add_text(text);
@@ -146,10 +190,7 @@
}
showLogo();
- if (stop_ms)
- usleep(stop_ms * 1000);
- else
- select(0, NULL, NULL, NULL, NULL);
+ wait_signalfd(sigfd, stop_ms);
goto cleanup;
}
@@ -158,9 +199,10 @@
if (image_count == 1)
loadLogo(images[0], images_dir);
i = 0;
- while (i <= 100){
+ while (i <= 100) {
osUpdateScreenShowProgress(i);
- usleep(1000 * progress_ms / 100);
+ if (wait_signalfd(sigfd, progress_ms / 100))
+ break;
i++;
}
@@ -182,10 +224,7 @@
goto cleanup;
}
- if (stop_ms)
- never_stop = false;
- else
- never_stop = true;
+ never_stop = !stop_ms;
i = 0;
while (never_stop || time_left > 0) {
@@ -197,7 +236,8 @@
}
showLogo();
- usleep(1000 * period);
+ if (wait_signalfd(sigfd, period))
+ break;
time_left -= period;
i++;
i = i % image_count;
@@ -207,14 +247,13 @@
}
if (text) {
- if (stop_ms)
- usleep(1000 * stop_ms);
- else
- select(0, NULL, NULL, NULL, NULL);
+ wait_signalfd(sigfd, stop_ms);
goto cleanup;
}
cleanup:
+ if (sigfd != -1)
+ close(sigfd);
osUpdateScreenExit();
out:
return ret;
|