File modules/sv/server.c

  $Revision: 1.58 $

Example code: A server for a client to connect to.
Status: NOT REVUED, NOT TESTED
Authors: Chris Ottrey, Joao Damas, heavy rewrite by Andrei Robachevsky, Marek Bukowy
Online References:

Included Files


Preprocessor definitions

String sizes

#define STR_S 63

#define STR_M 255

#define STR_L 1023

#define STR_XL 4095

#define STR_XXL 16383

#define MAX_SOURCES 100


Typedef svr_counter_t

typedef struct {...} svr_counter_t
struct 
   { 
     int count; 
     pthread_mutex_t lock; Mutex lock.Used for synchronizing changes.
     pthread_cond_t cond; condition variable
   } 

Typedef svr_args

typedef struct {...} svr_args
struct 
   { 
     void (*function)(int); 
     int conn_sock; 
     int accept_sock; 
     int limit; 
     svr_counter_t* counter; 
     char* name; 
   } 

Global Variable Lock

pthread_mutex_t Lock
Included from include/ca_defs.h
Visible in:  modules/ac/access_control.c
   modules/ca/ca_configFns.c
   modules/ca/ca_initFn.c
   modules/ca/ca_sanityCheck.c
   modules/ca/ca_sourceLoader.c
   modules/ca/ca_values.c
   modules/er/er_macro.c
   modules/pc/pc_commands.c
   modules/pm/pm_serials.c
   modules/pm/protocol_mirror.c
   modules/pw/protocol_whois.c
   modules/qc/query_command.c
   modules/qi/query_instructions.c
   modules/rp/rp_convert.c
   modules/rp/rp_load.c
   modules/rp/rp_search.c
   modules/rp/rp_tree.c
   modules/rp/rp_update.c
   modules/sv/server.c
Used in: ca_get_adminIntElement()modules/ca/ca_configFns.c
  ca_get_adminStrElement()modules/ca/ca_configFns.c
  ca_get_boolean()modules/ca/ca_configFns.c
  ca_get_dirlist()modules/ca/ca_configFns.c
  ca_get_int()modules/ca/ca_configFns.c
  ca_get_string()modules/ca/ca_configFns.c
  ca_set_boolean()modules/ca/ca_configFns.c
  ca_set_string()modules/ca/ca_configFns.c
  ca_srchandle2Intelement()modules/ca/ca_configFns.c
  ca_srchandle2Strelement()modules/ca/ca_configFns.c

Global Variable SV_whois_sock

int SV_whois_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_config_sock

int SV_config_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_mirror_sock

int SV_mirror_sock
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_update_sock

int SV_update_sock[100]
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Variable SV_starttime

Server starting time

time_t SV_starttime
Visible in:  modules/sv/server.c
  show_uptime()modules/pc/pc_commands.c
Used in: SV_start()modules/sv/server.c
  show_uptime()modules/pc/pc_commands.c


Global Variable SV_pidfile

char* SV_pidfile
Visible in:  modules/sv/server.c
Used in: SV_shutdown()modules/sv/server.c
  SV_start()modules/sv/server.c

Global Function SV_do_child()

  Handle whois/config/mirror connections. Takes a pointer to the
  service description structure, containing a connected socket, limit
  of active threads, pointer to the counter of them. Does not stop to
  obey the limits, assumes this to be checked and assumes that it is
  already counted.  Decrements the counter on exit.

Precondition: the counter must be incremented before this function is called.
Author: marek
void* SV_do_child ( void* varg )
void* SV_do_child
Actually, does not return anything useful. Just NULL.
void* varg
service description structure.
Calls: ER_dbg_va()modules/er/er.c
  counter_add()modules/sv/server.c
  counter_state()modules/sv/server.c
  TA_add(), TA_delete(), close(), free(), pthread_self()
Used in: main_loop()modules/sv/server.c

Global Function SV_shutdown()

  Shutdown the server.

More:
  Authors:
        andrei
Stops the server.
  1. Close listening sockets (whois, config, mirror and updates)
  2. Stop all threads by triggering do_server variable.
.properties
void SV_shutdown ( void )
Prototyped in: modules/sv/server.h
Calls: CO_set_const()modules/co/constants.c
  ER_inf_va()modules/er/er.c
  ER_perror()modules/er/er.c
  close(), ctime_r(), fprintf(), gettimeofday(), sleep(), sprintf(), strcpy(), unlink()
Called by: SV_signal_thread()modules/sv/server.c
References Variables: SV_config_sockmodules/sv/server.c
  SV_mirror_sockmodules/sv/server.c
  SV_pidfilemodules/sv/server.c
  SV_update_sockmodules/sv/server.c
  SV_whois_sockmodules/sv/server.c

Global Function SV_signal_thread()

  Handle signals.

Changes the flags: do_nrtm do_update do_whoisd
More:
  Author:
        andrei
void* SV_signal_thread ( void )
Prototyped in: modules/sv/server.h
Calls: CO_get_do_update()modules/co/constants.c
  CO_set_const()modules/co/constants.c
  SV_shutdown()modules/sv/server.c
  log_print()modules/sv/server.c
  pthread_exit(), pthread_sigmask(), sigaddset(), sigemptyset(), sigwait(), sprintf(), strcpy()

Global Function SV_sleep()

int SV_sleep ( int delay )
Prototyped in: modules/sv/server.h
Calls: CO_get_do_server()modules/co/constants.c
  sleep()
Called by: AC_decay()modules/ac/access_control.c

