bin/load/loader.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following functions.
  1. stop_updates
  2. error_init
  3. main

   1 /***************************************
   2   $Revision: 1.16 $
   3 
   4   loader.c - core of the database loading.
   5 
   6   Status: NOT REVUED, TESTED
   7 
   8   ******************/ /******************
   9   Filename            : loader.c
  10   Authors             : Andrei Robachevsky
  11   OSs Tested          : Solaris 7,8
  12   ******************/ /******************
  13   Copyright (c) 2000,2001,2002                    RIPE NCC
  14  
  15   All Rights Reserved
  16   
  17   Permission to use, copy, modify, and distribute this software and its
  18   documentation for any purpose and without fee is hereby granted,
  19   provided that the above copyright notice appear in all copies and that
  20   both that copyright notice and this permission notice appear in
  21   supporting documentation, and that the name of the author not be
  22   used in advertising or publicity pertaining to distribution of the
  23   software without specific, written prior permission.
  24   
  25   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  26   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  27   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  28   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  29   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  30   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31   ***************************************/
  32 
  33 
  34 #include "rip.h"
  35 
  36 #include <sys/types.h>
  37 #include <fcntl.h>
  38 #include <signal.h>
  39 
  40 
  41 void stop_updates()
     /* [<][>][^][v][top][bottom][index][help] */
  42 {
  43  char print_buf[STR_M];
  44  
  45  fprintf(stderr, "Updates interrupted..\n");
  46  sprintf(print_buf, "%d", 0);
  47  CO_set_const("UD.do_update", print_buf);
  48  return;
  49 }
  50                       
  51 
  52 void error_init(int argc, char ** argv) {
     /* [<][>][^][v][top][bottom][index][help] */
  53   char *slash;     
  54   char progname[32];
  55   
  56   slash = strrchr(argv[0],'/');                            
  57   strncpy(progname, (slash != NULL) ? slash+1 : argv[0], 31);
  58   progname[31]=0;
  59   
  60   /* Initialize error module but do not process ER_DEF definitions from the config */
  61   ER_init(progname, 0);
  62 #if 1
  63  /* ripupdlog: logs all update transactions */
  64  /* add one more definition */
  65   {
  66     char *err_msg = NULL;
  67     char *buf = 
  68       "CREATE err_log { FORMAT SEVCHAR|FACSYMB|TEXTLONG|DATETIME SOCK 2 }"
  69       "( FAC ALL SEV E- )"
  70       "( FAC UD ASP 0xffffffff SEV I )"
  71       ;
  72 
  73     int parsres = ER_parse_spec(buf, &err_msg);
  74 
  75     if( parsres != 0 ) { /* print only on failure */
  76       puts(err_msg);
  77     }
  78     
  79     UT_free(err_msg);
  80     
  81     dieif( parsres != 0 );
  82   }
  83 #endif 
  84 
  85 } /* error_init() */
  86 
  87 
  88 /***********************************************
  89 ******* MAIN **********************************
  90 ***********************************************/
  91 
  92 
  93 int main(int argc, char** argv) {
     /* [<][>][^][v][top][bottom][index][help] */
  94 int c;
  95 int fd;
  96 extern int optind;
  97 extern char *optarg;
  98 int errflg = 0;
  99 int dummy_allowed;
 100 int start_object;
 101 int num_ok, num_failed;
 102 long num_skip=0;
 103 int load_pass = -1;
 104 UD_stream_t ud_stream;
 105 char *prop_file_name=NULL;
 106 char *source_name = NULL;
 107 ca_dbSource_t *source_hdl;
 108 char *db_host, *db_name, *db_user, *db_passwd;
 109 int db_port;
 110 char *co_result;
 111 
 112 
 113 struct sigaction sig;
 114   
 115   num_ok=0; num_failed=0;
 116   dummy_allowed=0;
 117   load_pass = 
 118   
 119   start_object = 1;
 120 
 121         while ((c = getopt(argc, argv, "n:M:L:p:s:?")) != EOF)
 122                 switch (c) {
 123                 case 'n':
 124                         num_skip=atol(optarg);
 125                         break;  
 126                 case 'p':
 127                         prop_file_name = optarg;
 128                         break;
 129                 case 'L':
 130                         load_pass=atoi(optarg);
 131                         dummy_allowed=1;
 132                         break;
 133                 case 's':
 134                         source_name=optarg;
 135                         break;
 136                 case '?':
 137                 default :
 138                         errflg++;
 139                         break;
 140                 }
 141 
 142                 /* check that all parameters were specified */
 143                 if (prop_file_name == NULL){
 144                   errflg++;
 145                   fprintf(stderr, "configuration file is not specified (-p)\n");
 146                 }
 147                 if (load_pass == -1) {
 148                   errflg++;
 149                   fprintf(stderr, "load pass number is not specified (-L)\n");
 150                 }
 151                 if (source_name == NULL) {
 152                   errflg++;
 153                   fprintf(stderr, "source is not specified (-s)\n");
 154                 }
 155                 if (errflg) {
 156                         fprintf(stderr,"usage: loader -L pass# [-n num_skip] -p properties -s source file\n");
 157                         exit (2);
 158                 } 
 159 
 160   
 161   /* reach properly to SIGINT and SIGTERM */
 162   sig.sa_handler=stop_updates;
 163   sigemptyset(&sig.sa_mask);
 164   sig.sa_flags=SA_RESTART;
 165   sigaction(SIGINT, &sig, NULL);
 166   sigaction(SIGTERM, &sig, NULL);
 167  
 168   co_result = CO_set();
 169   UT_free(co_result);
 170   
 171   /* 3a. Populate dictionary and load config */
 172   ca_init(prop_file_name);
 173 
 174   /* 4. initalise error system */
 175   error_init(argc, argv);
 176   
 177   /* Zero the structure */
 178    memset(&ud_stream, 0, sizeof(ud_stream));
 179 
 180   /* set mode of operation: unprotected (dummy allowed), updates, standalone */
 181    ud_stream.ud_mode= (B_DUMMY | B_UPDATE | B_STANDALONE );
 182   
 183    /* get the source handle */
 184    source_hdl = ca_get_SourceHandleByName(source_name);
 185 
 186    /* if NO_NHR option was specified - this is to be done for foreign databases */
 187    if(IS_NO_NHR(ca_get_srcmode(source_hdl)))ud_stream.ud_mode |= B_NO_NHR;
 188 
 189    
 190 /* Connect to the database */
 191    db_host = ca_get_srcdbmachine(source_hdl);
 192    db_port = ca_get_srcdbport(source_hdl);
 193    db_name = ca_get_srcdbname(source_hdl);
 194    db_user = ca_get_srcdbuser(source_hdl);
 195    db_passwd = ca_get_srcdbpassword(source_hdl);
 196  
 197   fprintf(stderr, "D: Making SQL connection to %s@%s ...", db_name, db_host);
 198 
 199 /*  ud_stream.db_connection=SQ_get_connection2(); */
 200   ud_stream.db_connection=SQ_get_connection(db_host, db_port, db_name, db_user, db_passwd);
 201      
 202  
 203   if(! ud_stream.db_connection) {
 204    fprintf(stderr, "D: ERROR: no SQL connection\n");
 205     exit(1);
 206   }
 207         
 208   fprintf(stderr, "OK\n");
 209 
 210                             
 211                                                                                                             
 212  ud_stream.nrtm=NULL;
 213                                             
 214  ud_stream.log.num_ok=0;
 215  ud_stream.log.num_failed=0;
 216 
 217   UT_free(db_host);
 218   UT_free(db_name);
 219   UT_free(db_user);
 220   UT_free(db_passwd);
 221 
 222 
 223  if(optind<argc) fd=open(argv[optind],O_RDONLY, 0666); else fd=0;
 224 
 225  if (fd==-1) {  fprintf(stderr, "Cannot open data stream. Exiting..\n");
 226                         exit(1); }
 227 
 228 
 229   ud_stream.condat.sock = fd;
 230   ud_stream.num_skip=num_skip;
 231   ud_stream.load_pass=load_pass;
 232 
 233 /* load the dictionary */
 234   rpsl_load_dictionary(RPSL_DICT_CORE);
 235              
 236              
 237 
 238 /* Start to process the stream */
 239 
 240   fprintf(stderr, "starting processing stream\n");
 241   num_ok=UD_process_stream(&ud_stream);
 242   fprintf(stderr, "processing stream finished\n");
 243   fprintf(stderr, "%d objects processed\n", num_ok);
 244 
 245 return(0);
 246 
 247 } /* main() */
 248 
 249 
 250 
 251 

/* [<][>][^][v][top][bottom][index][help] */