Пример #1
0
static void *slavenet_main(void *arg){

  struct slave_element_st *slave = (struct slave_element_st*)arg;
  int status = 0;

  pthread_cleanup_push(slavenet_cleanup_info, arg);
  pthread_cleanup_push(slavenet_try_close, arg);

  status = slavenet_init_info(slave);
  if(status != 0)
    set_quit_flag();
  else{
    status = slavenet_open(slave);
    /*
     * If slavenet_open() returns 2, it is a configuration error and we quit
     * the application. Otherwise we assume that, if there is an error,
     * it is a temporary situation and try to reopen the connection in
     * the loop.
     */
    if(status == 2)
      set_quit_flag();
  }

  if(status == 0)
    slave_stats_connect(slave);

  while(get_quit_flag() == 0){
    /*
     * If the slave_fd is -1 it means that the socket was closed
     * due to a reading error. It could be a real error, or the server
     * not sending anything within the timeout limit and the like.
     * reopen() waits some time and tries to reopen it.
     */
    if(slave->slave_fd == -1){
      log_errx("Closed connection to %s. Reconnecting.", slave->mastername);
      status = slavenet_reopen(slave);
      if(slave->slave_fd != -1)
	slave_stats_connect(slave);
    }

    if(slave->slave_fd != -1){
      status = slavenet_loop(slave);
      /*
       * When this function returns 1 it means there was a connection error.
       * When it returns 2 it is a processing error and there is no need
       * to close the connection.
       */
      if(status == 1)
	status = slavenet_close(slave);
    }
  }

  pthread_cleanup_pop(1);
  pthread_cleanup_pop(1);

  return(NULL);
}
Пример #2
0
/* Quit GDB if SIGTERM is received.
   GDB would quit anyway, but this way it will clean up properly.  */
void
handle_sigterm (int sig)
{
  signal (sig, handle_sigterm);

  sync_quit_force_run = 1;
  set_quit_flag ();

  mark_async_signal_handler (async_sigterm_token);
}
Пример #3
0
void
handle_sigint (int sig)
{
  signal (sig, handle_sigint);

  /* We could be running in a loop reading in symfiles or something so
     it may be quite a while before we get back to the event loop.  So
     set quit_flag to 1 here.  Then if QUIT is called before we get to
     the event loop, we will unwind as expected.  */
  set_quit_flag ();

  /* In case nothing calls QUIT before the event loop is reached, the
     event loop handles it.  */
  mark_async_signal_handler (sigint_token);
}
Пример #4
0
/* Quit GDB if SIGTERM is received.
   GDB would quit anyway, but this way it will clean up properly.  */
void
handle_sigterm (int sig)
{
    signal (sig, handle_sigterm);

    /* Call quit_force in a signal safe way.
       quit_force itself is not signal safe.  */
    if (target_can_async_p ())
        mark_async_signal_handler (async_sigterm_token);
    else
    {
        sync_quit_force_run = 1;
        set_quit_flag ();
    }
}
Пример #5
0
/* Tell the event loop what to do if SIGINT is received.
   See event-signal.c.  */
