## bin/load/loader.c

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

## FUNCTIONS

This source file includes following functions.

1. stop_updates
2. error_init
3. main

       1 /***************************************
       2   $Revision: 1.12 $
       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                         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 <sys/types.h>
      35 #include <fcntl.h>
      36 #include <signal.h>
      37
      38 #include <ud.h>
      39 #include <ud_int.h>
      40 #include <constants.h>
      41 #include <properties.h>
      42
      43
      44 #include "ca_configFns.h"
      45 #include "ca_macros.h"
      46 #include "ca_srcAttribs.h"
      47
      48
      49
      50
      51 void stop_updates()
         /* [<][>][^][v][top][bottom][index][help] */
      52 {
      53  char print_buf[STR_M];
      54
      55  fprintf(stderr, "Updates interrupted..\n");
      56  sprintf(print_buf, "%d", 0);
      57  CO_set_const("UD.do_update", print_buf);
      58  return;
      59 }
      60
      61
      62 void error_init(int argc, char ** argv) {
         /* [<][>][^][v][top][bottom][index][help] */
      63   char *slash;
      64   char progname[32];
      65
      66   slash = strrchr(argv[0],'/');
      67   strncpy(progname, (slash != NULL) ? slash+1 : argv[0], 31);
      68   progname[31]=0;
      69
      70   /* Initialize error module but do not process ER_DEF definitions from the config */
      71   ER_init(progname, 0);
      72
      73  /* ripupdlog: logs all update transactions */
      74  /* add one more definition */
      75   {
      76     char *err_msg = NULL;
      77     char *buf =
      78       "CREATE err_log { FORMAT SEVCHAR|FACSYMB|TEXTLONG|DATETIME SOCK 2 }"
      79       "( FAC ALL SEV E- )"
      80       "( FAC UD ASP 0xffffffff SEV I )"
      81       ;
      82
      83     int parsres = ER_parse_spec(buf, &err_msg);
      84
      85     if( parsres != 0 ) { /* print only on failure */
      86       puts(err_msg);
      87     }
      88
      89     wr_free(err_msg);
      90
      91     dieif( parsres != 0 );
      92   }
      93
      94
      95 } /* error_init() */
      96
      97
      98 /***********************************************
      99 ******* MAIN **********************************
     100 ***********************************************/
     101
     102
     103 int main(int argc, char** argv) {
         /* [<][>][^][v][top][bottom][index][help] */
     104 int c;
     105 int fd;
     106 extern int optind;
     107 extern char *optarg;
     108 int errflg = 0;
     109 int dummy_allowed;
     110 int start_object;
     111 int num_ok, num_failed;
     112 long num_skip=0;
     113 struct _nrtm *nrtm=NULL;
     114 UD_stream_t ud_stream;
     115 Log_t log;
     116 int current_serial=-1;
     117 int load_pass=0;
     118 int delay=1;
     119 char *prop_file_name=NULL;
     120 int do_update;
     121 char *source_name = "RIPE";
     122 ca_dbSource_t *source_hdl;
     123 char *db_host, *db_name, *db_user, *db_passwd;
     124 int db_port;
     125 char *co_result;
     126
     127
     128 struct sigaction sig;
     129
     130   num_ok=0; num_failed=0;
     131   dummy_allowed=0;
     132
     133   start_object = 1;
     134
     135         while ((c = getopt(argc, argv, "n:M:L:p:s:?")) != EOF)
     136                 switch (c) {
     137                 case 'n':
     138                         num_skip=atol(optarg);
     139                         break;
     140                 case 'p':
     141                         prop_file_name = optarg;
     142                         break;
     143                 case 'L':
     144                         load_pass=atoi(optarg);
     145                         dummy_allowed=1;
     146                         break;
     147                 case 's':
     148                         source_name=optarg;
     149                         break;
     150                 case '?':
     151                 default :
     152                         errflg++;
     153                         break;
     154                 }
     155                 if (errflg) {
     156                         fprintf(stderr,"usage: standalone [-L pass#] [-n num_skip] [-p properties] file\n");
     157                         exit (2);
     158                 }
     159
     160
     161 sig.sa_handler=stop_updates;
     162 sigemptyset(&sig.sa_mask);
     163 sig.sa_flags=SA_RESTART;
     164 sigaction(SIGINT, &sig, NULL);
     165 sigaction(SIGTERM, &sig, NULL);
     166
     167   co_result = CO_set();
     168   free(co_result);
     169
     170   /* 3a. Populate dictionary and load config */
     171   ca_init(prop_file_name);
     172
     173   /* 4. initalise error system */
     174   error_init(argc, argv);
     175
     176   /* Zero the structure */
     177    memset(&ud_stream, 0, sizeof(ud_stream));
     178
     179   /* set mode of operation: unprotected (dummy allowed), updates, standalone */
     180    ud_stream.ud_mode= (B_DUMMY | B_UPDATE | B_STANDALONE );
     181
     182    /* get the source handle */
     183    source_hdl = ca_get_SourceHandleByName(source_name);
     184
     185    /* if NO_NHR option was specified - this is to be done for foreign databases */
     186    if(IS_NO_NHR(ca_get_srcmode(source_hdl)))ud_stream.ud_mode |= B_NO_NHR;
     187
     188
     189 /* Connect to the database */
     190    db_host = ca_get_srcdbmachine(source_hdl);
     191    db_port = ca_get_srcdbport(source_hdl);
     192    db_name = ca_get_srcdbname(source_hdl);
     193    db_user = ca_get_srcdbuser(source_hdl);
     194    db_passwd = ca_get_srcdbpassword(source_hdl);
     195
     196   fprintf(stderr, "D: Making SQL connection to %s@%s ...", db_name, db_host);
     197
     198 /*  ud_stream.db_connection=SQ_get_connection2(); */
     199   ud_stream.db_connection=SQ_get_connection(db_host, db_port, db_name, db_user, db_passwd);
     200
     201
     202   if(! ud_stream.db_connection) {
     203    fprintf(stderr, "D: ERROR: no SQL connection\n");
     204     return;
     205   }
     206
     207   fprintf(stderr, "OK\n");
     208
     209
     210
     211  ud_stream.nrtm=NULL;
     212
     213  ud_stream.log.num_ok=0;
     214  ud_stream.log.num_failed=0;
     215
     216   free(db_host);
     217   free(db_name);
     218   free(db_user);
     219   free(db_passwd);
     220
     221
     222  if(optind<argc) fd=open(argv[optind],O_RDONLY, 0666); else fd=0;
     223
     224  if (fd==-1) {  fprintf(stderr, "Cannot open data stream. Exiting..\n");
     225                         exit(1); }
     226
     227
     228   ud_stream.condat.sock = fd;
     229   ud_stream.num_skip=num_skip;
     230   ud_stream.load_pass=load_pass;
     231
     232 /* Start to process the stream */
     233
     234 fprintf(stderr, "starting processing stream\n");
     235   num_ok=UD_process_stream(&ud_stream);
     236 fprintf(stderr, "processing stream finished\n");
     237 fprintf(stderr, "%d objects processed\n", num_ok);
     238
     239 return(0);
     240
     241 } /* main() */
     242
     243
     244
     245

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