forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.c
1966 lines (1855 loc) · 58.8 KB
/
system.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <sys/wait.h>
#include <syslog.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef forLinux
#include <bsd/stdlib.h>
#include <bsd/string.h>
#endif
#include "citrine.h"
#include "siphash.h"
// call this function to start a nanosecond-resolution timer
struct timespec timer_start(){
struct timespec start_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
return start_time;
}
// call this function to end a timer, returning nanoseconds elapsed as a long
long timer_end(struct timespec start_time){
struct timespec end_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
long diffInNanos = (end_time.tv_sec - start_time.tv_sec) * (long)1e9 + (end_time.tv_nsec - start_time.tv_nsec);
return diffInNanos;
}
int CtrTimerStartEnd = 0;
struct timespec vartime;
long time_elapsed_nanos;
/**
* @internal
* GarbageCollector Marker
*/
void ctr_gc_mark(ctr_object* object) {
ctr_object* el;
ctr_mapitem* item;
ctr_object* o;
ctr_object* k;
long i;
if (object->info.type == CTR_OBJECT_TYPE_OTARRAY) {
for (i = 0; i < object->value.avalue->head; i++) {
el = *(object->value.avalue->elements+i);
el->info.mark = 1;
ctr_gc_mark(el);
}
}
item = object->properties->head;
while(item) {
k = item->key;
o = item->value;
o->info.mark = 1;
k->info.mark = 1;
ctr_gc_mark(o);
item = item->next;
}
item = object->methods->head;
while(item) {
o = item->value;
k = item->key;
o->info.mark = 1;
k->info.mark = 1;
ctr_gc_mark(o);
item = item->next;
}
}
/**
* @internal
* GarbageCollector Sweeper
*/
void ctr_gc_sweep( int all ) {
ctr_object* previousObject = NULL;
ctr_object* currentObject = ctr_first_object;
ctr_object* nextObject = NULL;
ctr_mapitem* mapItem = NULL;
ctr_mapitem* tmp = NULL;
while(currentObject) {
ctr_gc_object_counter ++;
if ( ( currentObject->info.mark==0 && currentObject->info.sticky==0 ) || all){
ctr_gc_dust_counter ++;
/* remove from linked list */
if (previousObject) {
if (currentObject->gnext) {
previousObject->gnext = currentObject->gnext;
nextObject = currentObject->gnext;
} else {
previousObject->gnext = NULL;
nextObject = NULL;
}
} else {
if (currentObject->gnext) {
ctr_first_object = currentObject->gnext;
nextObject = currentObject->gnext;
} else {
ctr_first_object = NULL;
nextObject = NULL;
}
}
if (currentObject->methods->head) {
mapItem = currentObject->methods->head;
while(mapItem) {
tmp = mapItem->next;
ctr_heap_free( mapItem );
mapItem = tmp;
}
}
if (currentObject->properties->head) {
mapItem = currentObject->properties->head;
while(mapItem) {
tmp = mapItem->next;
ctr_heap_free( mapItem );
mapItem = tmp;
}
}
ctr_heap_free( currentObject->methods );
ctr_heap_free( currentObject->properties );
switch (currentObject->info.type) {
case CTR_OBJECT_TYPE_OTSTRING:
if (currentObject->value.svalue != NULL) {
if (currentObject->value.svalue->vlen > 0) {
ctr_heap_free( currentObject->value.svalue->value );
}
ctr_heap_free( currentObject->value.svalue );
}
break;
case CTR_OBJECT_TYPE_OTARRAY:
ctr_heap_free( currentObject->value.avalue->elements );
ctr_heap_free( currentObject->value.avalue );
break;
case CTR_OBJECT_TYPE_OTEX:
if (currentObject->value.rvalue != NULL) ctr_heap_free( currentObject->value.rvalue );
break;
}
ctr_heap_free( currentObject );
currentObject = nextObject;
} else {
ctr_gc_kept_counter ++;
if (currentObject->info.sticky==1) ctr_gc_sticky_counter++;
if (currentObject->info.mark == 1) {
currentObject->info.mark = 0;
}
previousObject = currentObject;
currentObject = currentObject->gnext;
}
}
}
/**
* @internal
* Garbage Collector sweep.
*/
void ctr_gc_internal_collect() {
ctr_object* context;
int oldcid;
ctr_gc_dust_counter = 0;
ctr_gc_object_counter = 0;
ctr_gc_kept_counter = 0;
ctr_gc_sticky_counter = 0;
context = ctr_contexts[ctr_context_id];
oldcid = ctr_context_id;
while(ctr_context_id > -1) {
ctr_gc_mark(context);
ctr_context_id--;
context = ctr_contexts[ctr_context_id];
}
ctr_gc_sweep( 0 );
ctr_context_id = oldcid;
}
/**
* Broom
*
* GarbageCollector, to invoke use:
*
* [Broom] sweep.
*/
ctr_object* ctr_gc_collect (ctr_object* myself, ctr_argument* argumentList) {
ctr_gc_internal_collect(); /* calls internal because automatic GC has to use this function as well and requires low overhead. */
return myself;
}
/**
* [Broom] dust
*
* Returns the number of objects collected.
*/
ctr_object* ctr_gc_dust(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_dust_counter);
}
/**
* [Broom] objectCount
*
* Returns the total number of objects considered in the latest collect
* cycle.
*/
ctr_object* ctr_gc_object_count(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_object_counter);
}
/**
* [Broom] keptCount
*
* Returns the total number of objects that have been marked during the
* latest cycle and have therefore been allowed to stay in memory.
*/
ctr_object* ctr_gc_kept_count(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_kept_counter);
}
/**
* [Broom] keptAlloc
*
* Returns the amount of allocated memory.
*/
ctr_object* ctr_gc_kept_alloc(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_alloc);
}
/**
* [Broom] stickyCount
*
* Returns the total number of objects that have a sticky flag.
* These objects will never be removed.
*/
ctr_object* ctr_gc_sticky_count(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_sticky_counter);
}
/**
* [Broom] memoryLimit
*
* Sets the memory limit, if this limit gets exceeded the program will produce
* an out-of-memory error.
*/
ctr_object* ctr_gc_setmemlimit(ctr_object* myself, ctr_argument* argumentList) {
ctr_gc_memlimit = (uint64_t) ctr_internal_cast2number( argumentList->object )->value.nvalue;
ctr_size poolSize = ctr_gc_memlimit;
return myself;
}
/**
* [Broom] mode: [Number]
*
* Selects mode of operation for GC.
*
* Available Modes:
* 0 - No Garbage Collection
* 1 - Activate Garbage Collector (default)
* 4 - Activate Garbage Collector for every single step (testing only)
* 8 - Activate experimental Pool Memory Allocation Manager (experimental!)
*/
ctr_object* ctr_gc_setmode(ctr_object* myself, ctr_argument* argumentList) {
ctr_gc_mode = (int) ctr_internal_cast2number( argumentList->object )->value.nvalue;
if (ctr_gc_mode & 8) {
ctr_pool_init(ctr_gc_memlimit/2);
}
return myself;
}
/**
* [Broom] toString.
*
* Returns the number of objects that have been collected by the
* garbage collector.
*/
ctr_object* ctr_gc_to_string(ctr_object* myself, ctr_argument* argumentList) {
return ctr_internal_cast2string( ctr_gc_dust( myself, argumentList ) );
}
/**
* [Broom] toNumber.
*
* Returns the number of objects that have been collected by the
* garbage collector.
*/
ctr_object* ctr_gc_to_number(ctr_object* myself, ctr_argument* argumentList) {
return ctr_internal_cast2number( ctr_gc_dust( myself, argumentList ) );
}
/**
* [Program] shell: [String]
*
* Performs a Shell operation. The Shell object uses a fluid API, so you can
* mix shell code with programming logic. For instance to list the contents
* of a directory use:
*
* Shell ls
*
* This will output the contents of the current working directly, you
* can also pass keyword messages like so:
*
* Shell echo: 'Hello from the Shell!'.
*
* The example above will output the specified message to the console.
* Every message you send will be turned into a string and dispatched to
* the 'call:' message.
*/
ctr_object* ctr_program_shell(ctr_object* myself, ctr_argument* argumentList) {
ctr_check_permission( CTR_SECPRO_NO_SHELL );
FILE* stream;
char* outputBuffer;
ctr_argument* newArgumentList;
ctr_object* appendString;
ctr_object* outputString;
outputBuffer = ctr_heap_allocate( 512 );
ctr_object* arg = ctr_internal_cast2string(argumentList->object);
long vlen = arg->value.svalue->vlen;
char* comString = ctr_heap_allocate( sizeof( char ) * ( vlen + 1 ) );
memcpy(comString, arg->value.svalue->value, vlen);
memcpy(comString+vlen,"\0",1);
newArgumentList = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
if ( !( stream = popen( comString, "r" ) ) ) {
CtrStdFlow = ctr_build_string_from_cstring( "Unable to execute command." );
}
outputString = ctr_build_empty_string();
while ( fgets( outputBuffer, 512, stream ) ) {
appendString = ctr_build_string_from_cstring( outputBuffer );
newArgumentList->object = appendString;
ctr_string_append( outputString, newArgumentList );
}
ctr_heap_free( outputBuffer );
ctr_heap_free( newArgumentList );
ctr_heap_free( comString );
return outputString;
}
/**
* @internal
*
* Shell Object uses a fluid API.
*/
ctr_object* ctr_slurp_respond_to(ctr_object* myself, ctr_argument* argumentList) {
ctr_argument* newArgumentList;
ctr_object* newCommandObj;
ctr_object* commandObj;
ctr_object* key;
newCommandObj = argumentList->object;
key = ctr_build_string_from_cstring( "command" );
commandObj = ctr_internal_object_find_property( myself, key, CTR_CATEGORY_PRIVATE_PROPERTY );
newArgumentList = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
if ( commandObj == NULL ) {
commandObj = ctr_build_empty_string();
ctr_internal_object_set_property( myself, key, commandObj, CTR_CATEGORY_PRIVATE_PROPERTY );
} else {
newArgumentList->object = ctr_build_string_from_cstring( " " );
ctr_string_append( commandObj, newArgumentList );
}
newArgumentList->object = newCommandObj;
ctr_string_append( commandObj, newArgumentList );
ctr_heap_free( newArgumentList );
return myself;
}
/**
* @internal
*
* Slurp uses a fluid API
*/
ctr_object* ctr_slurp_respond_to_and(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* str;
ctr_argument* newArgumentList;
newArgumentList = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
str = ctr_internal_cast2string( argumentList->object );
if ( (str->value.svalue->vlen > 0) && *(str->value.svalue->value + (str->value.svalue->vlen - 1)) == ':' ) {
char* ncstr = ctr_heap_allocate( str->value.svalue->vlen - 1 );
memcpy( ncstr, str->value.svalue->value, str->value.svalue->vlen - 1 );
newArgumentList->object = ctr_build_string( ncstr, str->value.svalue->vlen - 1 );
ctr_slurp_respond_to( myself, newArgumentList );
ctr_heap_free( ncstr );
} else {
newArgumentList->object = argumentList->object;
ctr_slurp_respond_to( myself, newArgumentList );
}
newArgumentList->object = argumentList->next->object;
ctr_slurp_respond_to( myself, newArgumentList );
ctr_heap_free( newArgumentList );
return myself;
}
/**
* [Slurp]
*
* Slurp is an object that takes any message and converts it to a string.
* The message 'obtain' can be used to acquire the generated string.
* The Slurp object is a separate object with minimal messages to avoid
* 'message collision'.
*/
/**
* [Slurp] obtain.
*
* Obtains the string generated using the Slurp object.
* A Slurp object collects all messages send to it and flushes its buffer while
* returning the resulting string after an 'obtain' message has been received.
*
* Usage:
*
* Slurp hello world.
* Pen write: (Slurp obtain).
*
* This will output: 'hello world'.
* Use the Slurp object to integrate verbose shell commands, other programming languages
* (like SQL) etc into your main program without overusing strings.
*
* Note that we can't use the = and * unfortunately right now
* because = is also a method in the main object. While * can be used
* theoretically, it expects an identifier, and 'from' is not a real
* identifier, it's just another unary message, so instead of using a binary
* * we simply use a keyword message select: with argument '*' and then
* proceed our SQL query with a comma (,) to chain the rest.
* This is an artifact of the fact that the DSL has to be embedded within
* the language of Citrine. However even with these restrictions (some of which might be
* alleviated in future versions) it's quite comfortable and readable to interweave
* an external language in your Citrine script code.
*
* Usage:
*
* ☞ query := Slurp
* select: '*',
* from
* users
* where
* user_id=: 1.
*
* #result: select * from users where user_id= 1
*/
ctr_object* ctr_slurp_obtain( ctr_object* myself, ctr_argument* argumentList ) {
ctr_object* commandObj;
ctr_object* key;
key = ctr_build_string_from_cstring( "command" );
commandObj = ctr_internal_object_find_property( myself, ctr_build_string_from_cstring( "command" ), CTR_CATEGORY_PRIVATE_PROPERTY );
if ( commandObj == NULL ) {
commandObj = ctr_build_empty_string();
}
ctr_internal_object_delete_property( myself, key, CTR_CATEGORY_PRIVATE_PROPERTY );
return commandObj;
}
/**
* [Slurp] toString.
*
* Sending the message 'toString' to a slurp object is the same as sending the
* obtain message. It will cause the Slurp object to answer with the collected
* string information from previous interactions. If for some reason the
* obtain message does not return a string, this message will answer with
* an empty string, otherwise the resulting string from 'obtain' will be
* returned.
*/
ctr_object* ctr_slurp_to_string( ctr_object* myself, ctr_argument* argumentList ) {
ctr_object* commandObj = ctr_slurp_obtain(myself, argumentList);
if (commandObj->info.type != CTR_OBJECT_TYPE_OTSTRING) {
return ctr_build_empty_string();
}
return commandObj;
}
/**
* [Program] argument: [Number]
*
* Obtains an argument from the CLI invocation.
*/
ctr_object* ctr_program_argument(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* numberObject = ctr_internal_cast2number(argumentList->object);
int n = (int) numberObject->value.nvalue;
if (n >= ctr_argc || n < 0) return CtrStdNil;
return ctr_build_string(ctr_argv[n], strlen(ctr_argv[n]));
}
/**
* [Program] argCount
*
* Returns the number of CLI arguments passed to the script.
*/
ctr_object* ctr_program_num_of_args(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float( (ctr_number) ctr_argc );
}
/**
* [Program] exit
*
* Exits program immediately.
*/
ctr_object* ctr_program_exit(ctr_object* myself, ctr_argument* argumentList) {
CtrStdFlow = CtrStdExit;
return CtrStdNil;
}
/**
* [Program] env: [String]
*
* Returns the value of an environment variable.
*
* Usage:
*
* x := Command env: 'MY_PATH_VAR'.
*/
ctr_object* ctr_program_get_env(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* envVarNameObj;
char* envVarNameStr;
char* envVal;
ctr_check_permission( CTR_SECPRO_NO_FILE_READ );
envVarNameObj = ctr_internal_cast2string(argumentList->object);
envVarNameStr = ctr_heap_allocate((envVarNameObj->value.svalue->vlen+1)*sizeof(char));
strncpy(envVarNameStr, envVarNameObj->value.svalue->value, envVarNameObj->value.svalue->vlen);
*(envVarNameStr + (envVarNameObj->value.svalue->vlen)) = '\0';
envVal = getenv(envVarNameStr);
ctr_heap_free(envVarNameStr );
if (envVal == NULL) {
return CtrStdNil;
}
return ctr_build_string_from_cstring(envVal);
}
/**
* [Program] env: [Key] val: [Value]
*
* Sets the value of an environment variable.
*/
ctr_object* ctr_program_set_env(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* envVarNameObj;
ctr_object* envValObj;
char* envVarNameStr;
char* envValStr;
ctr_check_permission( CTR_SECPRO_NO_FILE_WRITE );
envVarNameObj = ctr_internal_cast2string(argumentList->object);
envValObj = ctr_internal_cast2string(argumentList->next->object);
envVarNameStr = ctr_heap_allocate_cstring( envVarNameObj );
envValStr = ctr_heap_allocate_cstring( envValObj );
setenv(envVarNameStr, envValStr, 1);
ctr_heap_free( envValStr );
ctr_heap_free( envVarNameStr );
return myself;
}
/**
* [Program] waitForInput
*
* Ask a question on the command-line, resumes program
* only after pressing the enter key.
* Only reads up to 100 characters.
*
* Usage:
*
* Pen write: 'What is your name ?'.
* x := Command waitForInput.
* Pen write: 'Hello ' + x + ' !', brk.
*
* The example above asks the user for his/her name and
* then displays the input received.
*/
ctr_object* ctr_program_waitforinput(ctr_object* myself, ctr_argument* argumentList) {
ctr_check_permission( CTR_SECPRO_COUNTDOWN );
int c;
ctr_size bytes = 0;
char* buff;
ctr_size page = 10;
ctr_object* userInput;
buff = ctr_heap_allocate(page * sizeof(char));
while ((c = getchar()) != '\n') {
buff[bytes] = c;
bytes++;
if (bytes > page) {
page *= 2;
buff = (char*) ctr_heap_reallocate(buff, page * sizeof(char));
if (buff == NULL) {
CtrStdFlow = ctr_build_string_from_cstring("Out of memory");
return CtrStdNil;
}
}
}
userInput = ctr_build_string(buff, bytes);
ctr_heap_free(buff);
return userInput;
}
/**
* [Program] input.
*
* Reads all raw input from STDIN.
* The input message reads the standard input stream of the application
* which allows you to deal with pipes for instance. However this
* mechanism can also be used to read raw POSTs in case of CGI applications.
* Note that unlike other implementations the input messages also collects
* null bytes, a null byte \0 in the input stream will NOT cause it to end.
* Also note that the trailing newline (in case of CLI applications) will
* be stripped so you don't have to do this manually. This allows for
* one-liners like the one in the example below.
* The input message is not allowed if message countdown has been activated
* (Program remainingMessages:) because it might wait for content and this
* is not allowed in a countdown sandbox.
*
* Usage:
*
* echo "hello" | ctr test.ctr
*
* x := Program input.
* Pen write: x. #hello (without newline)
*
*/
ctr_object* ctr_program_input(ctr_object* myself, ctr_argument* argumentList) {
ctr_check_permission( CTR_SECPRO_COUNTDOWN );
ctr_size bytes = 0;
ctr_size page = 64;
char buffer[page];
size_t content_size = 0;
char *content = ctr_heap_allocate(0);
clearerr(stdin);
int reading = 1;
while(reading) {
bytes = fread(buffer, sizeof(char), page, stdin);
content_size += bytes;
content = ctr_heap_reallocate(content, content_size);
memcpy(content + (content_size - bytes),buffer,bytes);
reading = !(bytes != page && feof(stdin));
}
/* strip the last newline */
if ( content_size > 0 && *(content+(content_size-1))=='\n' ) {
content_size -= 1;
}
ctr_object* str = ctr_build_string( content, content_size );
ctr_heap_free( content );
return str;
}
/**
* @internal
*
* Checks whether the user is allowed to perform this kind of operation.
*/
void ctr_check_permission( uint8_t operationID ) {
char* reason;
if ( ( ctr_program_security_profile & operationID ) ) {
reason = "This program is not allowed to perform this operation.";
if ( operationID == CTR_SECPRO_NO_SHELL ) {
reason = "This program is not allowed to execute shell commands.";
}
if ( operationID == CTR_SECPRO_NO_FILE_WRITE ) {
reason = "This program is not allowed to modify or delete any files or folders.";
}
if ( operationID == CTR_SECPRO_NO_FILE_READ ) {
reason = "This program is not allowed to perform any file operations.";
}
if ( operationID == CTR_SECPRO_NO_INCLUDE ) {
reason = "This program is not allowed to include any other files for code execution.";
}
if ( operationID == CTR_SECPRO_FORK ) {
reason = "This program is not allowed to spawn other processes or serve remote objects.";
}
fprintf(stderr, "%s\n", reason );
exit(1);
}
}
/**
* [Program] forbidShell
*
* This method is part of the security profiles feature of Citrine.
* This will forbid the program to execute any shell operations. All
* external libraries and plugins are assumed to respect this setting as well.
*
* Usage:
*
* Program forbidShell.
*/
ctr_object* ctr_program_forbid_shell( ctr_object* myself, ctr_argument* argumentList ) {
ctr_program_security_profile |= CTR_SECPRO_NO_SHELL;
return myself;
}
/**
* [Program] forbidFileWrite
*
* This method is part of the security profiles feature of Citrine.
* This will forbid the program to modify, create or delete any files. All
* external libraries and plugins are assumed to respect this setting as well.
*
* Usage:
*
* Program forbidFileWrite.
*/
ctr_object* ctr_program_forbid_file_write( ctr_object* myself, ctr_argument* argumentList ) {
ctr_program_security_profile |= CTR_SECPRO_NO_FILE_WRITE;
return myself;
}
/**
* [Program] forbidFileRead
*
* This method is part of the security profiles feature of Citrine.
* This will forbid the program to read any files. In fact this will prevent you from
* creating the file object at all.
* This will also prevent you from reading environment variables.
* All external libraries and plugins are assumed to respect this setting as well.
* Forbidding a program to read files also has the effect to forbid including other
* source files.
*
* Usage:
*
* Program forbidFileRead.
*/
ctr_object* ctr_program_forbid_file_read( ctr_object* myself, ctr_argument* argumentList ) {
ctr_program_security_profile |= CTR_SECPRO_NO_FILE_READ;
return myself;
}
/**
* [Program] forbidInclude
*
* This method is part of the security profiles feature of Citrine.
* This will forbid the program to include any other files. All
* external libraries and plugins are assumed to respect this setting as well.
*
* Usage:
*
* Program forbidInclude.
*/
ctr_object* ctr_program_forbid_include( ctr_object* myself, ctr_argument* argumentList ) {
ctr_program_security_profile |= CTR_SECPRO_NO_INCLUDE;
return myself;
}
/**
* [Program] forbidFork.
*/
ctr_object* ctr_program_forbid_fork( ctr_object* myself, ctr_argument* argumentList ) {
ctr_program_security_profile |= CTR_SECPRO_FORK;
return myself;
}
/**
* [Program] remainingMessages: [Number]
*
* This method is part of the security profiles feature of Citrine.
* This will initiate a countdown for the program, you can specify the maximum quota of
* messages the program may process, once this quota has been exhausted the program will
* be killed entirely (no exception).
*
* Usage:
*
* Program remainingMessages: 100.
*/
ctr_object* ctr_program_countdown( ctr_object* myself, ctr_argument* argumentList ) {
if ( ctr_program_security_profile & CTR_SECPRO_COUNTDOWN ) {
fprintf(stderr, "Message quota cannot change.\n" );
exit(1);
}
ctr_program_security_profile |= CTR_SECPRO_COUNTDOWN;
ctr_program_maxtick = (uint64_t) ctr_internal_cast2number( argumentList->object )->value.nvalue;
return myself;
}
/**
* [Program] flush.
*
* Flushes the STDOUT output buffer.
*/
ctr_object* ctr_program_flush(ctr_object* myself, ctr_argument* ctr_argumentList) {
fflush(stdout);
return myself;
}
/**
* [Program] new: [Block].
*
* Forks the program into two programs.
* Creates another program that will run at the same time as the
* current program. Both the parent and the child will obtain a reference
* to the newly created program. The child will obtain a reference to
* itself passed as a parameter to the code block while the parent will
* obtain its version of the program instance as the return value of the
* new: message.
*
* Note that spawning a new program will leak memory.
* The file descriptors used to setup communication between parent and
* child will be removed when the main program ends but any newly created
* program will add a descriptor pair to the set. This is a limitation
* in the current implementation.
*
* Usage:
*
* child := Program new: { :program
* Pen write: 'Child', brk.
* }.
* Pen write: 'Parent'.
*/
ctr_object* ctr_program_fork(ctr_object* myself, ctr_argument* argumentList) {
int p;
int* ps;
FILE* pipes;
ctr_object* child;
ctr_argument* newArgumentList;
ctr_resource* rs;
ctr_check_permission( CTR_SECPRO_COUNTDOWN );
ctr_check_permission( CTR_SECPRO_FORK );
newArgumentList = ctr_heap_allocate( sizeof( ctr_argument ) );
child = ctr_internal_create_object( CTR_OBJECT_TYPE_OTOBJECT );
child->link = myself;
ps = ctr_heap_allocate( sizeof(int) * 4 );
pipes = ctr_heap_allocate_tracked( sizeof( FILE ) * 2 );
rs = ctr_heap_allocate_tracked( sizeof( ctr_resource ) );
rs->type = 2;
rs->ptr = (void*) pipes;
child->value.rvalue = rs;
newArgumentList->object = child;
pipe(ps);
pipe(ps + 2);
p = fork();
if ( p < 0 ) {
CtrStdFlow = ctr_build_string_from_cstring( "Unable to fork" );
ctr_heap_free( newArgumentList );
ctr_heap_free( rs );
return CtrStdNil;
}
if ( p == 0 ) {
close(*(ps + 0));
close(*(ps + 3));
*((FILE**)rs->ptr + 1) = fdopen( *(ps + 1), "wb" );
*((FILE**)rs->ptr + 2) = fdopen( *(ps + 2), "rb" );
setvbuf( *((FILE**)rs->ptr + 1), NULL, _IONBF, 0 );
setvbuf( *((FILE**)rs->ptr + 2), NULL, _IONBF, 0 );
rs->type = 3;
ctr_block_runIt( argumentList->object, newArgumentList );
fclose(*((FILE**)rs->ptr + 1));
fclose(*((FILE**)rs->ptr + 2));
ctr_heap_free( newArgumentList );
ctr_heap_free( ps);
CtrStdFlow = CtrStdExit;
return CtrStdNil;
} else {
ctr_internal_object_set_property(
child,
ctr_build_string_from_cstring( "pid" ),
ctr_build_number_from_float( (ctr_number) p ),
CTR_CATEGORY_PRIVATE_PROPERTY
);
close(*(ps+1));
close(*(ps+2));
*((FILE**)rs->ptr + 3) = fdopen( *(ps + 3), "wb" );
*((FILE**)rs->ptr + 0) = fdopen( *(ps + 0), "rb" );
setvbuf( *((FILE**)rs->ptr + 3), NULL, _IONBF, 0 );
setvbuf( *((FILE**)rs->ptr + 0), NULL, _IONBF, 0 );
ctr_heap_free( newArgumentList );
ctr_heap_free( ps );
}
return child;
}
/**
* [Program] message: [String].
*
* Sends a message to another program, i.e. a child or a parent that is
* running at the same time.
*/
ctr_object* ctr_program_message(ctr_object* myself, ctr_argument* argumentList) {
char* str;
ctr_size n;
ctr_object* string;
ctr_resource* rs;
int q;
FILE* fd;
string = ctr_internal_cast2string( argumentList->object );
str = ctr_heap_allocate_cstring( string );
n = argumentList->object->value.svalue->vlen;
q = 1;
rs = myself->value.rvalue;
if (rs->type == 2) q = 3;
fd = *((FILE**)rs->ptr + q);
fwrite( &n, sizeof(ctr_size), 1, fd);
fwrite( str, 1, n, fd);
ctr_heap_free(str);
return myself;
}
/**
* [Program] listen: [Block].
*
* Stops the current flow of the program and starts listening for
* messages from other programs that are running at the same time.
* Upon receiving a message, the specified block will be invocated
* and passed the message that has been received.
*/
ctr_object* ctr_program_listen(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* program;
ctr_object* answer;
ctr_resource* r;
ctr_argument* newArgumentList;
int q;
ctr_size sz;
FILE* fd;
char* blob;
program = myself;
q = 0;
r = program->value.rvalue;
if (r->type == 3) q = 2;
fd = *((FILE**)r->ptr + q);
sz = 0;
fread(&sz, sizeof(ctr_size), 1, fd);
blob = ctr_heap_allocate( sz );
fread( blob, 1, sz, fd );
newArgumentList = ctr_heap_allocate( sizeof(ctr_argument) );
newArgumentList->object = ctr_build_string( blob, sz );
answer = ctr_block_runIt( argumentList->object, newArgumentList );
ctr_heap_free( blob );
ctr_heap_free(newArgumentList);
return answer;
}
/**
* [Program] join
*
* Rejoins the program with the main program.
* This message will cause the current program to stop and wait
* for the child program to end.
*/
ctr_object* ctr_program_join(ctr_object* myself, ctr_argument* argumentList) {
int pid;
ctr_resource* rs = myself->value.rvalue;
if (rs == NULL) return CtrStdNil;
if (rs->type == 3) {
CtrStdFlow = ctr_build_string_from_cstring( "a child process can not join." );
return CtrStdNil;
}
pid = (int) ctr_internal_object_find_property(
myself,
ctr_build_string_from_cstring("pid"),
CTR_CATEGORY_PRIVATE_PROPERTY
)->value.nvalue;
fclose(*((FILE**)rs->ptr + 0));
fclose(*((FILE**)rs->ptr + 3));
waitpid(pid, 0, 0);
return CtrStdNil;
}
/**
* [Program] pid
*
* Returns the process identification number associated with the
* program. If the program instance refers to the currently running
* program PID 0 will be returned.
*/
ctr_object* ctr_program_pid(ctr_object* myself, ctr_argument* argumentList ) {
ctr_object* pidObject;
pidObject = ctr_internal_object_find_property(
myself,
ctr_build_string_from_cstring("pid"),
CTR_CATEGORY_PRIVATE_PROPERTY
);
if (pidObject == NULL) return CtrStdNil;
return ctr_internal_cast2number( pidObject );
}
/**
* [Program] toString
*
* Returns a string representation of the program. This will be something like
* [PID:2833], or in case of the currently active program: [PID:0].
*/
ctr_object* ctr_program_to_string(ctr_object* myself, ctr_argument* argumentList ) {
return ctr_internal_cast2string(ctr_program_pid(myself, argumentList));
}
/**
* [Program] toNumber
*
* Returns the program pid as a number.
*/
ctr_object* ctr_program_to_number(ctr_object* myself, ctr_argument* argumentList ) {
return ctr_internal_cast2number(ctr_program_pid(myself, argumentList));
}
/**
* @internal
*
* Internal generic logging function.
* All logging functionality uses this function under the hood.
*/
ctr_object* ctr_program_log_generic(ctr_object* myself, ctr_argument* argumentList, int level) {
char* message;
ctr_check_permission( CTR_SECPRO_COUNTDOWN );
message = ctr_heap_allocate_cstring(