void
handle_sigint (int sig)
{
  signal (sig, handle_sigint);

  /* We could be running in a loop reading in symfiles or something so
     it may be quite a while before we get back to the event loop.  So
     set quit_flag to 1 here.  Then if QUIT is called before we get to
     the event loop, we will unwind as expected.  */

  set_quit_flag ();

  /* If immediate_quit is set, we go ahead and process the SIGINT right
     away, even if we usually would defer this to the event loop.  The
     assumption here is that it is safe to process ^C immediately if
     immediate_quit is set.  If we didn't, SIGINT would be really
     processed only the next time through the event loop.  To get to
     that point, though, the command that we want to interrupt needs to
     finish first, which is unacceptable.  If immediate quit is not set,
     we process SIGINT the next time through the loop, which is fine.  */
  gdb_call_async_signal_handler (sigint_token, immediate_quit);
}
Пример #6
0
static int loop(void){
  /*
   * Read a string from stdin, and then send it ten times to
   * the client queues.
   */
  int status;
  char buf[DBQ_RECLEN];
  int i;

  /*
   * Process new connections, hangups, etc.
   */
  status = poll_loop(gct);
  if(status != 0)
    fprintf(stdout, "Status from poll loop: %d\n", status);

  memset(buf, '\0', DBQ_RECLEN);
  while(fgets(buf, DBQ_RECLEN, stdin) != NULL){
    /*
     * Process new connections, hangups, etc.
     */
    status = poll_loop_nowait_nonblock(gct);
    if(status != 0)
      fprintf(stdout, "Status from poll loop: %d\n", status);

    if(strncmp(buf, "quit", 4) == 0){
      set_quit_flag();
      break;
    }
    for(i = 1; i <= 4; ++i){
      write_client_queues(buf, DBQ_RECLEN);
    }

    join_finished_threads();
    spawn_client_threads();
  }

  return(0);
}
Пример #7
0
int main(int argc, char **argv){

  int status = 0;

  init_globals();
  atexit(cleanup);

  /*
   * PROBLEM
   * We used to call init_signals() only after init_daemon(). But in
   * that case, when started with -F or -D -D, the signals are
   * not caught in Linunx and OSX (they are caught in FreeBSD). nbspd and
   * npemwind die, but leave the pid file and the web server.
   * [It seems that the signals are not blocked in the main thread as
   * the code in signal.c should ensure.]
   * Adding this call here
   *
   * status = init_signals();
   *
   * makes OSX and Linux respond well when the daemon is run in the foreground.
   * If the call is made after the tcl configure(), the problem repeats;
   * it has to be before the configure() function.
   *
   * The problem is that in FreeBSD-7.1, when init_signals() is called here,
   * then no threads are spawned afterwards.
   *
   * The solution was to split init_signals() in two parts, one that
   * block the signals and the other spawns the thread. I don't fully
   * understand what in tcl is causing this (Fri Mar 13 11:43:09 AST 2009).
   */
  status = init_signals_block();

  if(status == 0){
    /*
     * This will configure it with the default configuration
     * file, if it exists.
     *
     * This note and Tcl... code line is taken from the nbsp source code.
     * [First initialize the tcl library once and for all. It was not
     * necessary to call this in unix, but cygwin needs it or EvalFile
     * seg faults.]
     */
    Tcl_FindExecutable(argv[0]);
    status = configure();
  }

  if(status == 0)
    status = parse_args(argc, argv);

  if(status == 0){
    if(g.configfile != NULL){
      /*
       * This will reconfigure it with the user-supplied config file
       */
      status = configure();
    }
  }

  /*
   * if [-C] was given, print the configuration and exit.
   */
  if(status == 0){
    if(g.option_C == 1){
      print_confoptions();
      return(0);
    }
  }

  if(status == 0)
    status = validate_configuration();

  /*
   * user and group are configurable so this must be done after reading
   * configuration options.
   */
  if(status == 0)
    status = drop_privs();

  if(status == 0)
    status = init_server_list();

  if(status == 0)
    status = init_directories();

  /*
   * The last configuration step, just before becoming a daemon.
   */
  if(status == 0)
    status = exec_startscript();

  if((status == 0) && (g.f_ndaemon == 0))
    status = init_daemon();

  set_log_debug(g.f_debug);
  set_log_verbose(g.f_verbose);

  if(status == 0)
    status = init_signals_thread();

  /*
   * This has to be done after daemon() so that the lock file contains the
   * daemon's pid, not the starting program's.
   */
  if(status == 0)
    status = init_lock();

  /*
   * There are no shared queues in npemwin, otherwise the initialization
   * would go here.
   *
   *  if(status == 0)
   *	status = init_queues();
   */

  if(status == 0){
    if(g.serverprotocol != PROTOCOL_NONE) {
      g.f_server_enabled = 1;
      status = init_server();
    }
  }

  if(status == 0){
    if(g.httpd_enable > 0){
      status = spawn_httpd_server();
    }
  }

  if(status == 0){
    if(g.bbserver_enable > 0){
      status = spawn_bbregistrar();
    }
  }

  if(status == 0)
    status = init_emwin_qfiles();

  if(status == 0)
    init_periodic();

  /*
   * If there are initialization errors, ask all threads to quit.
   */
  if(status != 0)
    set_quit_flag();

  while(get_quit_flag() == 0){
      status = loop();
  }

  if(status != 0)
    status = EXIT_FAILURE;

  return(status);
}