Commit 2b01382b authored by spiiroin's avatar spiiroin

[debug] Use wrapper for system() calls. MER#1774

Calling system() function blocks usb-moded mainloop until the subprocess
finishes. Additionally usb-moded mostly does not check whether the
calls succeeded or not.

When in verbose mode, log execution of subprocesses.

If system() returns non-zero exit value, log a warning to indicate
potentially failed command execution.
Signed-off-by: spiiroin's avatarSimo Piiroinen <simo.piiroinen@jollamobile.com>
parent d5c5651f
...@@ -194,7 +194,7 @@ static int set_mass_storage_mode(struct mode_list_elem *data) ...@@ -194,7 +194,7 @@ static int set_mass_storage_mode(struct mode_list_elem *data)
usb_moded_unload_module(MODULE_MASS_STORAGE); usb_moded_unload_module(MODULE_MASS_STORAGE);
sprintf(command2, "modprobe %s luns=%d \n", MODULE_MASS_STORAGE, mountpoints); sprintf(command2, "modprobe %s luns=%d \n", MODULE_MASS_STORAGE, mountpoints);
log_debug("usb-load command = %s \n", command2); log_debug("usb-load command = %s \n", command2);
ret = system(command2); ret = usb_moded_system(command2);
if(ret) if(ret)
return(ret); return(ret);
} }
...@@ -209,14 +209,14 @@ static int set_mass_storage_mode(struct mode_list_elem *data) ...@@ -209,14 +209,14 @@ static int set_mass_storage_mode(struct mode_list_elem *data)
else else
mountpath = mounts[i]; mountpath = mounts[i];
umount: command = g_strconcat("mount | grep ", mountpath, NULL); umount: command = g_strconcat("mount | grep ", mountpath, NULL);
ret = system(command); ret = usb_moded_system(command);
g_free(command); g_free(command);
if(!ret) if(!ret)
{ {
/* no check for / needed as that will fail to umount anyway */ /* no check for / needed as that will fail to umount anyway */
command = g_strconcat("umount ", mountpath, NULL); command = g_strconcat("umount ", mountpath, NULL);
log_debug("unmount command = %s\n", command); log_debug("unmount command = %s\n", command);
ret = system(command); ret = usb_moded_system(command);
g_free(command); g_free(command);
if(ret != 0) if(ret != 0)
{ {
...@@ -251,7 +251,7 @@ umount: command = g_strconcat("mount | grep ", mountpath, NULL); ...@@ -251,7 +251,7 @@ umount: command = g_strconcat("mount | grep ", mountpath, NULL);
{ {
sprintf(command2, "echo %i > /sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/nofua", fua, i); sprintf(command2, "echo %i > /sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/nofua", fua, i);
log_debug("usb lun = %s active\n", command2); log_debug("usb lun = %s active\n", command2);
system(command2); usb_moded_system(command2);
sprintf(command2, "/sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/file", i); sprintf(command2, "/sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/file", i);
log_debug("usb lun = %s active\n", command2); log_debug("usb lun = %s active\n", command2);
write_to_file(command2, mounts[i]); write_to_file(command2, mounts[i]);
...@@ -301,13 +301,13 @@ static int unset_mass_storage_mode(struct mode_list_elem *data) ...@@ -301,13 +301,13 @@ static int unset_mass_storage_mode(struct mode_list_elem *data)
else else
mountpath = mounts[i]; mountpath = mounts[i];
command = g_strconcat("mount | grep ", mountpath, NULL); command = g_strconcat("mount | grep ", mountpath, NULL);
ret = system(command); ret = usb_moded_system(command);
g_free(command); g_free(command);
if(ret) if(ret)
{ {
command = g_strconcat("mount ", mountpath, NULL); command = g_strconcat("mount ", mountpath, NULL);
log_debug("mount command = %s\n",command); log_debug("mount command = %s\n",command);
ret = system(command); ret = usb_moded_system(command);
g_free(command); g_free(command);
/* mount returns 0 if success */ /* mount returns 0 if success */
if(ret != 0 ) if(ret != 0 )
...@@ -321,7 +321,7 @@ static int unset_mass_storage_mode(struct mode_list_elem *data) ...@@ -321,7 +321,7 @@ static int unset_mass_storage_mode(struct mode_list_elem *data)
{ {
command = g_strconcat("mount -t tmpfs tmpfs -o ro --size=512K ", mount, NULL); command = g_strconcat("mount -t tmpfs tmpfs -o ro --size=512K ", mount, NULL);
log_debug("Total failure, mount ro tmpfs as fallback\n"); log_debug("Total failure, mount ro tmpfs as fallback\n");
ret = system(command); ret = usb_moded_system(command);
g_free(command); g_free(command);
} }
usb_moded_send_error_signal(RE_MOUNT_FAILED); usb_moded_send_error_signal(RE_MOUNT_FAILED);
...@@ -342,7 +342,7 @@ static int unset_mass_storage_mode(struct mode_list_elem *data) ...@@ -342,7 +342,7 @@ static int unset_mass_storage_mode(struct mode_list_elem *data)
{ {
sprintf(command2, "echo \"\" > /sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/file", i); sprintf(command2, "echo \"\" > /sys/devices/platform/musb_hdrc/gadget/gadget-lun%d/file", i);
log_debug("usb lun = %s inactive\n", command2); log_debug("usb lun = %s inactive\n", command2);
system(command2); usb_moded_system(command2);
} }
} }
g_strfreev(mounts); g_strfreev(mounts);
...@@ -456,7 +456,7 @@ int set_dynamic_mode(void) ...@@ -456,7 +456,7 @@ int set_dynamic_mode(void)
char command[256]; char command[256];
g_snprintf(command, 256, "ifdown %s ; ifup %s", data->network_interface, data->network_interface); g_snprintf(command, 256, "ifdown %s ; ifup %s", data->network_interface, data->network_interface);
system(command); usb_moded_system(command);
#else #else
usb_network_down(data); usb_network_down(data);
network = usb_network_up(data); network = usb_network_up(data);
......
...@@ -155,13 +155,13 @@ static int set_usb_ip_forward(struct mode_list_elem *data, struct ipforward_data ...@@ -155,13 +155,13 @@ static int set_usb_ip_forward(struct mode_list_elem *data, struct ipforward_data
} }
write_to_file("/proc/sys/net/ipv4/ip_forward", "1"); write_to_file("/proc/sys/net/ipv4/ip_forward", "1");
snprintf(command, 128, "/sbin/iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", nat_interface); snprintf(command, 128, "/sbin/iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", nat_interface);
system(command); usb_moded_system(command);
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface); snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
system(command); usb_moded_system(command);
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface); snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
system(command); usb_moded_system(command);
free(interface); free(interface);
free(nat_interface); free(nat_interface);
...@@ -178,7 +178,7 @@ static void clean_usb_ip_forward(void) ...@@ -178,7 +178,7 @@ static void clean_usb_ip_forward(void)
connman_reset_state(); connman_reset_state();
#endif #endif
write_to_file("/proc/sys/net/ipv4/ip_forward", "0"); write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
system("/sbin/iptables -F FORWARD"); usb_moded_system("/sbin/iptables -F FORWARD");
} }
#ifdef OFONO #ifdef OFONO
...@@ -782,10 +782,10 @@ static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on) ...@@ -782,10 +782,10 @@ static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on)
/* /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false */ /* /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false */
if(wifistatus && !on) if(wifistatus && !on)
system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false"); usb_moded_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false");
if(wifistatus && on) if(wifistatus && on)
/* turn on wifi after tethering is over and wifi was on before */ /* turn on wifi after tethering is over and wifi was on before */
system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true"); usb_moded_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true");
return(0); return(0);
} }
...@@ -972,8 +972,8 @@ int usb_network_up(struct mode_list_elem *data) ...@@ -972,8 +972,8 @@ int usb_network_up(struct mode_list_elem *data)
const char *service = NULL; const char *service = NULL;
/* make sure connman will recognize the gadget interface NEEDED? */ /* make sure connman will recognize the gadget interface NEEDED? */
//system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/gadget net.connman.Technology.SetProperty string:Powered variant:boolean:true"); //usb_moded_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/gadget net.connman.Technology.SetProperty string:Powered variant:boolean:true");
//system("/sbin/ifconfig rndis0 up"); //usb_moded_system("/sbin/ifconfig rndis0 up");
log_debug("waiting for connman to pick up interface\n"); log_debug("waiting for connman to pick up interface\n");
sleep(1); sleep(1);
...@@ -1089,25 +1089,25 @@ int usb_network_up(struct mode_list_elem *data) ...@@ -1089,25 +1089,25 @@ int usb_network_up(struct mode_list_elem *data)
if(!strcmp(ip, "dhcp")) if(!strcmp(ip, "dhcp"))
{ {
sprintf(command, "dhclient -d %s\n", interface); sprintf(command, "dhclient -d %s\n", interface);
ret = system(command); ret = usb_moded_system(command);
if(ret != 0) if(ret != 0)
{ {
sprintf(command, "udhcpc -i %s\n", interface); sprintf(command, "udhcpc -i %s\n", interface);
system(command); usb_moded_system(command);
} }
} }
else else
{ {
sprintf(command, "ifconfig %s %s %s\n", interface, ip, netmask); sprintf(command, "ifconfig %s %s %s\n", interface, ip, netmask);
system(command); usb_moded_system(command);
} }
/* TODO: Check first if there is a gateway set */ /* TODO: Check first if there is a gateway set */
if(gateway) if(gateway)
{ {
sprintf(command, "route add default gw %s\n", gateway); sprintf(command, "route add default gw %s\n", gateway);
system(command); usb_moded_system(command);
} }
free(interface); free(interface);
...@@ -1179,7 +1179,7 @@ int usb_network_down(struct mode_list_elem *data) ...@@ -1179,7 +1179,7 @@ int usb_network_down(struct mode_list_elem *data)
return(0); return(0);
sprintf(command, "ifconfig %s down\n", interface); sprintf(command, "ifconfig %s down\n", interface);
system(command); usb_moded_system(command);
/* dhcp client shutdown happens on disconnect automatically */ /* dhcp client shutdown happens on disconnect automatically */
if(data->nat) if(data->nat)
......
...@@ -1130,6 +1130,24 @@ void usb_moded_stop(int exitcode) ...@@ -1130,6 +1130,24 @@ void usb_moded_stop(int exitcode)
g_main_loop_quit(usb_moded_mainloop); g_main_loop_quit(usb_moded_mainloop);
} }
/** Wrapper to give visibility to blocking system() calls usb-moded is making
*/
int
usb_moded_system_(const char *file, int line, const char *func,
const char *command)
{
log_debug("EXEC %s; from %s:%d: %s()",
command, file, line, func);
int rc = system(command);
if( rc != 0 )
log_warning("EXEC %s; exit code is %d", command, rc);
return rc;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int opt = 0, opt_idx = 0; int opt = 0, opt_idx = 0;
...@@ -1220,7 +1238,7 @@ int main(int argc, char* argv[]) ...@@ -1220,7 +1238,7 @@ int main(int argc, char* argv[])
* INITIALIZE * INITIALIZE
* - - - - - - - - - - - - - - - - - - - */ * - - - - - - - - - - - - - - - - - - - */
/* silence system() calls */ /* silence usb_moded_system() calls */
if(log_type != LOG_TO_STDERR || log_level != LOG_DEBUG ) if(log_type != LOG_TO_STDERR || log_level != LOG_DEBUG )
{ {
freopen("/dev/null", "a", stdout); freopen("/dev/null", "a", stdout);
......
...@@ -96,4 +96,7 @@ extern int cable_connection_delay; ...@@ -96,4 +96,7 @@ extern int cable_connection_delay;
void usb_moded_stop(int exitcode); void usb_moded_stop(int exitcode);
int usb_moded_system_(const char *file, int line, const char *func, const char *command);
#define usb_moded_system(command) usb_moded_system_(__FILE__,__LINE__,__FUNCTION__,(command))
#endif /* USB_MODED_H */ #endif /* USB_MODED_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment