bin/load/loader.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- stop_updates
- error_init
- main
1 /***************************************
2 $Revision: 1.15 $
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 UD_stream_t ud_stream;
104 int load_pass=2;
105 char *prop_file_name=NULL;
106 char *source_name = "RIPE";
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
118 start_object = 1;
119
120 while ((c = getopt(argc, argv, "n:M:L:p:s:?")) != EOF)
121 switch (c) {
122 case 'n':
123 num_skip=atol(optarg);
124 break;
125 case 'p':
126 prop_file_name = optarg;
127 break;
128 case 'L':
129 load_pass=atoi(optarg);
130 dummy_allowed=1;
131 break;
132 case 's':
133 source_name=optarg;
134 break;
135 case '?':
136 default :
137 errflg++;
138 break;
139 }
140 if (errflg) {
141 fprintf(stderr,"usage: standalone [-L pass#] [-n num_skip] [-p properties] file\n");
142 exit (2);
143 }
144
145
146 sig.sa_handler=stop_updates;
147 sigemptyset(&sig.sa_mask);
148 sig.sa_flags=SA_RESTART;
149 sigaction(SIGINT, &sig, NULL);
150 sigaction(SIGTERM, &sig, NULL);
151
152 co_result = CO_set();
153 UT_free(co_result);
154
155 /* 3a. Populate dictionary and load config */
156 ca_init(prop_file_name);
157
158 /* 4. initalise error system */
159 error_init(argc, argv);
160
161 /* Zero the structure */
162 memset(&ud_stream, 0, sizeof(ud_stream));
163
164 /* set mode of operation: unprotected (dummy allowed), updates, standalone */
165 ud_stream.ud_mode= (B_DUMMY | B_UPDATE | B_STANDALONE );
166
167 /* get the source handle */
168 source_hdl = ca_get_SourceHandleByName(source_name);
169
170 /* if NO_NHR option was specified - this is to be done for foreign databases */
171 if(IS_NO_NHR(ca_get_srcmode(source_hdl)))ud_stream.ud_mode |= B_NO_NHR;
172
173
174 /* Connect to the database */
175 db_host = ca_get_srcdbmachine(source_hdl);
176 db_port = ca_get_srcdbport(source_hdl);
177 db_name = ca_get_srcdbname(source_hdl);
178 db_user = ca_get_srcdbuser(source_hdl);
179 db_passwd = ca_get_srcdbpassword(source_hdl);
180
181 fprintf(stderr, "D: Making SQL connection to %s@%s ...", db_name, db_host);
182
183 /* ud_stream.db_connection=SQ_get_connection2(); */
184 ud_stream.db_connection=SQ_get_connection(db_host, db_port, db_name, db_user, db_passwd);
185
186
187 if(! ud_stream.db_connection) {
188 fprintf(stderr, "D: ERROR: no SQL connection\n");
189 exit(1);
190 }
191
192 fprintf(stderr, "OK\n");
193
194
195
196 ud_stream.nrtm=NULL;
197
198 ud_stream.log.num_ok=0;
199 ud_stream.log.num_failed=0;
200
201 UT_free(db_host);
202 UT_free(db_name);
203 UT_free(db_user);
204 UT_free(db_passwd);
205
206
207 if(optind<argc) fd=open(argv[optind],O_RDONLY, 0666); else fd=0;
208
209 if (fd==-1) { fprintf(stderr, "Cannot open data stream. Exiting..\n");
210 exit(1); }
211
212
213 ud_stream.condat.sock = fd;
214 ud_stream.num_skip=num_skip;
215 ud_stream.load_pass=load_pass;
216
217 /* load the dictionary */
218 rpsl_load_dictionary(RPSL_DICT_CORE);
219
220
221
222 /* Start to process the stream */
223
224 fprintf(stderr, "starting processing stream\n");
225 num_ok=UD_process_stream(&ud_stream);
226 fprintf(stderr, "processing stream finished\n");
227 fprintf(stderr, "%d objects processed\n", num_ok);
228
229 return(0);
230
231 } /* main() */
232
233
234
235