/* NetworkManager -- Network link manager * * Dan Williams * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * (C) Copyright 2004 Red Hat, Inc. */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include "NetworkManager.h" #include "nm-utils.h" #include "NetworkManagerUtils.h" #include "nm-device-interface.h" #include "nm-manager.h" #include "nm-hal-manager.h" #include "nm-device.h" #include "nm-device-802-3-ethernet.h" #include "nm-device-802-11-wireless.h" #include "NetworkManagerPolicy.h" #include "NetworkManagerDbus.h" #include "NetworkManagerAP.h" #include "NetworkManagerAPList.h" #include "NetworkManagerSystem.h" #include "nm-named-manager.h" #include "nm-dbus-vpn.h" #include "nm-dbus-manager.h" #include "nm-supplicant-manager.h" #include "nm-netlink-monitor.h" #include "nm-logging.h" #define NM_DEFAULT_PID_FILE LOCALSTATEDIR"/run/NetworkManager.pid" /* * Globals */ static NMManager *manager = NULL; static GMainLoop *main_loop = NULL; static NMData *nm_data = NULL; static void nm_data_free (NMData *data); static void nm_error_monitoring_device_link_state (NMNetlinkMonitor *monitor, GError *error, gpointer user_data) { /* FIXME: Try to handle the error instead of just printing it. */ nm_warning ("error monitoring wired ethernet link state: %s\n", error->message); } static gboolean nm_monitor_setup (void) { GError *error = NULL; NMNetlinkMonitor *monitor; monitor = nm_netlink_monitor_get (); nm_netlink_monitor_open_connection (monitor, &error); if (error != NULL) { nm_warning ("could not monitor wired ethernet devices: %s", error->message); g_error_free (error); g_object_unref (monitor); return FALSE; } g_signal_connect (G_OBJECT (monitor), "error", G_CALLBACK (nm_error_monitoring_device_link_state), NULL); nm_netlink_monitor_attach (monitor, NULL); /* Request initial status of cards */ nm_netlink_monitor_request_status (monitor, NULL); return TRUE; } /* * nm_data_new * * Create data structure used in callbacks from libhal. * */ static NMData *nm_data_new (void) { NMData * data; data = g_slice_new0 (NMData); return data; } /* * nm_data_free * * Free data structure used in callbacks. * */ static void nm_data_free (NMData *data) { g_return_if_fail (data != NULL); g_slice_free (NMData, data); } static void nm_name_owner_changed_handler (NMDBusManager *mgr, const char *name, const char *old, const char *new, gpointer user_data) { NMData * data = (NMData *) user_data; gboolean old_owner_good = (old && (strlen (old) > 0)); gboolean new_owner_good = (new && (strlen (new) > 0)); if (strcmp (name, NMI_DBUS_SERVICE) == 0) { if (!old_owner_good && new_owner_good) { /* NMI appeared, update stuff */ // FIXME: grab NMConnections from the info-daemon } } } static void nm_signal_handler (int signo) { static int in_fatal = 0; /* avoid loops */ if (in_fatal > 0) return; ++in_fatal; switch (signo) { case SIGSEGV: case SIGBUS: case SIGILL: case SIGABRT: nm_warning ("Caught signal %d. Generating backtrace...", signo); nm_logging_backtrace (); exit (1); break; case SIGFPE: case SIGPIPE: /* let the fatal signals interrupt us */ --in_fatal; nm_warning ("Caught signal %d, shutting down abnormally. Generating backtrace...", signo); nm_logging_backtrace (); g_main_loop_quit (main_loop); break; case SIGINT: case SIGTERM: /* let the fatal signals interrupt us */ --in_fatal; nm_warning ("Caught signal %d, shutting down normally.", signo); g_main_loop_quit (main_loop); break; case SIGHUP: --in_fatal; /* FIXME: * Reread config stuff like system config files, VPN service files, etc */ break; case SIGUSR1: --in_fatal; /* FIXME: * Play with log levels or something */ break; default: signal (signo, nm_signal_handler); break; } } static void setup_signals (void) { struct sigaction action; sigset_t mask; sigemptyset (&mask); action.sa_handler = nm_signal_handler; action.sa_mask = mask; action.sa_flags = 0; sigaction (SIGTERM, &action, NULL); sigaction (SIGINT, &action, NULL); sigaction (SIGILL, &action, NULL); sigaction (SIGBUS, &action, NULL); sigaction (SIGFPE, &action, NULL); sigaction (SIGHUP, &action, NULL); sigaction (SIGSEGV, &action, NULL); sigaction (SIGABRT, &action, NULL); sigaction (SIGUSR1, &action, NULL); } static void write_pidfile (const char *pidfile) { char pid[16]; int fd; if ((fd = open (pidfile, O_CREAT|O_WRONLY|O_TRUNC, 00644)) < 0) { nm_warning ("Opening %s failed: %s", pidfile, strerror (errno)); return; } snprintf (pid, sizeof (pid), "%d", getpid ()); if (write (fd, pid, strlen (pid)) < 0) nm_warning ("Writing to %s failed: %s", pidfile, strerror (errno)); if (close (fd)) nm_warning ("Closing %s failed: %s", pidfile, strerror (errno)); } /* * nm_print_usage * * Prints program usage. * */ static void nm_print_usage (void) { fprintf (stderr, "\n" "NetworkManager monitors all network connections and automatically\n" "chooses the best connection to use. It also allows the user to\n" "specify wireless access points which wireless cards in the computer\n" "should associate with.\n" "\n"); } /* * main * */ int main (int argc, char *argv[]) { GOptionContext *opt_ctx = NULL; gboolean become_daemon = FALSE; gboolean show_usage = FALSE; char * pidfile = NULL; char * user_pidfile = NULL; NMPolicy *policy; NMHalManager *hal_manager = NULL; NMVPNManager *vpn_manager = NULL; NMNamedManager *named_mgr = NULL; NMDBusManager * dbus_mgr; DBusConnection *dbus_connection; NMSupplicantManager * sup_mgr = NULL; int exit_status = EXIT_FAILURE; GOptionEntry options[] = { {"no-daemon", 0, 0, G_OPTION_ARG_NONE, &become_daemon, "Don't become a daemon", NULL}, {"pid-file", 0, 0, G_OPTION_ARG_FILENAME, &user_pidfile, "Specify the location of a PID file", "filename"}, {"info", 0, 0, G_OPTION_ARG_NONE, &show_usage, "Show application information", NULL}, {NULL} }; if (getuid () != 0) { g_printerr ("You must be root to run NetworkManager!\n"); goto exit; } bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); /* Parse options */ opt_ctx = g_option_context_new(""); g_option_context_add_main_entries(opt_ctx, options, NULL); g_option_context_parse(opt_ctx, &argc, &argv, NULL); g_option_context_free(opt_ctx); if (show_usage == TRUE) { nm_print_usage(); exit_status = EXIT_SUCCESS; goto exit; } pidfile = g_strdup (user_pidfile ? user_pidfile : NM_DEFAULT_PID_FILE); /* Tricky: become_daemon is FALSE by default, so unless it's TRUE because * of a CLI option, it'll become TRUE after this */ become_daemon = !become_daemon; if (become_daemon) { if (daemon (0, 0) < 0) { int saved_errno; saved_errno = errno; nm_error ("Could not daemonize: %s [error %u]", g_strerror (saved_errno), saved_errno); goto exit; } write_pidfile (pidfile); } /* * Set the umask to 0022, which results in 0666 & ~0022 = 0644. * Otherwise, if root (or an su'ing user) has a wacky umask, we could * write out an unreadable resolv.conf. */ umask (022); g_type_init (); if (!g_thread_supported ()) g_thread_init (NULL); dbus_g_thread_init (); setup_signals (); nm_logging_setup (become_daemon); nm_info ("starting..."); nm_system_init (); main_loop = g_main_loop_new (NULL, FALSE); /* Initialize our instance data */ nm_data = nm_data_new (); if (!nm_data) { nm_error ("Failed to initialize."); goto pidfile; } /* Create watch functions that monitor cards for link status. */ if (!nm_monitor_setup ()) goto done; /* Initialize our DBus service & connection */ dbus_mgr = nm_dbus_manager_get (); dbus_connection = nm_dbus_manager_get_dbus_connection (dbus_mgr); if (!dbus_connection) { nm_error ("Failed to initialize. " "Either dbus is not running, or the " "NetworkManager dbus security policy " "was not loaded."); goto done; } g_signal_connect (dbus_mgr, "name-owner-changed", G_CALLBACK (nm_name_owner_changed_handler), nm_data); manager = nm_manager_new (); g_object_set_data (G_OBJECT (manager), "NM_DATA_HACK", nm_data); policy = nm_policy_new (manager); nm_dbus_manager_register_signal_handler (dbus_mgr, NMI_DBUS_INTERFACE, NULL, nm_dbus_nmi_signal_handler, manager); /* Initialize the supplicant manager */ sup_mgr = nm_supplicant_manager_get (); if (!sup_mgr) { nm_error ("Failed to initialize the supplicant manager."); goto done; } vpn_manager = nm_vpn_manager_new (manager, nm_data); if (!vpn_manager) { nm_warning ("Failed to start the VPN manager."); goto done; } named_mgr = nm_named_manager_get (); if (!named_mgr) { nm_warning ("Failed to start the named manager."); goto done; } /* Start our DBus service */ if (!nm_dbus_manager_start_service (dbus_mgr)) { nm_warning ("Failed to start the named manager."); goto done; } hal_manager = nm_hal_manager_new (manager, nm_data); if (!hal_manager) goto done; /* If NMI is running, grab allowed wireless network lists from it ASAP */ #if 0 // FIXME: grab NMConnections instead if (nm_dbus_manager_name_has_owner (dbus_mgr, NMI_DBUS_SERVICE)) nm_policy_schedule_allowed_ap_list_update (nm_data); #endif /* Bring up the loopback interface. */ nm_system_enable_loopback (); /* Run the main loop */ exit_status = EXIT_SUCCESS; g_main_loop_run (main_loop); done: nm_print_open_socks (); if (vpn_manager) nm_vpn_manager_dispose (vpn_manager); nm_hal_manager_destroy (hal_manager); nm_policy_destroy (policy); if (manager) g_object_unref (manager); nm_data_free (nm_data); if (sup_mgr) g_object_unref (sup_mgr); /* nm_data_free needs the dbus connection, so must kill the * dbus manager after that. */ g_object_unref (dbus_mgr); nm_logging_shutdown (); pidfile: if (pidfile) unlink (pidfile); g_free (pidfile); exit: exit (exit_status); }