From 709e49a7fc7b67b72e947973968566d589e950be Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Thu, 31 May 2018 15:04:30 +0300 Subject: [PATCH] [usb_moded] Make init-done state generally available There are situations where usb-moded needs to behave in slightly different manner depending on whether it is executed as a part of device starup sequence or (restarted) after the device is already fully booted up. Make the related init-done functions available to all modules within usb-moded source tree. Signed-off-by: Simo Piiroinen --- src/usb_moded-dbus.c | 6 ++++++ src/usb_moded.c | 30 +++++++++++++++++++++++++++--- src/usb_moded.h | 5 +++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/usb_moded-dbus.c b/src/usb_moded-dbus.c index 0c8caec..96c703c 100644 --- a/src/usb_moded-dbus.c +++ b/src/usb_moded-dbus.c @@ -194,6 +194,9 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa if( type == DBUS_MESSAGE_TYPE_SIGNAL ) { if( !strcmp(interface, INIT_DONE_INTERFACE) && !strcmp(member, INIT_DONE_SIGNAL) ) { + /* Update the cached state value */ + set_init_done(true); + /* Auto-disable rescue mode when bootup is finished */ if( rescue_mode ) { rescue_mode = FALSE; @@ -598,6 +601,9 @@ gboolean usb_moded_dbus_init_connection(void) /* Listen to init-done signals */ dbus_bus_add_match(dbus_connection_sys, INIT_DONE_MATCH, 0); + /* Re-check flag file after adding signal listener */ + probe_init_done(); + /* Connect D-Bus to the mainloop */ dbus_connection_setup_with_g_main(dbus_connection_sys, NULL); diff --git a/src/usb_moded.c b/src/usb_moded.c index 2064d95..f507dc9 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -150,7 +150,6 @@ static gboolean set_disconnected_silent(gpointer data); static void usb_moded_init(void); static gboolean charging_fallback(gpointer data); static void usage(void); -static bool init_done_p(void); /* ============= Implementation starts here =========================================== */ /** set the usb connection status @@ -1192,13 +1191,35 @@ void delay_suspend(void) allow_suspend_cb, 0); } +/** Path to init-done flag file */ +static const char init_done_flagfile[] = "/run/systemd/boot-status/init-done"; + +/** cached init-done-reached state */ +static bool init_done_reached = false; + /** Check if system has already been successfully booted up * * @return true if init-done has been reached, or false otherwise */ -static bool init_done_p(void) +bool init_done_p(void) +{ + return init_done_reached; +} + +/** Update cached init-done-reached state */ +void set_init_done(bool reached) { - return access("/run/systemd/boot-status/init-done", F_OK) == 0; + if( init_done_reached != reached ) { + init_done_reached = reached; + log_debug("init_done -> %s", + init_done_reached ? "reached" : "not reached"); + } +} + +/** Check whether init-done flag file exists */ +void probe_init_done(void) +{ + set_init_done(access(init_done_flagfile, F_OK) == 0); } /** Request orderly exit from mainloop @@ -1394,6 +1415,9 @@ int main(int argc, char* argv[]) g_thread_init(NULL); #endif + /* Check if we are in mid-bootup */ + probe_init_done(); + /* Must be the 1st libdbus call that is made */ dbus_threads_init_default(); diff --git a/src/usb_moded.h b/src/usb_moded.h index f16df5f..b425f53 100644 --- a/src/usb_moded.h +++ b/src/usb_moded.h @@ -25,6 +25,7 @@ #define USB_MODED_H_ #include +#include #include #include #include @@ -103,4 +104,8 @@ void usb_moded_usleep_(const char *file, int line, const char *func, useconds_t #define usb_moded_msleep(msec) usb_moded_usleep_(__FILE__,__LINE__,__FUNCTION__,(msec)*1000) #define usb_moded_sleep(sec) usb_moded_usleep_(__FILE__,__LINE__,__FUNCTION__,(sec)*1000000) +bool init_done_p(void); +void set_init_done(bool reached); +void probe_init_done(void); + #endif /* USB_MODED_H */