Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[worker] Stop mtp daemon on mode switch. JB#43781
If entry to mtp mode gets aborted due to cable disconnect, it is possible
that mtp daemon has already been started but it does not yet show up when
probing. This can lead to leaving stray/defunct mtp daemon instances
behind - which then can cause attempt to activate mtp mode after reconnect
to fail.

When leaving mode, stop mtp daemon if it seems to be running or we have
made an attempt to start it.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Nov 13, 2018
1 parent 3b2cb9c commit 91244cd
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/usb_moded-worker.c
Expand Up @@ -157,6 +157,14 @@ static unsigned worker_mtp_start_delay = 120 * 1000;
*/
static unsigned worker_mtp_stop_delay = 15 * 1000;

/** Flag for: We have started mtp daemon
*
* If we have issued systemd unit start, we should also
* issue systemd unit stop even if probing for mtpd
* presense gives negative result.
*/
static bool worker_mtp_service_started = false;

static bool worker_mode_is_mtp_mode(const char *mode)
{
return mode && !strcmp(mode, "mtp_mode");
Expand Down Expand Up @@ -208,7 +216,7 @@ worker_stop_mtpd(void)
{
bool ack = false;

if( worker_mtpd_stopped_p(0) ) {
if( !worker_mtp_service_started && worker_mtpd_stopped_p(0) ) {
log_debug("mtp daemon is not running");
goto SUCCESS;
}
Expand All @@ -219,6 +227,9 @@ worker_stop_mtpd(void)
goto FAILURE;
}

/* Have succesfully stopped mtp service */
worker_mtp_service_started = false;

if( common_wait(worker_mtp_stop_delay, worker_mtpd_stopped_p, 0) != WAIT_READY ) {
log_warning("failed to stop mtp daemon; giving up");
goto FAILURE;
Expand All @@ -243,6 +254,9 @@ worker_start_mtpd(void)
goto SUCCESS;
}

/* Have attempted to start mtp service */
worker_mtp_service_started = true;

int rc = common_system("systemctl-user start buteo-mtp.service");
if( rc != 0 ) {
log_warning("failed to start mtp daemon; exit code = %d", rc);
Expand Down Expand Up @@ -495,8 +509,10 @@ worker_switch_to_mode(const char *mode)

log_debug("Cleaning up previous mode");

if( !worker_mode_is_mtp_mode(mode) )
worker_stop_mtpd();
/* Either mtp daemon is not needed, or it must be *started* in
* correct phase of gadget configuration when entering mtp mode.
*/
worker_stop_mtpd();

if( worker_get_usb_mode_data() ) {
modesetting_leave_dynamic_mode();
Expand Down Expand Up @@ -566,10 +582,7 @@ worker_switch_to_mode(const char *mode)
/* Undo any changes we might have might have already done */
if( worker_get_usb_mode_data() ) {
log_debug("Cleaning up failed mode switch");

if( worker_mode_is_mtp_mode(mode) )
worker_stop_mtpd();

worker_stop_mtpd();
modesetting_leave_dynamic_mode();
worker_set_usb_mode_data(NULL);
}
Expand Down

0 comments on commit 91244cd

Please sign in to comment.