static void
clean_dir (const char *dir)
{
  SocketEntry **entries;
  int n_entries;

  read_sockets (dir, &entries, &n_entries);

  /* open_socket() will fail conclusively after
   * several retries, so this loop is guaranteed
   * to terminate eventually
   */
  while (!handle_sockets (entries, n_entries))
    {
      fprintf (stderr, "Unable to determine state of some sockets, retrying in 2 seconds\n");
      sleep (2);
    }

  unhandled_count += (n_entries - alive_count - cleaned_count);
}
Example #2
0
int main(int argc, char **argv)
{

	int status;
	struct sigaction usr_action;
	struct sigaction term_action;
	sigset_t block_mask;

	int socks_status;

	connections.last_slot = -1;
	connections.running_pid = getpid();

	status = init(argc, argv, &connections);

	if (status == INIT_FAILURE)
	{
		if (to_printhelp == 1)
			print_help(argc, argv);
		exit(EXIT_FAILURE);
	}

	if (status == INIT_SUCCESS && to_printhelp == 1)
	{
		print_help(argc, argv);
		exit(EXIT_SUCCESS);
	}

	if (to_command_mode)
	{
		status = execute_command(&connections);
		exit(status);
	}

	Log(LOG_NOTICE, "Program started by User %d \n", getuid());
	/* Daemon-specific initialization */

	if (to_demonize)
	{

		status = daemonize(&connections);
		switch (status)
		{
			case DAEMONIZE_FAILURE:
				/*something gone wrong exit from parent */
				printf("Failed to daemonize.Check permissions\n");
				exit(EXIT_FAILURE);
				break;
			case DAEMONIZE_SUCCESS:
				/*ok we are the parent, we can exit gracefully */
				Log(LOG_INFO, "Starting as a daemon.Parent exit\n");
				printf("Starting as a daemon.Parent exit\n");
				exit(EXIT_SUCCESS);
				break;
		}

	}

	/* Establish the signal handler.  */
	sigfillset(&block_mask);
	usr_action.sa_handler = sleep_signal;
	usr_action.sa_mask = block_mask;
	usr_action.sa_flags = 0;
	sigaction(SIGUSR1, &usr_action, NULL);
	term_action.sa_handler = term_signal;
	term_action.sa_mask = block_mask;
	term_action.sa_flags = 0;
	sigaction(SIGTERM, &term_action, NULL);

	Log(LOG_INFO, "Init networking\n");

	status = initialize_network(&connections);

	Log(LOG_INFO, "Init timers\n");
	status = initialize_timers(&connections);

	/* The Big Loop */
	while (1)
	{
		Log(LOG_DEBUG, "Main loop\n");
		rebuild_connection_set(&connections);
		socks_status = select(connections.max_socket + 1, &connections.socket_set, NULL, NULL, NULL);
		if (socks_status < 0)
		{
			status = errno;
			/* We had an error, signal it */
			if (status != EINTR)
			{
				Log(LOG_ERR, "Error in select socket (%s) \n", strerror(status));
				exit(EXIT_FAILURE);
			}
		}
		else
			if (socks_status == 0)
			{
				/* Nothing to do probably a staying alive msg in debug mode */
			}
			else
			{
				read_sockets(&connections);
			}
	}
	closelog();
	exit(EXIT_SUCCESS);
}