143{
144
145 setbuf(stdout, NULL);
146 setbuf(stderr, NULL);
147
150
151
153
154 printf(
"Checking environment... experiment name is \"%s\", remote hostname is \"%s\"\n",
exp_name,
host_name);
155
157 printf(
"Error: trying to use a remote connection to host \"%s\". odbinit must run locally. Sorry.\n",
host_name);
158 exit(1);
159 }
160
161 bool cleanup = false;
162 bool dry_run = false;
163 bool create_exptab = false;
164 bool create_env = false;
166
167
168 for (
int i = 1;
i < argc;
i++) {
169 if (strcmp(argv[
i],
"-g") == 0) {
170
171 }
else if (strcmp(argv[
i],
"-n") == 0) {
172 dry_run = true;
173 }
else if (strcmp(argv[
i],
"--exptab") == 0) {
174 create_exptab = true;
175 }
else if (strcmp(argv[
i],
"--env") == 0) {
176 create_env = true;
177 }
else if (strcmp(argv[
i],
"--cleanup") == 0) {
178 cleanup = true;
179 }
else if (strcmp(argv[
i],
"-e") == 0) {
182 }
else if (strcmp(argv[
i],
"-s") == 0) {
185 } else {
187 }
188 }
189
190 printf(
"Checking command line... experiment \"%s\", cleanup %d, dry_run %d, create_exptab %d, create_env %d\n",
exp_name, cleanup, dry_run, create_exptab, create_env);
191
192#ifndef NO_LOCAL_ROUTINES
193
194 if (create_exptab) {
195 printf("Creating a new exptab file in the current directory...\n");
196
197 FILE *fpr = fopen("exptab", "r");
198 if (fpr) {
199 fclose(fpr);
200 printf("Error: exptab already exists in the current directory. Sorry...\n");
201 exit(1);
202 }
203
206 }
208 printf(
"Error: experiment name \"%s\" should not contain a space character. Sorry...\n",
exp_name);
209 exit(1);
210 }
211 const char*
pwd = getenv(
"PWD");
212 if (!
pwd || strlen(
pwd)<1) {
213 printf("Error: env.variable PWD is not defined or is empty. Sorry...\n");
214 exit(1);
215 }
216 if (strchr(
pwd,
' ')) {
217 printf(
"Error: env.variable PWD value \"%s\" should not contain a space character. Sorry...\n",
pwd);
218 exit(1);
219 }
220 const char* user = getenv("USER");
221 if (!user || strlen(user)<1) {
222 printf("Error: env.variable USER is not defined or is empty. Sorry...\n");
223 exit(1);
224 }
225 if (strchr(user, ' ')) {
226 printf("Error: env.variable USER value \"%s\" should not contain a space character. Sorry...\n", user);
227 exit(1);
228 }
229 printf(
"Experiment name [%s], experiment directory [%s], username [%s]\n",
exp_name,
pwd, user);
230
231 FILE*
fp = fopen(
"exptab",
"w");
233 printf("Error: Cannot write exptab file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
234 exit(1);
235 }
236
239
240 printf("\n");
241 printf(
"Please define env.variable MIDAS_EXPTAB=%s/%s\n",
pwd,
"exptab");
242 printf("\n");
243 printf("Done\n");
244 exit(0);
245 }
246
247 if (create_env) {
248 printf("Creating a new environment settings file in the current directory...\n");
249
250
251
252 FILE *fpr = fopen("env.sh", "r");
253 if (fpr) {
254 fclose(fpr);
255 printf("Error: env.sh already exists in the current directory. Sorry...\n");
256 exit(1);
257 }
258
259 fpr = fopen("env.csh", "r");
260 if (fpr) {
261 fclose(fpr);
262 printf("Error: env.csh already exists in the current directory. Sorry...\n");
263 exit(1);
264 }
265
266
267
268 const char*
pwd = getenv(
"PWD");
269 if (!
pwd || strlen(
pwd)<1) {
270 printf("Error: env.variable PWD is not defined or is empty. Sorry...\n");
271 exit(1);
272 }
273
274 std::string midas_exptab;
277 midas_exptab += "exptab";
278
279
280
281 std::string argv0 = argv[0];
282
283 if (argv0[0] != '/') {
284 printf("\n");
285 printf("Please run odbinit using the full path, i.e. run $HOME/packages/midas/linux/bin/odbinit\n");
286 printf("\n");
287 printf("Bye\n");
288 exit(1);
289 }
290
291
292
293 std::string midassys;
294
295 if (argv0[0] == '/') {
296 midassys += argv0;
297 } else {
299 midassys += "/";
300 midassys += argv0;
301 }
302
303 std::vector<std::string> aa =
split(
"/", midassys);
304 if (aa.size() > 0) {
305 aa.pop_back();
306 }
307 if (aa.size() > 0) {
308 aa.pop_back();
309 }
310 if (aa.size() > 0) {
311 aa.pop_back();
312 }
313
315
316
317
318 std::string path;
319
320 {
321 if (argv0[0] == '/') {
322 path += argv0;
323 } else {
325 path += "/";
326 path += argv0;
327 }
328
329 std::vector<std::string> aa =
split(
"/", path);
330 if (aa.size() > 0) {
331 aa.pop_back();
332 }
333
335 }
336
338
339
340
341 FILE *fpb = fopen("env.sh", "w");
342 if (!fpb) {
343 printf("Error: Cannot write env.sh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
344 exit(1);
345 }
346
347 FILE *fpc = fopen("env.csh", "w");
348 if (!fpc) {
349 printf("Error: Cannot write env.csh file, fopen() errno %d (%s). Sorry...\n", errno, strerror(errno));
350 exit(1);
351 }
352
353 fprintf(fpb, "#!echo You must source\n");
354 fprintf(fpc, "#!echo You must source\n");
355
356 fprintf(fpb, "export MIDASSYS=\"%s\"\n", midassys.c_str());
357 fprintf(fpc, "setenv MIDASSYS \"%s\"\n", midassys.c_str());
358
359 fprintf(fpb, "export MIDAS_EXPTAB=\"%s\"\n", midas_exptab.c_str());
360 fprintf(fpc, "setenv MIDAS_EXPTAB \"%s\"\n", midas_exptab.c_str());
361
362 fprintf(fpb, "export PATH=$PATH:\"%s\"\n", path.c_str());
363 fprintf(fpc, "setenv PATH $PATH\\:\"%s\"\n", path.c_str());
364
365 fprintf(fpb, "# define mserver connection\n");
366 fprintf(fpb, "case `hostname` in\n");
367 fprintf(fpb, "%s) unset MIDAS_SERVER_HOST ;;\n", hostname.c_str());
368 fprintf(fpb, "*) export MIDAS_SERVER_HOST=%s ;;\n", hostname.c_str());
369 fprintf(fpb, "esac\n");
370
371 fprintf(fpc, "# define mserver connection\n");
372 fprintf(fpc, "switch (`hostname`)\n");
373 fprintf(fpc, "case %s:\n", hostname.c_str());
374 fprintf(fpc, "unsetenv MIDAS_SERVER_HOST\n");
375 fprintf(fpc, "breaksw\n");
376 fprintf(fpc, "default:\n");
377 fprintf(fpc, "setenv MIDAS_SERVER_HOST %s\n", hostname.c_str());
378 fprintf(fpc, "endsw\n");
379
380 fprintf(fpb, "#end\n");
381 fprintf(fpc, "#end\n");
382
383 fclose(fpb);
384 fclose(fpc);
385
386 printf("\n");
387 printf("Please source env.sh or env.csh\n");
388 printf("\n");
389 printf("Done\n");
390 exit(0);
391 }
392
393
394
395 printf("Checking MIDASSYS...");
396 const char* midassys = getenv("MIDASSYS");
397
398 if (!midassys) {
399 printf("\n");
400 printf("Error: Env.variable MIDASSYS is not defined.\n");
401 printf("odbinit path [%s]\n", argv[0]);
402 printf("\n");
403 printf("Please run odbinit --env\n");
404 printf("\n");
405 printf("Bye.\n");
406 exit(1);
407 }
408
409 printf("...%s\n", midassys);
410
413
415
417 printf(
"Error: cm_list_experiments() status %d\n",
status);
418 printf("\n");
419 printf("Cannot get the list of experiments, maybe the exptab file is missing.\n");
420 printf("\n");
421 printf("To create a new exptab file in the current directory, run odbinit --exptab -e new_experiment_name\n");
422 printf("\n");
423 printf("Bye...\n");
424 exit(1);
425 }
426
428
429 if (exptab_filename.empty()) {
430 printf("Cannot get the name of the exptab file. Sorry...\n");
431 exit(1);
432 }
433
434 printf("Checking exptab... experiments defined in exptab file \"%s\":\n", exptab_filename.c_str());
435
436 bool found_exp = false;
437 for (
unsigned i=0;
i<exp_names.size();
i++) {
438 printf(
"%d: \"%s\"",
i, exp_names[
i].c_str());
442 printf(" <-- selected experiment");
444 found_exp = true;
445 }
446 printf("\n");
447 }
448
449 if (!found_exp) {
450 printf(
"Specified experiment \"%s\" not found in exptab. Sorry...\n",
exp_name);
451 exit(1);
452 }
453
454 std::string exp_dir;
455 std::string exp_user;
456
458
460 printf(
"Specified experiment \"%s\" not found in exptab, cm_get_exptab() returned %d. Sorry...\n",
exp_name,
status);
461 exit(1);
462 }
463
464 printf("\n");
465 printf(
"Checking exptab... selected experiment \"%s\", experiment directory \"%s\"\n",
exp_name, exp_dir.c_str());
466
470
471 printf("\n");
472
473 {
474 printf("Checking experiment directory \"%s\"\n", exp_dir.c_str());
475
476#ifdef S_ISDIR
477 struct stat stat_buf;
478 int v = stat(exp_dir.c_str(), &stat_buf);
479
480 if (v != 0) {
481 printf("Invalid experiment directory \"%s\" does not seem to exist, stat() returned %d, errno %d (%s)\n", exp_dir.c_str(), v, errno, strerror(errno));
482 printf("Sorry.\n");
483 exit(1);
484 }
485
486 if (!S_ISDIR(stat_buf.st_mode)) {
487 printf("Invalid experiment directory \"%s\" is not a directory\n", exp_dir.c_str());
488 printf("Sorry.\n");
489 exit(1);
490 }
491#else
492#error No support for stat() and S_ISDIR on this system!
493#endif
494 }
495
496 std::string odb_path;
497
498 {
499 std::string path;
500 path += exp_dir;
501 path += ".ODB.SHM";
502
503 FILE *
fp = fopen(path.c_str(),
"r");
506 printf("Found existing ODB save file: \"%s\"\n", path.c_str());
507 if (cleanup) {
508
509
510
511 odb_path = path;
512 } else {
513 printf("Looks like this experiment ODB is already initialized.\n");
514 printf("To create new empty ODB, please rerun odbinit with the \"--cleanup\" option.\n");
515 exit(1);
516 }
517 } else {
518 printf("Good: no ODB save file\n");
519 }
520 }
521
522 printf("\n");
523 printf("Checking shared memory...\n");
524
525 {
526 printf("Deleting old ODB shared memory...\n");
527 if (!dry_run) {
530 printf("Good: no ODB shared memory\n");
532 printf("Deleted existing ODB shared memory, please check that all MIDAS programs are stopped and try again.\n");
533 exit(1);
534 } else {
535 printf(
"ss_shm_delete(ODB) status %d\n",
status);
536 printf("Please check that all MIDAS programs are stopped and try again.\n");
537 exit(1);
538 }
539 }
540 }
541
542 {
543 printf("Deleting old ODB semaphore...\n");
544 if (!dry_run) {
548 printf("Deleting old ODB semaphore... create status %d, delete status %d\n", cstatus, dstatus);
549 }
550 }
551
552 if (odb_path.length() > 0 && cleanup) {
553 time_t now = time(NULL);
554 std::string path1;
555 path1 += odb_path;
556 path1 += ".";
558 printf("Preserving old ODB save file \"%s\" to \"%s\"\n", odb_path.c_str(), path1.c_str());
559 if (!dry_run) {
560 status = rename(odb_path.c_str(), path1.c_str());
562 printf(
"rename(%s, %s) returned %d, errno %d (%s)\n", odb_path.c_str(), path1.c_str(),
status, errno, strerror(errno));
563 printf("Sorry.\n");
564 exit(1);
565 }
566 }
567 }
568
569 printf("\n");
570
571 {
572 printf("Checking ODB size...\n");
573
575
576 printf(
"Requested ODB size is %d bytes (%.2f%s)\n",
odb_size,odb_size_human.first,odb_size_human.second.c_str());
577
578 std::string path1;
579 path1 += exp_dir;
580 path1 += "/";
581 path1 += ".ODB_SIZE.TXT";
582
583 printf("ODB size file is \"%s\"\n", path1.c_str());
584
585 FILE *
fp = fopen(path1.c_str(),
"r");
587 printf("ODB size file \"%s\" does not exist, creating it...\n", path1.c_str());
588 fp = fopen(path1.c_str(),
"w");
590 printf("Cannot create ODB size file \"%s\", fopen() errno %d (%s)\n", path1.c_str(), errno, strerror(errno));
591 printf("Sorry.\n");
592 exit(1);
593 }
595 {
597 fprintf(
fp,
"%d (%.2f%s)\n",
DEFAULT_ODB_SIZE,default_odb_size_human.first,default_odb_size_human.second.c_str());
598 }
599 else
600 {
601 fprintf(
fp,
"%d (%.2f%s)\n",
odb_size,odb_size_human.first,odb_size_human.second.c_str());
602 }
604
605 fp = fopen(path1.c_str(),
"r");
607 printf("Creation of ODB size file \"%s\" somehow failed.\n", path1.c_str());
608 printf("Sorry.\n");
609 exit(1);
610 }
611 }
612
613 int file_odb_size = 0;
614 std::pair<double,std::string> file_odb_size_human;
615 {
616 char buf[256];
617 char *s = fgets(buf,
sizeof(buf),
fp);
618 if (s) {
620 file_odb_size_human=
HumanUnits(file_odb_size);
621 }
622 }
624
625 printf("Saved ODB size from \"%s\" is %d bytes (%.2f%s)\n", path1.c_str(), file_odb_size,file_odb_size_human.first,file_odb_size_human.second.c_str());
626
629
631 printf(
"Requested ODB size %d is different from previous ODB size %d. You have 2 choices:\n",
odb_size, file_odb_size);
632 printf("1) to create ODB with old size, please try again without the \"-s\" switch.\n");
633 printf("2) to create ODB with new size, please delete the file \"%s\" and try again.\n", path1.c_str());
634 exit(1);
635 }
636 }
638 printf(
"We will initialize ODB for experiment \"%s\" on host \"%s\" with size %d bytes (%.2f%s)\n",
exp_name,
host_name,
odb_size, odb_size_human.first, odb_size_human.second.c_str());
639 printf("\n");
640
641
642 {
644 printf("Creating ODB...\n");
646 printf(
"Creating ODB... db_open_database() status %d\n",
status);
648 printf("Something went wrong... continuing...\n");
649 }
650 printf("Saving ODB...\n");
652 printf(
"Saving ODB... db_close_database() status %d\n",
status);
654 printf("Something went wrong... continuing...\n");
655 }
656 }
657
658 printf("Connecting to experiment...\n");
659
661
663 printf(
"Error: cm_connect_experiment() status %d\n",
status);
664 printf("Sorry...\n");
665 exit(1);
666 }
667
668 printf("\n");
669 printf(
"Connected to ODB for experiment \"%s\" on host \"%s\" with size %d bytes (%.2f%s)\n",
exp_name,
host_name,
odb_size,odb_size_human.first,odb_size_human.second.c_str());
670
672
673 {
679 printf(
"Checking experiment name... status %d, found \"%s\"\n",
status, buf);
680
681
682 }
683
684 printf("Disconnecting from experiment...\n");
686
687 printf("\n");
688 printf("Done\n");
689
690#else
691 printf("this version of odbinit is built with NO_LOCAL_ROUTINES and it will not work. odbinit only works locally!\n");
692#endif
693
694 return 0;
695}
INT cm_set_path(const char *path)
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
INT cm_list_experiments_local(STRING_LIST *exp_names)
INT cm_connect_experiment1(const char *host_name, const char *default_exp_name, const char *client_name, void(*func)(char *), INT odb_size, DWORD watchdog_timeout)
INT cm_disconnect_experiment(void)
int cm_get_exptab(const char *expname, std::string *dir, std::string *user)
std::string cm_get_exptab_filename()
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
INT cm_set_experiment_name(const char *name)
std::string ss_gethostname()
INT ss_suspend_init_odb_port()
INT ss_semaphore_create(const char *name, HNDLE *semaphore_handle)
INT ss_shm_delete(const char *name)
INT ss_semaphore_delete(HNDLE semaphore_handle, INT destroy_flag)
INT cm_msg_flush_buffer()
BOOL equal_ustring(const char *str1, const char *str2)
INT db_get_value(HNDLE hDB, HNDLE hKeyRoot, const char *key_name, void *data, INT *buf_size, DWORD type, BOOL create)
INT db_open_database(const char *xdatabase_name, INT database_size, HNDLE *hDB, const char *client_name)
INT db_close_database(HNDLE hDB)
char exp_name[NAME_LENGTH]
char host_name[HOST_NAME_LENGTH]
#define DEFAULT_WATCHDOG_TIMEOUT
std::vector< std::string > STRING_LIST
static std::string join(const char *sep, const std::vector< std::string > &v)
static std::string to_string(int v)
static std::vector< std::string > remove_dot_dot(const std::vector< std::string > &v)
int DecodeSize(const char *s)
std::pair< double, std::string > HumanUnits(int odb_size)
static std::vector< std::string > split(const char *sep, const std::string &s)