All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, thomas@coldfix.de
Cc: alsa-devel@alsa-project.org
Subject: [PATCH][RFC][alsa-utils 9/9] alsactl: handle detection of new sound card
Date: Fri,  5 Oct 2018 23:47:29 +0900	[thread overview]
Message-ID: <20181005144729.21388-10-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181005144729.21388-1-o-takashi@sakamocchi.jp>

At present, plug-and-play is not supported in a mode of 'monitor',
thus new sound card is not handled during runtime. This is not happy.

This commit uses Linux-specific inotify(7) to monitor '/dev/snd'
directory. When some files are newly added to the directory,
event dispatcher is suspended. Event sources are scanned again and the
dispatcher continue to run.

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

diff --git a/alsactl/monitor.c b/alsactl/monitor.c
index 4eb1ad1..6b5cfd4 100644
--- a/alsactl/monitor.c
+++ b/alsactl/monitor.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/epoll.h>
+#include <sys/inotify.h>
 #include <alsa/asoundlib.h>
 
 static int signal_type;
@@ -94,6 +95,12 @@ static int insert_source_entry(struct src_entry **head, snd_ctl_t *handle,
 	int count;
 	int err;
 
+	// Avoid duplication at retry case.
+	for (src = *head; src; src = src->list.next) {
+		if (!strcmp(src->name, name))
+			return 0;
+	}
+
 	src = calloc(1, sizeof(*src));
 	if (!src)
 		return -ENOMEM;
@@ -181,6 +188,28 @@ static int prepare_source_entry(struct src_entry **srcs, const char *name)
 	return 0;
 }
 
+static int dump_notification(int infd)
+{
+	char buf[1024];
+	ssize_t len;
+	int err = 0;
+
+	while (1) {
+		len = read(infd, buf, 1024);
+		if (len < 0) {
+			if (errno = EAGAIN)
+				break;
+			else
+				err = errno;
+			break;
+		}
+		if (len == 0)
+			break;
+	}
+
+	return err;
+}
+
 static int print_event(snd_ctl_t *ctl, const char *name)
 {
 	snd_ctl_event_t *event;
@@ -254,16 +283,20 @@ end:
 	return err;
 }
 
-static int prepare_dispatcher(int epfd, struct src_entry *srcs)
+static int prepare_dispatcher(int epfd, int infd, struct src_entry *srcs)
 {
+	struct epoll_event ev = {0};
 	struct src_entry *src;
 	int err = 0;
 
+	ev.events = EPOLLIN;
+	ev.data.fd = infd;
+	if (epoll_ctl(epfd, EPOLL_CTL_ADD, infd, &ev) < 0)
+		return -errno;
+
 	for (src = srcs; src; src = src->list.next) {
-		struct epoll_event ev = {
-			.events = EPOLLIN,
-			.data.ptr = (void *)src,
-		};
+		ev.events = EPOLLIN;
+		ev.data.ptr = (void *)src;
 		err = operate_dispatcher(epfd, EPOLL_CTL_ADD, &ev, src);
 		if (err < 0)
 			break;
@@ -272,7 +305,8 @@ static int prepare_dispatcher(int epfd, struct src_entry *srcs)
 	return err;
 }
 
-static int run_dispatcher(int epfd, struct src_entry *srcs)
+static int run_dispatcher(int epfd, int infd, struct src_entry *srcs,
+			  bool *retry)
 {
 	struct src_entry *src;
 	unsigned int max_ev_count;
@@ -309,7 +343,16 @@ static int run_dispatcher(int epfd, struct src_entry *srcs)
 
 		for (i = 0; i < count; ++i) {
 			struct epoll_event *ev = epev + i;
-			struct src_entry *src = (struct src_entry *)ev->data.ptr;
+			struct src_entry *src;
+
+			if (ev->data.fd == infd) {
+				err = dump_notification(infd);
+				if (err == 0)
+					*retry = true;
+				goto end;
+			}
+
+			src = ev->data.ptr;
 			if (ev->events & EPOLLIN)
 				print_event(src->handle, src->name);
 			if (ev->events & EPOLLERR) {
@@ -317,19 +360,22 @@ static int run_dispatcher(int epfd, struct src_entry *srcs)
 				remove_source_entry(src);
 			}
 		}
+		if (err < 0)
+			break;
 	}
-
+end:
 	free(epev);
-
 	return err;
 }
 
-static void clear_dispatcher(int epfd, struct src_entry *srcs)
+static void clear_dispatcher(int epfd, int infd, struct src_entry *srcs)
 {
 	struct src_entry *src;
 
 	for (src = srcs; src; src = src->list.next)
 		operate_dispatcher(epfd, EPOLL_CTL_DEL, NULL, src);
+
+	epoll_ctl(epfd, EPOLL_CTL_DEL, infd, NULL);
 }
 
 static void handle_unix_signal_for_finish(int sig)
@@ -360,6 +406,9 @@ int monitor(const char *name)
 {
 	struct src_entry *srcs = NULL;
 	int epfd;
+	int infd;
+	int wd = 0;
+	bool retry;
 	int err = 0;
 
 	err = prepare_signal_handler();
@@ -370,17 +419,35 @@ int monitor(const char *name)
 	if (epfd < 0)
 		return -errno;
 
+	infd = inotify_init1(IN_NONBLOCK);
+	if (infd < 0) {
+		err = -errno;
+		goto error;
+	}
+	wd = inotify_add_watch(infd, "/dev/snd/", IN_CREATE);
+	if (wd < 0) {
+		err = -errno;
+		goto error;
+	}
+retry:
+	retry = false;
 	err = prepare_source_entry(&srcs, name);
 	if (err < 0)
 		goto error;
 
-	err = prepare_dispatcher(epfd, srcs);
+	err = prepare_dispatcher(epfd, infd, srcs);
 	if (err >= 0)
-		err = run_dispatcher(epfd, srcs);
-	clear_dispatcher(epfd, srcs);
+		err = run_dispatcher(epfd, infd, srcs, &retry);
+	clear_dispatcher(epfd, infd, srcs);
+
+	if (retry)
+		goto retry;
 error:
+	if (wd > 0)
+		inotify_rm_watch(infd, wd);
 	clear_source_list(&srcs);
 
+	close(infd);
 	close(epfd);
 
 	return err;
-- 
2.19.0

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

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-05 14:47 [PATCH][RFC][alsa-utils 0/9] alsactl: monitor mode friendly for Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 1/9] alsactl: install signal handler Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 2/9] alsactl: split event loop code to a function Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 3/9] alsactl: add an iterator of registered instances of sound card Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 4/9] alsactl: use epoll(7) instead of poll(2) Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 5/9] alsactl: use link list to maintain source of events Takashi Sakamoto
2018-10-05 17:14   ` Jaroslav Kysela
2018-10-07  3:04     ` Takashi Sakamoto
2018-10-08 15:06       ` Takashi Iwai
2018-10-08 15:14       ` Jaroslav Kysela
2018-10-09  3:15         ` Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 6/9] alsactl: use a list of source for event dispatcher instead of an array of source Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 7/9] alsactl: obsolete array for maintenance of handlers Takashi Sakamoto
2018-10-05 14:47 ` [PATCH][RFC][alsa-utils 8/9] alsactl: handle disconnection of sound card Takashi Sakamoto
2018-10-05 14:47 ` Takashi Sakamoto [this message]
2018-10-05 14:51 ` [PATCH][RFC][alsa-utils 0/9] alsactl: monitor mode friendly for Takashi Sakamoto

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=20181005144729.21388-10-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=thomas@coldfix.de \
    --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.