All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, perex@perex.cz
Cc: alsa-devel@alsa-project.org
Subject: [alsa-utils][PATCH 9/9] alsactl: use signalfd to catch UNIX signal
Date: Sun, 14 Oct 2018 23:36:34 +0900	[thread overview]
Message-ID: <20181014143634.26066-10-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181014143634.26066-1-o-takashi@sakamocchi.jp>

In a mode of 'monitor, event loop runs to dispatch asynchronous event
emitted by control node. In this case, UNIX signal is used to terminate
the event loop.

This commit uses signalfd to catch the UNIX signal.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 alsactl/monitor.c | 57 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 7 deletions(-)

diff --git a/alsactl/monitor.c b/alsactl/monitor.c
index a3c3ea4..282fb1c 100644
--- a/alsactl/monitor.c
+++ b/alsactl/monitor.c
@@ -26,6 +26,8 @@
 #include <sys/inotify.h>
 #include <limits.h>
 #include <time.h>
+#include <signal.h>
+#include <sys/signalfd.h>
 #include <alsa/asoundlib.h>
 
 #include <stddef.h>
@@ -290,12 +292,18 @@ end:
 	return err;
 }
 
-static int prepare_dispatcher(int epfd, int infd, struct list_head *srcs)
+static int prepare_dispatcher(int epfd, int sigfd, int infd,
+			      struct list_head *srcs)
 {
 	struct epoll_event ev = {0};
 	struct src_entry *entry;
 	int err = 0;
 
+	ev.events = EPOLLIN;
+	ev.data.fd = sigfd;
+	if (epoll_ctl(epfd, EPOLL_CTL_ADD, sigfd, &ev) < 0)
+		return -errno;
+
 	ev.events = EPOLLIN;
 	ev.data.fd = infd;
 	if (epoll_ctl(epfd, EPOLL_CTL_ADD, infd, &ev) < 0)
@@ -312,7 +320,7 @@ static int prepare_dispatcher(int epfd, int infd, struct list_head *srcs)
 	return err;
 }
 
-static int run_dispatcher(int epfd, int infd, struct list_head *srcs,
+static int run_dispatcher(int epfd, int sigfd, int infd, struct list_head *srcs,
 			  bool *retry)
 {
 	struct src_entry *entry;
@@ -343,6 +351,9 @@ static int run_dispatcher(int epfd, int infd, struct list_head *srcs,
 		for (i = 0; i < count; ++i) {
 			struct epoll_event *ev = epev + i;
 
+			if (ev->data.fd == sigfd)
+				goto end;
+
 			if (ev->data.fd == infd) {
 				err = check_control_cdev(infd, retry);
 				if (err < 0 || *retry)
@@ -366,7 +377,8 @@ end:
 	return err;
 }
 
-static void clear_dispatcher(int epfd, int infd, struct list_head *srcs)
+static void clear_dispatcher(int epfd, int sigfd, int infd,
+			     struct list_head *srcs)
 {
 	struct src_entry *entry;
 
@@ -374,20 +386,49 @@ static void clear_dispatcher(int epfd, int infd, struct list_head *srcs)
 		operate_dispatcher(epfd, EPOLL_CTL_DEL, NULL, entry);
 
 	epoll_ctl(epfd, EPOLL_CTL_DEL, infd, NULL);
+
+	epoll_ctl(epfd, EPOLL_CTL_DEL, sigfd, NULL);
+}
+
+static int prepare_signalfd(int *sigfd)
+{
+	sigset_t mask;
+	int fd;
+
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+	sigaddset(&mask, SIGTERM);
+
+	if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
+		return -errno;
+
+	fd = signalfd(-1, &mask, 0);
+	if (fd < 0)
+		return -errno;
+	*sigfd = fd;
+
+	return 0;
 }
 
 int monitor(const char *name)
 {
 	LIST_HEAD(srcs);
+	int sigfd = 0;
 	int epfd;
 	int infd;
 	int wd = 0;
 	bool retry;
 	int err = 0;
 
+	err = prepare_signalfd(&sigfd);
+	if (err < 0)
+		return err;
+
 	epfd = epoll_create(1);
-	if (epfd < 0)
+	if (epfd < 0) {
+		close(sigfd);
 		return -errno;
+	}
 
 	infd = inotify_init1(IN_NONBLOCK);
 	if (infd < 0) {
@@ -405,10 +446,10 @@ retry:
 	if (err < 0)
 		goto error;
 
-	err = prepare_dispatcher(epfd, infd, &srcs);
+	err = prepare_dispatcher(epfd, sigfd, infd, &srcs);
 	if (err >= 0)
-		err = run_dispatcher(epfd, infd, &srcs, &retry);
-	clear_dispatcher(epfd, infd, &srcs);
+		err = run_dispatcher(epfd, sigfd, infd, &srcs, &retry);
+	clear_dispatcher(epfd, sigfd, infd, &srcs);
 
 	if (retry) {
 		// A simple makeshift for timing gap between creation of nodes
@@ -426,5 +467,7 @@ error:
 
 	close(epfd);
 
+	close(sigfd);
+
 	return err;
 }
-- 
2.19.1

  parent reply	other threads:[~2018-10-14 14:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-14 14:36 [alsa-utils][PATCH 0/9] alsactl: friendly to pluggable devices Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 1/9] alsactl: split event loop code to a function Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 2/9] alsactl: add an iterator of registered instances of sound card Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 3/9] alsactl: use epoll(7) instead of poll(2) Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 4/9] alsactl: use link list to maintain source of events Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 5/9] alsactl: use a list of source for event dispatcher instead of an array of source Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 6/9] alsactl: obsolete array for maintenance of handlers Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 7/9] alsactl: handle disconnection of sound card Takashi Sakamoto
2018-10-14 14:36 ` [alsa-utils][PATCH 8/9] alsactl: handle detection of new " Takashi Sakamoto
2018-10-14 14:36 ` Takashi Sakamoto [this message]
2018-10-14 15:06 ` [alsa-utils][PATCH 0/9] alsactl: friendly to pluggable devices Jaroslav Kysela

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181014143634.26066-10-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.