forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_rps.cc
More file actions
1755 lines (1649 loc) · 64.9 KB
/
main_rps.cc
File metadata and controls
1755 lines (1649 loc) · 64.9 KB
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_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the main function and related, program option parsing,
* code.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* 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/>.
*
******************************************************************************/
#include "refpersys.hh"
extern "C" const char rps_main_gitid[];
const char rps_main_gitid[]= RPS_GITID;
extern "C" const char rps_main_date[];
const char rps_main_date[]= __DATE__;
extern "C" const char rps_main_shortgitid[];
const char rps_main_shortgitid[]= RPS_SHORTGITID;
extern "C" char rps_buffer_proc_version[];
char rps_buffer_proc_version[rps_path_byte_size];
#pragma message "Compiling " __FILE__ " on operating system " RPS_OPERSYS " for architecture " RPS_ARCH
#if RPS_HAS_OPERSYS_GNU_Linux
#pragma message "Compiling " __FILE__ " on GNU/Linux"
#endif
#if RPS_HAS_ARCH_86_64
#pragma message "Compiling " __FILE__ " for 64 bits x86"
#endif
struct utsname rps_utsname;
char rps_progexe[rps_path_byte_size];
static std::atomic<std::uint8_t> rps_exit_atomic_code;
extern "C" char*rps_chdir_path_after_load;
char*rps_chdir_path_after_load;
std::vector<Rps_Plugin> rps_plugins_vector;
std::map<std::string,std::string> rps_pluginargs_map;
extern "C" char*rps_pidfile_path;
std::string rps_cpluspluseditor_str;
std::string rps_cplusplusflags_str;
std::string rps_dumpdir_str;
std::vector<std::string> rps_command_vec;
std::string rps_test_repl_string;
char*rps_pidfile_path;
/// the … is unicode U+2026 HORIZONTAL ELLIPSIS in UTF8 \xe2\x80\xA6
extern "C" std::atomic<long> rps_debug_atomic_counter;
std::atomic<long> rps_debug_atomic_counter;
const char* rps_get_proc_version(void)
{
return rps_buffer_proc_version;
} // end rps_get_proc_version
long
rps_incremented_debug_counter(void)
{
return 1+rps_debug_atomic_counter.fetch_add(1);
} // end rps_incremented_debug_counter
long
rps_debug_counter(void)
{
return rps_debug_atomic_counter.load();
} // end rps_debug_counter
static void rps_kill_wait_gui_process(void);
error_t rps_parse1opt (int key, char *arg, struct argp_state *state);
/// Keep the options in alphabetical order of the name
struct argp_option rps_progoptions[] =
{
/* ======= batch ======= */
{/*name:*/ "batch", ///
/*key:*/ RPSPROGOPT_BATCH, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Run in batch mode, that is without any user interface "
"(either graphical or command-line REPL).\n", //
/*group:*/0 ///
},
/* ======= run a REPL command after load ======= */
{/*name:*/ "command", ///
/*key:*/ RPSPROGOPT_COMMAND, ///
/*arg:*/ "REPL_COMMAND", ///
/*flags:*/ 0, ///
/*doc:*/ "Run the given REPL_COMMAND;\n"
"Try the help command for details.\n", //
/*group:*/0 ///
},
/* ======= edit the C++ code of a temporary plugin after load ======= */
{/*name:*/ "cplusplus-editor-after-load", ///
/*key:*/ RPSPROGOPT_CPLUSPLUSEDITOR_AFTER_LOAD, ///
/*arg:*/ "EDITOR", ///
/*flags:*/ 0, ///
/*doc:*/ "prefill some C++ temporary file for plugin code,\n"
" edit it with given EDITOR, then compile it"
" and run its " RPS_PLUGIN_INIT_NAME "(const Rps_Plugin*) function.\n"
" (if none is given, use $EDITOR from environment)\n"
, //
/*group:*/0 ///
},
/* ======= extra compilation flags for C++ code above after load ======= */
{/*name:*/ "cplusplus-flags-after-load", ///
/*key:*/ RPSPROGOPT_CPLUSPLUSFLAGS_AFTER_LOAD, ///
/*arg:*/ "FLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "set to FLAGS the extra compilation flags for the C++ code of the temporary plugin.\n", //
/*group:*/0 ///
},
/* ======= debug flags ======= */
{/*name:*/ "debug", ///
/*key:*/ RPSPROGOPT_DEBUG, ///
/*arg:*/ "DEBUGFLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "To set RefPerSys comma separated debug flags, pass --debug=help to get their list.\n"
" Also from $REFPERSYS_DEBUG environment variable, if provided\n", ///
/*group:*/0 ///
},
/* ======= debug after load flags ======= */
{/*name:*/ "debug-after-load", ///
/*key:*/ RPSPROGOPT_DEBUG_AFTER_LOAD, ///
/*arg:*/ "DEBUGFLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "To set RefPerSys comma separated debug flags after the sucessful load.\n", ///
/*group:*/0 ///
},
/* ======= debug file path ======= */
{/*name:*/ "debug-path", ///
/*key:*/ RPSPROGOPT_DEBUG_PATH, ///
/*arg:*/ "DEBUGFILEPATH", ///
/*flags:*/ 0, ///
/*doc:*/ "Output debug messages into given DEBUGFILEPATH instead of stderr.\n", ///
/*group:*/0 ///
},
/* ======= dump into given directory ======= */
{/*name:*/ "dump", ///
/*key:*/ RPSPROGOPT_DUMP, ///
/*arg:*/ "DUMPDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "Dump the persistent state to given DUMPDIR directory.\n", ///
/*group:*/0 ///
},
/* ======= FLTK GUI library ======= */
{/*name:*/ "fltk", ///
/*key:*/ RPSPROGOPT_FLTK, ///
/*arg:*/ "GUIPREFERENCES", ///
/*flags:*/ OPTION_ARG_OPTIONAL, ///
/*doc:*/ "pass, if GUIPREFERENCES is given,"
" to FLTK graphical library; enable FLTK graphics.\n"
"\t see fltk.org for details\n", ///
/*group:*/0 ///
},
/* ======= extra argument ======= */
{/*name:*/ "extra", ///
/*key:*/ RPSPROGOPT_EXTRA_ARG, ///
/*arg:*/ "EXTRA=ARG", ///
/*flags:*/ 0, ///
/*doc:*/ "To set for RefPerSys a named EXTRA argument to ARG.\n", ///
/*group:*/0 ///
},
/* ======= interface thru some FIFO, relevant for JSONRPC ======= */
{/*name:*/ "interface-fifo", ///
/*key:*/ RPSPROGOPT_INTERFACEFIFO, ///
/*arg:*/ "FIFO", ///
/*flags:*/ 0, ///
/*doc:*/ "use a pair of fifo(7) named FIFO.cmd (written) "
"and FIFO.out (read) for JSONRPC communication between RefPerSys"
" and some graphical user interface. So when RefPerSys is given"
" the program argument --interface-fifo=/tmp/chan, two"
" FIFO channels may be created, and are used: /tmp/chan.out"
" and /tmp/chan.cmd ... The /tmp/chan.out is written"
" by the GUI interface, and read by RefPerSys; the "
"/tmp/chan.cmd is read by the GUI interface, and written by "
"the RefPerSys process.\n"
, //
/*group:*/0 ///
},
/* ======= number of jobs or threads ======= */
{/*name:*/ "jobs", ///
/*key:*/ RPSPROGOPT_JOBS, ///
/*arg:*/ "NBJOBS", ///
/*flags:*/ 0, ///
/*doc:*/ "Run <NBJOBS> threads - default is 5, minimum 3, maximum 24.\n",
// see RPS_NBJOBS_MIN and RPS_NBJOBS_MAX in refpersys.hh and initial value below.
/*group:*/0 ///
},
/* ======= the load directory ======= */
{/*name:*/ "load", ///
/*key:*/ RPSPROGOPT_LOADDIR, ///
/*arg:*/ "LOADDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "loads persistent state from LOADDIR, defaults to the source directory", ///
/*group:*/0 ///
},
/* ======= without ASLR ; perhaps might not work in feb. 2023 ======= */
{/*name:*/ "no-aslr", ///
/*key:*/ RPSPROGOPT_NO_ASLR, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Forcibly disable Adress Space Layout Randomization. Might not work.\n", //
/*group:*/0 ///
},
/* ======= display the full git id ======= */
{/*name:*/ "full-git", ///
/*key:*/ RPSPROGOPT_FULL_GIT, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Output just the full gitid of the binary\n"
" (suffixed by + if locally changed)\n", //
/*group:*/0 ///
},
/* ======= display the short suffixed git id ======= */
{/*name:*/ "short-git", ///
/*key:*/ RPSPROGOPT_SHORT_GIT, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Output just the short gitid of the binary\n"
" (suffixed by + if locally changed)\n", //
/*group:*/0 ///
},
/* ======= without quick tests ======= */
{/*name:*/ "no-quick-tests", ///
/*key:*/ RPSPROGOPT_NO_QUICK_TESTS, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Disable quick tests after load by rps_small_quick_tests_after_load.\n", //
/*group:*/0 ///
},
/* ======= without terminal ======= */
{/*name:*/ "no-terminal", ///
/*key:*/ RPSPROGOPT_NO_TERMINAL, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Forcibly disable terminal ANSI escape codes, even if stdout is a tty.\n", //
/*group:*/0 ///
},
/* ======= dlopen a given plugin file after load ======= */
{/*name:*/ "plugin-after-load", ///
/*key:*/ RPSPROGOPT_PLUGIN_AFTER_LOAD, ///
/*arg:*/ "PLUGIN", ///
/*flags:*/ 0, ///
/*doc:*/ "dlopen(3) after load the given PLUGIN "
"(some *.so ELF shared object)"
" and run its " RPS_PLUGIN_INIT_NAME "(const Rps_Plugin*) function.\n", //
/*group:*/0 ///
},
/* ======= string argument to a previously given plugin file after load ======= */
{/*name:*/ "plugin-arg", ///
/*key:*/ RPSPROGOPT_PLUGIN_ARG, ///
/*arg:*/ "PLUGIN_NAME:PLUGIN_ARG", ///
/*flags:*/ 0, ///
/*doc:*/ "pass to the loaded plugin <PLUGIN_NAME> the string <PLUGIN_ARG> "
"(notice the colon separating them).\n", //
/*group:*/0 ///
},
/* ====== after loading heap & plugins, show help about preferences
===== */
{/*name:*/ "preferences-help", ///
/*key:*/ RPSPROGOPT_PREFERENCES_HELP, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "After loading heap and plugins, show help \n"
"about user preferences (given in the preferences file)\n"
, //
/*group:*/0 ///
},
/* ====== publish some data to a remote URL and Web service which
might make some statistics about RefPerSys ===== */
{/*name:*/ "publish-me", ///
/*key:*/ RPSPROGOPT_PUBLISH_ME, ///
/*arg:*/ "URL", ///
/*flags:*/ 0, ///
/*doc:*/ "Send to the given URL the build timestamp and builder.\n"
" See rps_publish_me function in curl_rps.cc source file.\n"
, //
/*group:*/0 ///
},
/* ======= random oids ======= */
{/*name:*/ "random-oid", ///
/*key:*/ RPSPROGOPT_RANDOMOID, ///
/*arg:*/ "NBOIDS", ///
/*flags:*/ 0, ///
/*doc:*/ "Print NBOIDS random object identifiers.\n",
/*group:*/0 ///
},
/* ======= the RefPerSys home directory ======= */
{/*name:*/ "refpersys-home", ///
/*key:*/ RPSPROGOPT_HOMEDIR, ///
/*arg:*/ "HOMEDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "Set the RefPerSys homedir, default to "
"$REFPERSYS_HOME or $HOME\n", ///
/*group:*/0 ///
},
/* ======= change current directory before loading ======= */
{/*name:*/ "chdir-before-load", ///
/*key:*/ RPSPROGOPT_CHDIR_BEFORE_LOAD, ///
/*arg:*/ "DIRECTORY", ///
/*flags:*/ 0, ///
/*doc:*/ "change directory before loading to $DIRECTORY\n", ///
/*group:*/0 ///
},
/* ======= change current directory after loading ======= */
{/*name:*/ "chdir-after-load", ///
/*key:*/ RPSPROGOPT_CHDIR_AFTER_LOAD, ///
/*arg:*/ "DIRECTORY", ///
/*flags:*/ 0, ///
/*doc:*/ "change directory after loading to $DIRECTORY\n", ///
/*group:*/0 ///
},
/* ======= Run RefPerSys for a limited time ======= */
{/*name:*/ "run-delay", ///
/*key:*/ RPSPROGOPT_RUN_DELAY, ///
/*arg:*/ "RUNDELAY", ///
/*flags:*/ 0, ///
/*doc:*/ "Run RefPerSys agenda and event loop for a limited real time,\n"
" e.g. --run-delay=50s or --run-delay=2m or --run-delay=5h\n\n", ///
/*group:*/0 ///
},
/* ======= run a shell command with system(3) after load ======= */
{/*name:*/ "run-after-load", ///
/*key:*/ RPSPROGOPT_RUN_AFTER_LOAD, ///
/*arg:*/ "SHELL_COMMAND", ///
/*flags:*/ 0, ///
/*doc:*/ "Run using system(3) the given shell SHELL_COMMAND after load and plugins;\n" //
" The following environment variables have been set:\n" //
"\t * $REFPERSYS_GITID to the git id (with a + suffix if locally changed);\n" //
"\t * $REFPERSYS_TOPDIR to the top directory with source code and persistore/ ...;\n" //
"\t * $REFPERSYS_PID to the process id running the refpersys executable;\n" //
"\t * $REFPERSYS_USER_OID to the objectid corresponding to current user;\n" //
"\n\n",
/*group:*/0 ///
},
/* ======= syslog-ing ======= */
{/*name:*/ "syslog", ///
/*key:*/ RPSPROGOPT_SYSLOG, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Use system log with syslog(3) ...\n", //
/*group:*/0 ///
},
/* ======= naming the run ======= */
{/*name:*/ "run-name", ///
/*key:*/ RPSPROGOPT_RUN_NAME, ///
/*arg:*/ "RUN_NAME", ///
/*flags:*/ 0, ///
/*doc:*/ "Set the name of this run to given RUN_NAME ...\n", //
/*group:*/0 ///
},
/* ======= showing some message ======= */
{/*name:*/ "echo", ///
/*key:*/ RPSPROGOPT_ECHO, ///
/*arg:*/ "MESSAGE", ///
/*flags:*/ 0, ///
/*doc:*/ "Show the given MESSAGE when parsing program argument ...\n", //
/*group:*/0 ///
},
/* ======= daemoning ======= */
{/*name:*/ "daemon", ///
/*key:*/ RPSPROGOPT_DAEMON, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Use daemon(3) ...\n", //
/*group:*/0 ///
},
/* ======= test the read-eval-print lexer ======== */
{/*name:*/ "test-repl-lexer", ///
/*key:*/ RPSPROGOPT_TEST_REPL_LEXER, ///
/*arg:*/ "TESTLEXSTRING", ///
/*flags:*/ 0, ///
/*doc:*/ "Test the read-eval-print-loop lexer on given TESTLEXSTRING."
" (this option might become obsolete).\n", //
/*group:*/0 ///
},
/* ======= type information ======= */
{/*name:*/ "type-info", ///
/*key:*/ RPSPROGOPT_TYPEINFO, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Show type information (and test tagged integers).\n" //
" (using rps_print_types_info from utilities_rps.cc)\n",
/*group:*/0 ///
},
/* ======= pid-file ======= */
{/*name:*/ "pid-file", ///
/*key:*/ RPSPROGOPT_PID_FILE, ///
/*arg:*/ "PID_FILE", ///
/*flags:*/ 0, ///
/*doc:*/ "Write the pid of the running process into given PID_FILE.\n", //
/*group:*/0 ///
},
/* ======= user preferences ======= */
{/*name:*/ "user-pref", ///
/*key:*/ RPSPROGOPT_USER_PREFERENCES, ///
/*arg:*/ "USER_PREF", ///
/*flags:*/ 0, ///
/*doc:*/ "Set the user preferences to given\n"
"USER_PREF file; Lines there starting with # are comments.\n"
"Lines before the first *REFPERSYS_USER_PREFERENCES are ignored.\n"
"\t So they could be some shell script....\n"
"See also --preferences-help option.\n"
"The format is en.wikipedia.org/wiki/INI_file with named values...\n"
"The preferences file has sections starting\n"
"with [secname]. Others are <name>=<value>, e.g.\n"
" color='black' or height=345 ...\n"
"\nDefault preference file is"
" $HOME/" REFPERSYS_DEFAULT_PREFERENCE_PATH "\n"
, //
/*group:*/0 ///
},
/* ======= version info ======= */
{/*name:*/ "version", ///
/*key:*/ RPSPROGOPT_VERSION, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Show version information, then exit.\n", //
/*group:*/0 ///
},
/* ======= terminating empty option ======= */
{/*name:*/(const char*)0, ///
/*key:*/0, ///
/*arg:*/(const char*)0, ///
/*flags:*/0, ///
/*doc:*/(const char*)0, ///
/*group:*/0 ///
}
};
struct backtrace_state* rps_backtrace_common_state;
const char* rps_progname;
int rps_argc;
char** rps_argv;
char* rps_program_invocation;
char* rps_run_command_after_load = nullptr;
char* rps_debugflags_after_load = nullptr;
std::vector<std::function<void(Rps_CallFrame*)>> rps_do_after_load_vect;
void* rps_proghdl = nullptr;
bool rps_batch = false;
bool rps_disable_aslr = false;
bool rps_without_terminal_escape = false;
bool rps_daemonized = false;
bool rps_without_quick_tests = false;
bool rps_test_repl_lexer = false;
bool rps_syslog_enabled = false;
bool rps_stdout_istty = false;
bool rps_stderr_istty = false;
std::atomic<unsigned> rps_debug_flags;
FILE* rps_debug_file;
pid_t rps_gui_pid;
thread_local Rps_Random Rps_Random::_rand_thr_;
typedef std::function<void(void)> rps_todo_func_t;
static std::vector<rps_todo_func_t> rps_main_todo_vect;
std::string rps_my_load_dir;
unsigned rps_call_frame_depth(const Rps_CallFrame*callframe)
{
if (callframe==nullptr) return 0;
else
return callframe->call_frame_depth();
} // end rps_call_frame_depth
void
rps_set_exit_code(std::uint8_t ex)
{
rps_exit_atomic_code.store(ex);
} // end rps_set_exit_code
int rps_nbjobs = RPS_NBJOBS_MIN + 2;
/// the rps_run_loaded_application is called after loading...
void
rps_run_loaded_application(int &argc, char **argv)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED, //
/*callerframe:*/RPS_NULL_CALL_FRAME,
Rps_ObjectRef tempob;
);
{
char cwdbuf[128];
memset (cwdbuf, 0, sizeof(cwdbuf));
if (!getcwd(cwdbuf, sizeof(cwdbuf)-1))
RPS_FATALOUT("rps_run_loaded_application failed to getcwd " << strerror(errno)
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application"));
RPS_INFORM("rps_run_loaded_application: start of %s (with %d args)\n"
".. gitid %s version %d.%d\n"
".. build timestamp %s\n"
".. last git commit %s\n"
".. md5sum %s\n"
".. in %s\n"
".. on host %s pid %d\n",
argv[0], argc, rps_gitid, rps_get_major_version(), rps_get_minor_version(),
rps_timestamp,
rps_lastgitcommit,
rps_md5sum,
cwdbuf,
rps_hostname(), (int)getpid());
}
/// if told, enable extra debugging after load
if (rps_debugflags_after_load)
{
rps_add_debug_cstr(rps_debugflags_after_load);
RPS_INFORMOUT("did set after load "
<< " of RefPerSys process " << (int)getpid() << std::endl
<< "… on " << rps_hostname()
<< " shortgit " << rps_shortgitid << " version " << rps_get_major_version() << "." << rps_get_minor_version()
<< " debug to "
<< Rps_Do_Output([&](std::ostream& out)
{
rps_output_debug_flags(out);
}));
}
////
if (rps_without_quick_tests)
{
RPS_INFORM("rps_run_loaded_application dont run quick tests after load");
}
else
{
RPS_DEBUG_LOG(LOWREP, "rps_run_loaded_application before running rps_small_quick_tests_after_load from "
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application/quick-tests")
<< std::endl
<< " with call frame:" << std::endl
<< Rps_ShowCallFrame(&_));
rps_small_quick_tests_after_load();
RPS_DEBUG_LOG(LOWREP, "rps_run_loaded_application after running rps_small_quick_tests_after_load");
};
/// create the fifos if a prefix is given with
if (!rps_get_fifo_prefix().empty())
{
RPS_DEBUG_LOG(REPL, "rps_run_loaded_application create fifo with prefix "
<< rps_get_fifo_prefix());
rps_do_create_fifos_from_prefix();
}
RPS_DEBUG_LOG(REPL, "rps_run_loaded_application after load & fifos"
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application"));
//// running the given Unix command after load
if (rps_run_command_after_load)
{
#warning TODO: this needs a code review
if (rps_get_fifo_prefix().empty())
RPS_INFORM("before running command '%s' after load with environment variables...\n"
" REFPERSYS_PID=%ld, REFPERSYS_GITID=%s, REFPERSYS_TOPDIR=%s",
rps_run_command_after_load, (long)getpid(), rps_gitid, rps_topdirectory);
else
RPS_INFORM("before running command '%s' after load with environment variables...\n"
"… REFPERSYS_PID=%ld, REFPERSYS_GITID=%s,\n"
"… REFPERSYS_TOPDIR=%s, REFPERSYS_FIFO_PREFIX=%s",
rps_run_command_after_load, (long)getpid(), rps_gitid,
rps_topdirectory, rps_get_fifo_prefix().c_str());
fflush(nullptr); /// needed before system
int nok = system(rps_run_command_after_load);
if (nok)
RPS_FATAL("failed to run command '%s' after load (status #%d)",
rps_run_command_after_load, nok);
else
RPS_INFORM("after successfully running command '%s' after load", rps_run_command_after_load);
}
else if (!rps_get_fifo_prefix().empty())
{
RPS_INFORM("before running default GUI command '%s' after load"
" with environment variables...\n"
"… REFPERSYS_PID=%ld, REFPERSYS_GITID=%s,\n"
" ... REFPERSYS_TOPDIR=%s, REFPERSYS_FIFO_PREFIX=%s",
rps_gui_script_executable, (long)getpid(), rps_gitid,
rps_topdirectory, rps_get_fifo_prefix().c_str());
std::string fifo_cmd_path, fifo_out_path;
fifo_cmd_path = rps_get_fifo_prefix() + ".cmd";
fifo_out_path = rps_get_fifo_prefix() + ".out";
/* create the fifo if they dont exist */
if (!rps_is_fifo(fifo_cmd_path) || !rps_is_fifo(fifo_out_path))
rps_do_create_fifos_from_prefix();
fflush(nullptr);
pid_t guipid = fork();
if (guipid < 0)
RPS_FATALOUT("failed to fork for running the GUI script"
<< rps_gui_script_executable);
if (guipid == 0)
{
// child process
// close many file desriptors
for (int fd=3; fd<256; fd++)
close(fd);
close(STDIN_FILENO);
int nullfd = open("/dev/null", O_RDONLY);
if (nullfd>0) //unlikely
dup2(nullfd, STDIN_FILENO);
execl(rps_gui_script_executable, rps_gui_script_executable,
rps_get_fifo_prefix().c_str(), nullptr);
perror(rps_gui_script_executable);
_exit(126);
return;
};
rps_gui_pid = guipid;
atexit(rps_kill_wait_gui_process);
}
//// if told, run an editor for C++ code
if (!rps_cpluspluseditor_str.empty() || !rps_cplusplusflags_str.empty())
{
RPS_INFORMOUT("rps_run_loaded_application should edit user C++ code with editor='"
<< rps_cpluspluseditor_str << "' and compile flags '" << rps_cplusplusflags_str << "'"
<< std::endl
<< " with call frame " << Rps_ShowCallFrame(&_));
rps_edit_run_cplusplus_code (&_);
}
//// initialize the FLTK windows in --fltk mode
if (rps_fltk_enabled ())
{
RPS_DEBUG_LOG(REPL, "rps_run_loaded_application initializing FLTK");
rps_fltk_initialize (argc, argv);
};
//// running the given plugins after load - should happen after
//// edition of C++ code
if (!rps_plugins_vector.empty())
{
int pluginix = 0;
std::string curplugname;
try
{
for (auto& curplugin : rps_plugins_vector)
{
curplugname = curplugin.plugin_name;
void* dopluginad = dlsym(curplugin.plugin_dlh, RPS_PLUGIN_INIT_NAME);
if (!dopluginad)
RPS_FATALOUT("cannot find symbol " RPS_PLUGIN_INIT_NAME " in plugin " << curplugname << ":" << dlerror());
rps_plugin_init_sig_t* pluginit = reinterpret_cast<rps_plugin_init_sig_t*>(dopluginad);
(*pluginit)(&curplugin);
RPS_INFORMOUT("rps_run_loaded_application initialized plugin#" << pluginix << " " << curplugname);
curplugname.erase();
pluginix ++;
};
}
catch (std::exception& exc)
{
RPS_WARNOUT("rps_run_loaded_application failed to run plugin #" << pluginix
<< rps_plugins_vector[pluginix].plugin_name
<< " got exception " << exc.what()
);
}
};
/////
///// testing the REPL lexer
if (!rps_test_repl_string.empty())
{
RPS_DEBUG_LOG(REPL, "running test repl string "
<< rps_test_repl_string);
try
{
rps_run_test_repl_lexer(rps_test_repl_string);
RPS_INFORMOUT("successfully done rps_run_test_repl_lexer on "
<< Rps_Cjson_String(rps_test_repl_string));
}
catch (std::exception& exc)
{
RPS_WARNOUT("rps_run_test_repl_lexer in rps_run_loaded_application failed on "
<< Rps_Cjson_String(rps_test_repl_string)
<< " with exception " << exc.what()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application/testrepl"));
return;
};
}
/////
#if RPS_USE_CURL
/// publish using web techniques information about this process
/// see bugs.gentoo.org/939581,
if (!rps_publisher_url_str.empty())
rps_curl_publish_me(rps_publisher_url_str.c_str());
#endif /*RPS_USE_CURL*/
//// command vectors
if (!rps_command_vec.empty())
{
RPS_INFORMOUT("before running " << rps_command_vec.size() << " command[s]");
int nbcmd = (int)rps_command_vec.size();
try
{
rps_do_repl_commands_vec(rps_command_vec);
RPS_INFORMOUT("after running successfully "
<< nbcmd << " commands");
}
catch (std::exception& exc)
{
RPS_WARNOUT("rps_run_loaded_application got exception "
<< exc.what()
<< " when running " << nbcmd << " commands:"
<< Rps_Do_Output([&](std::ostream& out)
{
for (int cix=0; cix<nbcmd; cix++)
{
out << std::endl << rps_command_vec[cix];
}
})
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application/exc"));
};
}
if (access(rps_gui_script_executable, X_OK))
RPS_WARNOUT("default GUI script " << rps_gui_script_executable << " is not executable");
////
////
////
if (!rps_get_fifo_prefix().empty())
{
#pragma message "main_rps.cc with RPSJSONRPC:" __DATE__ "@" __TIME__
RPS_INFORMOUT("initialize JSONRPC with rps_fifo_prefix:" << rps_get_fifo_prefix() << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application JSONRPC"));
rps_jsonrpc_initialize();
};
RPS_DEBUG_LOG(REPL, "rps_run_loaded_application ended in thread " << rps_current_pthread_name()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_run_loaded_application ending"));
} // end rps_run_loaded_application
static long
rps_fill_cplusplus_temporary_code(Rps_CallFrame*callerframe, Rps_ObjectRef tempobarg, int tcnt, const char*tempcppfilename)
{
long tfilsiz = -1;
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED, //
/*callerframe:*/callerframe,
Rps_ObjectRef tempob;
);
_f.tempob = tempobarg;
RPS_DEBUG_LOG(CMD, "rps_fill_cplusplus_temporary_code tempob=" << _f.tempob
<< " tempcppfilename=" << tempcppfilename
<< " from " << std::endl
<< Rps_ShowCallFrame(&_));
FILE* tfil = fopen(tempcppfilename, "w");
fprintf (tfil, "//// temporary [plugin] file %s for RefPerSys\n", tempcppfilename);
fprintf (tfil, "//// see refpersys.org website\n");
fprintf (tfil, "//// passed to commit %s\n", rps_lastgitcommit);
fprintf (tfil, "//// rps_shortgitid %s\n", rps_shortgitid);
fprintf (tfil, "//// rps_md5sum %s\n", rps_md5sum);
fprintf (tfil, "//// rps_timestamp %s\n", rps_timestamp);
fprintf (tfil, "//// GPLv3+ licensed - see /www.gnu.org/licenses/quick-guide-gplv3.en.html\n");
fprintf (tfil, "\n\n#" "include \"refpersys.hh\"\n\n");
fprintf (tfil, "\n" "void rps_do_plugin(const Rps_Plugin*plugin)\n{\n");
fprintf (tfil,
" RPS_LOCALFRAME(/*descr:*/\n"
" Rps_ObjectRef::find_object_by_string(rps_edit_cplusplus_callframe,\n"
" std::string{\"%s\"},\n"
" Rps_ObjectRef::Rps_Fail_If_Not_Found),\n"
" /*callerframe:*/rps_edit_cplusplus_callframe,\n"
" /***** your locals here ******/\n"
" );\n",
_f.tempob->oid().to_string().c_str());
fprintf (tfil, " RPS_ASSERT(plugin != nullptr);\n");
fprintf (tfil, " RPS_DEBUG_LOG(CMD, \"start plugin \"\n"
" << plugin->plugin_name << \" from \" << std::endl\n");
fprintf (tfil, " << RPS_FULL_BACKTRACE_HERE(1, \"temporary C++ plugin\"));\n");
fprintf (tfil, "#warning temporary incomplete %s\n", tempcppfilename);
fprintf (tfil, //
" RPS_INFORMOUT(\"did run temporary plugin \" << plugin->plugin_name\n"
" << \" from pid \" << (int)getpid()\n"
" << \" on \" << rps_hostname() << \" orig.git %s\"\n"
" << std::endl\n"
" << RPS_FULL_BACKTRACE_HERE(1, \"temporary %s#%d\"));\n",
rps_shortgitid, _f.tempob->oid().to_string().c_str(), tcnt);
fprintf (tfil, "} // end rps_do_plugin in %s\n", tempcppfilename);
fprintf (tfil, "\n\n\n");
fprintf (tfil,
"/*********\n" //
" ** for Emacs...\n" //
" ** Local-Variables: ;;\n" //
" ** compile-command: \"cd %s; %s %s /tmp/rpsplug_%s.so\" ;;",
rps_topdirectory, rps_plugin_builder, tempcppfilename, _f.tempob->oid().to_string().c_str());
fprintf (tfil, //
" ** End: ;;\n" //
" ********/\n");
fprintf (tfil, "\n\n\n // ********* eof %s *********\n", tempcppfilename);
fflush (tfil);
tfilsiz = ftell(tfil);
RPS_INFORMOUT("filled temporary plugin " << tempcppfilename << " with " << tfilsiz << " bytes from pid " << (int)getpid() << " git " << rps_shortgitid
<< std::endl << " using object " << _f.tempob << " count " << tcnt);
return tfilsiz;
} // end rps_fill_cplusplus_temporary_code
Rps_CallFrame*rps_edit_cplusplus_callframe;
void
rps_edit_run_cplusplus_code (Rps_CallFrame*callerframe)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callerframe,
Rps_ObjectRef tempob;
);
static int tcnt;
tcnt++;
double cpustartim = rps_process_cpu_time();
double realstartim = rps_wallclock_real_time();
//RPS_ASSERT(callerframe && callerframe->is_good_call_frame());
RPS_ASSERT_CALLFRAME (callerframe);
RPS_ASSERT(rps_is_main_thread());
rps_edit_cplusplus_callframe = &_;
char tempfilprefix[80];
memset (tempfilprefix, 0, sizeof(tempfilprefix));
_f.tempob =
Rps_ObjectRef::make_object(&_,
RPS_ROOT_OB(_3HIxVgAGg5303g7AZs), //temporary_cplusplus_code∈class
nullptr);
snprintf (tempfilprefix, sizeof(tempfilprefix), "/var/tmp/rpscpp_%s-r%u-p%u",
_f.tempob->oid().to_string().c_str(), (unsigned) Rps_Random::random_32u(),
(unsigned) getpid());
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code tempfilprefix=" << tempfilprefix
<< " tempob=" << _f.tempob);
RPS_ASSERT(strlen(tempfilprefix) < sizeof(tempfilprefix)-6);
char tempcppfilename [96];
memset (tempcppfilename, 0, sizeof(tempcppfilename));
strcpy(tempcppfilename, tempfilprefix);
strcat (tempcppfilename, ".cc");
RPS_ASSERT(strlen(tempcppfilename) < sizeof(tempcppfilename));
char tempsofilename [96];
memset (tempsofilename, 0, sizeof(tempcppfilename));
strcpy(tempsofilename, tempfilprefix);
strcat (tempsofilename, ".so");
RPS_ASSERT(strlen(tempsofilename) < sizeof(tempsofilename)-6);
(void) remove (tempsofilename);
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code tempob=" << _f.tempob
<< " tempcppfilename=" << tempcppfilename
<< " tempsofilename=" << tempsofilename
<< " from " << std::endl
<< Rps_ShowCallFrame(&_));
long tfilsiz = -1;
//// fill once the temporary file
tfilsiz = rps_fill_cplusplus_temporary_code(&_, _f.tempob, tcnt, tempcppfilename);
if (rps_cpluspluseditor_str.empty())
{
const char*editorenv = getenv("EDITOR");
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code "
<< (editorenv?"EDITOR=":"no $EDITOR")
<< (editorenv?editorenv:" in environment"));
if (!editorenv && !access("/usr/bin/editor", X_OK))
editorenv = "/usr/bin/editor";
RPS_DEBUG_LOG(CMD, "using " << editorenv << " to edit C++ code from "
<< Rps_ShowCallFrame(&_));
errno = 0;
if (access(editorenv, X_OK))
RPS_FATALOUT("rps_edit_run_cplusplus_code without any editor " << editorenv << ":"
<< strerror(errno)
<< " - from "
<< Rps_ShowCallFrame(&_)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_edit_run_cplusplus_code *no-editor*"));
rps_cpluspluseditor_str.assign(editorenv);
}
bool cppcompilegood = false;
while (!cppcompilegood)
{
std::ostringstream cmdout;
cmdout << rps_cpluspluseditor_str << " " << tempcppfilename;
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code before running " << cmdout.str());
int cmdbad = system(cmdout.str().c_str());
if (cmdbad != 0)
{
RPS_FATALOUT("rps_edit_run_cplusplus_code failed to edit with " << cmdout.str()
<< " which exited " << cmdbad);
};
struct stat tempstat;
memset (&tempstat, 0, sizeof(tempstat));
if (stat(tempcppfilename, &tempstat))
RPS_FATALOUT("rps_edit_run_cplusplus_code failed to stat file " << tempcppfilename << ":" << strerror(errno)
<< std::endl
<< " - from "
<< Rps_ShowCallFrame(&_));
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code tempcppfilename=" << tempcppfilename
<< " with " << tempstat.st_size << " bytes");
if ((long)tempstat.st_size == (long)tfilsiz)
RPS_WARNOUT("rps_edit_run_cplusplus_code unchanged size " << tfilsiz << " of temporary C++ file " << tempcppfilename
<< std::endl
<< " - from "
<< Rps_ShowCallFrame(&_)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1,
"rps_edit_run_cplusplus_code *unchangedsize*"));
RPS_INFORMOUT("rps_edit_run_cplusplus_code should compile C++ code in " << tempcppfilename
<< std::endl
<< " - from "
<< RPS_FULL_BACKTRACE_HERE(1,
"rps_edit_run_cplusplus_code")
<< std::endl);
std::string cwdpath;
bool needchdir = false;
{
// not very good, but in practice good enough before bootstrapping
// see https://softwareengineering.stackexchange.com/q/289427/40065
char cwdbuf[rps_path_byte_size];
memset(cwdbuf, 0, sizeof(cwdbuf));
if (!getcwd(cwdbuf, sizeof(cwdbuf)-1))
RPS_FATAL("rps_edit_run_cplusplus_code getcwd failed: %m");
cwdpath = std::string(cwdbuf);
}
needchdir = cwdpath != std::string{};
//// our compilation command is...
std::string buildplugincmd;
buildplugincmd.reserve(140);
if (needchdir)
{
buildplugincmd += "cd '";
buildplugincmd += Rps_QuotedC_String(rps_topdirectory);
buildplugincmd += "' && ";
};
buildplugincmd += rps_gnu_make;
buildplugincmd += " one-plugin ";
buildplugincmd += "REFPERSYS_PLUGIN_SOURCE='";
buildplugincmd += Rps_QuotedC_String(tempcppfilename);
buildplugincmd += "' ";
buildplugincmd += "REFPERSYS_PLUGIN_SHARED_OBJECT='";
buildplugincmd += Rps_QuotedC_String(tempsofilename);
buildplugincmd += "'\n";
errno= 0;
if (needchdir)
{
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code before chdir to " << rps_topdirectory
<< " from " << cwdpath << " for C++ file " << tempcppfilename);
if (chdir(rps_topdirectory))
RPS_FATALOUT("rps_edit_run_cplusplus_code failed to chdir to " << rps_topdirectory
<< ":" << strerror(errno));
}
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code buildplugincmd: " << buildplugincmd);
errno = 0;
RPS_INFORMOUT("building temporary plugin with " << buildplugincmd << " from pid:" << (int)getpid());
fflush(nullptr);
int buildres = system(buildplugincmd.c_str());
if (buildres != 0)
{
RPS_WARNOUT("rps_edit_run_cplusplus_code build command " << buildplugincmd
<< " failed -> " << buildres
<< " - from "
<< Rps_ShowCallFrame(&_)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_edit_run_cplusplus_code build failure"));
cppcompilegood = false;
}
else
cppcompilegood = true;
if (needchdir)
{
RPS_DEBUG_LOG(CMD, "rps_edit_run_cplusplus_code before chdir to " << cwdpath << " for C++ file " << tempcppfilename);
if (chdir(cwdpath.c_str()))