-
Notifications
You must be signed in to change notification settings - Fork 19
/
process.c
2310 lines (1906 loc) · 51.9 KB
/
process.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2023, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#define _CRT_SECURE_NO_WARNINGS 1
/*#define O_DEBUG 1*/
#undef O_DEBUG
#define _GNU_SOURCE /* get pipe2() */
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include "error.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PRCTL
#include <sys/prctl.h>
#endif
#ifdef _REENTRANT
#include <pthread.h>
#endif
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h> /* _NSGetEnviron() on MacOS */
#endif
static atom_t ATOM_stdin;
static atom_t ATOM_stdout;
static atom_t ATOM_stderr;
static atom_t ATOM_std;
static atom_t ATOM_null;
static atom_t ATOM_process;
static atom_t ATOM_detached;
static atom_t ATOM_cwd;
static atom_t ATOM_env;
static atom_t ATOM_environment;
static atom_t ATOM_priority;
static atom_t ATOM_window;
static atom_t ATOM_timeout;
static atom_t ATOM_release;
static atom_t ATOM_infinite;
static atom_t ATOM_text;
static atom_t ATOM_binary;
static atom_t ATOM_octet;
static atom_t ATOM_utf8;
static atom_t ATOM_ascii;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_unicode_be;
static atom_t ATOM_unicode_le;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_process_error2;
static functor_t FUNCTOR_system_error2;
static functor_t FUNCTOR_pipe1;
static functor_t FUNCTOR_pipe2;
static functor_t FUNCTOR_stream1;
static functor_t FUNCTOR_exit1;
static functor_t FUNCTOR_killed1;
static functor_t FUNCTOR_eq2; /* =/2 */
static functor_t FUNCTOR_type1;
static functor_t FUNCTOR_encoding1;
#define MAYBE 2
#if O_DEBUG
#define DEBUG(g) g
#else
#define DEBUG(g) (void)0
#endif
#ifdef __WINDOWS__
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
typedef wchar_t echar; /* environment character */
#define ecslen(s) wcslen(s)
#ifndef CREATE_BREAKAWAY_FROM_JOB
#define CREATE_BREAKAWAY_FROM_JOB 0x1000000
#endif
#else
typedef char echar;
#define ecslen(s) strlen(s)
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ISSUES:
- Deal with child errors (no cwd, cannot execute, etc.)
- Complete test suite
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*******************************
* ADMIN *
*******************************/
typedef enum std_type
{ std_std,
std_null,
std_pipe,
std_stream
} std_type;
typedef struct p_stream
{ term_t term; /* P in pipe(P) */
std_type type; /* type of stream */
IOENC encoding; /* Encoding for the stream */
#ifdef __WINDOWS__
HANDLE fd[2]; /* pipe handles */
#else
int fd[2]; /* pipe handles */
#endif
int cloexec; /* close on exec activated */
} p_stream;
typedef struct ecbuf
{ echar *buffer;
size_t size;
size_t allocated;
} ecbuf;
typedef struct p_options
{ atom_t exe_name; /* exe as atom */
#ifdef __WINDOWS__
wchar_t *exe; /* Executable */
wchar_t *cmdline; /* Command line */
wchar_t *cwd; /* CWD of new process */
#else
char *exe; /* Executable */
char **argv; /* argument vector */
char *cwd; /* CWD of new process */
char **envp; /* New environment */
#endif
ecbuf envbuf; /* environment buffer */
term_t pid; /* process(PID) */
int pipes; /* #pipes found */
p_stream streams[3];
int detached; /* create as detached */
int window; /* Show a window? */
int priority; /* Process priority */
} p_options;
typedef struct wait_options
{ double timeout;
int has_timeout;
int release;
} wait_options;
typedef enum create_method
{ PCREATE_SPAWN,
PCREATE_VFORK,
PCREATE_FORK
} create_method;
static create_method create_process_method = PCREATE_SPAWN;
#ifdef __WINDOWS__
static int win_command_line(term_t t, size_t arity,
const wchar_t *exepath, wchar_t **cmdline);
#endif
/*******************************
* STRING BUFFER *
*******************************/
static void
free_ecbuf(ecbuf *b)
{ if ( b->buffer )
{ PL_free(b->buffer);
b->buffer = NULL;
}
}
static int
add_ecbuf(ecbuf *b, echar *data, size_t len)
{ if ( b->size + len > b->allocated )
{ size_t newsize = (b->allocated ? b->allocated * 2 : 2048);
while( b->size + len > newsize )
newsize *= 2;
if ( b->buffer )
{ b->buffer = PL_realloc(b->buffer, newsize*sizeof(echar));
} else
{ b->buffer = PL_malloc(newsize*sizeof(echar));
}
b->allocated = newsize;
}
memcpy(b->buffer+b->size, data, len*sizeof(echar));
b->size += len;
return TRUE;
}
/*******************************
* ENVIRONMENT PARSING *
*******************************/
static int
get_echars_arg_ex(int i, term_t from, term_t arg, echar **sp, size_t *lenp)
{ const echar *s, *e;
if ( !PL_get_arg(i, from, arg) )
return FALSE;
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, lenp, sp,
CVT_ATOMIC|CVT_EXCEPTION) )
#else
if ( !PL_get_nchars(arg, lenp, sp,
CVT_ATOMIC|CVT_EXCEPTION|REP_FN) )
#endif
return FALSE;
for(s = *sp, e = s+*lenp; s<e; s++)
{ if ( !*s )
return PL_domain_error("text_non_zero_code", arg);
}
return TRUE;
}
#ifdef __WINDOWS__
#define ECHARS(s) L##s
#else
#define ECHARS(s) s
#endif
static int
already_in_env(const echar *env, int count, const echar *e)
{ for(; count-- > 0; env += ecslen(env)+1)
{ const echar *s, *q;
for(s=env, q=e; *s && *q && *s == *q && *s != '=' && *q != '='; s++,q++)
;
if (*s == *q && *s == '=' )
return TRUE;
}
return FALSE;
}
static int
parse_environment(term_t t, p_options *info, int pass)
{ term_t tail = PL_copy_term_ref(t);
term_t head = PL_new_term_ref();
term_t tmp = PL_new_term_ref();
ecbuf *eb = &info->envbuf;
int count = 0;
#ifndef __WINDOWS__
echar *q;
char **ep;
int c = 0;
#endif
if ( eb->buffer )
return PL_permission_error("redefine", "environment", t);
while( PL_get_list(tail, head, tail) )
{ echar *s;
size_t len;
if ( !PL_is_functor(head, FUNCTOR_eq2) )
return PL_type_error("environment_variable", head);
if ( !get_echars_arg_ex(1, head, tmp, &s, &len) )
return FALSE;
add_ecbuf(eb, s, len);
add_ecbuf(eb, ECHARS("="), 1);
if ( !get_echars_arg_ex(2, head, tmp, &s, &len) )
return FALSE;
add_ecbuf(eb, s, len);
add_ecbuf(eb, ECHARS("\0"), 1);
count++;
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
if ( pass && count == 0 )
return TRUE; /* environment([]) is a no-op */
if ( pass )
{
#ifndef __WINDOWS__
#ifdef HAVE__NSGETENVIRON
char **environ = *_NSGetEnviron();
#else
extern char **environ;
#endif
char **e;
int count0 = count;
for(e=environ; e && *e; e++)
{ if ( !already_in_env(eb->buffer, count0, *e) )
{ add_ecbuf(eb, *e, strlen(*e));
add_ecbuf(eb, ECHARS("\0"), 1);
count++;
}
}
#else /*__WINDOWS__*/
echar *env = GetEnvironmentStringsW();
if ( env )
{ int count0 = count;
while(*env)
{ size_t len = wcslen(env);
if ( !already_in_env(eb->buffer, count0, env) )
{ add_ecbuf(eb, env, len+1);
count++;
}
env += len+1;
}
}
#endif /*__WINDOWS__*/
}
#ifdef __WINDOWS__
add_ecbuf(eb, ECHARS("\0"), 1);
if ( count == 0 )
add_ecbuf(eb, ECHARS("\0"), 1);
#else
info->envp = PL_malloc((count+1)*sizeof(char*));
for(ep=info->envp, c=0, q=eb->buffer; c<count; c++, ep++)
{ *ep = q;
q += strlen(q)+1;
}
assert((size_t)(q-eb->buffer) == eb->size);
*ep = NULL;
#endif
return TRUE;
}
static int
get_type(term_t head, IOENC *enc)
{ atom_t a;
if ( PL_get_atom_ex(head, &a) )
{ if ( a == ATOM_text )
*enc = ENC_ANSI;
else if ( a == ATOM_binary )
*enc = ENC_OCTET;
else
return PL_domain_error("stream_type", head);
return TRUE;
}
return FALSE;
}
/* TBD: provide a public API for translating encoding names to
* the IOENC enum
*/
static int
get_encoding(term_t head, IOENC *enc)
{ atom_t a;
if ( PL_get_atom_ex(head, &a) )
{ IOENC e;
if ( (e=PL_atom_to_encoding(a)) != ENC_UNKNOWN )
{ *enc = e;
return TRUE;
}
return PL_domain_error("encoding", head);
}
return FALSE;
}
static int
get_stream(term_t t, p_options *info, p_stream *stream, atom_t name)
{ atom_t a;
int i;
if ( PL_get_atom(t, &a) )
{ if ( a == ATOM_null )
{ stream->type = std_null;
return TRUE;
} else if ( a == ATOM_std )
{ stream->type = std_std;
return TRUE;
} else
{ return PL_domain_error("process_stream", t);
}
} else if ( PL_is_functor(t, FUNCTOR_pipe1) ||
PL_is_functor(t, FUNCTOR_pipe2) )
{ stream->term = PL_new_term_ref();
stream->encoding = ENC_ANSI;
_PL_get_arg(1, t, stream->term);
if ( !PL_is_variable(stream->term) )
{ for (i = 0; i < info->pipes; i++)
{ if (PL_compare(info->streams[i].term, t) == 0)
break;
}
if (i == info->pipes)
return PL_uninstantiation_error(stream->term);
}
if ( PL_is_functor(t, FUNCTOR_pipe2) )
{ term_t tail = PL_new_term_ref();
term_t head = PL_new_term_ref();
_PL_get_arg(2, t, tail);
while(PL_get_list_ex(tail, head, tail))
{ if ( PL_is_functor(head, FUNCTOR_type1) )
{ _PL_get_arg(1, head, head);
if ( !get_type(head, &stream->encoding) )
return FALSE;
} else if ( PL_is_functor(head, FUNCTOR_encoding1) )
{ _PL_get_arg(1, head, head);
if ( !get_encoding(head, &stream->encoding) )
return FALSE;
} else
return PL_domain_error("pipe_option", head);
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
}
stream->type = std_pipe;
info->pipes++;
return TRUE;
} else if ( PL_is_functor(t, FUNCTOR_stream1) )
{ IOSTREAM *s;
stream->term = PL_new_term_ref();
_PL_get_arg(1, t, stream->term);
if ( !PL_get_stream(stream->term, &s,
name == ATOM_stdin ? SIO_INPUT : SIO_OUTPUT) )
return FALSE;
stream->type = std_stream;
#ifdef __WINDOWS__
HANDLE h = Swinhandle(s);
if ( h )
stream->fd[0] = stream->fd[1] = h;
else
return PL_domain_error("file_stream", stream->term);
#else
int fd = Sfileno(s);
if ( fd >= 0 )
stream->fd[0] = stream->fd[1] = fd;
else
return PL_domain_error("file_stream", stream->term);
#endif
return TRUE;
} else
return PL_type_error("process_stream", t);
}
static int
parse_options(term_t options, p_options *info)
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
term_t arg = PL_new_term_ref();
info->window = MAYBE;
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return PL_type_error("option", head);
_PL_get_arg(1, head, arg);
if ( name == ATOM_stdin )
{ if ( !get_stream(arg, info, &info->streams[0], name) )
return FALSE;
} else if ( name == ATOM_stdout )
{ if ( !get_stream(arg, info, &info->streams[1], name) )
return FALSE;
} else if ( name == ATOM_stderr )
{ if ( !get_stream(arg, info, &info->streams[2], name) )
return FALSE;
} else if ( name == ATOM_process )
{ info->pid = PL_copy_term_ref(arg);
} else if ( name == ATOM_detached )
{ if ( !PL_get_bool_ex(arg, &info->detached) )
return FALSE;
} else if ( name == ATOM_cwd )
{
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, NULL, &info->cwd,
CVT_ATOM|CVT_STRING|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
#else
if ( !PL_get_chars(arg, &info->cwd,
CVT_ATOM|CVT_STRING|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
#endif
} else if ( name == ATOM_window )
{ if ( !PL_get_bool_ex(arg, &info->window) )
return FALSE;
} else if ( name == ATOM_env )
{ if ( !parse_environment(arg, info, FALSE) )
return FALSE;
} else if ( name == ATOM_environment )
{ if ( !parse_environment(arg, info, TRUE) )
return FALSE;
} else if ( name == ATOM_priority )
{ int tmp;
if ( !PL_get_integer_ex(arg, &tmp) )
return FALSE;
if ( tmp < -20 || tmp > 19 )
return PL_domain_error("priority_option", arg);
info->priority = tmp;
} else
return PL_domain_error("process_option", head);
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
return TRUE;
}
static int
get_exe(term_t exe, p_options *info)
{ size_t arity;
term_t arg = PL_new_term_ref();
if ( !PL_get_name_arity(exe, &info->exe_name, &arity) )
return PL_type_error("callable", exe);
PL_put_atom(arg, info->exe_name);
#ifdef __WINDOWS__
if ( !PL_get_wchars(arg, NULL, &info->exe, CVT_ATOM|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
if ( !win_command_line(exe, arity, info->exe, &info->cmdline) )
return FALSE;
#else /*__WINDOWS__*/
if ( !PL_get_chars(arg, &info->exe, CVT_ATOM|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
if ( !(info->argv = PL_malloc((arity+2)*sizeof(char*))) )
return PL_resource_error("memory");
memset(info->argv, 0, (arity+2)*sizeof(char*));
if ( !(info->argv[0] = PL_malloc(strlen(info->exe)+1)) )
return PL_resource_error("memory");
strcpy(info->argv[0], info->exe);
{ size_t i;
for(i=1; i<=arity; i++)
{ _PL_get_arg(i, exe, arg);
if ( !PL_get_chars(arg, &info->argv[i],
CVT_ATOMIC|CVT_EXCEPTION|BUF_MALLOC|REP_FN) )
return FALSE;
}
info->argv[i] = NULL;
}
#endif /*__WINDOWS__*/
return TRUE;
}
static void
free_options(p_options *info) /* TBD: close streams */
{ if ( info->exe )
{ PL_free(info->exe);
info->exe = NULL;
}
if ( info->cwd )
{ PL_free(info->cwd);
info->cwd = NULL;
}
#ifndef __WINDOWS__
if ( info->envp )
{ PL_free(info->envp);
info->envp = NULL;
}
#endif
free_ecbuf(&info->envbuf);
#ifdef __WINDOWS__
if ( info->cmdline )
{ PL_free(info->cmdline);
info->cmdline = NULL;
}
#else /*__WINDOWS__*/
if ( info->argv )
{ char **a;
for(a=info->argv; *a; a++)
{ if ( *a )
PL_free(*a);
}
PL_free(info->argv);
info->argv = NULL;
}
#endif /*__WINDOWS__*/
}
/*******************************
* PROCESS READS *
*******************************/
#define PROCESS_MAGIC 0x29498001
typedef struct process_context
{ int magic; /* PROCESS_MAGIC */
#ifdef __WINDOWS__
HANDLE handle; /* process handle */
#else
pid_t pid; /* the process id */
#endif
int open_mask; /* Open streams */
int pipes[3]; /* stdin/stdout/stderr */
atom_t exe_name; /* exe as atom */
} process_context;
static int wait_for_process(process_context *pc);
static int
process_fd(void *handle, process_context **PC)
{ process_context *pc = (process_context*) ((uintptr_t)handle&~(uintptr_t)0x3);
int pipe = (int)(uintptr_t)handle & 0x3;
if ( pc->magic == PROCESS_MAGIC )
{ if ( PC )
*PC = pc;
return pc->pipes[pipe];
}
return -1;
}
static ssize_t
Sread_process(void *handle, char *buf, size_t size)
{ int fd = process_fd(handle, NULL);
return (*Sfilefunctions.read)((void*)(uintptr_t)fd, buf, size);
}
static ssize_t
Swrite_process(void *handle, char *buf, size_t size)
{ int fd = process_fd(handle, NULL);
return (*Sfilefunctions.write)((void*)(uintptr_t)fd, buf, size);
}
static int
Sclose_process(void *handle)
{ process_context *pc;
int fd = process_fd(handle, &pc);
if ( fd >= 0 )
{ int which = (int)(uintptr_t)handle & 0x3;
int rc;
rc = (*Sfilefunctions.close)((void*)(uintptr_t)fd);
pc->open_mask &= ~(1<<which);
DEBUG(Sdprintf("Closing fd[%d]; mask = 0x%x\n", which, pc->open_mask));
if ( !pc->open_mask )
{ int rcw = wait_for_process(pc);
return rcw ? 0 : -1;
}
return rc;
}
return -1;
}
static int
Scontrol_process(void *handle, int action, void *arg)
{ process_context *pc;
int fd = process_fd(handle, &pc);
switch(action)
{ case SIO_GETFILENO:
{ int *fdp = arg;
*fdp = fd;
return 0;
}
default:
return (*Sfilefunctions.control)((void*)(uintptr_t)fd, action, arg);
}
}
static IOFUNCTIONS Sprocessfunctions =
{ Sread_process,
Swrite_process,
NULL, /* seek */
Sclose_process,
Scontrol_process,
NULL /* seek64 */
};
static IOSTREAM *
open_process_pipe(process_context *pc, p_options *info, int which, int fdn)
{ void *handle;
#ifdef __WINDOWS__
HANDLE fd = info->streams[which].fd[fdn];
#else
int fd = info->streams[which].fd[fdn];
#endif
int flags = SIO_RECORDPOS|SIO_FBUF;
IOSTREAM *s;
pc->open_mask |= (1<<which);
#ifdef __WINDOWS__
pc->pipes[which] = Swin_open_osfhandle(fd, _O_BINARY);
#else
pc->pipes[which] = fd;
#endif
if ( info->streams[which].encoding != ENC_OCTET )
flags |= SIO_TEXT;
if ( which == 0 )
flags |= SIO_OUTPUT;
else
flags |= SIO_INPUT;
handle = (void *)((uintptr_t)pc | (uintptr_t)which);
if ( (s=Snew(handle, flags, &Sprocessfunctions)) )
s->encoding = info->streams[which].encoding;
return s;
}
/*******************************
* OS STUFF *
*******************************/
#ifdef __WINDOWS__
CRITICAL_SECTION process_lock;
#define LOCK() EnterCriticalSection(&process_lock);
#define UNLOCK() LeaveCriticalSection(&process_lock);
/* We need a root job to put non-detached processes into */
static HANDLE rootJob = (HANDLE)0;
static IOFUNCTIONS Sprocessfilefunctions = { 0, };
static void
win_init(void)
{ InitializeCriticalSection(&process_lock);
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
rootJob = CreateJobObject(NULL, NULL);
if (rootJob == NULL) /* Already in a job */
return;
memset(&jeli, 0, sizeof(jeli));
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
/* This will fail (silently) if we dont have permission to manage processes */
SetInformationJobObject(rootJob, JobObjectExtendedLimitInformation,
&jeli, sizeof(jeli));
memcpy(&Sprocessfilefunctions, &Sfilefunctions, sizeof(IOFUNCTIONS));
Sprocessfilefunctions.seek = NULL;
Sprocessfilefunctions.seek64 = NULL;
}
#include "win_error.c"
typedef struct arg_string
{ size_t len;
wchar_t *text;
wchar_t quote;
} arg_string;
#define QMISC 0x1
#define QDBLQ 0x2
#define QSBLQ 0x4
static int
set_quote(arg_string *as)
{ int needq = 0;
const wchar_t *s = as->text;
for(; *s; s++)
{ if ( !iswalnum(*s) )
{ if ( *s == '"' )
needq |= QDBLQ;
else if ( *s == '\'' )
needq |= QSBLQ;
else
needq |= QMISC;
}
}
if ( !needq )
{ as->quote = 0;
return TRUE;
}
needq &= ~QMISC;
switch( needq )
{ case QDBLQ:
as->quote = '\'';
return TRUE;
case 0:
case QSBLQ:
as->quote = '"';
return TRUE;
default:
return FALSE;
}
}
static int
win_command_line(term_t t, size_t arity, const wchar_t *exe, wchar_t **cline)
{ if ( arity > 0 )
{ arg_string *av = PL_malloc((arity+1)*sizeof(*av));
term_t arg = PL_new_term_ref();
size_t cmdlen;
wchar_t *cmdline, *o;
const wchar_t *b;
size_t i;
if ( (b=wcsrchr(exe, '\\')) )
b++;
else
b = exe;
av[0].text = (wchar_t*)b;
av[0].len = wcslen(av[0].text);
set_quote(&av[0]);
cmdlen = av[0].len+(av[0].quote?2:0)+1;
for( i=1; i<=arity; i++)
{ _PL_get_arg(i, t, arg);
if ( !PL_get_wchars(arg, &av[i].len, &av[i].text,
CVT_ATOMIC|CVT_EXCEPTION|BUF_MALLOC) )
return FALSE;
if ( wcslen(av[i].text) != av[i].len )
return PL_domain_error("no_zero_code_atom", arg);
if ( !set_quote(&av[i]) )
return PL_domain_error("dos_quotable_atom", arg);
cmdlen += av[i].len+(av[i].quote?2:0)+1;
}
cmdline = PL_malloc(cmdlen*sizeof(wchar_t));
for( o=cmdline,i=0; i<=arity; )
{ wchar_t *s = av[i].text;
if ( av[i].quote )
*o++ = av[i].quote;
wcsncpy(o, s, av[i].len);
o += av[i].len;
if ( i > 0 )
PL_free(s); /* do not free shared exename */
if ( av[i].quote )
*o++ = av[i].quote;
if (++i <= arity)
*o++ = ' ';
}
*o = 0;
PL_free(av);
*cline = cmdline;
} else
{ *cline = NULL;
}
return TRUE;
}
typedef struct win_process
{ DWORD pid;
HANDLE handle;
HANDLE job;
struct win_process *next;
} win_process;
static win_process *processes;
static void
register_process(DWORD pid, HANDLE h, HANDLE job)
{ win_process *wp = PL_malloc(sizeof(*wp));
wp->pid = pid;
wp->handle = h;
wp->job = job;
LOCK();
wp->next = processes;
processes = wp;
UNLOCK();
}
static int
unregister_process(DWORD pid)
{ win_process **wpp, *wp;
LOCK();
for(wpp=&processes, wp=*wpp; wp; wpp=&wp->next, wp=*wpp)
{ if ( wp->pid == pid )
{ *wpp = wp->next;
PL_free(wp);
UNLOCK();
return TRUE;
}
}