-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_BM.c
2721 lines (2536 loc) · 102 KB
/
main_BM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// file main_BM.c
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
----
Contact me (Basile Starynkevitch) by email
***/
#include "bismon.h"
/* for get_nprocs(3) */
#include <sys/sysinfo.h>
static atomic_int nb_warnings_BM;
struct timespec startrealtimespec_BM;
void *dlprog_BM;
const char *myprogname_BM;
bool gui_is_running_BM;
volatile bool showdebugmsg_BM;
bool parsedebugmsg_BM;
int nbworkjobs_BM;
int randomseed_BM;
bool debug_after_load_BM;
char myhostname_BM[80];
const char *contributors_filepath_BM;
const char *passwords_filepath_BM;
const char *added_passwords_filepath_BM;
const char *contact_filepath_BM;
char *contact_name_BM;
char *contact_email_BM;
double sleepdelay_bm;
const char *project_name_BM;
const char *plugin_before_load_BM;
const char *sigusr1_dump_prefix_BM;
const volatile char *unix_json_socket_BM;
bool dont_indent_generated_code_BM;
void *dlh_before_load_bm;
int sigfd_BM = -1; /* for signalfd(2) */
atomic_int oniontimerfd_BM = -1; /* for timerfd_create(2) */
char real_executable_BM[128];
typedef void action_after_load_sigt (const char *);
static gchar **do_after_load_bm;
////////////////////////////////////////////////////////////////
extern void parse_program_options_BM (int argc, char **argv);
extern void show_program_options_BM (FILE * out, int argc, char **argv);
static const char *chdir_after_load_bm;
thread_local struct threadinfo_stBM *curthreadinfo_BM;
thread_local volatile struct failurehandler_stBM *curfailurehandle_BM;
volatile struct backstrace_state *backtracestate_BM;
static void backtracerrorcb_BM (void *data, const char *msg, int errnum);
static void test_make_empty_sigusr1_dump_dir_BM (void);
extern void run_testplugins_after_load_BM (void);
static void add_passwords_from_file_BM (const char *addedpasspath);
static void write_pid_into_file_and_kill_old_BM (const char *pidfilepath);
void cleanup_temporary_dir_after_exit_BM (void); /* for atexit */
////////////////
extern void weakfailure_BM (void);
const char *
bismon_home_BM (void)
{
static const char *bh;
if (UNLIKELY_BM (!bh))
{
const char *bismon_home = getenv ("BISMON_HOME");
const char *home = getenv ("HOME");
if (bismon_home)
bh = bismon_home;
else if (home)
bh = home;
if (!bh)
FATAL_BM ("improbable bismon_home_BM failure (%m)");
};
return bh;
} /* end bismon_home_BM */
// consider putting a gdb breakpoint here
void
weakfailure_BM (void)
{
fflush (NULL);
} /* end weakfailure_BM */
void
warning_at_BM (const char *fil, int lin)
{
ASSERT_BM (fil != NULL && lin > 0);
int nbw = 1 + atomic_fetch_add (&nb_warnings_BM, 1);
if (nbw % 10 == 0)
fputc ('\n', stderr);
fprintf (stderr, "BISMON WARNING#%03d: %s: %d: ", nbw,
basename_BM (fil), lin);
fflush (stderr);
} /* end warning_at_BM */
void
weakassertfailureat_BM (const char *condmsg, const char *fil, int lin)
{
char thnambuf[16];
memset (thnambuf, 0, sizeof (thnambuf));
pthread_getname_np (pthread_self (), thnambuf, sizeof (thnambuf));
fprintf (stderr, "**** weakassertfailureat_BM (%s:%d) %s (tid#%d/%s)\n",
fil, lin, condmsg, (int) gettid_BM (), thnambuf);
fflush (stderr);
if (backtracestate_BM)
{
fprintf (stderr, "\n\n\n** full backtrace **\n");
fflush (stderr);
backtrace_print_BM ((struct backtrace_state *) backtracestate_BM, 1,
stderr);
fprintf (stderr, "\n----- end full backtrace ------\n\n");
fflush (stderr);
}
else
{
WARNPRINTF_BM ("weakassertfailureat_BM <%s:%d> %s - no backtracestate",
fil, lin, condmsg);
void *backbuf[50];
memset (backbuf, 0, sizeof (backbuf));
int nb = backtrace (backbuf, sizeof (backbuf) / sizeof (void *));
backtrace_symbols_fd (backbuf, nb, STDERR_FILENO);
}
weakfailure_BM ();
} /* end weakassertfailureat_BM */
value_tyBM
objrout_placeholder_BM (struct stackframe_stBM *stkf __attribute__((unused)), //
const value_tyBM arg1 __attribute__((unused)), //
const value_tyBM arg2 __attribute__((unused)), //
const value_tyBM arg3 __attribute__((unused)), //
const value_tyBM arg4 __attribute__((unused)), //
const quasinode_tyBM * restargs
__attribute__((unused)))
{
weakassertfailureat_BM ("objrout_placeholder_BM", __FILE__, __LINE__);
return NULL;
} /* end objrout_placeholder_BM */
void
abort_BM (void)
{
weakfailure_BM ();
abort ();
} /* end abort_BM */
////////////////////////////////////////////////////////////////
char temporary_dir_BM[256];
char *load_dir_bm;
char *dump_dir_BM;
char *dump_after_load_dir_bm;
char *css_file_bm = "bismon.css";
char *gui_log_name_bm = "_bismon.log"; /* default log file */
char *pid_filepath_bm = "_bismon.pid"; /* default pid file */
char *comment_bm;
char *module_to_emit_bm;
int count_emit_has_predef_bm;
int nb_added_predef_bm;
char *print_contributor_of_oid_bm;
char *mailhtml_file_bm;
char *mailhtml_contributor_bm;
char *mailhtml_subject_bm;
char *mailhtml_attachment_bm;
char *password_file_comment_BM;
static bool want_finalgc_bm; /* to run a final GC */
static bool want_cleanup_bm; /* to make valgrind more happy; see http://valgrind.org/ for more */
int count_init_afterload_bm; /* used count in arr_init_afterload_bm */
int size_init_afterload_bm; /* allocated size of arr_init_afterload_bm */
char **arr_init_afterload_bm; // allocated size is size_init_afterload_bm
// and each string is strdup-ed
#define MAXADDEDPREDEF_BM 16
struct
{
const char *pr_comment;
const char *pr_name;
} added_predef_bm[MAXADDEDPREDEF_BM];
char **added_contributors_arr_bm;
int count_added_contributors_bm;
int size_added_contributors_bm;
char **removed_contributors_arr_bm;
int count_removed_contributors_bm;
int size_removed_contributors_bm;
#define MAXPARSED_VALUES_AFTER_LOAD_BM 10
char *parsed_values_after_loadarr_bm[MAXPARSED_VALUES_AFTER_LOAD_BM + 1];
int nb_parsed_values_after_load_bm;
#define MAXPARSED_FILES_AFTER_LOAD_BM 30
char *parsed_files_after_loadarr_bm[MAXPARSED_FILES_AFTER_LOAD_BM + 1];
int nb_parsed_files_after_load_bm;
#define MAXTESTPLUGINS_AFTER_LOAD_BM 10
char *testplugins_after_loadarr_bm[MAXTESTPLUGINS_AFTER_LOAD_BM];
int nb_testplugins_after_load_bm;
bool batch_bm;
bool give_version_bm;
void
failure_at_BM (int failcode, const char *fil, int lineno,
const value_tyBM reasonv, const value_tyBM placev,
struct stackframe_stBM *stkf)
{
if (curfailurehandle_BM)
{
if (curfailurehandle_BM->failh_magic != FAILUREHANDLEMAGIC_BM)
FATAL_AT_BM (fil, lineno,
"corrupted curfailurehandle_BM@%p for failcode %d",
curfailurehandle_BM, failcode);
curfailurehandle_BM->failh_reason = reasonv;
curfailurehandle_BM->failh_place = placev;
if (!curfailurehandle_BM->failh_silent)
{
char thnambuf[16];
memset (thnambuf, 0, sizeof (thnambuf));
pthread_getname_np (pthread_self (), thnambuf, sizeof (thnambuf));
fprintf (stderr, "\n\n*** failure code#%d %s:%d (tid#%d/%s) ***\n",
failcode, fil ? fil : "???", lineno, (int) gettid_BM (),
thnambuf);
if (backtracestate_BM)
{
backtrace_print_BM ((struct backtrace_state *)
backtracestate_BM, 1, stderr);
fprintf (stderr, "\n----- end failure backtrace ------\n");
}
fflush (stderr);
// we need that debug_outstr_value_BM should not fail...
fprintf (stderr,
"*#* failure code#%d from %s:%d\n"
"fail reason : %s\n"
"fail place : %s\n",
failcode, fil ? fil : "???", lineno,
debug_outstr_value_BM (reasonv, stkf, 0),
debug_outstr_value_BM (placev, stkf, 0));
fprintf (stderr, "#*#*#*#*#*#*#*#*#*#*\n\n");
fflush (stderr);
}
longjmp (((struct failurehandler_stBM *)
curfailurehandle_BM)->failh_jmpbuf, failcode);
}
else
{
FATAL_AT_BM (fil, lineno,
"unhandled failure code#%d reason %s",
failcode, debug_outstr_value_BM (reasonv, stkf, 0));
}
} /* end failure_at_BM */
void
failure_BM (int failcode, const value_tyBM reasonv,
struct stackframe_stBM *stkf)
{
failure_at_BM (failcode, "??", 0, reasonv, taggedint_BM (failcode), stkf);
} /* end failure_BM */
void
fatal_stop_at_BM (const char *fil, int lineno)
{
char thnambuf[16];
memset (thnambuf, 0, sizeof (thnambuf));
pthread_getname_np (pthread_self (), thnambuf, sizeof (thnambuf));
fprintf (stderr, "** FATAL STOP %s:%d (tid#%d/%s)\n",
fil ? fil : "???", lineno, (int) gettid_BM (), thnambuf);
fflush (stderr);
void *backarr[2 * TINYSIZE_BM];
memset (backarr, 0, sizeof (backarr));
int backdepth = backtrace (backarr, sizeof (backarr) / sizeof (void *));
backtrace_symbols_fd (backarr, backdepth, STDERR_FILENO);
if (backtracestate_BM)
{
fprintf (stderr, "\n\n\n** full fatal backtrace **\n");
fflush (stderr);
backtrace_print_BM ((struct backtrace_state *) backtracestate_BM, 1,
stderr);
fprintf (stderr, "\n----- end full fatal backtrace ------\n\n");
}
fflush (stderr);
abort_BM ();
} /* end fatal_stop_at_BM */
static void add_new_predefined_bm (void);
static void do_test_mailhtml_bm (void);
static void init_afterload_bm (void);
static void show_net_info_bm (void);
static bool
run_command_bm (const gchar * optname __attribute__((unused)), //
const gchar * val, //
gpointer data __attribute__((unused)), //
GError ** perr)
{
ASSERT_BM (val != NULL);
INFOPRINTF_BM ("running command: %s\n", val);
int ok = system (val);
if (ok == 0)
return TRUE;
g_set_error (perr, 0, ok, "command %s failed with status %d", val, ok);
return FALSE;
} /* end run_command_bm */
static bool
set_project_name_bm (const gchar * optname __attribute__((unused)), //
const gchar * val, //
gpointer data __attribute__((unused)), //
GError ** perr)
{
if (val == NULL)
FATAL_BM ("missing project name");
bool goodname = true;
if (!isalpha (val[0]) && val[0] != '_')
goodname = false;
for (const gchar * pc = val; *pc && goodname; pc++)
if (!isalnum (*pc) && *pc != '_')
goodname = false;
if (!goodname)
{
WARNPRINTF_BM ("invalid project name %s, should be C-identifier like",
(const char *) val);
g_set_error (perr, 0, 1,
"invalid project name %s, should be C-identifier like",
(const char *) val);
return FALSE;
}
project_name_BM = g_strdup (val);
INFOPRINTF_BM ("using Bismon project name %s", project_name_BM);
return TRUE;
} /* end set_project_name_bm */
void
show_net_info_bm (void)
{
/// show some networking information
printf ("\n**** Bismon pid %d networking information ***\n",
(int) getpid ());
fflush (NULL);
{
int cod = system (SHOW_NET_COMMAND_BM);
if (cod > 0)
WARNPRINTF_BM ("command '%s' failed with #%d", SHOW_NET_COMMAND_BM,
cod);
fflush (NULL);
}
printf ("***** end of bismon pid %d networking information ***\n",
(int) getpid ());
fflush (NULL);
} /* end show_net_info_bm */
static bool
handle_init_afterload_bm (const gchar * optname __attribute__((unused)), //
const gchar * val, //
gpointer data __attribute__((unused)), //
GError ** perr __attribute__((unused)))
{
ASSERT_BM (val != NULL);
DBGPRINTF_BM ("init_afterload #%d: %s (@%p)\n", count_init_afterload_bm,
val, val);
if (count_init_afterload_bm + 1 >= size_init_afterload_bm)
{
int newsiz = prime_above_BM (4 * count_init_afterload_bm / 3 + 30);
char **newarr = calloc (newsiz, sizeof (char *));
if (!newarr)
FATAL_BM ("failed to calloc for %d init_afterload", newsiz);
if (count_init_afterload_bm > 0)
memcpy (newarr, arr_init_afterload_bm,
count_init_afterload_bm * sizeof (char *));
free (arr_init_afterload_bm), arr_init_afterload_bm = newarr;
size_init_afterload_bm = newsiz;
}
char *dupval = strdup (val);
if (!dupval)
FATAL_BM ("failed to strdup %s for init_afterload#%d", val,
count_init_afterload_bm);
arr_init_afterload_bm[count_init_afterload_bm] = dupval;
count_init_afterload_bm++;
INFOPRINTF_BM ("should do after load: %s\n", val);
return true;
} /* end handle_init_afterload_bm */
static void
get_parse_value_after_load_bm (const gchar * optname __attribute__((unused)),
const gchar * val,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
if (nb_parsed_values_after_load_bm >= MAXPARSED_VALUES_AFTER_LOAD_BM)
FATAL_BM ("too many %d parsed values after load with --parse-value",
nb_parsed_values_after_load_bm);
NONPRINTF_BM ("get_parse_value_after_load #%d.. valen=%d:\n%s",
nb_parsed_values_after_load_bm, (int) strlen (val), val);
parsed_values_after_loadarr_bm[nb_parsed_values_after_load_bm++] =
strdup (val);
} /* end get_parse_value_after_load_bm */
static void
get_parse_file_after_load_bm (const gchar * optname __attribute__((unused)),
const gchar * val,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
if (nb_parsed_files_after_load_bm >= MAXPARSED_FILES_AFTER_LOAD_BM)
FATAL_BM ("too many %d parsed values after load with --parse-file",
nb_parsed_files_after_load_bm);
if (access ((const char *) val, R_OK))
FATAL_BM ("cannot access file %s to be parsed (%m)", val);
NONPRINTF_BM ("get_parse_file_after_load #%d.. valen=%d:\n%s",
nb_parsed_files_after_load_bm, (int) strlen (val), val);
parsed_files_after_loadarr_bm[nb_parsed_files_after_load_bm++] =
(char *) val;
} /* end get_parse_file_after_load_bm */
static void
get_testplugin_after_load_bm (const gchar * optname __attribute__((unused)),
const gchar * val,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
if (nb_testplugins_after_load_bm >= MAXTESTPLUGINS_AFTER_LOAD_BM)
FATAL_BM ("too many %d testplugins after load with --test-plugin",
nb_testplugins_after_load_bm);
DBGPRINTF_BM ("get_testplugin_after_load_bm #%d.. valen=%d:\n%s",
nb_testplugins_after_load_bm, (int) strlen (val), val);
testplugins_after_loadarr_bm[nb_testplugins_after_load_bm++] = strdup (val);
} /* end get_testplugin_after_load_bm */
static bool
add_predef_bm (const gchar * optname __attribute__((unused)),
const gchar * val,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
ASSERT_BM (val != NULL);
if (!validname_BM (val))
FATAL_BM ("invalid predef name %s", val);
if (nb_added_predef_bm >= MAXADDEDPREDEF_BM)
FATAL_BM ("too many added predefined %i", nb_added_predef_bm);
// in principle the strdup-s below should be checked, but in
// practice this is so rarely used that we don't bother
if (comment_bm)
added_predef_bm[nb_added_predef_bm].pr_comment = strdup (comment_bm);
added_predef_bm[nb_added_predef_bm].pr_name = strdup (val);
nb_added_predef_bm++;
comment_bm = NULL;
return true;
} /* end add_predef_bm */
static bool
add_contributor_bm (const gchar * optname __attribute__((unused)),
const gchar * contrib,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
if (count_added_contributors_bm >= size_added_contributors_bm)
{
int newsiz = prime_above_BM (3 * count_added_contributors_bm / 2 + 16);
char **newarr = calloc (newsiz, sizeof (char *));
if (!newarr || newsiz > MAXSIZE_BM / 2) /*very unlikely to happen in practice */
FATAL_BM ("cannot grow added contributors array to %d for %s - %m",
newsiz, contrib);
if (count_added_contributors_bm > 0)
memcpy (newarr, added_contributors_arr_bm,
count_added_contributors_bm * sizeof (char *));
free (added_contributors_arr_bm);
added_contributors_arr_bm = newarr;
size_added_contributors_bm = newsiz;
};
char *newcontrib = strdup (contrib);
if (!newcontrib)
FATAL_BM ("failed to strdup added contributor %s - %m", contrib);
added_contributors_arr_bm[count_added_contributors_bm++] = newcontrib;
return true;
} /* end add_contributor_bm */
static bool
remove_contributor_bm (const gchar * optname __attribute__((unused)),
const gchar * contrib,
gpointer data __attribute__((unused)),
GError ** perr __attribute__((unused)))
{
if (count_removed_contributors_bm >= size_removed_contributors_bm)
{
int newsiz =
prime_above_BM (3 * count_removed_contributors_bm / 2 + 16);
char **newarr = calloc (newsiz, sizeof (char *));
if (!newarr || newsiz > MAXSIZE_BM / 2) /*very unlikely to happen in practice */
FATAL_BM ("cannot grow removed contributors array to %d for %s - %m",
newsiz, contrib);
if (count_removed_contributors_bm > 0)
memcpy (newarr, removed_contributors_arr_bm,
count_removed_contributors_bm * sizeof (char *));
free (removed_contributors_arr_bm);
removed_contributors_arr_bm = newarr;
size_removed_contributors_bm = newsiz;
};
char *newcontrib = strdup (contrib);
if (!newcontrib)
FATAL_BM ("failed to strdup removed contributor %s - %m", contrib);
removed_contributors_arr_bm[count_removed_contributors_bm++] = newcontrib;
return true;
} /* end remove_contributor_bm */
////////////////////////////////////////////////////////////////
const GOptionEntry optionstab_bm[] = {
//////////////////
/* for the shorter variant like --debug */
#define BISMONPROG_LONG_OPTION(Lopt) Lopt
#define BISMONPROG_SHORT_OPTION(Shopt) Shopt
#include "progoptions_BM.h"
/* for the Bismon variant like --bismon-debug: */
#define BISMONPROG_LONG_OPTION(Lopt) "bismon-" Lopt
#define BISMONPROG_SHORT_OPTION(Shopt) (char)0
#include "progoptions_BM.h"
///
/// end of options
{} //// last entry should be all zeros
}; // end of variable optionstab_bm
///////////////////////////////////////////////////////////////
static void
check_delims_BM (void)
{
int delimcnt = 0;
char *prevdelim = "";
#define HAS_DELIM_BM(Str,Name) do { \
delimcnt++; \
if (strcmp(Str,prevdelim)<=0) \
FATAL_BM("unsorted delimiter#%d '%s' and '%s'", \
delimcnt, Str, prevdelim); \
prevdelim = Str; \
} while(0);
#include "bm_delim.h"
if (delimcnt != BM_NB_DELIM)
FATAL_BM ("expected %d delimiters, got %d", BM_NB_DELIM, delimcnt);
} /* end check_delims_BM */
void
add_new_predefined_bm (void)
{
for (int pix = 0; pix < nb_added_predef_bm; pix++)
{
const char *predname = added_predef_bm[pix].pr_name;
const char *predcomm = added_predef_bm[pix].pr_comment;
if (!validname_BM (predname))
FATAL_BM ("predefined name '%s' invalid", predname);
const objectval_tyBM *predobj = findnamedobj_BM (predname);
if (!predobj)
{
predobj = makeobj_BM ();
registername_BM (predobj, predname);
}
else
{
char idpred[32];
memset (idpred, 0, sizeof (idpred));
idtocbuf32_BM (objid_BM (predobj), idpred);
INFOPRINTF_BM ("existing %s becomes predefined %s\n", idpred,
predname);
};
objtouchnow_BM ((objectval_tyBM *) predobj);
if (predcomm)
objputattr_BM ((objectval_tyBM *) predobj, BMP_comment,
(value_tyBM) makestring_BM (predcomm));
objputspacenum_BM ((objectval_tyBM *) predobj, PredefSp_BM);
char idpred[32];
memset (idpred, 0, sizeof (idpred));
idtocbuf32_BM (objid_BM (predobj), idpred);
if (predcomm)
INFOPRINTF_BM ("made predefined %s (%s) - %s\n", predname, idpred,
predcomm);
else
INFOPRINTF_BM ("made predefined %s (%s)\n", predname, idpred);
}
} /* end add_new_predefined_bm */
static int
idqcmp_BM (const void *p1, const void *p2)
{
return cmpid_BM (*(rawid_tyBM *) p1, *(rawid_tyBM *) p2);
} /* end idqcmp_BM */
static void give_prog_version_BM (const char *progname);
static void do_emit_module_from_main_BM (void);
void
do_emit_module_from_main_BM (void)
{
LOCALFRAME_BM (NULL, /*descr: */ BMP_emit_module,
objectval_tyBM * modulob; //
objectval_tyBM * parsob; //
value_tyBM resultv; //
value_tyBM failres; //
value_tyBM failplace; //
);
WEAKASSERTRET_BM (module_to_emit_bm != NULL);
_.failres = NULL;
_.failplace = NULL;
int failcod = 0;
struct failurelockset_stBM flockset = { };
struct failurehandler_stBM *prevfailurehandle =
(struct failurehandler_stBM *) curfailurehandle_BM;
initialize_failurelockset_BM (&flockset, sizeof (flockset));
LOCAL_FAILURE_HANDLE_BM (&flockset, lab_failureemit, failcod, _.failres,
_.failplace);
if (failcod > 0)
lab_failureemit:{
destroy_failurelockset_BM (&flockset);
curfailurehandle_BM = prevfailurehandle;
{
WARNPRINTF_BM
("Failed to emit module from main %s, with failcode#%d, failres %s\n"
"failplace %s",
objectdbg_BM (_.modulob), failcod,
OUTSTRVALUE_BM (_.failres), OUTSTRVALUE_BM (_.failplace));
return;
};
};
INFOPRINTF_BM ("begin emit module from main: %s\n", module_to_emit_bm);
_.parsob = makeobj_BM ();
bool gotobj = false;
struct parser_stBM *pars =
makeparser_memopen_BM (module_to_emit_bm, strlen (module_to_emit_bm),
_.parsob);
_.modulob = parsergetobject_BM (pars, CURFRAME_BM, 0, &gotobj);
DBGPRINTF_BM ("do_emit_module_from_main_BM modulob=%s parsob=%s",
objectdbg_BM (_.modulob), objectdbg1_BM (_.parsob));
objlock_BM (_.modulob);
_.resultv = send0_BM (_.modulob, BMP_emit_module, CURFRAME_BM);
objunlock_BM (_.modulob);
objclearpayload_BM (_.parsob);
destroy_failurelockset_BM (&flockset);
curfailurehandle_BM = prevfailurehandle;
DBGPRINTF_BM ("do_emit_module_from_main_bm end modulob=%s result %s", //
objectdbg_BM (_.modulob), //
debug_outstr_value_BM (_.resultv, CURFRAME_BM, 0));
if (_.resultv)
INFOPRINTF_BM ("successful emit module from main: %s\n",
module_to_emit_bm);
else
FATAL_BM ("failed emit module from main: %s", module_to_emit_bm);
char modulidbuf[32];
memset (modulidbuf, 0, sizeof (modulidbuf));
idtocbuf32_BM (objid_BM (_.modulob), modulidbuf);
char makemodulecmd[128];
memset (makemodulecmd, 0, sizeof (makemodulecmd));
snprintf (makemodulecmd, sizeof (makemodulecmd),
"%s/build-bismon-module.sh %s", bismon_directory, modulidbuf);
DBGPRINTF_BM ("do_emit_module_from_main_bm makemodulecmd=%s",
makemodulecmd);
fflush (NULL);
int cmdcod = system (makemodulecmd);
if (cmdcod)
FATAL_BM ("failed module making %s (%d)", makemodulecmd, cmdcod);
INFOPRINTF_BM ("successfully compiled emitted module %s (%s) from main\n",
module_to_emit_bm, modulidbuf);
fflush (NULL);
return;
} /* end do_emit_module_from_main_BM */
static void parse_values_after_load_BM (void);
static void add_contributors_after_load_BM (void);
static void remove_contributors_after_load_BM (void);
static void initialize_contributors_path_BM (void);
static void initialize_passwords_path_BM (void);
static void initialize_contact_path_BM (void);
static void parse_contact_BM (void);
static void emit_has_predef_BM (void);
static void do_dump_after_load_BM (void);
static bool is_nice_locale_BM (const char *);
static void check_locale_BM (void);
static void do_actions_after_load_bm (gchar **);
void
check_locale_BM (void)
{
bool explainlocale = false;
char *oldloc = setlocale (LC_ALL, NULL);
char *oldnumloc = setlocale (LC_NUMERIC, NULL);
char *oldctypeloc = setlocale (LC_CTYPE, NULL);
DBGPRINTF_BM ("oldlocale LC_ALL %s LC_NUMERIC %s LC_CTYPE %s", oldloc,
oldnumloc, oldctypeloc);
if (oldloc && !is_nice_locale_BM (oldloc))
{
WARNPRINTF_BM
("your LC_ALL locale '%s' is strange but should be in English, encoded in UTF-8.\n",
oldloc);
explainlocale = true;
}
if (oldnumloc && !is_nice_locale_BM (oldloc))
{
WARNPRINTF_BM
("your LC_NUMERIC locale '%s' is strange but should be in English, encoded in UTF-8, such that ...\n"
" 3.14 (with decimal dot) should be parsed and printable as an approximation of Pi, and\n"
" 6.022e23 should be parsed and printable as an approximation of the Avogadro constant.\n",
oldnumloc);
explainlocale = true;
}
if (oldctypeloc && !is_nice_locale_BM (oldctypeloc))
{
WARNPRINTF_BM
("your LC_CTYPE locale '%s' is strange but should be in English, encoded in UTF-8.\n",
oldnumloc);
explainlocale = true;
}
if (explainlocale)
{
WARNPRINTF_BM
("Bismon requires an English UTF-8 locale, in particular for floating point numbers in the persistent store.\n"
"Read carefully http://man7.org/linux/man-pages/man7/locale.7.html\n"
"You may use the 'locale' and/or 'localectl' programs to check your current locale.\n"
"You may want to set your locale thru several environment variables like LC_ALL LC_NUMERIC LC_CTYPE LANG LANGUAGE etc...\n"
"... using the export or setenv builtin of your shell\n"
"Please read carefully the %s/README.md file (Localization section)\n",
bismon_directory);
}
//
// force the LC_NUMERIC locale to English UTF-8
if (!setlocale (LC_NUMERIC, "C.UTF-8")
&& !setlocale (LC_NUMERIC, "C.utf8")
&& !setlocale (LC_NUMERIC, "POSIX")
&& !setlocale (LC_NUMERIC, "POSIX.utf8")
&& !setlocale (LC_NUMERIC, "POSIX.utf-8")
&& !setlocale (LC_NUMERIC, "en_US.utf8")
&& !setlocale (LC_NUMERIC, "en_US.utf-8")
&& !setlocale (LC_NUMERIC, "en_GB.utf8")
&& !setlocale (LC_NUMERIC, "en_GB.utf-8"))
FATAL_BM
("failed to setlocale LC_NUMERIC appropriately to English UTF-8, previous was %s",
oldnumloc ? : "*unset*");
DBGPRINTF_BM ("now LC_NUMERIC locale is %s", setlocale (LC_NUMERIC, NULL));
//
// force the LC_ALL locale to English UTF-8
if (!setlocale (LC_ALL, "C.UTF-8")
&& !setlocale (LC_ALL, "C.utf8")
&& !setlocale (LC_ALL, "POSIX")
&& !setlocale (LC_ALL, "POSIX.utf8")
&& !setlocale (LC_ALL, "POSIX.utf-8")
&& !setlocale (LC_ALL, "en_US.utf8")
&& !setlocale (LC_ALL, "en_US.utf-8")
&& !setlocale (LC_ALL, "en_GB.utf8")
&& !setlocale (LC_ALL, "en_GB.utf-8"))
FATAL_BM
("failed to setlocale LC_ALL appropriately to English UTF-8, previous was %s",
oldloc ? : "*unset*");
DBGPRINTF_BM ("now LC_ALL locale is %s", setlocale (LC_ALL, NULL));
double x = 0;
int pos = 0;
if (sscanf ("4.5;", "%lf%n", &x, &pos) < 1 || x != 4.5 || pos != 3)
FATAL_BM
("something wrong (probably your locale setting, which should be C.UTF-8)."
" Since '4.5;' is scanned as %f at position#%d", x, pos);
DBGPRINTF_BM ("after sscanf x=%f, pos#%d", x, pos);
char *end = NULL;
x = strtod ("4.5/", &end);
DBGPRINTF_BM ("after strtod x=%f, end=%s", x, end);
if (x != 4.5 || !end || *end != '/')
FATAL_BM
("something wrong (probably your locale setting, which should be C.UTF-8)."
" Since '4.5/' is not converted as %f end at %s", x, end);
x = 0.0;
if (sscanf ("-6.022e23;", "%lf%n", &x, &pos) < 1 || pos != 9
|| fabs (x + 6.022e+23) > 1e20)
FATAL_BM
("something wrong (probably your locale setting, which should be C.UTF-8)."
" Since '-6.022e23;' is scanned as %g at position#%d", x, pos);
} /* end of check_locale_BM */
void
cleanup_temporary_dir_after_exit_BM (void)
{
FILE *fat = popen ("/bin/at now + 15 minutes", "w");
if (!fat)
FATAL_BM ("popen /bin/at now + 15 minutes failed (%m)");
fprintf (fat, "/bin/rm -rf %s", temporary_dir_BM);
fflush (fat);
fclose (fat);
} /* end cleanup_temporary_dir_after_exit_BM */
////////////////////////////////////////////////////////////////
//// see also https://github.com/dtrebbien/GNOME.supp and
//// https://stackoverflow.com/q/16659781/841108 to use valgrind...
int
main (int argc, char **argv)
{
clock_gettime (CLOCK_MONOTONIC, &startrealtimespec_BM);
if (argc <= 0)
{ // this should never happen in practice, but see
// https://stackoverflow.com/q/49817316/841108
fprintf (stderr, "[bismon] requires at least one argument\n");
exit (EXIT_FAILURE);
}
myprogname_BM = argv[0];
if (argc > 1 && (!strcmp (argv[1], "-D") || !strcmp (argv[1], "--debug")))
showdebugmsg_BM = true;
if (argc > 1 && !strcmp (argv[1], "--version"))
give_prog_version_BM (argv[0]);
dlprog_BM = dlopen (NULL, RTLD_NOW | RTLD_GLOBAL);
if (!dlprog_BM)
{
fprintf (stderr, "%s: dlopen for whole program fails %s\n",
argv[0], dlerror ());
exit (EXIT_FAILURE);
}
memset ((char *) myhostname_BM, 0, sizeof (myhostname_BM));
if (gethostname ((char *) myhostname_BM, sizeof (myhostname_BM) - 1))
FATAL_BM ("gethostname failure %m");
INFOPRINTF_BM ("BISMON (%s git %s pid %d) starting on host %s (build %s)\n",
myprogname_BM, bismon_shortgitid, (int) getpid (),
myhostname_BM, bismon_timestamp);
if (access ("/bin/at", X_OK))
FATAL_BM ("BISMON (%s pid %d git %s) requires a /bin/at (%m)",
myprogname_BM, (int) getpid (), bismon_shortgitid);
if (access ("/usr/bin/indent", X_OK))
FATAL_BM ("BISMON (%s pid %d git %s) requires a /usr/bin/indent (%m)",
myprogname_BM, (int) getpid (), bismon_shortgitid);
if (access ("/usr/bin/astyle", X_OK))
FATAL_BM ("BISMON (%s pid %d git %s) requires a /usr/bin/astyle (%m)",
myprogname_BM, (int) getpid (), bismon_shortgitid);
bool skiplocalcheck = false;
{
// check the locale(7), unless using print-contributor-of-oid
for (int ix = 0; ix < argc && !skiplocalcheck; ix++)
if (!strncmp
(argv[ix], "--print-contributor-of-oid",
strlen ("--print-contributor-of-oid")))
skiplocalcheck = true;
}
///
{
double nwt = clocktime_BM (CLOCK_REALTIME);
intptr_t y2kwt = timetoY2Kmillisec_BM (nwt);
ASSERT_BM (fabs (Y2Kmillisectotime_BM (y2kwt) - nwt) < 0.5);
}
backtracestate_BM //
= (volatile struct backstrace_state *)
backtrace_create_state ( /*filename: */ NULL,
/*threaded: */ true,
/*errorcb: */
backtracerrorcb_BM,
/*data: */ NULL);
parse_program_options_BM (argc, argv);
if (randomseed_BM > 0)
{
g_random_set_seed (randomseed_BM);
INFOPRINTF_BM
("set -using g_random_set_seed- the Glib PRNG random seed to %d",
randomseed_BM);
if (getpid () % 2 == 0)
WARNPRINTF_BM ("even pid %d for BISMON", (int) getpid ());
else
INFOPRINTF_BM ("odd pid %d for BISMON", (int) getpid ());
}
if (plugin_before_load_BM)
{
char plugbuf[256];
memset (plugbuf, 0, sizeof (plugbuf));
if (!access (plugin_before_load_BM, X_OK))
dlh_before_load_bm = dlopen (plugin_before_load_BM,
RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);
else
if (snprintf
(plugbuf, sizeof (plugbuf), "%s.so", plugin_before_load_BM) > 0
&& !access (plugbuf, X_OK))
dlh_before_load_bm =
dlopen (plugbuf, RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);
else
if (snprintf
(plugbuf, sizeof (plugbuf), "%s/Plugins/%s.so", bismon_directory,
plugin_before_load_BM) > 0 && !access (plugbuf, X_OK))
dlh_before_load_bm =
dlopen (plugbuf, RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);
else
FATAL_BM
("fail to find --plugin-before-load %s in bismon directory %s",
plugin_before_load_BM, bismon_directory);
if (!dlh_before_load_bm)
FATAL_BM ("failed to dlopen plugin before load %s : %s (%m)",
plugin_before_load_BM, dlerror ());
};
if (!temporary_dir_BM[0])
{
time_t nowt = 0;
time (&nowt);
snprintf (temporary_dir_BM, sizeof (temporary_dir_BM),
"/var/tmp/bismon-p%d-t%ld", (int) getpid (), (long) nowt);
DBGPRINTF_BM ("temporary dir %s", temporary_dir_BM);
if (mkdir (temporary_dir_BM, 0700))
FATAL_BM ("failed to make temporary directory %s", temporary_dir_BM);
INFOPRINTF_BM ("made temporary directory %s", temporary_dir_BM);
atexit (cleanup_temporary_dir_after_exit_BM);
};
{
char temptestpath[384];
memset (temptestpath, 0, sizeof (temptestpath));
snprintf (temptestpath, sizeof (temptestpath),
"%s/__BISMON_TEMPORARY_DIR", temporary_dir_BM);
FILE *fw = fopen (temptestpath, "w");
if (!fw)
FATAL_BM ("failed to write temporary %s (%m)", temptestpath);
fprintf (fw, "# Bismon %s pid %d on %s temporary %s\n",
bismon_gitid, (int) getpid (), myhostname_BM, temptestpath);
fflush (fw);
fsync (fileno (fw));
fclose (fw);
};
if (!skiplocalcheck)
check_locale_BM ();
{
const char *glib_mismatch =
glib_check_version (GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
GLIB_MICRO_VERSION);
if (glib_mismatch)
FATAL_BM ("GLIB version mismatch: %s", glib_mismatch);
};
initialize_garbage_collector_BM ();
check_delims_BM ();
initialize_globals_BM ();
initialize_predefined_objects_BM ();
initialize_predefined_names_BM ();
initialize_agenda_BM ();
////
///
if (showdebugmsg_BM)
fprintf (stderr,
"debug messages enabled %s pid %d timestamp %s commit %s\n",
myprogname_BM, (int) getpid (), bismon_timestamp,
bismon_lastgitcommit);
if (give_version_bm)
give_prog_version_BM (myprogname_BM);