diff --git a/configure.ac b/configure.ac index 67dbfbf..e5127da 100644 --- a/configure.ac +++ b/configure.ac @@ -22,7 +22,7 @@ test_gcc_flag() { # We use gnu99 instead of c99 because many have interpreted the standard # in a way that int64_t isn't defined on non-64 bit platforms. -CFLAGS="-Os -std=gnu99 -Wall -Wextra -pipe -Wold-style-definition -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wno-unused-parameter -finline-small-functions -Wno-unused-result -fstack-protector -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now -fPIE -fpie -pie" +CFLAGS="-Os -std=gnu99 -Wall -Wextra -pipe -Wold-style-definition -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -finline-small-functions -fstack-protector -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now -fPIE -fpie -pie" LDFLAGS="-z relro -z now" AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug],[Enable debug @<:@default=false@:>@]), diff --git a/src/usb_moded-appsync.c b/src/usb_moded-appsync.c index eac7a77..4c82b2b 100644 --- a/src/usb_moded-appsync.c +++ b/src/usb_moded-appsync.c @@ -52,7 +52,7 @@ static void cancel_enumerate_usb_timer(void); static GList *sync_list = NULL; static guint enumerate_usb_id = 0; -static struct timeval sync_tv; +static struct timeval sync_tv = {0, 0}; #ifdef APP_SYNC_DBUS static int no_dbus = 0; #else diff --git a/src/usb_moded-appsync.h b/src/usb_moded-appsync.h index f1ef638..0234957 100644 --- a/src/usb_moded-appsync.h +++ b/src/usb_moded-appsync.h @@ -32,6 +32,8 @@ #define APP_INFO_SYSTEMD_KEY "systemd" #define APP_INFO_POST "post" +/** Application activation state + */ typedef enum { /** Application is not relevant for the current mode */ APP_STATE_DONTCARE = 0, @@ -46,15 +48,13 @@ typedef enum { */ typedef struct list_elem { - /*@{*/ - char *name; /* name of the app to launch */ - char *mode; /* mode in which to launch the app */ - char *launch; /* dbus launch command/address */ - app_state_t state; /* marker to check if the app has started sucessfully */ - int systemd; /* marker to know if we start it with systemd or not */ - int post; /* marker to indicate when to start the app */ - /*@}*/ -}list_elem; + char *name; /**< name of the app to launch */ + char *mode; /**< mode in which to launch the app */ + char *launch; /**< dbus launch command/address */ + app_state_t state; /**< marker to check if the app has started sucessfully */ + int systemd; /**< marker to know if we start it with systemd or not */ + int post; /**< marker to indicate when to start the app */ +} list_elem; void readlist(int diag); int activate_sync(const char *mode); diff --git a/src/usb_moded-config.c b/src/usb_moded-config.c index d6c0513..26a74e5 100644 --- a/src/usb_moded-config.c +++ b/src/usb_moded-config.c @@ -528,7 +528,12 @@ set_config_result_t set_mode_whitelist(const char *whitelist) g_free(mode_setting); current_mode = get_usb_mode(); - if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && valid_mode(current_mode)) { + if (!strcmp(current_mode, MODE_UNDEFINED)) { + /* Disconnected -> do nothing */ + } + else if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && valid_mode(current_mode)) { + /* Invalid mode that is not MODE_ASK or MODE_CHARGING_FALLBACK + * -> switch to MODE_CHARGING_FALLBACK */ usb_moded_mode_cleanup(get_usb_module()); set_usb_mode(MODE_CHARGING_FALLBACK); } diff --git a/src/usb_moded-config.h b/src/usb_moded-config.h index 3570bc0..c6ed11d 100644 --- a/src/usb_moded-config.h +++ b/src/usb_moded-config.h @@ -85,10 +85,12 @@ char * get_mode_whitelist(void); int is_roaming_not_allowed(void); +/** Configuration change result + */ typedef enum set_config_result_t { - SET_CONFIG_ERROR = -1, - SET_CONFIG_UPDATED, - SET_CONFIG_UNCHANGED + SET_CONFIG_ERROR = -1, /**< Value change failed */ + SET_CONFIG_UPDATED, /**< Value change succeeded */ + SET_CONFIG_UNCHANGED, /**< Value did not change */ } set_config_result_t; int conf_file_merge(void); diff --git a/src/usb_moded-dbus.c b/src/usb_moded-dbus.c index 3a2e685..0c8caec 100644 --- a/src/usb_moded-dbus.c +++ b/src/usb_moded-dbus.c @@ -47,8 +47,6 @@ static DBusConnection *dbus_connection_sys = NULL; static gboolean have_service_name = FALSE; -extern gboolean rescue_mode; - /** * Issues "sig_usb_config_ind" signal. */ @@ -188,6 +186,11 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa if(!interface || !member || !object) goto EXIT; + log_debug("DBUS %s %s.%s from %s", + dbus_message_type_to_string(type), + interface, member, + dbus_message_get_sender(msg) ?: "N/A"); + if( type == DBUS_MESSAGE_TYPE_SIGNAL ) { if( !strcmp(interface, INIT_DONE_INTERFACE) && !strcmp(member, INIT_DONE_SIGNAL) ) { diff --git a/src/usb_moded-dyn-config.h b/src/usb_moded-dyn-config.h index 81e7d2d..aa9ab96 100644 --- a/src/usb_moded-dyn-config.h +++ b/src/usb_moded-dyn-config.h @@ -65,36 +65,34 @@ android engineers prefered to have sysfs entries... go figure... */ */ typedef struct mode_list_elem { - /*@{ */ - char *mode_name; /* mode name */ - char *mode_module; /* needed module for given mode */ - int appsync; /* requires appsync or not */ - int network; /* bring up network or not */ - int mass_storage; /* Use mass-storage functions */ - char *network_interface; /* Which network interface to bring up if network needs to be enabled */ - char *sysfs_path; /* path to set sysfs options */ - char *sysfs_value; /* option name/value to write to sysfs */ - char *sysfs_reset_value; /* value to reset the the sysfs to default */ - char *softconnect; /* value to be written to softconnect interface */ - char *softconnect_disconnect; /* value to set on the softconnect interface to disable after disconnect */ - char *softconnect_path; /* path for the softconnect */ - char *android_extra_sysfs_path; /* path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value; /* static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path2; /* path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value2; /* static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path3; /* path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value3; /* static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path4; /* path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value4; /* static value that never changes that needs to be set by sysfs :( */ - char *idProduct; /* product id to assign to a specific profile */ - char *idVendorOverride; /* Temporary vendor override for special modes used by odms in testing/manufacturing */ - int nat; /* If NAT should be set up in this mode or not */ - int dhcp_server; /* if a DHCP server needs to be configured and started or not */ + char *mode_name; /**< Mode name */ + char *mode_module; /**< Needed module for given mode */ + int appsync; /**< Requires appsync or not */ + int network; /**< Bring up network or not */ + int mass_storage; /**< Use mass-storage functions */ + char *network_interface; /**< Which network interface to bring up if network needs to be enabled */ + char *sysfs_path; /**< Path to set sysfs options */ + char *sysfs_value; /**< Option name/value to write to sysfs */ + char *sysfs_reset_value; /**< Value to reset the the sysfs to default */ + char *softconnect; /**< Value to be written to softconnect interface */ + char *softconnect_disconnect; /**< Value to set on the softconnect interface to disable after disconnect */ + char *softconnect_path; /**< Path for the softconnect */ + char *android_extra_sysfs_path; /**< Path for static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_value; /**< Static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_path2; /**< Path for static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_value2; /**< Static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_path3; /**< Path for static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_value3; /**< Static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_path4; /**< Path for static value that never changes that needs to be set by sysfs :( */ + char *android_extra_sysfs_value4; /**< Static value that never changes that needs to be set by sysfs :( */ + char *idProduct; /**< Product id to assign to a specific profile */ + char *idVendorOverride; /**< Temporary vendor override for special modes used by odms in testing/manufacturing */ + int nat; /**< If NAT should be set up in this mode or not */ + int dhcp_server; /**< if a DHCP server needs to be configured and started or not */ #ifdef CONNMAN - char* connman_tethering; /* connman's tethering technology path */ + char* connman_tethering; /**< Connman's tethering technology path */ #endif - /*@} */ -}mode_list_elem; +} mode_list_elem; /* diag is used to select a secondary configuration location for diagnostic purposes */ GList *read_mode_list(int diag); diff --git a/src/usb_moded-log.c b/src/usb_moded-log.c index 06084ee..5a19c8c 100644 --- a/src/usb_moded-log.c +++ b/src/usb_moded-log.c @@ -65,12 +65,14 @@ static void log_gettime(struct timeval *tv) timersub(tv, &log_begtime, tv); } -/** - * Print the logged messages to the selected output +/** Print the logged messages to the selected output * - * @param lev The wanted log level - * @param fmt The message to be logged - * @param va The stdarg variable list + * @param file Source file name + * @param func Function name + * @param line Line in source file + * @param lev The wanted log level + * @param fmt The message format string + * @param va Arguments for the format string */ void log_emit_va(const char *file, const char *func, int line, int lev, const char *fmt, va_list va) { @@ -138,6 +140,15 @@ void log_emit_va(const char *file, const char *func, int line, int lev, const ch errno = saved; } +/** Print the logged messages to the selected output + * + * @param file Source file name + * @param func Function name + * @param line Line in source file + * @param lev The wanted log level + * @param fmt The message format string + * @param ... Arguments for the format string + */ void log_emit_real(const char *file, const char *func, int line, int lev, const char *fmt, ...) { va_list va; diff --git a/src/usb_moded-modules-android.c b/src/usb_moded-modules-android.c index 630af54..dc29bd2 100644 --- a/src/usb_moded-modules-android.c +++ b/src/usb_moded-modules-android.c @@ -38,9 +38,6 @@ #include "usb_moded-config.h" #include "usb_moded-modesetting.h" -/* kmod context - initialized at start in usb_moded_init */ -struct kmod_ctx *ctx; - /** load module * * @param module Name of the module to load diff --git a/src/usb_moded-network.c b/src/usb_moded-network.c index 0b6f50d..72e2d82 100644 --- a/src/usb_moded-network.c +++ b/src/usb_moded-network.c @@ -52,7 +52,7 @@ #define UDHCP_CONFIG_DIR "/run/usb-moded" #define UDHCP_CONFIG_LINK "/etc/udhcpd.conf" -const char default_interface[] = "usb0"; +static const char default_interface[] = "usb0"; /** IP forwarding configuration block */ typedef struct ipforward_data @@ -100,6 +100,8 @@ static int check_interface(char *interface) static char* get_interface(struct mode_list_elem *data) { + (void)data; // FIXME: why is this passed in the 1st place? + char *interface = 0; char *setting = get_network_setting(NETWORK_INTERFACE_KEY); diff --git a/src/usb_moded-trigger.c b/src/usb_moded-trigger.c index e09f346..8ad73fe 100644 --- a/src/usb_moded-trigger.c +++ b/src/usb_moded-trigger.c @@ -47,7 +47,7 @@ static struct udev *udev = 0; static struct udev_monitor *mon = 0; static GIOChannel *iochannel = 0; static guint watch_id = 0; -static const char *dev_name; +static const char *dev_name = 0; /* static function definitions */ static gboolean monitor_udev(GIOChannel *iochannel G_GNUC_UNUSED, GIOCondition cond, @@ -56,6 +56,8 @@ static void udev_parse(struct udev_device *dev); static void notify_issue (gpointer data) { + (void)data; + log_debug("trigger watch destroyed\n!"); /* clean up & restart trigger */ trigger_stop(); diff --git a/src/usb_moded-udev.c b/src/usb_moded-udev.c index 7d86cd3..1e39b7f 100644 --- a/src/usb_moded-udev.c +++ b/src/usb_moded-udev.c @@ -40,10 +40,10 @@ #include "usb_moded-modes.h" /* global variables */ -static struct udev *udev; -static struct udev_monitor *mon; -static GIOChannel *iochannel; -static guint watch_id; +static struct udev *udev = 0; +static struct udev_monitor *mon = 0; +static GIOChannel *iochannel = 0; +static guint watch_id = 0; static char *dev_name = 0; static int cleanup = 0; /* track cable and charger connects disconnects */ @@ -70,6 +70,8 @@ static gboolean cable_connection_timeout_cb(gpointer data); static void notify_issue (gpointer data) { + (void)data; + /* we do not want to restart when we try to clean up */ if(cleanup) return; @@ -335,6 +337,8 @@ static void setup_charger_connection(void) static gboolean cable_connection_timeout_cb(gpointer data) { + (void)data; + log_debug("connect delay: timeout"); cable_connection_timeout_id = 0; diff --git a/src/usb_moded-util.c b/src/usb_moded-util.c index f716fdf..2007f78 100644 --- a/src/usb_moded-util.c +++ b/src/usb_moded-util.c @@ -29,8 +29,7 @@ #include "usb_moded-dbus.h" -DBusError error; -DBusConnection *conn = 0; +static DBusConnection *conn = 0; static int query_mode (void) { @@ -408,7 +407,7 @@ int main (int argc, char *argv[]) } /* init dbus */ - dbus_error_init(&error); + DBusError error = DBUS_ERROR_INIT; conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); if (!conn) diff --git a/src/usb_moded.c b/src/usb_moded.c index 9d9b96b..2064d95 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -65,17 +65,29 @@ # define VERBOSE_WAKELOCKING 0 #endif +/** A struct containing all the usb_moded info needed + */ +typedef struct usb_mode +{ + gboolean connected; /**< Connection status, 1 for connected */ + gboolean mounted; /**< Mount status, 1 for mounted -UNUSED atm- */ + gboolean android_usb_broken; /**< Used to keep an active gadget for broken Android kernels */ + char *mode; /**< The mode name */ + char *module; /**< The module name for the specific mode */ + struct mode_list_elem *data; /**< Contains the mode data */ +} usb_mode; + /* global definitions */ static int usb_moded_exitcode = EXIT_FAILURE; static GMainLoop *usb_moded_mainloop = NULL; gboolean rescue_mode = FALSE; -gboolean diag_mode = FALSE; -gboolean hw_fallback = FALSE; -gboolean android_broken_usb = FALSE; -gboolean android_ignore_udev_events = FALSE; -gboolean android_ignore_next_udev_disconnect_event = FALSE; +static gboolean diag_mode = FALSE; +static gboolean hw_fallback = FALSE; +static gboolean android_broken_usb = FALSE; +static gboolean android_ignore_udev_events = FALSE; +static gboolean android_ignore_next_udev_disconnect_event = FALSE; #ifdef SYSTEMD static gboolean systemd_notify = FALSE; #endif @@ -120,9 +132,17 @@ static void set_cable_connection_delay(int delay_ms) } } -struct usb_mode current_mode; -guint charging_timeout = 0; -static GList *modelist; +static struct usb_mode current_mode = { + .connected = FALSE, + .mounted = FALSE, + .android_usb_broken = FALSE, + .mode = NULL, + .module = NULL, + .data = NULL, +}; + +static guint charging_timeout = 0; +static GList *modelist = 0; /* static helper functions */ static gboolean set_disconnected(gpointer data); @@ -186,6 +206,8 @@ void set_usb_connected(gboolean connected) static gboolean set_disconnected(gpointer data) { + (void)data; + /* let usb settle */ usb_moded_sleep(1); /* only disconnect for real if we are actually still disconnected */ @@ -209,6 +231,8 @@ static gboolean set_disconnected(gpointer data) /* set disconnected without sending signals. */ static gboolean set_disconnected_silent(gpointer data) { + (void)data; + if(!get_usb_connection_state()) { log_debug("Resetting connection data after HUP\n"); @@ -767,6 +791,10 @@ static void usb_moded_cleanup(void) /* charging fallback handler */ static gboolean charging_fallback(gpointer data) { + (void)data; + + charging_timeout = 0; + /* if a mode has been set we don't want it changed to charging * after 5 seconds. We set it to ask, so anything different * means a mode has been set */ @@ -780,7 +808,6 @@ static gboolean charging_fallback(gpointer data) free(current_mode.mode); current_mode.mode = strdup(MODE_ASK); current_mode.data = NULL; - charging_timeout = 0; log_info("Falling back on charging mode.\n"); return(FALSE); @@ -1352,8 +1379,12 @@ int main(int argc, char* argv[]) /* silence usb_moded_system() calls */ if( log_get_type() != LOG_TO_STDERR && log_get_level() != LOG_DEBUG ) { - freopen("/dev/null", "a", stdout); - freopen("/dev/null", "a", stderr); + if( !freopen("/dev/null", "a", stdout) ) { + log_err("can't redirect stdout: %m"); + } + if( !freopen("/dev/null", "a", stderr) ) { + log_err("can't redirect stderr: %m"); + } } #if !GLIB_CHECK_VERSION(2, 36, 0) diff --git a/src/usb_moded.h b/src/usb_moded.h index e4912b7..f16df5f 100644 --- a/src/usb_moded.h +++ b/src/usb_moded.h @@ -43,23 +43,12 @@ #define USB_MODED_LOCKFILE "/var/run/usb_moded.pid" #define MAX_READ_BUF 512 -/** - * a struct containing all the usb_moded info needed +/** Mode list types */ -typedef struct usb_mode -{ - /*@{*/ - gboolean connected; /* connection status, 1 for connected */ - gboolean mounted; /* mount status, 1 for mounted -UNUSED atm- */ - gboolean android_usb_broken; /* Used to keep an active gadget for broken Android kernels */ - char *mode; /* the mode name */ - char *module; /* the module name for the specific mode */ - struct mode_list_elem *data; /* contains the mode data */ - /*@}*/ -}usb_mode; - typedef enum mode_list_type_t { + /** All configured modes */ SUPPORTED_MODES_LIST, + /** Configured modes that can be activated */ AVAILABLE_MODES_LIST } mode_list_type_t; @@ -99,6 +88,7 @@ void allow_suspend(void); void delay_suspend(void); extern int cable_connection_delay; +extern gboolean rescue_mode; void usb_moded_stop(int exitcode);