dhcp-helper: refactor error handling
Don't exit(1) from fatal_error() because that skips destroying local variables in main(). Just return regularly.
This commit is contained in:
@@ -74,26 +74,26 @@ build_signal_parameters (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
fatal_error (void)
|
kill_pid (void)
|
||||||
{
|
{
|
||||||
const char *pid_str = getenv ("pid");
|
const char *pid_str;
|
||||||
int pid = 0;
|
pid_t pid = 0;
|
||||||
|
|
||||||
|
pid_str = getenv ("pid");
|
||||||
if (pid_str)
|
if (pid_str)
|
||||||
pid = strtol (pid_str, NULL, 10);
|
pid = strtol (pid_str, NULL, 10);
|
||||||
if (pid) {
|
if (pid) {
|
||||||
g_printerr ("Fatal error occured, killing dhclient instance with pid %d.\n", pid);
|
g_printerr ("Fatal error occured, killing dhclient instance with pid %d.\n", pid);
|
||||||
kill (pid, SIGTERM);
|
kill (pid, SIGTERM);
|
||||||
}
|
}
|
||||||
|
|
||||||
exit (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
GDBusConnection *connection;
|
gs_unref_object GDBusConnection *connection = NULL;
|
||||||
GError *error = NULL;
|
gs_free_error GError *error = NULL;
|
||||||
|
gboolean success = FALSE;
|
||||||
|
|
||||||
nm_g_type_init ();
|
nm_g_type_init ();
|
||||||
|
|
||||||
@@ -104,8 +104,7 @@ main (int argc, char *argv[])
|
|||||||
g_dbus_error_strip_remote_error (error);
|
g_dbus_error_strip_remote_error (error);
|
||||||
g_printerr ("Error: could not connect to NetworkManager D-Bus socket: %s\n",
|
g_printerr ("Error: could not connect to NetworkManager D-Bus socket: %s\n",
|
||||||
error->message);
|
error->message);
|
||||||
g_error_free (error);
|
goto out;
|
||||||
fatal_error ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!g_dbus_connection_emit_signal (connection,
|
if (!g_dbus_connection_emit_signal (connection,
|
||||||
@@ -117,18 +116,19 @@ main (int argc, char *argv[])
|
|||||||
&error)) {
|
&error)) {
|
||||||
g_dbus_error_strip_remote_error (error);
|
g_dbus_error_strip_remote_error (error);
|
||||||
g_printerr ("Error: Could not send DHCP Event signal: %s\n", error->message);
|
g_printerr ("Error: Could not send DHCP Event signal: %s\n", error->message);
|
||||||
g_error_free (error);
|
goto out;
|
||||||
fatal_error ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!g_dbus_connection_flush_sync (connection, NULL, &error)) {
|
if (!g_dbus_connection_flush_sync (connection, NULL, &error)) {
|
||||||
g_dbus_error_strip_remote_error (error);
|
g_dbus_error_strip_remote_error (error);
|
||||||
g_printerr ("Error: Could not flush D-Bus connection: %s\n", error->message);
|
g_printerr ("Error: Could not flush D-Bus connection: %s\n", error->message);
|
||||||
g_error_free (error);
|
goto out;
|
||||||
fatal_error ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_object_unref (connection);
|
success = TRUE;
|
||||||
return 0;
|
out:
|
||||||
|
if (!success)
|
||||||
|
kill_pid ();
|
||||||
|
return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user