static void on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { the_daemon = daemon_new (connection); }
static void on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { Daemon *daemon; GError *local_error = NULL; GError **error = &local_error; if (!ensure_directory (ICONDIR, error)) { goto out; } if (!ensure_directory (USERDIR, error)) { goto out; } daemon = daemon_new (); if (daemon == NULL) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to initialize daemon"); goto out; } openlog ("accounts-daemon", LOG_PID, LOG_DAEMON); syslog (LOG_INFO, "started daemon version %s", VERSION); closelog (); openlog ("accounts-daemon", 0, LOG_AUTHPRIV); out: if (local_error != NULL) { g_printerr ("%s\n", local_error->message); g_clear_error (&local_error); g_main_loop_quit (loop); } }
static void start_server (void) { int client_sd; int daemon_sd; int connected_sd; struct sockaddr_in client_sa; struct sockaddr_in daemon_sa; struct sockaddr_in connected_sa; socklen_t size; fd_set socket_set; int nfds; struct ifreq if_info; struct sockaddr_in *if_addr; char addr[INET_ADDRSTRLEN]; struct client *c; struct daemon *d; struct parsed_cmd *pcmd = NULL; char *ident_msg; int port; char *colon; /* Prepare all the threads */ slow_pool = NULL; fast_pool = NULL; clients_pool = NULL; daemons_pool = NULL; ABORT_IF (!(slow_pool = pool_create (prefs->nb_proc)), "Unable to create slow_pool") ABORT_IF (!(fast_pool = pool_create (prefs->nb_proc)), "Unable to create fast_pool") ABORT_IF (!(clients_pool = pool_create (prefs->max_clients)), "Unable to create clients_pool") ABORT_IF (!(daemons_pool = pool_create (prefs->max_daemons)), "Unable to create daemons_pool") /* Create the shared directory if it does not exist already */ ABORT_IF (create_dir (prefs->shared_folder, (mode_t)0755) < 0, "Unable to create shared directory") /* Initialize global pointers and their semaphores */ clients = NULL; ABORT_IF (sem_init (&clients_lock, 0, 1) < 0, "Unable to sem_init clients_lock") daemons = NULL; ABORT_IF (sem_init (&daemons_lock, 0, 1) < 0, "Unable to sem_init daemons_lock") file_cache = NULL; ABORT_IF (sem_init (&file_cache_lock, 0, 1) < 0, "Unable to sem_init file_cache_lock") list_client = NULL; ABORT_IF (sem_init (&list_lock, 0, 1) < 0, "Unable to sem_init list_lock") downloads = NULL; ABORT_IF (sem_init (&downloads_lock, 0, 1) < 0, "Unable to sem_init download_queue_lock") client_sa.sin_family = AF_INET; client_sa.sin_addr.s_addr = INADDR_ANY; client_sa.sin_port = htons (prefs->client_port); client_sd = socket_init (&client_sa); ABORT_IF (client_sd < 0, "Unable to socket_init client_sd") daemon_sa.sin_family = AF_INET; daemon_sa.sin_addr.s_addr = INADDR_ANY; daemon_sa.sin_port = htons (prefs->daemon_port); daemon_sd = socket_init (&daemon_sa); ABORT_IF (daemon_sd < 0, "Unable to socket_init daemon_sd") #if 1 /* We get our ip */ memcpy (if_info.ifr_name, prefs->interface, strlen (prefs->interface) + 1); if (ioctl (daemon_sd, SIOCGIFADDR, &if_info) == -1) { log_failure (log_file, "Can't get my ip from interface"); log_failure (log_file, "LOL ERRNO : %s\n", strerror (errno)); goto abort; } if_addr = (struct sockaddr_in *)&if_info.ifr_addr; inet_ntop (AF_INET, &if_addr->sin_addr, my_ip, INET_ADDRSTRLEN); log_success (log_file, "Found my IP : %s", my_ip); #endif /* socket_set contains both client_sd and daemon_sd */ FD_ZERO (&socket_set); size = sizeof (connected_sa); nfds = NFDS (client_sd, daemon_sd); for (;;) { /* * It is VERY important to FD_SET at each loop, because select * will FD_UNSET the socket descriptors */ FD_SET (client_sd, &socket_set); FD_SET (daemon_sd, &socket_set); /* Block until a socket is ready to accept */ if (select (nfds, &socket_set, NULL, NULL, NULL) < 0) { log_failure (log_file, "main () : select failed"); } if (FD_ISSET (client_sd, &socket_set)) { if ((connected_sd = (accept (client_sd, (struct sockaddr *) &connected_sa, &size))) < 0) { log_failure (log_file, "Failed to accept incoming connection."); break; } /* Can we handle this client? */ if (client_count () > prefs->max_clients) { socket_sendline (connected_sd, " < Too many clients\n"); goto close_socket; } /* Then, let's handle him */ if (!inet_ntop (AF_INET, &connected_sa.sin_addr, addr, INET_ADDRSTRLEN)) { socket_sendline (connected_sd, " < Oops\n"); goto close_socket; } if (!(c = client_new (connected_sd, addr))) { socket_sendline (connected_sd, " < Sorry pal :(\n"); } pool_queue (clients_pool, handle_client, c); } else if (FD_ISSET (daemon_sd, &socket_set)) { if ((connected_sd = (accept (daemon_sd, (struct sockaddr *) &connected_sa, &size))) < 0) { log_failure (log_file, "Failed to accept incoming connection."); break; } /* Can we handle this daemon? */ if (daemon_count () > prefs->max_daemons) { socket_sendline (connected_sd, " < Too many daemons\n"); goto close_socket; } /* Let's identify him first */ ident_msg = socket_try_getline (connected_sd, IDENTIFICATION_TIMEOUT); if (!ident_msg) { socket_sendline (connected_sd, "error: identification timed out\n"); goto close_socket; } if (cmd_parse_failed ((pcmd = cmd_parse (ident_msg, NULL)))) { pcmd = NULL; goto close_socket; } if (pcmd->argc < 2) goto close_socket; if (strcmp (pcmd->argv[0], "neighbour") != 0) goto close_socket; if (!(colon = strchr (pcmd->argv[1], ':'))) goto close_socket; port = atoi (colon + 1); free (ident_msg); cmd_parse_free (pcmd); pcmd = NULL; if (!inet_ntop (AF_INET, &connected_sa.sin_addr, addr, INET_ADDRSTRLEN)) { socket_sendline (connected_sd, " < Oops\n"); goto close_socket; } /* Now we've got his port, let him go in */ if (!(d = daemon_new (connected_sd, addr, port))) { socket_sendline (connected_sd, " < Sorry pal :(\n"); goto close_socket; } pool_queue (daemons_pool, handle_daemon, d); } else { /* This should never happen : neither client nor daemon!? */ log_failure (log_file, "Unknown connection"); } continue; close_socket: if (pcmd) { cmd_parse_free (pcmd); pcmd = NULL; } close (connected_sd); } abort: if (slow_pool) pool_destroy (slow_pool); if (fast_pool) pool_destroy (fast_pool); if (clients_pool) pool_destroy (clients_pool); if (daemons_pool) pool_destroy (daemons_pool); conf_free (prefs); exit (EXIT_FAILURE); }
int main (int argc, char *argv[]) { Daemon *daemon; DBusGConnection *bus; DBusGProxy *system_bus_proxy; GError *error; gint ret; GOptionContext *context; static gboolean replace; static gboolean show_version; static GOptionEntry entries[] = { { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Output version information and exit"), NULL }, { "replace", 0, 0, G_OPTION_ARG_NONE, &replace, N_("Replace existing instance"), NULL }, { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL }, { NULL } }; ret = 1; error = NULL; setlocale (LC_ALL, ""); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); g_type_init (); if (!g_setenv ("GIO_USE_VFS", "local", TRUE)) { g_warning ("Couldn't set GIO_USE_GVFS"); goto out; } context = g_option_context_new (""); g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); g_option_context_set_summary (context, _("Provides D-Bus interfaces for querying and manipulating\nuser account information.")); g_option_context_add_main_entries (context, entries, NULL); error = NULL; if (!g_option_context_parse (context, &argc, &argv, &error)) { g_warning ("%s", error->message); g_error_free (error); goto out; } g_option_context_free (context); if (show_version) { g_print ("mateaccounts-daemon " VERSION "\n"); ret = 0; goto out; } g_log_set_default_handler (log_handler, NULL); bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); if (bus == NULL) { g_warning ("Could not connect to system bus: %s", error->message); g_error_free (error); goto out; } system_bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (system_bus_proxy == NULL) { g_warning ("Could not construct system_bus_proxy object"); goto out; } if (!acquire_name_on_proxy (system_bus_proxy, replace)) { g_warning ("Could not acquire name"); goto out; } daemon = daemon_new (); if (daemon == NULL) goto out; openlog ("mateaccounts-daemon", LOG_PID, LOG_DAEMON); syslog (LOG_INFO, "started daemon version %s", VERSION); closelog (); openlog ("mateaccounts-daemon", 0, LOG_AUTHPRIV); loop = g_main_loop_new (NULL, FALSE); g_debug ("entering main loop\n"); g_main_loop_run (loop); g_main_loop_unref (loop); ret = 0; out: return ret; }