Skip to content

Commit

Permalink
[network] Improve dealing with missing interfaces.
Browse files Browse the repository at this point in the history
When the interface is missing or not yet up we try to
gracefully give it a second try. As sometimes something
like a functionfs using gadget could take a while to get
initialized. Also do not fail on a failure to set up the
dhcp server. Falling back on manual setup is better than
not being able to do so.

Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Jul 2, 2015
1 parent 56e6613 commit 0bb1cc4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/usb_moded-modesetting.c
Expand Up @@ -318,6 +318,7 @@ int set_dynamic_mode(void)

struct mode_list_elem *data;
int ret = 1;
int network = 1;

data = get_usb_mode_data();

Expand Down Expand Up @@ -377,14 +378,14 @@ int set_dynamic_mode(void)
system(command);
#else
usb_network_down(data);
usb_network_up(data);
network = usb_network_up(data);
#endif /* DEBIAN */
}

/* Needs to be called before application post synching so
that the dhcp server has the right config */
if(data->nat || data->dhcp_server)
ret = usb_network_set_up_dhcpd(data);
usb_network_set_up_dhcpd(data);

/* no need to execute the post sync if there was an error setting the mode */
if(data->appsync && !ret)
Expand All @@ -395,6 +396,15 @@ int set_dynamic_mode(void)
connman_set_tethering(data->connman_tethering, TRUE);
#endif

/* try a second time to bring up the network if it failed the first time,
this can happen with functionfs based gadgets (which is why we sleep for a bit */
if(network != 0)
{
log_debug("Retry setting up te network\n");
sleep(1);
usb_network_up(data);
}

if(ret)
usb_moded_send_error_signal(MODE_SETTING_FAILED);
return(ret);
Expand Down
13 changes: 12 additions & 1 deletion src/usb_moded-network.c
Expand Up @@ -122,8 +122,10 @@ static char* get_interface(struct mode_list_elem *data)

check = check_interface(interface);
if(check)
{
log_warning("Configured interface is incorrect, nor does usb0 exists. Check your config!\n");
/* TODO: Make it so that interface configuration gets skipped when no usable interface exists */
return(0);
}

log_debug("interface = %s\n", interface);
return interface;
Expand All @@ -139,6 +141,8 @@ static int set_usb_ip_forward(struct mode_list_elem *data, struct ipforward_data
char command[128];

interface = get_interface(data);
if(interface == NULL)
return(1);
nat_interface = get_network_nat_interface();
if((nat_interface == NULL) && (ipforward->nat_interface != NULL))
nat_interface = strdup(ipforward->nat_interface);
Expand Down Expand Up @@ -362,6 +366,8 @@ static int write_udhcpd_conf(struct ipforward_data *ipforward, struct mode_list_
strcat(ipend, "10");

interface = get_interface(data);
if(interface == NULL)
return(1);
/* print all data in the file */
fprintf(conffile, "start\t%s\n", ipstart);
fprintf(conffile, "end\t%s\n", ipend);
Expand Down Expand Up @@ -1071,6 +1077,9 @@ int usb_network_up(struct mode_list_elem *data)
ip = get_network_ip();
gateway = get_network_gateway();

if(interface == NULL)
return(1);

if(ip == NULL)
{
sprintf(command,"ifconfig %s 192.168.2.15", interface);
Expand Down Expand Up @@ -1166,6 +1175,8 @@ int usb_network_down(struct mode_list_elem *data)
char command[128];

interface = get_interface(data);
if(interface == NULL)
return(0);

sprintf(command, "ifconfig %s down\n", interface);
system(command);
Expand Down

0 comments on commit 0bb1cc4

Please sign in to comment.