Global Function SV_start()

  Start the server.

More:
  Authors:
        ottrey
        joao
Starts up the server.
  1. Create sockets on the necessary ports (whois, config and mirror)
  2. Start new threads for each service.
.properties
int SV_start ( char* pidfile )
Prototyped in: modules/sv/server.h
Calls: AC_acc_load()modules/ac/access_control.c
  AC_build()modules/ac/access_control.c
  ER_inf_va()modules/er/er.c
  ER_perror()modules/er/er.c
  PW_init()modules/pw/protocol_whois.c
  SK_getsock()modules/sk/sk_socket.c
  SK_init()modules/sk/cd_watchdog.c
  SQ_close_connection()modules/sq/mysql_driver.c
  SQ_get_connection()modules/sq/mysql_driver.c
  SV_concurrent_server()modules/sv/server.c
  ca_get_SourceHandleByPosition()modules/ca/ca_configFns.c
  ca_get_int()modules/ca/ca_configFns.c
  ca_srchandle2Intelement()modules/ca/ca_configFns.c
  ca_srchandle2Strelement()modules/ca/ca_configFns.c
  TH_create(), TR_recover(), close(), ctime_r(), fprintf(), free(), getpid(), gettimeofday(), open(), printf(), pthread_exit(), sprintf(), strlen(), write()
References Functions: AC_decay()modules/ac/access_control.c
  PC_interact()modules/pc/protocol_config.c
  PM_interact()modules/pm/protocol_mirror.c
  PW_interact()modules/pw/protocol_whois.c
  radix_init()modules/sv/server.c
  UD_do_nrtm(), UD_do_updates()
References Variables: SV_config_sockmodules/sv/server.c
  SV_mirror_sockmodules/sv/server.c
  SV_pidfilemodules/sv/server.c
  SV_starttimemodules/sv/server.c
  SV_update_sockmodules/sv/server.c
  SV_whois_sockmodules/sv/server.c

Global Function radix_init()

  Loading the radix tree. Started as a separate thread.

Author: marek
void radix_init ( void )
Calls: RP_init_trees()modules/rp/rp_tree.c
  RP_sql_load_reg()modules/rp/rp_load.c
  ca_get_SourceHandleByPosition()modules/ca/ca_configFns.c
  TH_init_read_write_lockw(), fprintf(), pthread_exit(), wr_log_set()
Used in: SV_start()modules/sv/server.c
References Variables: rx_forest_rwlockmodules/rp/rp_tree.c

Local Function SV_concurrent_server()

  This is the routine that creates the main threads. 

void * do_function The function to call for each type of service
More:
  Author:
        ottrey
	joao
	marek
static void SV_concurrent_server ( int sock, int limit, char* name, void do_function(int) )
int sock
The socket to connect to.
int limit
Limit of active clients (0 == no limit)
char* name
&nbs;
void do_function(int)
&nbs;
Calls: TH_create(), fprintf(), pthread_cond_init(), pthread_mutex_init(), wr_real_calloc()
Called by: SV_start()modules/sv/server.c
References Functions: main_loop()modules/sv/server.c

Local Function counter_add()

  function to operate on the counter structures -  
  takes the increment (can be negative), changes the value
  using the locks and everything,  

int counter_add returns the new value.
Author: marek
static int counter_add ( svr_counter_t* cst, int incval )
svr_counter_t* cst
counter structure
int incval
increment value (can be negative)
Calls: pthread_cond_signal(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: SV_do_child()modules/sv/server.c
  counter_state()modules/sv/server.c
  main_loop()modules/sv/server.c

Local Function counter_state()

  int 
  counter_state         returns the current value of a counter

Author: marek
static int counter_state ( svr_counter_t* cst )
svr_counter_t* cst
counter
Calls: counter_add()modules/sv/server.c
Called by: SV_do_child()modules/sv/server.c

Local Function counter_wait()

  waits until the counter is in the range [0-limit].
  unless the limit is 0, in which case the check is disabled.

Author: marek
static int counter_wait ( svr_counter_t* cst, int limit )
int counter_wait
returns the new value of the counter after wait
svr_counter_t* cst
counter
int limit
limit / range, or 0 to disable the check
Calls: pthread_cond_wait(), pthread_mutex_lock(), pthread_mutex_unlock()
Called by: main_loop()modules/sv/server.c

Local Function log_print()

static void log_print ( const char* arg )
Calls: printf()
Called by: SV_signal_thread()modules/sv/server.c

Local Function main_loop()

  Waits for an incoming connection on the and spawns a new thread to
  handle it.  Takes a pointer to the service description structure
  containing the number of the listening socket, limit of active
  threads, pointer to the counter of them, and the function to call
  with a connected socket.  Increments the counter before starting 
  a client thread to run SV_do_child(). 

More:
  Author:
        ottrey
	joao
	andrei (do_server)
	marek (rewritten/simplified/added limits)
static void* main_loop ( void* arg )
void* arg
pointer to the service description structure.
Calls: CO_get_do_server()modules/co/constants.c
  ER_dbg_va()modules/er/er.c
  SK_accept_connection()modules/sk/sk_socket.c
  counter_add()modules/sv/server.c
  counter_wait()modules/sv/server.c
  TA_add(), TA_delete(), TA_increment(), TA_setactivity(), TH_create(), malloc(), memcpy(), pthread_exit(), snprintf()
Used in: SV_concurrent_server()modules/sv/server.c
References Functions: SV_do_child()modules/sv/server.c