Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[usb_moded] Use common code for evaluating "can-export" state
When usb-moded is compiled with MEEGOLOCK feature enabled, only limited
functionality is available while device is locked. However the scattered
use of MEEGOLOCK compilation time conditionals makes the code
unnecessarily difficult to read.

Add a utility function to handle evaluation of the can-export state and
remove the now unnecessary preprocessor conditionals.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Aug 24, 2018
1 parent 046e1b7 commit 623c2ee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 91 deletions.
72 changes: 27 additions & 45 deletions src/usb_moded-trigger.c
Expand Up @@ -211,51 +211,33 @@ void trigger_stop(void)

static void trigger_parse_udev_properties(struct udev_device *dev)
{
const char *tmp = 0;
char *trigger = 0;
char *trigger_property = 0;
char *trigger_value = 0;
char *trigger_mode = 0;

trigger = config_get_trigger_property();
tmp = udev_device_get_property_value(dev, trigger);
if(!tmp)
{
/* do nothing and return */
free(trigger);
return;
}
else
{
free(trigger);
trigger = config_get_trigger_value();
if(trigger)
{
if(!strcmp(tmp, trigger))
{
#if defined MEEGOLOCK
if(devicelock_have_export_permission())
#endif /* MEEGOLOCK */
if(strcmp(config_get_trigger_mode(), usbmoded_get_usb_mode()) != 0)
{
usbmoded_set_usb_mode(config_get_trigger_mode());
}
free(trigger);
}
else
{
free(trigger);
return;
}
}
else
/* for triggers without trigger value */
{
#if defined MEEGOLOCK
if(devicelock_have_export_permission())
#endif /* MEEGOLOCK */
if(strcmp(config_get_trigger_mode(), usbmoded_get_usb_mode()) != 0)
{
usbmoded_set_usb_mode(config_get_trigger_mode());
}
}
return;
const char *value = 0;

if( !usbmoded_can_export() )
goto EXIT;

if( !(trigger_mode = config_get_trigger_mode()) )
goto EXIT;

if( !(trigger_property = config_get_trigger_property()) )
goto EXIT;

if( !(value = udev_device_get_property_value(dev, trigger_property)) )
goto EXIT;

if( (trigger_value = config_get_trigger_value()) ) {
if( strcmp(trigger_value, value) )
goto EXIT;
}

usbmoded_set_usb_mode(trigger_mode);

EXIT:
free(trigger_value);
free(trigger_property);
free(trigger_mode);
}
82 changes: 36 additions & 46 deletions src/usb_moded.c
Expand Up @@ -224,6 +224,7 @@ static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);

bool usbmoded_can_export (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
Expand Down Expand Up @@ -419,23 +420,11 @@ usbmoded_rethink_usb_charging_fallback(void)
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;

#ifdef MEEGOLOCK
/* If device locking is supported, the device must be in
* unlocked state (or rescue mode must be active).
*/
if( !devicelock_have_export_permission() && !usbmoded_rescue_mode ) {
log_notice("device is locked; stay in %s", usb_mode);
if( !usbmoded_can_export() ) {
log_notice("exporting data not allowed; stay in %s", usb_mode);
goto EXIT;
}

/* Device must be in USER state or in rescue mode
*/
if( !dsme_in_user_state() && !usbmoded_rescue_mode ) {
log_notice("device is not in USER mode; stay in %s", usb_mode);
goto EXIT;
}
#endif

log_debug("attempt to leave %s", usb_mode);
usbmoded_set_usb_connected_state();

Expand Down Expand Up @@ -490,20 +479,10 @@ static void usbmoded_switch_to_mode(const char *mode)
goto CHARGE;
}

#ifdef MEEGOLOCK
/* Potentially data exposing modes are allowed only when
* device has been booted to user mode and unlocked.
*
* Except if rescue mode is still active.
*/
bool can_export = (dsme_in_user_state() &&
devicelock_have_export_permission());

if( !can_export && !usbmoded_rescue_mode ) {
if( !usbmoded_can_export() ) {
log_warning("Policy does not allow mode: %s", mode);
goto CHARGE;
}
#endif

/* go through all the dynamic modes if the modelist exists*/
for( GList *iter = modelist; iter; iter = g_list_next(iter) )
Expand Down Expand Up @@ -683,7 +662,6 @@ static bool usbmoded_set_connection_state(bool state)
void usbmoded_set_usb_connected_state(void)
{
char *mode_to_set = 0;
bool can_export = true;

if( usbmoded_rescue_mode ) {
log_debug("Entering rescue mode!\n");
Expand All @@ -704,28 +682,18 @@ void usbmoded_set_usb_connected_state(void)

mode_to_set = config_get_mode_setting();

#ifdef MEEGOLOCK
/* Check if we are allowed to export system contents 0 is unlocked.
* We check also if the device is in user state or not.
* If not we do not export anything. We presume ACT_DEAD charging
*/
can_export = (devicelock_have_export_permission()
&& dsme_in_user_state());
#endif

if( mode_to_set && can_export ) {
/* If charging mode is the only available selection,
* don't ask just select it */
if( !strcmp(MODE_ASK, mode_to_set) ) {
gchar *available_modes = usbmoded_get_mode_list(AVAILABLE_MODES_LIST);
if( !g_strcmp0(available_modes, MODE_CHARGING) ) {
// FIXME free() vs g_free() conflict
free(mode_to_set);
mode_to_set = available_modes;
available_modes = 0;
}
/* If there is only one allowed mode, use it without
* going through ask-mode */
if( !strcmp(MODE_ASK, mode_to_set) ) {
// FIXME free() vs g_free() conflict
gchar *available = usbmoded_get_mode_list(AVAILABLE_MODES_LIST);
if( *available && !strchr(available, ',') ) {
free(mode_to_set), mode_to_set = available, available = 0;
}
g_free(available);
}

if( mode_to_set && usbmoded_can_export() ) {
usbmoded_set_usb_mode(mode_to_set);
}
else {
Expand Down Expand Up @@ -1164,6 +1132,28 @@ void usbmoded_delay_suspend(void)
usbmoded_allow_suspend_timer_cb, 0);
}

/** Check if exposing device data is currently allowed
*
* @return true exposing data is ok, or false otherwise
*/
bool usbmoded_can_export(void)
{
bool can_export = true;

#ifdef MEEGOLOCK
/* Modes that potentially expose data are allowed only when
* device is running in user mode and device is unlocked */
can_export = (dsme_in_user_state() &&
devicelock_have_export_permission());

/* Having bootup rescue mode active is an exception */
if( usbmoded_rescue_mode )
can_export = true;
#endif

return can_export;
}

/** Check if system has already been successfully booted up
*
* @return true if init-done has been reached, or false otherwise
Expand Down
1 change: 1 addition & 0 deletions src/usb_moded.h
Expand Up @@ -129,6 +129,7 @@ void usbmoded_acquire_wakelock (const char *wakelo
void usbmoded_release_wakelock (const char *wakelock_name);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_can_export (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
Expand Down

0 comments on commit 623c2ee

Please sign in to comment.