* [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET @ 2018-11-28 15:00 Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz ` (8 more replies) 0 siblings, 9 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This adds handling of systemd NOTIFY_SOCKET so application using mainloop instance do properly notify systemd what is their state. --- Makefile.am | 8 ++- src/shared/mainloop-glib.c | 6 ++ src/shared/mainloop-notify.c | 104 +++++++++++++++++++++++++++++++++++ src/shared/mainloop-notify.h | 25 +++++++++ src/shared/mainloop.c | 10 ++++ src/shared/mainloop.h | 1 + 6 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/shared/mainloop-notify.c create mode 100644 src/shared/mainloop-notify.h diff --git a/Makefile.am b/Makefile.am index 0b26ccc3e..124c32482 100644 --- a/Makefile.am +++ b/Makefile.am @@ -130,12 +130,16 @@ endif src_libshared_glib_la_SOURCES = $(shared_sources) \ src/shared/io-glib.c \ src/shared/timeout-glib.c \ - src/shared/mainloop-glib.c + src/shared/mainloop-glib.c \ + src/shared/mainloop-notify.h \ + src/shared/mainloop-notify.c src_libshared_mainloop_la_SOURCES = $(shared_sources) \ src/shared/io-mainloop.c \ src/shared/timeout-mainloop.c \ - src/shared/mainloop.h src/shared/mainloop.c + src/shared/mainloop.h src/shared/mainloop.c \ + src/shared/mainloop-notify.h \ + src/shared/mainloop-notify.c if ELL src_libshared_ell_la_SOURCES = $(shared_sources) \ diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c index 8436969bb..4403d02d8 100644 --- a/src/shared/mainloop-glib.c +++ b/src/shared/mainloop-glib.c @@ -36,6 +36,7 @@ #include <glib.h> #include "mainloop.h" +#include "mainloop-notify.h" static GMainLoop *main_loop; static int exit_status; @@ -43,6 +44,7 @@ static int exit_status; void mainloop_init(void) { main_loop = g_main_loop_new(NULL, FALSE); + mainloop_notify_init(); } void mainloop_quit(void) @@ -72,9 +74,13 @@ int mainloop_run(void) g_main_loop_run(main_loop); + mainloop_sd_notify("STOPPING=1"); + g_main_loop_unref(main_loop); main_loop = NULL; + mainloop_notify_exit(); + return exit_status; } diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c new file mode 100644 index 000000000..81b0e15ba --- /dev/null +++ b/src/shared/mainloop-notify.c @@ -0,0 +1,104 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> + +#include <sys/socket.h> +#include <sys/un.h> + +#include "mainloop.h" +#include "mainloop-notify.h" + +static char *notify_sock; +static int notify_fd = -1; + +void mainloop_notify_init(void) +{ + const char *sock; + + sock = getenv("NOTIFY_SOCKET"); + if (!sock) + return; + + /* check for abstract socket or absolute path */ + if (sock[0] != '@' && sock[0] != '/') + return; + + notify_sock = strdup(sock); + + notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); +} + +void mainloop_notify_exit(void) +{ + if (notify_fd > 0) { + close(notify_fd); + notify_fd = -1; + } + + free(notify_sock); + notify_sock = NULL; +} + +int mainloop_sd_notify(const char *state) +{ + struct sockaddr_un addr; + struct msghdr msghdr; + struct iovec iovec; + + if (notify_fd <= 0) + return -ENOTCONN; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, notify_sock, sizeof(addr.sun_path) - 1); + + if (addr.sun_path[0] == '@') + addr.sun_path[0] = '\0'; + + memset(&iovec, 0, sizeof(iovec)); + iovec.iov_base = (char *) state; + iovec.iov_len = strlen(state); + + memset(&msghdr, 0, sizeof(msghdr)); + msghdr.msg_name = &addr; + msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + + strlen(notify_sock); + + if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) + msghdr.msg_namelen = sizeof(struct sockaddr_un); + + msghdr.msg_iov = &iovec; + msghdr.msg_iovlen = 1; + + return sendmsg(notify_fd, &msghdr, MSG_NOSIGNAL); +} diff --git a/src/shared/mainloop-notify.h b/src/shared/mainloop-notify.h new file mode 100644 index 000000000..721b5fbed --- /dev/null +++ b/src/shared/mainloop-notify.h @@ -0,0 +1,25 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +void mainloop_notify_init(void); +void mainloop_notify_exit(void); diff --git a/src/shared/mainloop.c b/src/shared/mainloop.c index e6ab9c43d..d7200a837 100644 --- a/src/shared/mainloop.c +++ b/src/shared/mainloop.c @@ -30,13 +30,17 @@ #include <errno.h> #include <unistd.h> #include <stdlib.h> +#include <stddef.h> #include <string.h> #include <signal.h> #include <sys/signalfd.h> #include <sys/timerfd.h> #include <sys/epoll.h> +#include <sys/socket.h> +#include <sys/un.h> #include "mainloop.h" +#include "mainloop-notify.h" #define MAX_EPOLL_EVENTS 10 @@ -83,6 +87,8 @@ void mainloop_init(void) mainloop_list[i] = NULL; epoll_terminate = 0; + + mainloop_notify_init(); } void mainloop_quit(void) @@ -180,9 +186,13 @@ int mainloop_run(void) } } + mainloop_sd_notify("STOPPING=1"); + close(epoll_fd); epoll_fd = 0; + mainloop_notify_exit(); + return exit_status; } diff --git a/src/shared/mainloop.h b/src/shared/mainloop.h index b83caabc8..73ed81187 100644 --- a/src/shared/mainloop.h +++ b/src/shared/mainloop.h @@ -49,3 +49,4 @@ int mainloop_remove_timeout(int id); int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback, void *user_data, mainloop_destroy_func destroy); +int mainloop_sd_notify(const char *state); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 02/10] share/mainloop: Add watchdog support 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz ` (7 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This adds watchdog notification support by sending "WATCHDOG=1" twice as frequent as required by WATCHDOG_USEC. --- src/shared/mainloop-notify.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c index 81b0e15ba..49ca4f6c9 100644 --- a/src/shared/mainloop-notify.c +++ b/src/shared/mainloop-notify.c @@ -37,13 +37,27 @@ #include "mainloop.h" #include "mainloop-notify.h" +#include "timeout.h" + +#define WATCHDOG_TRIGGER_FREQ 2 static char *notify_sock; static int notify_fd = -1; +static unsigned int watchdog; + +static bool watchdog_callback(void *user_data) +{ + mainloop_sd_notify("WATCHDOG=1"); + + return true; +} + void mainloop_notify_init(void) { const char *sock; + const char *watchdog_usec; + int msec; sock = getenv("NOTIFY_SOCKET"); if (!sock) @@ -56,6 +70,17 @@ void mainloop_notify_init(void) notify_sock = strdup(sock); notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); + + watchdog_usec = getenv("WATCHDOG_USEC"); + if (!watchdog_usec) + return; + + msec = atoi(watchdog_usec) / 1000; + if (msec < 0) + return; + + watchdog = timeout_add(msec / WATCHDOG_TRIGGER_FREQ, + watchdog_callback, NULL, NULL); } void mainloop_notify_exit(void) @@ -67,6 +92,8 @@ void mainloop_notify_exit(void) free(notify_sock); notify_sock = NULL; + + timeout_remove(watchdog); } int mainloop_sd_notify(const char *state) -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 04/10] core: Use mainloop_sd_notify " Luiz Augusto von Dentz ` (6 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> mainloop_notify takes care of sending the messages to NOTIFY_SOCKET and includes the handling of WATCHDOG_USEC as well. --- tools/btmon-logger.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tools/btmon-logger.c b/tools/btmon-logger.c index c3ba17939..1a42824ee 100644 --- a/tools/btmon-logger.c +++ b/tools/btmon-logger.c @@ -48,8 +48,6 @@ #include "src/shared/mainloop.h" #include "src/shared/btsnoop.h" -#include "src/systemd.h" - #define MONITOR_INDEX_NONE 0xffff struct monitor_hdr { @@ -283,7 +281,7 @@ int main(int argc, char *argv[]) mainloop_init(); - sd_notify(0, "STATUS=Starting up"); + mainloop_sd_notify("STATUS=Starting up"); while (true) { int opt; @@ -375,12 +373,12 @@ int main(int argc, char *argv[]) printf("Bluetooth monitor logger ver %s\n", VERSION); - sd_notify(0, "STATUS=Running"); - sd_notify(0, "READY=1"); + mainloop_sd_notify("STATUS=Running"); + mainloop_sd_notify("READY=1"); exit_status = mainloop_run(); - sd_notify(0, "STATUS=Quitting"); + mainloop_sd_notify("STATUS=Quitting"); btsnoop_unref(btsnoop_file); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 04/10] core: Use mainloop_sd_notify instead of sd_notify 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 05/10] core: Remove old code related to sd_notify Luiz Augusto von Dentz ` (5 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> mainloop_sd_notify takes care of sending the messages to NOTIFY_SOCKET and includes the handling of WATCHDOG_USEC as well. --- src/main.c | 51 +++++++++++---------------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/src/main.c b/src/main.c index 4716f5388..7ab1349cc 100644 --- a/src/main.c +++ b/src/main.c @@ -51,6 +51,7 @@ #include "backtrace.h" #include "shared/att-types.h" +#include "shared/mainloop.h" #include "lib/uuid.h" #include "hcid.h" #include "sdpd.h" @@ -59,7 +60,6 @@ #include "dbus-common.h" #include "agent.h" #include "profile.h" -#include "systemd.h" #define BLUEZ_NAME "org.bluez" @@ -486,11 +486,9 @@ static void log_handler(const gchar *log_domain, GLogLevelFlags log_level, btd_backtrace(0xffff); } -static GMainLoop *event_loop; - void btd_exit(void) { - g_main_loop_quit(event_loop); + mainloop_quit(); } static gboolean quit_eventloop(gpointer user_data) @@ -524,7 +522,7 @@ static gboolean signal_handler(GIOChannel *channel, GIOCondition cond, g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS, quit_eventloop, NULL); - sd_notify(0, "STATUS=Powering down"); + mainloop_sd_notify("STATUS=Powering down"); adapter_shutdown(); } @@ -616,7 +614,7 @@ static void disconnect_dbus(void) static void disconnected_dbus(DBusConnection *conn, void *data) { info("Disconnected from D-Bus. Exiting."); - g_main_loop_quit(event_loop); + mainloop_quit(); } static int connect_dbus(void) @@ -644,13 +642,6 @@ static int connect_dbus(void) return 0; } -static gboolean watchdog_callback(gpointer user_data) -{ - sd_notify(0, "WATCHDOG=1"); - - return TRUE; -} - static gboolean parse_debug(const char *key, const char *value, gpointer user_data, GError **error) { @@ -691,8 +682,7 @@ int main(int argc, char *argv[]) uint16_t sdp_mtu = 0; uint32_t sdp_flags = 0; int gdbus_flags = 0; - guint signal, watchdog; - const char *watchdog_usec; + guint signal; init_defaults(); @@ -719,7 +709,7 @@ int main(int argc, char *argv[]) btd_backtrace_init(); - event_loop = g_main_loop_new(NULL, FALSE); + mainloop_init(); signal = setup_signalfd(); @@ -729,7 +719,7 @@ int main(int argc, char *argv[]) G_LOG_FLAG_RECURSION, log_handler, NULL); - sd_notify(0, "STATUS=Starting up"); + mainloop_sd_notify("STATUS=Starting up"); if (option_configfile) main_conf_file_path = option_configfile; @@ -788,26 +778,12 @@ int main(int argc, char *argv[]) DBG("Entering main loop"); - sd_notify(0, "STATUS=Running"); - sd_notify(0, "READY=1"); + mainloop_sd_notify("STATUS=Running"); + mainloop_sd_notify("READY=1"); - watchdog_usec = getenv("WATCHDOG_USEC"); - if (watchdog_usec) { - unsigned int seconds; + mainloop_run(); - seconds = atoi(watchdog_usec) / (1000 * 1000); - info("Watchdog timeout is %d seconds", seconds); - - watchdog = g_timeout_add_seconds_full(G_PRIORITY_HIGH, - seconds / 2, - watchdog_callback, - NULL, NULL); - } else - watchdog = 0; - - g_main_loop_run(event_loop); - - sd_notify(0, "STATUS=Quitting"); + mainloop_sd_notify("STATUS=Quitting"); g_source_remove(signal); @@ -824,8 +800,6 @@ int main(int argc, char *argv[]) if (main_opts.mode != BT_MODE_LE) stop_sdp_server(); - g_main_loop_unref(event_loop); - if (main_conf) g_key_file_free(main_conf); @@ -833,9 +807,6 @@ int main(int argc, char *argv[]) info("Exit"); - if (watchdog > 0) - g_source_remove(watchdog); - __btd_log_cleanup(); return 0; -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 05/10] core: Remove old code related to sd_notify 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (2 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 04/10] core: Use mainloop_sd_notify " Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz ` (4 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This is no longer needed since mainloop instances can handle it now. --- Makefile.am | 1 - Makefile.tools | 2 +- src/systemd.c | 107 ------------------------------------------------- src/systemd.h | 28 ------------- 4 files changed, 1 insertion(+), 137 deletions(-) delete mode 100644 src/systemd.c delete mode 100644 src/systemd.h diff --git a/Makefile.am b/Makefile.am index 124c32482..a69baea10 100644 --- a/Makefile.am +++ b/Makefile.am @@ -185,7 +185,6 @@ src_bluetoothd_SOURCES = $(builtin_sources) \ src/bluetooth.ver \ src/main.c src/log.h src/log.c \ src/backtrace.h src/backtrace.c \ - src/systemd.h src/systemd.c \ src/rfkill.c src/hcid.h src/sdpd.h \ src/sdpd-server.c src/sdpd-request.c \ src/sdpd-service.c src/sdpd-database.c \ diff --git a/Makefile.tools b/Makefile.tools index 35412ca61..d3d901bc9 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -46,7 +46,7 @@ endif if LOGGER libexec_PROGRAMS += tools/btmon-logger -tools_btmon_logger_SOURCES = tools/btmon-logger.c src/systemd.c src/systemd.h +tools_btmon_logger_SOURCES = tools/btmon-logger.c tools_btmon_logger_LDADD = src/libshared-mainloop.la tools_btmon_logger_DEPENDENCIES = src/libshared-mainloop.la \ tools/bluetooth-logger.service diff --git a/src/systemd.c b/src/systemd.c deleted file mode 100644 index 0a4b35d06..000000000 --- a/src/systemd.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2012 Intel Corporation. All rights reserved. - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <stdio.h> -#include <errno.h> -#include <unistd.h> -#include <stdlib.h> -#include <stddef.h> -#include <string.h> -#include <sys/socket.h> -#include <sys/un.h> - -#include "systemd.h" - -int sd_listen_fds(int unset_environment) -{ - return 0; -} - -int sd_notify(int unset_environment, const char *state) -{ - const char *sock; - struct sockaddr_un addr; - struct msghdr msghdr; - struct iovec iovec; - int fd, err; - - if (!state) { - err = -EINVAL; - goto done; - } - - sock = getenv("NOTIFY_SOCKET"); - if (!sock) - return 0; - - /* check for abstract socket or absolute path */ - if (sock[0] != '@' && sock[0] != '/') { - err = -EINVAL; - goto done; - } - - fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - err = -errno; - goto done; - } - - memset(&addr, 0, sizeof(addr)); - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, sock, sizeof(addr.sun_path) - 1); - - if (addr.sun_path[0] == '@') - addr.sun_path[0] = '\0'; - - memset(&iovec, 0, sizeof(iovec)); - iovec.iov_base = (char *) state; - iovec.iov_len = strlen(state); - - memset(&msghdr, 0, sizeof(msghdr)); - msghdr.msg_name = &addr; - msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + - strlen(sock); - - if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) - msghdr.msg_namelen = sizeof(struct sockaddr_un); - - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; - - if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) - err = -errno; - else - err = 1; - - close(fd); - -done: - if (unset_environment) - unsetenv("NOTIFY_SOCKET"); - - return err; -} diff --git a/src/systemd.h b/src/systemd.h deleted file mode 100644 index 0ef7c82a9..000000000 --- a/src/systemd.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * BlueZ - Bluetooth protocol stack for Linux - * - * Copyright (C) 2012 Intel Corporation. All rights reserved. - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#define SD_LISTEN_FDS_START 3 - -int sd_listen_fds(int unset_environment); - -int sd_notify(int unset_environment, const char *state); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (3 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 05/10] core: Remove old code related to sd_notify Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal Luiz Augusto von Dentz ` (3 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> If the id is 0 that makes it is invalid already. --- src/shared/timeout-glib.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/shared/timeout-glib.c b/src/shared/timeout-glib.c index 4163bced7..fd71ca430 100644 --- a/src/shared/timeout-glib.c +++ b/src/shared/timeout-glib.c @@ -71,8 +71,12 @@ unsigned int timeout_add(unsigned int timeout, timeout_func_t func, void timeout_remove(unsigned int id) { - GSource *source = g_main_context_find_source_by_id(NULL, id); + GSource *source; + if (!id) + return; + + source = g_main_context_find_source_by_id(NULL, id); if (source) g_source_destroy(source); } -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (4 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal Luiz Augusto von Dentz ` (2 subsequent siblings) 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This consolidates the handling of signalfd in similar ways as ELL does. --- src/shared/mainloop-glib.c | 1 + src/shared/mainloop-notify.c | 81 ++++++++++++++++++++++++++++++++++++ src/shared/mainloop.h | 1 + 3 files changed, 83 insertions(+) diff --git a/src/shared/mainloop-glib.c b/src/shared/mainloop-glib.c index 4403d02d8..a83c35482 100644 --- a/src/shared/mainloop-glib.c +++ b/src/shared/mainloop-glib.c @@ -37,6 +37,7 @@ #include "mainloop.h" #include "mainloop-notify.h" +#include "io.h" static GMainLoop *main_loop; static int exit_status; diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c index 49ca4f6c9..81a2cef28 100644 --- a/src/shared/mainloop-notify.c +++ b/src/shared/mainloop-notify.c @@ -31,13 +31,17 @@ #include <stdlib.h> #include <stddef.h> #include <string.h> +#include <signal.h> +#include <sys/signalfd.h> #include <sys/socket.h> #include <sys/un.h> #include "mainloop.h" #include "mainloop-notify.h" #include "timeout.h" +#include "util.h" +#include "io.h" #define WATCHDOG_TRIGGER_FREQ 2 @@ -46,6 +50,14 @@ static int notify_fd = -1; static unsigned int watchdog; +struct signal_data { + struct io *io; + mainloop_signal_func func; + void *user_data; +}; + +static struct signal_data *signal_data; + static bool watchdog_callback(void *user_data) { mainloop_sd_notify("WATCHDOG=1"); @@ -129,3 +141,72 @@ int mainloop_sd_notify(const char *state) return sendmsg(notify_fd, &msghdr, MSG_NOSIGNAL); } + +static bool signal_read(struct io *io, void *user_data) +{ + struct signal_data *data = user_data; + struct signalfd_siginfo si; + ssize_t result; + int fd; + + fd = io_get_fd(io); + + result = read(fd, &si, sizeof(si)); + if (result != sizeof(si)) + return false; + + if (data && data->func) + data->func(si.ssi_signo, data->user_data); + + return true; +} + +static struct io *setup_signalfd(void) +{ + struct io *io; + sigset_t mask; + int fd; + + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); + sigaddset(&mask, SIGUSR2); + sigaddset(&mask, SIGCHLD); + + if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) + return NULL; + + fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC); + if (fd < 0) + return NULL; + + io = io_new(fd); + + io_set_close_on_destroy(io, true); + io_set_read_handler(io, signal_read, NULL, NULL); + + return io; +} + +int mainloop_run_with_signal(mainloop_signal_func func, void *user_data) +{ + struct io *io; + int ret; + + if (!func) + return -EINVAL; + + io = setup_signalfd(); + if (!io) + return -errno; + + signal_data = new0(struct signal_data, 1); + signal_data->io = io; + + ret = mainloop_run(); + + io_destroy(io); + free(signal_data); + + return ret; +} diff --git a/src/shared/mainloop.h b/src/shared/mainloop.h index 73ed81187..1ede62797 100644 --- a/src/shared/mainloop.h +++ b/src/shared/mainloop.h @@ -36,6 +36,7 @@ void mainloop_quit(void); void mainloop_exit_success(void); void mainloop_exit_failure(void); int mainloop_run(void); +int mainloop_run_with_signal(mainloop_signal_func func, void *user_data); int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback, void *user_data, mainloop_destroy_func destroy); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (5 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 09/10] core: Make use of mainloop_run_with_signal Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 10/10] shared/tester: " Luiz Augusto von Dentz 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This removes mainloop_set_signal and replaces it usage with mainloop_run_with_signal. --- android/bluetoothd-snoop.c | 9 +---- android/system-emulator.c | 10 +---- emulator/b1ee.c | 9 +---- emulator/hfp.c | 10 +---- emulator/main.c | 9 +---- monitor/main.c | 9 +---- peripheral/main.c | 10 +---- src/shared/mainloop.c | 79 -------------------------------------- src/shared/shell.c | 54 ++------------------------ tools/3dsp.c | 9 +---- tools/advtest.c | 9 +---- tools/bluemoon.c | 9 +---- tools/btattach.c | 9 +---- tools/btconfig.c | 9 +---- tools/btgatt-client.c | 9 +---- tools/btgatt-server.c | 9 +---- tools/btinfo.c | 9 +---- tools/btmon-logger.c | 9 +---- tools/btproxy.c | 9 +---- tools/eddystone.c | 9 +---- tools/ibeacon.c | 9 +---- tools/oobtest.c | 9 +---- 22 files changed, 24 insertions(+), 292 deletions(-) diff --git a/android/bluetoothd-snoop.c b/android/bluetoothd-snoop.c index 8d9a2d087..25cddee34 100644 --- a/android/bluetoothd-snoop.c +++ b/android/bluetoothd-snoop.c @@ -219,7 +219,6 @@ static void set_capabilities(void) int main(int argc, char *argv[]) { const char *path; - sigset_t mask; __btd_log_init(NULL, 0); @@ -234,12 +233,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - if (!strcmp(DEFAULT_SNOOP_FILE, path)) rename(DEFAULT_SNOOP_FILE, DEFAULT_SNOOP_FILE ".old"); @@ -250,7 +243,7 @@ int main(int argc, char *argv[]) info("bluetoothd_snoop: started"); - mainloop_run(); + mainloop_run_with_signal(signal_callback, NULL); close_monitor(); diff --git a/android/system-emulator.c b/android/system-emulator.c index 1545dcea1..8bcf660e2 100644 --- a/android/system-emulator.c +++ b/android/system-emulator.c @@ -216,19 +216,11 @@ static void signal_callback(int signum, void *user_data) int main(int argc, char *argv[]) { const char SYSTEM_SOCKET_PATH[] = "\0android_system"; - sigset_t mask; struct sockaddr_un addr; int fd; mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGCHLD); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Android system emulator ver %s\n", VERSION); snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0])); @@ -254,5 +246,5 @@ int main(int argc, char *argv[]) /* Make sure bluetoothd creates files with proper permissions */ umask(0177); - return mainloop_run(); + return mainloop_run_with_signal(signal_callback, NULL); } diff --git a/emulator/b1ee.c b/emulator/b1ee.c index 1253a3dca..0d5226fcc 100644 --- a/emulator/b1ee.c +++ b/emulator/b1ee.c @@ -265,7 +265,6 @@ int main(int argc, char *argv[]) char *server_port = NULL, *sniffer_port = NULL; int ret = EXIT_FAILURE; ssize_t written; - sigset_t mask; for (;;) { int opt; @@ -323,17 +322,11 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - mainloop_add_fd(sniffer_fd, EPOLLIN, sniffer_read_callback, NULL, NULL); mainloop_add_fd(server_fd, EPOLLIN, server_read_callback, NULL, NULL); mainloop_add_fd(vhci_fd, EPOLLIN, vhci_read_callback, NULL, NULL); - ret = mainloop_run(); + ret = mainloop_run_with_signal(signal_callback, NULL); goto done; diff --git a/emulator/hfp.c b/emulator/hfp.c index e70054adf..29ec63e7d 100644 --- a/emulator/hfp.c +++ b/emulator/hfp.c @@ -103,18 +103,10 @@ static void signal_callback(int signum, void *user_data) int main(int argc, char *argv[]) { - sigset_t mask; - mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - if (!open_connection()) return EXIT_FAILURE; - return mainloop_run(); + return mainloop_run_with_signal(signal_callback, NULL); } diff --git a/emulator/main.c b/emulator/main.c index 56246a3de..68c53488e 100644 --- a/emulator/main.c +++ b/emulator/main.c @@ -90,7 +90,6 @@ int main(int argc, char *argv[]) int amptest_count = 0; int vhci_count = 0; enum vhci_type vhci_type = VHCI_TYPE_BREDRLE; - sigset_t mask; int i; mainloop_init(); @@ -154,12 +153,6 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluetooth emulator ver %s\n", VERSION); for (i = 0; i < letest_count; i++) { @@ -227,5 +220,5 @@ int main(int argc, char *argv[]) fprintf(stderr, "Failed to open monitor server\n"); } - return mainloop_run(); + return mainloop_run_with_signal(signal_callback, NULL); } diff --git a/monitor/main.c b/monitor/main.c index 5fa87ea3f..8640c023d 100644 --- a/monitor/main.c +++ b/monitor/main.c @@ -112,7 +112,6 @@ int main(int argc, char *argv[]) unsigned short ellisys_port = 0; const char *str; int exit_status; - sigset_t mask; mainloop_init(); @@ -219,12 +218,6 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluetooth monitor ver %s\n", VERSION); keys_setup(); @@ -258,7 +251,7 @@ int main(int argc, char *argv[]) if (tty && control_tty(tty, tty_speed) < 0) return EXIT_FAILURE; - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); keys_cleanup(); diff --git a/peripheral/main.c b/peripheral/main.c index d7e10f3d3..93abe2042 100644 --- a/peripheral/main.c +++ b/peripheral/main.c @@ -184,7 +184,6 @@ static void signal_callback(int signum, void *user_data) int main(int argc, char *argv[]) { - sigset_t mask; int exit_status; if (getpid() == 1 && getppid() == 0) @@ -192,13 +191,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGCHLD); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluetooth periperhal ver %s\n", VERSION); prepare_filesystem(); @@ -235,7 +227,7 @@ int main(int argc, char *argv[]) if (is_init) attach_start(); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); if (is_init) attach_stop(); diff --git a/src/shared/mainloop.c b/src/shared/mainloop.c index d7200a837..3c429c490 100644 --- a/src/shared/mainloop.c +++ b/src/shared/mainloop.c @@ -67,16 +67,6 @@ struct timeout_data { void *user_data; }; -struct signal_data { - int fd; - sigset_t mask; - mainloop_signal_func callback; - mainloop_destroy_func destroy; - void *user_data; -}; - -static struct signal_data *signal_data; - void mainloop_init(void) { unsigned int i; @@ -108,45 +98,10 @@ void mainloop_exit_failure(void) epoll_terminate = 1; } -static void signal_callback(int fd, uint32_t events, void *user_data) -{ - struct signal_data *data = user_data; - struct signalfd_siginfo si; - ssize_t result; - - if (events & (EPOLLERR | EPOLLHUP)) { - mainloop_quit(); - return; - } - - result = read(fd, &si, sizeof(si)); - if (result != sizeof(si)) - return; - - if (data->callback) - data->callback(si.ssi_signo, data->user_data); -} - int mainloop_run(void) { unsigned int i; - if (signal_data) { - if (sigprocmask(SIG_BLOCK, &signal_data->mask, NULL) < 0) - return EXIT_FAILURE; - - signal_data->fd = signalfd(-1, &signal_data->mask, - SFD_NONBLOCK | SFD_CLOEXEC); - if (signal_data->fd < 0) - return EXIT_FAILURE; - - if (mainloop_add_fd(signal_data->fd, EPOLLIN, - signal_callback, signal_data, NULL) < 0) { - close(signal_data->fd); - return EXIT_FAILURE; - } - } - while (!epoll_terminate) { struct epoll_event events[MAX_EPOLL_EVENTS]; int n, nfds; @@ -163,14 +118,6 @@ int mainloop_run(void) } } - if (signal_data) { - mainloop_remove_fd(signal_data->fd); - close(signal_data->fd); - - if (signal_data->destroy) - signal_data->destroy(signal_data->user_data); - } - for (i = 0; i < MAX_MAINLOOP_ENTRIES; i++) { struct mainloop_data *data = mainloop_list[i]; @@ -384,29 +331,3 @@ int mainloop_remove_timeout(int id) { return mainloop_remove_fd(id); } - -int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback, - void *user_data, mainloop_destroy_func destroy) -{ - struct signal_data *data; - - if (!mask || !callback) - return -EINVAL; - - data = malloc(sizeof(*data)); - if (!data) - return -ENOMEM; - - memset(data, 0, sizeof(*data)); - data->callback = callback; - data->destroy = destroy; - data->user_data = user_data; - - data->fd = -1; - memcpy(&data->mask, mask, sizeof(sigset_t)); - - free(signal_data); - signal_data = data; - - return 0; -} diff --git a/src/shared/shell.c b/src/shared/shell.c index aabbc5d0e..8ccfe855e 100644 --- a/src/shared/shell.c +++ b/src/shared/shell.c @@ -864,27 +864,18 @@ static bool io_hup(struct io *io, void *user_data) return false; } -static bool signal_read(struct io *io, void *user_data) +static void signal_callback(int signum, void *user_data) { static bool terminated = false; - struct signalfd_siginfo si; - ssize_t result; - int fd; - fd = io_get_fd(io); - - result = read(fd, &si, sizeof(si)); - if (result != sizeof(si)) - return false; - - switch (si.ssi_signo) { + switch (signum) { case SIGINT: if (data.input && !data.mode) { rl_replace_line("", 0); rl_crlf(); rl_on_new_line(); rl_redisplay(); - return true; + return; } /* @@ -907,38 +898,6 @@ static bool signal_read(struct io *io, void *user_data) terminated = true; break; } - - return false; -} - -static struct io *setup_signalfd(void) -{ - struct io *io; - sigset_t mask; - int fd; - - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { - perror("Failed to set signal mask"); - return 0; - } - - fd = signalfd(-1, &mask, 0); - if (fd < 0) { - perror("Failed to create signal descriptor"); - return 0; - } - - io = io_new(fd); - - io_set_close_on_destroy(io, true); - io_set_read_handler(io, signal_read, NULL, NULL); - io_set_disconnect_handler(io, io_hup, NULL, NULL); - - return io; } static void rl_init_history(void) @@ -1132,14 +1091,9 @@ static void env_destroy(void *data) int bt_shell_run(void) { - struct io *signal; int status; - signal = setup_signalfd(); - - status = mainloop_run(); - - io_destroy(signal); + status = mainloop_run_with_signal(signal_callback, NULL); bt_shell_cleanup(); diff --git a/tools/3dsp.c b/tools/3dsp.c index 686fe139d..77a70c037 100644 --- a/tools/3dsp.c +++ b/tools/3dsp.c @@ -570,7 +570,6 @@ int main(int argc, char *argv[]) uint16_t index = 0; const char *str; bool use_raw = false; - sigset_t mask; int exit_status; for (;;) { @@ -624,12 +623,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("3D Synchronization Profile testing ver %s\n", VERSION); if (use_raw) { @@ -654,7 +647,7 @@ int main(int argc, char *argv[]) else if (glasses_role) start_glasses(); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(hci_dev); diff --git a/tools/advtest.c b/tools/advtest.c index b02301c39..050b570df 100644 --- a/tools/advtest.c +++ b/tools/advtest.c @@ -374,7 +374,6 @@ static const struct option main_options[] = { int main(int argc ,char *argv[]) { - sigset_t mask; int exit_status; for (;;) { @@ -403,12 +402,6 @@ int main(int argc ,char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - mgmt = mgmt_new_default(); if (!mgmt) { fprintf(stderr, "Failed to open management socket\n"); @@ -423,7 +416,7 @@ int main(int argc ,char *argv[]) goto done; } - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(adv_dev); bt_hci_unref(scan_dev); diff --git a/tools/bluemoon.c b/tools/bluemoon.c index 6051f3002..800541110 100644 --- a/tools/bluemoon.c +++ b/tools/bluemoon.c @@ -923,7 +923,6 @@ int main(int argc, char *argv[]) { const char *str; bool use_raw = false; - sigset_t mask; int exit_status; for (;;) { @@ -1000,12 +999,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluemoon configuration utility ver %s\n", VERSION); if (check_firmware) { @@ -1030,7 +1023,7 @@ int main(int argc, char *argv[]) bt_hci_send(hci_dev, CMD_READ_VERSION, NULL, 0, read_version_complete, NULL, NULL); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(hci_dev); diff --git a/tools/btattach.c b/tools/btattach.c index 5adbc8d83..6eb99bac3 100644 --- a/tools/btattach.c +++ b/tools/btattach.c @@ -231,7 +231,6 @@ int main(int argc, char *argv[]) { const char *bredr_path = NULL, *amp_path = NULL, *proto = NULL; bool flowctl = true, raw_device = false; - sigset_t mask; int exit_status, count = 0, proto_id = HCI_UART_H4; unsigned int speed = B115200; @@ -284,12 +283,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - if (proto) { unsigned int i; @@ -348,7 +341,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); return exit_status; } diff --git a/tools/btconfig.c b/tools/btconfig.c index f171bfbc1..c1ef5834a 100644 --- a/tools/btconfig.c +++ b/tools/btconfig.c @@ -75,7 +75,6 @@ static const struct option main_options[] = { int main(int argc, char *argv[]) { - sigset_t mask; int exit_status; for (;;) { @@ -105,12 +104,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - mgmt = mgmt_new_default(); if (!mgmt) { fprintf(stderr, "Unable to open mgmt_socket\n"); @@ -120,7 +113,7 @@ int main(int argc, char *argv[]) if (getenv("MGMT_DEBUG")) mgmt_set_debug(mgmt, mgmt_debug, "mgmt: ", NULL); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); mgmt_cancel_all(mgmt); mgmt_unregister_all(mgmt); diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c index 51bc3629d..31e849aae 100644 --- a/tools/btgatt-client.c +++ b/tools/btgatt-client.c @@ -1518,7 +1518,6 @@ int main(int argc, char *argv[]) bdaddr_t src_addr, dst_addr; int dev_id = -1; int fd; - sigset_t mask; struct client *cli; while ((opt = getopt_long(argc, argv, "+hvs:m:t:d:i:", @@ -1639,15 +1638,9 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_cb, NULL, NULL); - print_prompt(); - mainloop_run(); + mainloop_run_with_signal(signal_cb, NULL); printf("\n\nShutting down...\n"); diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c index 89812bd75..bc32789d9 100644 --- a/tools/btgatt-server.c +++ b/tools/btgatt-server.c @@ -1139,7 +1139,6 @@ int main(int argc, char *argv[]) int sec = BT_SECURITY_LOW; uint8_t src_type = BDADDR_LE_PUBLIC; uint16_t mtu = 0; - sigset_t mask; bool hr_visible = false; struct server *server; @@ -1250,15 +1249,9 @@ int main(int argc, char *argv[]) printf("Running GATT server\n"); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_cb, NULL, NULL); - print_prompt(); - mainloop_run(); + mainloop_run_with_signal(signal_cb, NULL); printf("\n\nShutting down...\n"); diff --git a/tools/btinfo.c b/tools/btinfo.c index 8e36577d7..5e609739c 100644 --- a/tools/btinfo.c +++ b/tools/btinfo.c @@ -226,7 +226,6 @@ int main(int argc, char *argv[]) const char *str; bool use_raw = false; bool power_down = false; - sigset_t mask; int fd, i, exit_status; for (;;) { @@ -284,12 +283,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluetooth information utility ver %s\n", VERSION); fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI); @@ -345,7 +338,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(hci_dev); diff --git a/tools/btmon-logger.c b/tools/btmon-logger.c index 1a42824ee..65953d261 100644 --- a/tools/btmon-logger.c +++ b/tools/btmon-logger.c @@ -276,7 +276,6 @@ int main(int argc, char *argv[]) size_t size_limit = 0; bool parents = false; int exit_status; - sigset_t mask; char *endptr; mainloop_init(); @@ -365,18 +364,12 @@ int main(int argc, char *argv[]) drop_capabilities(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Bluetooth monitor logger ver %s\n", VERSION); mainloop_sd_notify("STATUS=Running"); mainloop_sd_notify("READY=1"); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); mainloop_sd_notify("STATUS=Quitting"); diff --git a/tools/btproxy.c b/tools/btproxy.c index 58fa958e6..fd53ca264 100644 --- a/tools/btproxy.c +++ b/tools/btproxy.c @@ -780,7 +780,6 @@ int main(int argc, char *argv[]) bool use_redirect = false; uint8_t type = HCI_PRIMARY; const char *str; - sigset_t mask; for (;;) { int opt; @@ -870,12 +869,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - if (connect_address || use_redirect) { int host_fd, dev_fd; @@ -930,5 +923,5 @@ int main(int argc, char *argv[]) NULL, NULL); } - return mainloop_run(); + return mainloop_run_with_signal(signal_callback, NULL); } diff --git a/tools/eddystone.c b/tools/eddystone.c index f412c90b0..4764c677c 100644 --- a/tools/eddystone.c +++ b/tools/eddystone.c @@ -244,7 +244,6 @@ int main(int argc, char *argv[]) { uint16_t index = 0; const char *str; - sigset_t mask; int exit_status; for (;;) { @@ -290,12 +289,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Low Energy Eddystone utility ver %s\n", VERSION); hci_dev = bt_hci_new_user_channel(index); @@ -307,7 +300,7 @@ int main(int argc, char *argv[]) start_eddystone(); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(hci_dev); diff --git a/tools/ibeacon.c b/tools/ibeacon.c index 4b358049c..40078d5ed 100644 --- a/tools/ibeacon.c +++ b/tools/ibeacon.c @@ -237,7 +237,6 @@ int main(int argc, char *argv[]) { uint16_t index = 0; const char *str; - sigset_t mask; int exit_status; for (;;) { @@ -283,12 +282,6 @@ int main(int argc, char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - printf("Low Energy iBeacon utility ver %s\n", VERSION); hci_dev = bt_hci_new_user_channel(index); @@ -300,7 +293,7 @@ int main(int argc, char *argv[]) start_ibeacon(); - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); bt_hci_unref(hci_dev); diff --git a/tools/oobtest.c b/tools/oobtest.c index e77320bab..c228b65b1 100644 --- a/tools/oobtest.c +++ b/tools/oobtest.c @@ -1026,7 +1026,6 @@ static const struct option main_options[] = { int main(int argc ,char *argv[]) { - sigset_t mask; int exit_status; for (;;) { @@ -1128,12 +1127,6 @@ int main(int argc ,char *argv[]) mainloop_init(); - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - mainloop_set_signal(&mask, signal_callback, NULL, NULL); - mgmt = mgmt_new_default(); if (!mgmt) { fprintf(stderr, "Failed to open management socket\n"); @@ -1148,7 +1141,7 @@ int main(int argc ,char *argv[]) goto done; } - exit_status = mainloop_run(); + exit_status = mainloop_run_with_signal(signal_callback, NULL); done: mgmt_unref(mgmt); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 09/10] core: Make use of mainloop_run_with_signal 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (6 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 10/10] shared/tester: " Luiz Augusto von Dentz 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This don't require setting up signalfd. --- src/main.c | 64 +++--------------------------------------------------- 1 file changed, 3 insertions(+), 61 deletions(-) diff --git a/src/main.c b/src/main.c index 7ab1349cc..67eb6ac59 100644 --- a/src/main.c +++ b/src/main.c @@ -497,24 +497,11 @@ static gboolean quit_eventloop(gpointer user_data) return FALSE; } -static gboolean signal_handler(GIOChannel *channel, GIOCondition cond, - gpointer user_data) +static void signal_callback(int signum, void *user_data) { static bool terminated = false; - struct signalfd_siginfo si; - ssize_t result; - int fd; - if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) - return FALSE; - - fd = g_io_channel_unix_get_fd(channel); - - result = read(fd, &si, sizeof(si)); - if (result != sizeof(si)) - return FALSE; - - switch (si.ssi_signo) { + switch (signum) { case SIGINT: case SIGTERM: if (!terminated) { @@ -532,46 +519,6 @@ static gboolean signal_handler(GIOChannel *channel, GIOCondition cond, __btd_toggle_debug(); break; } - - return TRUE; -} - -static guint setup_signalfd(void) -{ - GIOChannel *channel; - guint source; - sigset_t mask; - int fd; - - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - sigaddset(&mask, SIGUSR2); - - if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { - perror("Failed to set signal mask"); - return 0; - } - - fd = signalfd(-1, &mask, 0); - if (fd < 0) { - perror("Failed to create signal descriptor"); - return 0; - } - - channel = g_io_channel_unix_new(fd); - - g_io_channel_set_close_on_unref(channel, TRUE); - g_io_channel_set_encoding(channel, NULL, NULL); - g_io_channel_set_buffered(channel, FALSE); - - source = g_io_add_watch(channel, - G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - signal_handler, NULL); - - g_io_channel_unref(channel); - - return source; } static char *option_debug = NULL; @@ -682,7 +629,6 @@ int main(int argc, char *argv[]) uint16_t sdp_mtu = 0; uint32_t sdp_flags = 0; int gdbus_flags = 0; - guint signal; init_defaults(); @@ -711,8 +657,6 @@ int main(int argc, char *argv[]) mainloop_init(); - signal = setup_signalfd(); - __btd_log_init(option_debug, option_detach); g_log_set_handler("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | @@ -781,12 +725,10 @@ int main(int argc, char *argv[]) mainloop_sd_notify("STATUS=Running"); mainloop_sd_notify("READY=1"); - mainloop_run(); + mainloop_run_with_signal(signal_callback, NULL); mainloop_sd_notify("STATUS=Quitting"); - g_source_remove(signal); - plugin_cleanup(); btd_profile_cleanup(); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2 10/10] shared/tester: Make use of mainloop_run_with_signal 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz ` (7 preceding siblings ...) 2018-11-28 15:00 ` [PATCH v2 09/10] core: Make use of mainloop_run_with_signal Luiz Augusto von Dentz @ 2018-11-28 15:00 ` Luiz Augusto von Dentz 8 siblings, 0 replies; 10+ messages in thread From: Luiz Augusto von Dentz @ 2018-11-28 15:00 UTC (permalink / raw) To: linux-bluetooth From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> This don't require setting up signalfd. --- src/shared/tester.c | 79 +++++---------------------------------------- 1 file changed, 8 insertions(+), 71 deletions(-) diff --git a/src/shared/tester.c b/src/shared/tester.c index 168d0de91..3d79e411d 100644 --- a/src/shared/tester.c +++ b/src/shared/tester.c @@ -44,6 +44,7 @@ #include <valgrind/memcheck.h> #endif +#include "src/shared/mainloop.h" #include "src/shared/util.h" #include "src/shared/tester.h" #include "src/shared/log.h" @@ -105,7 +106,6 @@ struct test_case { void *user_data; }; -static GMainLoop *main_loop; static char *tester_name; static GList *test_list; @@ -463,7 +463,7 @@ static void next_test_case(void) if (!test_current) { g_timer_stop(test_timer); - g_main_loop_quit(main_loop); + mainloop_quit(); return; } @@ -778,73 +778,19 @@ void tester_wait(unsigned int seconds, tester_wait_func_t func, print_progress(test->name, COLOR_BLACK, "waiting %u seconds", seconds); } -static gboolean signal_handler(GIOChannel *channel, GIOCondition condition, - gpointer user_data) +static void signal_callback(int signum, void *user_data) { static bool terminated = false; - struct signalfd_siginfo si; - ssize_t result; - int fd; - if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) { - g_main_loop_quit(main_loop); - return FALSE; - } - - fd = g_io_channel_unix_get_fd(channel); - - result = read(fd, &si, sizeof(si)); - if (result != sizeof(si)) - return FALSE; - - switch (si.ssi_signo) { + switch (signum) { case SIGINT: case SIGTERM: if (!terminated) - g_main_loop_quit(main_loop); + mainloop_quit(); terminated = true; break; } - - return TRUE; -} - -static guint setup_signalfd(void) -{ - GIOChannel *channel; - guint source; - sigset_t mask; - int fd; - - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGTERM); - - if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { - perror("Failed to set signal mask"); - return 0; - } - - fd = signalfd(-1, &mask, 0); - if (fd < 0) { - perror("Failed to create signal descriptor"); - return 0; - } - - channel = g_io_channel_unix_new(fd); - - g_io_channel_set_close_on_unref(channel, TRUE); - g_io_channel_set_encoding(channel, NULL, NULL); - g_io_channel_set_buffered(channel, FALSE); - - source = g_io_add_watch(channel, - G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - signal_handler, NULL); - - g_io_channel_unref(channel); - - return source; } bool tester_use_quiet(void) @@ -897,7 +843,7 @@ void tester_init(int *argc, char ***argv) exit(EXIT_SUCCESS); } - main_loop = g_main_loop_new(NULL, FALSE); + mainloop_init(); tester_name = strrchr(*argv[0], '/'); if (!tester_name) @@ -911,25 +857,16 @@ void tester_init(int *argc, char ***argv) int tester_run(void) { - guint signal; int ret; - if (!main_loop) - return EXIT_FAILURE; - if (option_list) { - g_main_loop_unref(main_loop); + mainloop_quit(); return EXIT_SUCCESS; } - signal = setup_signalfd(); - g_idle_add(start_tester, NULL); - g_main_loop_run(main_loop); - - g_source_remove(signal); - g_main_loop_unref(main_loop); + mainloop_run_with_signal(signal_callback, NULL); ret = tester_summarize(); -- 2.17.2 ^ permalink raw reply related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2018-11-28 15:00 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-11-28 15:00 [PATCH v2 01/10] share/mainloop: Add handling of NOTIFY_SOCKET Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 02/10] share/mainloop: Add watchdog support Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 03/10] tool/btmon-logger: Use mainloop_notify instead of sd_notify Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 04/10] core: Use mainloop_sd_notify " Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 05/10] core: Remove old code related to sd_notify Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 06/10] shared/timeout-glib: Check 0 id when removing timeout Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 07/10] shared/mainloop: Add mainloop_run_with_signal Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 08/10] shared/mainloop: Remove mainloop_set_signal Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 09/10] core: Make use of mainloop_run_with_signal Luiz Augusto von Dentz 2018-11-28 15:00 ` [PATCH v2 10/10] shared/tester: " Luiz Augusto von Dentz
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).