forked from sandvine/SandScripthon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpal.c
793 lines (652 loc) · 18.9 KB
/
pal.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
/* SandScript meets Python.
*
* This is a SandScript shared library that loads a Python interpreter
* and let users write policy subsystems in Python. It depends on the
* special "SandScript" Python module to do so.
*
* At this point, the code is pretty alpha and may have bugs and
* memory leaks.
*
* Due to a limitation of the shared library API, we can only export
* so many functions, currently set to max=100.
*/
#include <Python.h>
#include <bytesobject.h>
#include <syslog.h>
#include "sandscript-library-interface/sharedLibTypes.h"
#include "sandscript-library-interface/sharedLibManifest.h"
#ifndef VERSION
#define VERSION "tip"
#endif
#ifndef GITCOMMIT
#define GITCOMMIT "HEAD"
#endif
#define PYMOD "SandScript"
#define PYUSERMOD "policy"
#define PYUSERDIR1 "/usr/local/sandvine/etc"
#define PYUSERDIR2 "/usr/local/sandvine/loadable/python"
PyObject* pyFuncList[100]; // Max functions that can exist.
bool pslFunc(PyObject* item, psl_Value* resp, const psl_Value* const* args) {
PyObject* fp = PyTuple_GetItem(item, 0);
PyObject* al = PyTuple_GetItem(item, 2);
PyObject* rt = PyTuple_GetItem(item, 3);
int i, na = PyTuple_Size(al);
PyObject* pa = PyTuple_New(na);
for (i=0; i < na; i++) {
PyObject* arg = NULL;
switch (PyLong_AsLong(PyTuple_GetItem(al, i))) {
case psl_boolean:
arg = args[i]->b ? Py_True : Py_False;
break;
case psl_string:
arg = PyBytes_FromStringAndSize(
args[i]->s.begin,
args[i]->s.length
);
break;
case psl_integer:
arg = PyLong_FromLong((long) args[i]->i);
break;
case psl_float:
arg = PyFloat_FromDouble(args[i]->f);
break;
case psl_ipaddress:
arg = PyByteArray_FromStringAndSize(
(const char*) args[i]->a,
sizeof(args[i]->a)
);
break;
}
if (arg) {
Py_INCREF(arg);
PyTuple_SetItem(pa, i, arg);
}
}
PyObject* pr = PyObject_CallObject(fp, pa);
for (i=0; i < PyTuple_Size(pa); i++) {
Py_DECREF(PyTuple_GetItem(pa, i));
}
Py_DECREF(pa);
if (!pr) return false;
if (pr == Py_None) {
Py_DECREF(pr);
return false;
}
bool rv = false;
switch (PyLong_AsLong(rt)) {
case psl_boolean:
if (PyBool_Check(pr)) {
resp->b = pr == Py_True;
rv = true;
}
break;
case psl_string:
if (PyBytes_Check(pr)) {
resp->s.begin = PyBytes_AsString(pr);
resp->s.length = strlen(resp->s.begin);
rv = true;
}
break;
case psl_integer:
if (PyLong_Check(pr)) {
resp->i = PyLong_AsLong(pr);
rv = true;
}
break;
case psl_float:
if (PyFloat_Check(pr)) {
resp->f = PyFloat_AsDouble(pr);
rv = true;
}
break;
case psl_ipaddress:
if (PyByteArray_Check(pr)) {
char* s = PyByteArray_AsString(pr);
int sl = PyByteArray_Size(pr);
if (sl <= 16) {
memcpy(resp->a, s, sl);
rv = true;
}
}
break;
}
Py_DECREF(pr);
return rv;
}
bool pslFunc0(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[0], resp, args);
}
bool pslFunc1(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[1], resp, args);
}
bool pslFunc2(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[2], resp, args);
}
bool pslFunc3(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[3], resp, args);
}
bool pslFunc4(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[4], resp, args);
}
bool pslFunc5(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[5], resp, args);
}
bool pslFunc6(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[6], resp, args);
}
bool pslFunc7(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[7], resp, args);
}
bool pslFunc8(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[8], resp, args);
}
bool pslFunc9(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[9], resp, args);
}
bool pslFunc10(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[10], resp, args);
}
bool pslFunc11(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[11], resp, args);
}
bool pslFunc12(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[12], resp, args);
}
bool pslFunc13(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[13], resp, args);
}
bool pslFunc14(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[14], resp, args);
}
bool pslFunc15(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[15], resp, args);
}
bool pslFunc16(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[16], resp, args);
}
bool pslFunc17(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[17], resp, args);
}
bool pslFunc18(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[18], resp, args);
}
bool pslFunc19(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[19], resp, args);
}
bool pslFunc20(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[20], resp, args);
}
bool pslFunc21(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[21], resp, args);
}
bool pslFunc22(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[22], resp, args);
}
bool pslFunc23(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[23], resp, args);
}
bool pslFunc24(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[24], resp, args);
}
bool pslFunc25(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[25], resp, args);
}
bool pslFunc26(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[26], resp, args);
}
bool pslFunc27(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[27], resp, args);
}
bool pslFunc28(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[28], resp, args);
}
bool pslFunc29(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[29], resp, args);
}
bool pslFunc30(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[30], resp, args);
}
bool pslFunc31(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[31], resp, args);
}
bool pslFunc32(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[32], resp, args);
}
bool pslFunc33(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[33], resp, args);
}
bool pslFunc34(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[34], resp, args);
}
bool pslFunc35(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[35], resp, args);
}
bool pslFunc36(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[36], resp, args);
}
bool pslFunc37(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[37], resp, args);
}
bool pslFunc38(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[38], resp, args);
}
bool pslFunc39(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[39], resp, args);
}
bool pslFunc40(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[40], resp, args);
}
bool pslFunc41(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[41], resp, args);
}
bool pslFunc42(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[42], resp, args);
}
bool pslFunc43(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[43], resp, args);
}
bool pslFunc44(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[44], resp, args);
}
bool pslFunc45(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[45], resp, args);
}
bool pslFunc46(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[46], resp, args);
}
bool pslFunc47(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[47], resp, args);
}
bool pslFunc48(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[48], resp, args);
}
bool pslFunc49(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[49], resp, args);
}
bool pslFunc50(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[50], resp, args);
}
bool pslFunc51(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[51], resp, args);
}
bool pslFunc52(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[52], resp, args);
}
bool pslFunc53(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[53], resp, args);
}
bool pslFunc54(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[54], resp, args);
}
bool pslFunc55(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[55], resp, args);
}
bool pslFunc56(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[56], resp, args);
}
bool pslFunc57(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[57], resp, args);
}
bool pslFunc58(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[58], resp, args);
}
bool pslFunc59(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[59], resp, args);
}
bool pslFunc60(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[60], resp, args);
}
bool pslFunc61(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[61], resp, args);
}
bool pslFunc62(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[62], resp, args);
}
bool pslFunc63(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[63], resp, args);
}
bool pslFunc64(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[64], resp, args);
}
bool pslFunc65(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[65], resp, args);
}
bool pslFunc66(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[66], resp, args);
}
bool pslFunc67(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[67], resp, args);
}
bool pslFunc68(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[68], resp, args);
}
bool pslFunc69(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[69], resp, args);
}
bool pslFunc70(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[70], resp, args);
}
bool pslFunc71(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[71], resp, args);
}
bool pslFunc72(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[72], resp, args);
}
bool pslFunc73(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[73], resp, args);
}
bool pslFunc74(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[74], resp, args);
}
bool pslFunc75(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[75], resp, args);
}
bool pslFunc76(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[76], resp, args);
}
bool pslFunc77(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[77], resp, args);
}
bool pslFunc78(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[78], resp, args);
}
bool pslFunc79(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[79], resp, args);
}
bool pslFunc80(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[80], resp, args);
}
bool pslFunc81(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[81], resp, args);
}
bool pslFunc82(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[82], resp, args);
}
bool pslFunc83(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[83], resp, args);
}
bool pslFunc84(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[84], resp, args);
}
bool pslFunc85(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[85], resp, args);
}
bool pslFunc86(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[86], resp, args);
}
bool pslFunc87(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[87], resp, args);
}
bool pslFunc88(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[88], resp, args);
}
bool pslFunc89(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[89], resp, args);
}
bool pslFunc90(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[90], resp, args);
}
bool pslFunc91(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[91], resp, args);
}
bool pslFunc92(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[92], resp, args);
}
bool pslFunc93(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[93], resp, args);
}
bool pslFunc94(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[94], resp, args);
}
bool pslFunc95(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[95], resp, args);
}
bool pslFunc96(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[96], resp, args);
}
bool pslFunc97(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[97], resp, args);
}
bool pslFunc98(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[98], resp, args);
}
bool pslFunc99(psl_Value* resp, const psl_Value* const* args) {
return pslFunc(pyFuncList[99], resp, args);
}
psl_Function* pslFuncList [] = {
&pslFunc0,
&pslFunc1,
&pslFunc2,
&pslFunc3,
&pslFunc4,
&pslFunc5,
&pslFunc6,
&pslFunc7,
&pslFunc8,
&pslFunc9,
&pslFunc10,
&pslFunc11,
&pslFunc12,
&pslFunc13,
&pslFunc14,
&pslFunc15,
&pslFunc16,
&pslFunc17,
&pslFunc18,
&pslFunc19,
&pslFunc20,
&pslFunc21,
&pslFunc22,
&pslFunc23,
&pslFunc24,
&pslFunc25,
&pslFunc26,
&pslFunc27,
&pslFunc28,
&pslFunc29,
&pslFunc30,
&pslFunc31,
&pslFunc32,
&pslFunc33,
&pslFunc34,
&pslFunc35,
&pslFunc36,
&pslFunc37,
&pslFunc38,
&pslFunc39,
&pslFunc40,
&pslFunc41,
&pslFunc42,
&pslFunc43,
&pslFunc44,
&pslFunc45,
&pslFunc46,
&pslFunc47,
&pslFunc48,
&pslFunc49,
&pslFunc50,
&pslFunc51,
&pslFunc52,
&pslFunc53,
&pslFunc54,
&pslFunc55,
&pslFunc56,
&pslFunc57,
&pslFunc58,
&pslFunc59,
&pslFunc60,
&pslFunc61,
&pslFunc62,
&pslFunc63,
&pslFunc64,
&pslFunc65,
&pslFunc66,
&pslFunc67,
&pslFunc68,
&pslFunc69,
&pslFunc70,
&pslFunc71,
&pslFunc72,
&pslFunc73,
&pslFunc74,
&pslFunc75,
&pslFunc76,
&pslFunc77,
&pslFunc78,
&pslFunc79,
&pslFunc80,
&pslFunc81,
&pslFunc82,
&pslFunc83,
&pslFunc84,
&pslFunc85,
&pslFunc86,
&pslFunc87,
&pslFunc88,
&pslFunc89,
&pslFunc90,
&pslFunc91,
&pslFunc92,
&pslFunc93,
&pslFunc94,
&pslFunc95,
&pslFunc96,
&pslFunc97,
&pslFunc98,
&pslFunc99,
};
psl_DataType* buildFuncArgs(PyObject* args) {
int na = PyTuple_Size(args);
psl_DataType* pa = calloc(na, sizeof(psl_DataType));
assert(pa);
int i;
for (i=0; i < na; i++) {
PyObject* arg = PyTuple_GetItem(args, i);
if (!PyLong_Check(arg)) return NULL;
Py_INCREF(arg);
pa[i] = PyLong_AsLong(arg);
//Py_DECREF(arg);
}
return pa;
}
psl_Manifest*
buildManifest(PyObject* fl)
{
int nf = PyList_Size(fl);
int max = sizeof(pslFuncList) / sizeof(pslFuncList[0]);
if (nf < 1 || nf > max)
return NULL;
psl_FunctionDescription** fd = calloc(nf, sizeof(psl_FunctionDescription*));
assert(fd);
int i;
for (i=0; i < nf; i++)
{
PyObject* item = PyList_GetItem(fl, i);
if (!PyTuple_Check(item) && PyTuple_Size(item) != 4)
return NULL;
Py_INCREF(item);
PyObject* func = PyTuple_GetItem(item, 0);
if (!PyCallable_Check(func))
return NULL;
Py_INCREF(func);
PyObject* name = PyTuple_GetItem(item, 1);
if (!PyUnicode_Check(name))
return NULL;
Py_INCREF(name);
PyObject* args = PyTuple_GetItem(item, 2);
if (!PyTuple_Check(args))
return NULL;
psl_DataType* pslargs = buildFuncArgs(args);
if (!pslargs)
return NULL;
Py_INCREF(args);
PyObject* retval = PyTuple_GetItem(item, 3);
if (!PyLong_Check(retval))
return NULL;
psl_DataType pslretval = PyLong_AsLong(retval);
Py_INCREF(retval);
fd[i] = malloc(sizeof(psl_FunctionDescription));
assert(fd[i]);
Py_ssize_t size;
fd[i]->functionName = PyUnicode_AsUTF8AndSize(name, &size);
fd[i]->flags = 0;
fd[i]->functionPointer = pslFuncList[i];
fd[i]->returnType = pslretval;
fd[i]->numArgs = PyTuple_Size(args);
fd[i]->argTypes = pslargs;
pyFuncList[i] = item;
}
psl_Manifest* m = malloc(sizeof(psl_Manifest));
assert(m);
m->version = 1;
m->numFunctionDescriptions = nf;
m->functionDescriptions = (const psl_FunctionDescription* const*) fd;
return m;
}
const psl_Manifest*
GetManifest(void)
{
syslog(LOG_INFO, "SandScripthon %s (%s)", VERSION, GITCOMMIT);
Py_Initialize();
// Set path.
PyObject* path1 = PyUnicode_FromString(PYUSERDIR1);
PyList_Insert(PySys_GetObject("path"), 0, path1);
Py_INCREF(path1);
PyObject* path2 = PyUnicode_FromString(PYUSERDIR2);
PyList_Insert(PySys_GetObject("path"), 0, path2);
Py_INCREF(path2);
// Import SandScript module.
PyObject* sf = PyImport_ImportModule(PYMOD);
if (!sf)
goto fail;
Py_INCREF(sf);
// Import user module.
PyObject* uf = PyImport_ImportModule(PYUSERMOD);
if (!uf)
goto fail;
Py_INCREF(uf);
// Load list of registered functions.
PyObject* fl = PyObject_GetAttrString(sf, "_func");
if (!PyList_Check(fl))
goto fail;
Py_INCREF(fl);
psl_Manifest* m = buildManifest(fl);
if (!m)
goto fail;
return m;
fail:
if (PyErr_Occurred())
{
PyObject *ptype = NULL, *pvalue = NULL, *ptraceback = NULL;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if (ptype != NULL)
{
PyErr_NormalizeException(&ptype,&pvalue,&ptraceback);
if (!ptraceback)
{
ptraceback = Py_None;
Py_INCREF(ptraceback);
}
if (ptype)
{
if (pvalue)
{
syslog(LOG_ERR, "SandScripthon error: %s", Py_TYPE(pvalue)->tp_name);
}
if (ptraceback)
{
char * b = PyBytes_AsString(ptraceback);
syslog(LOG_ERR, "SandScripthon traceback: %s", b);
}
}
}
}
Py_Finalize();
return NULL;
}
/*
int main() {
const psl_Manifest* m = GetManifest();
if (m) fprintf(stderr, "got %p\n", m);
}
*/