Beispiel #1
0
static void
handler (int sig, siginfo_t *si, void *unused)
{
  syslog (LOG_NOTICE, "Terminate server with signal: %d", sig);

  quit_server (0, 0);
}
Beispiel #2
0
int main(int argc, char *argv[]) {

	Irc freenode;
	struct pollfd pfd[4];
	int i, ready, murm_listenfd = -1;

	initialize(argc, argv);

	for (i = 0; i < SIZE(pfd); i++) {
		pfd[i].fd = -1;
		pfd[i].events = POLLIN;
	}
	if (add_murmur_callbacks(cfg.murmur_port))
		pfd[MURM_LISTEN].fd = murm_listenfd = sock_listen(LOCALHOST, CB_LISTEN_PORT_S);
	else
		fprintf(stderr, "Could not connect to Murmur\n");

	if ((pfd[MPD].fd = mpdfd = mpd_connect(cfg.mpd_port)) < 0)
		fprintf(stderr, "Could not connect to MPD\n");

	// Connect to server and set IRC details
	if (!(freenode = irc_connect(cfg.server, cfg.port)))
		exit_msg("Irc connection failed");

	pfd[IRC].fd = get_socket(freenode);
	set_nick(freenode, cfg.nick);
	set_user(freenode, cfg.user);
	for (i = 0; i < cfg.channels_set; i++)
		join_channel(freenode, cfg.channels[i]);

	while ((ready = poll(pfd, SIZE(pfd), TIMEOUT)) > 0) {
		// Keep reading & parsing lines as long the connection is active and act on any registered actions found
		if (pfd[IRC].revents & POLLIN)
			while (parse_irc_line(freenode) > 0);

		if (pfd[MURM_LISTEN].revents & POLLIN)
			if ((pfd[MURM_ACCEPT].fd = accept_murmur_connection(murm_listenfd)) > 0)
				pfd[MURM_LISTEN].fd = -1; // Stop listening for connections

		if (pfd[MURM_ACCEPT].revents & POLLIN) {
			if (!listen_murmur_callbacks(freenode, pfd[MURM_ACCEPT].fd)) {
				pfd[MURM_ACCEPT].fd = -1;
				pfd[MURM_LISTEN].fd = murm_listenfd; // Start listening again for Murmur connections
			}
		}
		if (pfd[MPD].revents & POLLIN)
			if (!print_song(freenode, default_channel(freenode)))
				pfd[MPD].fd = mpdfd = mpd_connect(cfg.mpd_port);
	}
	// If we reach here, it means we got disconnected from server. Exit with error (1)
	if (ready == -1)
		perror("poll");
	else
		fprintf(stderr, "%d minutes passed without getting a message, exiting...\n", TIMEOUT / 1000 / 60);

	quit_server(freenode, cfg.quit_msg);
	cleanup();
	return 1;
}
int main(int argc, char *argv[]) {
    int i;

    init();                                 /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */

    check_par(argc, argv);                  /* Check command arguments number */
    open_config(argv);                      /* Open config file and check if it failed */
    open_log(argv);                         /* Open log file and check if it failed */

    get_ipaddr();                           /* Get server IP address */

    create_socket();                        /* Create a socket */
    bind_socket();                          /* Bind the socket */
    listen_socket();                        /* Listen at the socket */

    print_server_info();                    /* Print server information */

    while (TRUE) {                          /* Read until the end of file */
        if (read_flag) {
            if (fscanf(fcfg, "%s", enc_txt) == EOF) {
                finish_flag = 1;
                break;
            } else {
                fscanf(fcfg, "%s", dec_txt);
            }
        }
        read_flag = 0;

        init_select();                      /* Select function */
        if (select_func() == -1) break;

        for (i = 0; i < max_fds + 1; i++) {
            if (FD_ISSET(i, &rfds)) {
                if (i == sockfd) {                              /* If have a new client connect */
                    if (accept_new_cli() == -1) break;          /* Try to accept new client */
                    if (check_connect() == -1) break;           /* Check connect message from client */
                    if (print_client_info() == -1) break;       /* Print the information of client side */
                    store_client_ip();                          /* Store the client ip address */
                    break;
                } else {                                        /* If have new message from client side */
                    client_ip = get_host_by_sockfd(i);          /* Get the client ip address by socket */
                    recv_socket_msg(i, recv_mark);              /* Get the message from socket */
                    handle_client_msg(i);                       /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */
                    break;
                }
            }
            if (main_flag == EXIT_FAILURE) break;
        }
        if (main_flag == EXIT_FAILURE) break;
    }

    remained_cli = ask_clients_quit();                          /* Ask clients quit and count the remain clients number */
    wait_clients_quit();                                        /* Wait for all clients quit */
    quit_server();                                              /* Clean up and quit server, also print the message to log */

    return main_flag;
}
Beispiel #4
0
int
main (int argc, char **argv)
{
  int i = 0, ret = 0;
  int maxfd = -1;
  char buf[256];
  fd_set orig, fds;
  struct server server_info;
  struct protocol_module *(*load_func) (void) = NULL;
  lt_dlhandle module = NULL;

  memset (buf, 0, sizeof (buf));
  FD_ZERO (&orig);
  FD_ZERO (&fds);

  if (argc != 3)
    {
      fprintf (stderr, "Bad arguments: use madoka-server <ipaddr> <port>\n");
      exit (EXIT_FAILURE);
    }

  server_info.port_num = strtol (argv[2], NULL, 10);

  if (errno)
    {
      perror ("Bad port value");
      exit (EXIT_FAILURE);
    }

  /* Prepare server */
  openlog (MADOKA_LOG_NAME, LOG_PID, LOG_DAEMON);
  skeleton_daemon ();
  syslog (LOG_NOTICE, "Start madoka-server with ip and port: %s:%d", argv[1],
	  server_info.port_num);

#if defined (HAVE_GNUTLS)
  /* Enable SSL */
  gnutls_global_init ();
#endif

  /* Check module list and alloc memory for this. */
  modules = malloc (sizeof (*modules) * modules_list_num);
  memset (modules, 0, sizeof (*modules) * modules_list_num);

  if (!modules)
    {
      syslog (LOG_ERR, "Error in malloc(): %s", strerror (errno));
      exit (EXIT_FAILURE);
    }

  /* Prepare all modules to load */

  lt_dlinit ();

  for (i = 0; i < modules_list_num; i++)
    {

      /* Put module filename */
      strcpy (buf, MADOKA_MODULE_DIR);
      strcat (buf, "/");
      strcat (buf, modules_list_name[i]);
      strcat (buf, LT_MODULE_EXT);

      module = lt_dlopen (buf);

      if (!module)
	{
	  syslog (LOG_WARNING,
		  "Error while loading module in lt_dlopen(): %s\n",
		  lt_dlerror ());
	  continue;		/* Check next module */
	}

      load_func = lt_dlsym (module, "module_init");

      if (!load_func)
	{
	  syslog (LOG_WARNING,
		  "Error while loading module function in lt_dlsym(): %s\n",
		  lt_dlerror ());
	  continue;		/* Check next module */
	}

      modules[i] = load_func ();

      if (!modules[i])
	{
	  syslog (LOG_WARNING,
		  "Error while executing module preinit function");
	  continue;		/* Check next module */
	}

      ret = modules[i]->init (&server_info);

      if (ret < 0)
	{
	  syslog (LOG_WARNING, "Error while executing module init function");
	  modules[i]->is_loaded = 0;
	  continue;		/* Invalid module and check next */
	}

      FD_SET (ret, &orig);
      modules[i]->fd = ret;

      if (maxfd < ret)
	maxfd = ret;

      modules[i]->is_loaded = 1;
      syslog (LOG_NOTICE, "Module %s loaded successfully",
	      modules_list_name[i]);

      /* Set run_server if some module is loaded successfully */
      run_server = 1;
    }

  /* If we can't run server, exit it with code 1 */
  if (!run_server)
    quit_server (0, 1);

  /* Prepare monitor socket */
  while (run_server)
    {
      fds = orig;

      ret = select (maxfd + 1, &fds, NULL, NULL, NULL);

      for (i = 0; (i < modules_list_num && ret > 0); i++)
	if (FD_ISSET (modules[i]->fd, &fds))
	  {
	    modules[i]->worker (modules[i]->fd);
	    ret--;
	  }
    }

  quit_server (0, 0);

  return 0;
}