/* Run client and the server if needed. */ static void start_moc (const struct parameters *params, lists_t_strs *args) { int server_sock; if (params->foreground) { set_me_server (); server_init (params->debug, params->foreground); server_loop (); return; } server_sock = server_connect (); if (server_sock != -1 && params->only_server) fatal ("Server is already running!"); if (server_sock == -1) { int i = 0; int notify_pipe[2]; ssize_t rc; printf ("Running the server...\n"); /* To notify the client that the server socket is ready */ if (pipe(notify_pipe)) fatal ("pipe() failed: %s", xstrerror (errno)); switch (fork()) { case 0: /* child - start server */ set_me_server (); server_init (params->debug, params->foreground); rc = write (notify_pipe[1], &i, sizeof(i)); if (rc < 0) fatal ("write() to notify pipe failed: %s", xstrerror (errno)); close (notify_pipe[0]); close (notify_pipe[1]); server_loop (); options_free (); decoder_cleanup (); io_cleanup (); files_cleanup (); rcc_cleanup (); common_cleanup (); exit (EXIT_SUCCESS); case -1: fatal ("fork() failed: %s", xstrerror (errno)); default: close (notify_pipe[1]); if (read(notify_pipe[0], &i, sizeof(i)) != sizeof(i)) fatal ("Server exited!"); close (notify_pipe[0]); server_sock = server_connect (); if (server_sock == -1) { perror ("server_connect()"); fatal ("Can't connect to the server!"); } } } if (params->only_server) send_int (server_sock, CMD_DISCONNECT); else { xsignal (SIGPIPE, SIG_IGN); if (!ping_server (server_sock)) fatal ("Can't connect to the server!"); init_interface (server_sock, params->debug, args); interface_loop (); interface_end (); } close (server_sock); }
int main (int argc, char *argv[]) { struct parameters params; lists_t_strs *deferred_overrides; lists_t_strs *args; #ifdef HAVE_UNAME_SYSCALL int rc; struct utsname uts; #endif #ifdef PACKAGE_REVISION logit ("This is Music On Console (revision %s)", PACKAGE_REVISION); #else logit ("This is Music On Console (version %s)", PACKAGE_VERSION); #endif #ifdef CONFIGURATION logit ("Configured:%s", CONFIGURATION); #endif #ifdef HAVE_UNAME_SYSCALL rc = uname (&uts); if (rc == 0) logit ("Running on: %s %s %s", uts.sysname, uts.release, uts.machine); #endif log_command_line (argc, argv); files_init (); if (get_home () == NULL) fatal ("Could not determine user's home directory!"); memset (¶ms, 0, sizeof(params)); options_init (); deferred_overrides = lists_strs_new (4); /* set locale according to the environment variables */ if (!setlocale(LC_ALL, "")) logit ("Could not set locale!"); args = process_command_line (argc, argv, ¶ms, deferred_overrides); if (params.dont_run_iface && params.only_server) fatal ("-c, -a and -p options can't be used with --server!"); if (!params.config_file) params.config_file = xstrdup (create_file_name ("config")); options_parse (params.config_file); if (params.config_file) free (params.config_file); params.config_file = NULL; process_deferred_overrides (deferred_overrides); lists_strs_free (deferred_overrides); deferred_overrides = NULL; check_moc_dir (); io_init (); rcc_init (); decoder_init (params.debug); srand (time(NULL)); if (!params.only_server && params.dont_run_iface) server_command (¶ms, args); else start_moc (¶ms, args); lists_strs_free (args); options_free (); decoder_cleanup (); io_cleanup (); rcc_cleanup (); files_cleanup (); compat_cleanup (); exit (EXIT_SUCCESS); }