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
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
#pike __REAL_VERSION__ 
 
constant create_process = __builtin.create_process; 
 
#if constant(__builtin.TraceProcess) 
constant TraceProcess = __builtin.TraceProcess; 
#endif 
 
#if defined(__NT__) 
constant path_separator = ";"; 
#else 
constant path_separator = ":"; 
#endif 
 
#if constant(Stdio.__HAVE_SEND_FD__) 
protected Stdio.File forkd_pipe; 
protected create_process forkd_pid; 
 
//! Encoder for data to be sent to @[Tools.Standalone.forkd]. 
//! 
//! @seealso 
//!   @[ForkdDecoder] 
class ForkdEncoder(Stdio.File remote_fd) 
{ 
  int fd_counter; 
  mixed nameof(mixed what) 
  { 
    if (objectp(what)) { 
      if (what->_fd) { 
        remote_fd->send_fd(what->_fd); 
        return fd_counter++; 
      } 
    } 
    error("Unsupported argument for RemoteProcess.\n"); 
  } 
} 
 
//! Decoder for data received by @[Tools.Standalone.forkd]. 
//! 
//! @seealso 
//!   @[ForkdEncoder] 
class ForkdDecoder(array(Stdio.Fd) fds) 
{ 
  object objectof(mixed code) 
  { 
    return fds[code]; 
  } 
} 
 
// Ensure that there's a live forkd running. 
protected int assert_forkd() 
{ 
  if (!forkd_pid || !forkd_pid->kill(0)) { 
    forkd_pipe = Stdio.File(); 
    forkd_pid = spawn_pike(({ "-x", "forkd" }), 
                           ([ 
                             "forkd":0, 
                             "uid":getuid(), 
                             "gid":getgid(), 
                             "noinitgroups":1, 
                             "fds":({ 
                               forkd_pipe->pipe(Stdio.PROP_BIDIRECTIONAL| 
                                                Stdio.PROP_SEND_FD), 
                             }), 
                           ])); 
    if (forkd_pipe->read(1) != "\0") { 
      // werror("Failed to start forkd.\n"); 
      return 0; 
    } 
  } 
  return forkd_pid && forkd_pid->kill(0); 
} 
#endif 
 
protected int(0..1) forkd_default = 0; 
 
//! Set the default value for the @expr{"forkd"@} modifier 
//! to @[Process]. 
//! 
//! @note 
//!   The default default value is @expr{0@} (zero). 
//! 
//! @seealso 
//!   @[get_forkd_default()], @[Process()->create()] 
void set_forkd_default(int(0..1) mode) 
{ 
  forkd_default = mode; 
} 
 
//! Get the default value for the @expr{"forkd"@} modifier 
//! to @[Process]. 
//! 
//! @note 
//!   The default default value is @expr{0@} (zero). 
//! 
//! @seealso 
//!   @[set_forkd_default()], @[Process()->create()] 
int(0..1) get_forkd_default() 
{ 
  return forkd_default; 
} 
 
//! Slightly polished version of @[create_process]. 
//! 
//! In addition to the features supported by @[create_process], 
//! it also supports: 
//! 
//! @ul 
//!   @item 
//!     Callbacks on timeout and process termination. 
//!   @item 
//!     Using @[Tools.Standalone.forkd] via RPC to 
//!     spawn the new process. 
//! @endul 
//! 
//! @seealso 
//!   @[create_process], @[Tools.Standalone.forkd] 
class Process 
{ 
  //! Based on @[create_process]. 
  inherit create_process; 
 
  protected function(Process:void) read_cb; 
  protected function(Process:void) timeout_cb; 
 
  protected Stdio.File process_fd; 
 
  protected Pike.Backend process_backend; 
 
  protected mixed process_poll; 
 
  //! @param command_args 
  //!   Either a command line array, as the command_args 
  //!   argument to @[create_process()], or a string that 
  //!   will be splitted into a command line array by 
  //!   calling @[split_quoted_string()] in an operating 
  //!   system dependant mode. 
  //! @param modifiers 
  //!   In addition to the modifiers that @[create_process] accepts, 
  //!   this object also accepts 
  //!   @mapping 
  //!     @member function(Process:void) "read_callback" 
  //!       This callback is called when there is data to be read 
  //!       from the process. 
  //!     @member function(Process:void) "timeout_callback" 
  //!       This callback is called if the process times out. 
  //!     @member int "timeout" 
  //!       The time it takes for the process to time out. Default is 
  //!       15 seconds. 
  //!     @member int(0..1) "forkd" 
  //!       Use @[Tools.Standalone.forkd] to actually spawn the process. 
  //!   @endmapping 
  //! 
  //! @note 
  //!   The default value for the @expr{"forkd"@} modifier may be 
  //!   set via @[set_forkd_default()]. 
  //! 
  //! @seealso 
  //!   @[create_process], @[create_process()->create()], 
  //!   @[split_quoted_string()], @[Tools.Standalone.forkd], 
  //!   @[set_forkd_default()], @[get_forkd_default()] 
  protected void create( string|array(string) command_args, 
                         void|mapping(string:mixed) modifiers ) 
  { 
    if( stringp( command_args ) ) { 
      command_args = split_quoted_string( [string]command_args 
#ifdef __NT__ 
                                ,1 
#endif /* __NT__ */ 
                                ); 
    } 
 
    mapping(string:mixed) new_modifiers = (modifiers || ([])) + ([]); 
 
    if (new_modifiers->keep_signals) { 
      // This option is currently not supported with forkd. 
      m_delete(new_modifiers, "forkd"); 
    } 
 
#if constant(Stdio.__HAVE_SEND_FD__) 
    // Forkd mode requires send_fd(). 
    if (undefinedp(new_modifiers->forkd)) 
      new_modifiers->forkd = forkd_default; 
    if (new_modifiers->forkd && assert_forkd()) { 
      process_fd = Stdio.File(); 
      forkd_pipe-> 
        send_fd(process_fd->pipe(Stdio.PROP_BIDIRECTIONAL|Stdio.PROP_SEND_FD)); 
      while (forkd_pipe->write("\0") < 0) 
        ; 
 
      m_delete(new_modifiers, "forkd"); 
      __callback = m_delete(new_modifiers, "callback"); 
 
      if (undefinedp(new_modifiers->uid)) { 
        new_modifiers->uid = geteuid(); 
        if (undefinedp(new_modifiers->gid)) { 
          new_modifiers->gid = getegid(); 
        } 
        if (!new_modifiers->setgroups) { 
          new_modifiers->setgroups = getgroups(); 
        } 
      } else if (new_modifiers->noinitgroups) { 
        if (!new_modifiers->setgroups) { 
          new_modifiers->setgroups = getgroups(); 
        } 
      } 
      m_delete(new_modifiers, "noinitgroups"); 
 
      if (!new_modifiers->env) { 
        new_modifiers->env = getenv(); 
      } 
 
      if (new_modifiers->cwd) { 
        new_modifiers->cwd = combine_path(getcwd(), new_modifiers->cwd); 
      } else { 
        new_modifiers->cwd = getcwd(); 
      } 
 
      if (!new_modifiers->stdin) new_modifiers->stdin = Stdio.stdin; 
      if (!new_modifiers->stdout) new_modifiers->stdout = Stdio.stdout; 
      if (!new_modifiers->stderr) new_modifiers->stderr = Stdio.stderr; 
 
      string data = encode_value(({ command_args, new_modifiers }), 
                                 ForkdEncoder(process_fd)); 
      data = sprintf("%4c%s", sizeof(data), data); 
      // Wait for forkd to acknowledge the process_fd. 
      if (process_fd->read(4) != "\0\0\0\0") { 
        process_fd->close(); 
        process_fd = UNDEFINED; 
        error("Failed to create spawn request.\n"); 
      } 
      int bytes = process_fd->write(data); 
      if (bytes != sizeof(data)) { 
        int fd_errno = process_fd->errno(); 
        process_fd->close(); 
        process_fd = UNDEFINED; 
        error("Failed to write spawn request (%d != %d, errno: %d, errmsg: %s).\n", 
              bytes, sizeof(data), fd_errno, strerror(fd_errno) || "?"); 
      } 
      process_backend = Pike.SmallBackend(); 
      process_backend->add_file(process_fd); 
      process_fd->set_nonblocking(got_data, UNDEFINED, got_close); 
 
      // Wait for the process to fork (or fail). 
      while (__status == -1) { 
        process_backend(3600.0); 
      } 
 
      if (process_fd) { 
        process_poll = call_out(do_poll_loop, 0.1); 
      } 
    } else 
#endif 
    { 
      ::create( [array(string)]command_args, new_modifiers ); 
    } 
 
    if (modifiers) { 
      if(read_cb = modifiers->read_callback) 
        call_out(watcher, 0.1); 
 
      if( (timeout_cb = modifiers->timeout_callback) || 
          modifiers->timeout ) 
        call_out(killer, modifiers->timeout||15); 
    } 
  } 
 
#if constant(Stdio.__HAVE_SEND_FD__) 
  protected void do_close() 
  { 
    if (process_fd) { 
      process_fd->set_blocking(); 
      process_fd->close(); 
    } 
    process_fd = UNDEFINED; 
    process_backend = UNDEFINED; 
    remove_call_out(process_poll); 
  } 
 
  protected string recv_buf = ""; 
  protected void got_data(mixed ignored, string data) 
  { 
    recv_buf += data; 
    while (sizeof(recv_buf) > 3) { 
      while (has_prefix(recv_buf, "\0\0\0\0")) recv_buf = recv_buf[4..]; 
      if (sizeof(recv_buf) < 5) return; 
      int len = 0; 
      sscanf(recv_buf, "%04c", len); 
      if (sizeof(recv_buf) < len + 4) return; 
      data = recv_buf[4..len+3]; 
      recv_buf = recv_buf[len+4..]; 
      [string tag, mixed packet] = decode_value(data); 
 
      switch(tag) { 
      case "ERROR": 
        __result = 255; 
        __status = 2; 
        do_close(); 
        error(packet); 
        break; 
      case "PID": 
        __pid = packet; 
        if (__status == -1) __status = 0; 
        if (__callback) __callback(this); 
        break; 
      case "SIGNAL": 
        __last_signal = packet; 
        break; 
      case "START": 
        __status = 0; 
        if (__callback) __callback(this); 
        break; 
      case "STOP": 
        __status = 1; 
        if (__callback) __callback(this); 
        break; 
      case "EXIT": 
        __result = packet; 
        __status = 2; 
        do_close(); 
        if (__callback) __callback(this); 
        break; 
      default: 
        __result = 255; 
        __status = 2; 
        do_close(); 
        error("Unsupported packet from forkd: %O\n", tag); 
        break; 
      } 
    } 
  } 
 
  protected void got_close(mixed ignored) 
  { 
    do_close(); 
    if (__status == -1) { 
      // Early close. 
      __result = 255; 
      __status = 2; 
    } 
  } 
 
  protected void do_poll(float t) 
  { 
    mixed err = catch { 
        process_backend && process_backend(t); 
        return; 
      }; 
    // Filter errors about the backend already running. 
    if (arrayp(err) && sizeof(err) && 
        stringp(err[0]) && has_prefix(err[0], "Backend already ")) { 
      sleep(t); 
      return; 
    } 
    throw(err); 
  } 
 
  protected void do_poll_loop() 
  { 
    process_poll = UNDEFINED; 
    if (!process_backend) return; 
    do_poll(0.0); 
    process_poll = call_out(do_poll_loop, 0.1); 
  } 
 
  int last_signal() 
  { 
    do_poll(0.0); 
    return ::last_signal(); 
  } 
 
  int(-1..2) status() 
  { 
    do_poll(0.0); 
    return ::status(); 
  } 
 
  int wait() 
  { 
    if (process_backend) { 
      float t = 0.0; 
      do { 
        do_poll(t); 
        if (t < 1.0) t += 0.1; 
      } while (__status <= 0); 
      return __result; 
    } 
    return ::wait(); 
  } 
#endif /* __HAVE_SEND_FD__ */ 
 
  protected void _destruct() { 
    remove_call_out(watcher); 
    remove_call_out(killer); 
#if constant(Stdio.__HAVE_SEND_FD__) 
    remove_call_out(process_poll); 
#endif 
  } 
 
  protected void watcher() { 
    // It was another sigchld, but not one from our process. 
    if(status()==0) 
      call_out(watcher, 0.1); 
    else { 
      remove_call_out(killer); 
      if(read_cb) read_cb(this); 
    } 
  } 
 
  protected void killer() { 
    remove_call_out(watcher); 
#if constant(kill) 
    ::kill(signum("SIGKILL")); 
#endif 
    if(timeout_cb) timeout_cb(this); 
  } 
} 
 
// FIXME: Should probably be located elsewhere. 
string locate_binary(array(string) path, string name) 
{ 
#ifdef __NT__ 
  if( !has_suffix(lower_case(name), ".exe") ) 
    name += ".exe"; 
#endif 
 
  foreach(path, string dir) 
  { 
    string fname = combine_path(dir, name); 
    Stdio.Stat info = file_stat(fname); 
    if (info && (info->mode & 0111)) 
      return fname; 
  } 
  return 0; 
} 
 
protected array(string) runpike; 
 
//! Spawn a new pike process similar to the current. 
//! 
//! @param argv 
//!   Arguments for the new process. 
//! 
//! @param options 
//!   Process creation options. See @[Process.Process] for details. May also 
//!   specify "add_predefines", "add_program_path", or "add_include_path" in 
//!   order to include these components in command path (module path is 
//!   included by default.) 
//! 
//! @param launcher 
//!   Optional launcher prefix command used to spawn the pike binary. 
//! 
//!   When used this is typically something like 
//!   @expr{({ "/usr/bin/valgrind" })@}. 
//! 
//!   Defaults to the empty array. 
//! 
//! @seealso 
//!   @[Process.Process] 
Process spawn_pike(array(string) argv, void|mapping(string:mixed) options, 
                   array(string)|void launcher) 
{ 
  if (!runpike) { 
    array(string) res = ({ 
      master()->_pike_file_name, 
    }); 
    if (master()->_master_file_name) 
      res+=({"-m"+master()->_master_file_name}); 
    foreach (master()->pike_module_path;;string path) 
      res+=({"-M" + path}); 
    if(options && options->add_predefines) 
    { 
      foreach (master()->predefines; string key; string value) 
        if( stringp( value ) ) 
          res+=({"-D" + key + "=" + value}); 
        else if( intp( value ) ) 
          res+=({"-D" + key }); 
    } 
    if(options && options->add_program_path) 
    { 
      foreach (master()->pike_program_path;; string value) 
        res+=({"-P" + value}); 
    } 
    if(options && options->add_include_path) 
    { 
      foreach (master()->pike_include_path;; string value) 
        res+=({"-I" + value}); 
    } 
 
    if (sizeof(res) && !has_value(res[0],"/") 
#ifdef __NT__ 
      && !has_value(res[0], "\\") 
#endif /* __NT__ */ 
      ) 
      res[0] = search_path(res[0]); 
    runpike = res; 
  } 
  if (!launcher) launcher = ({}); 
  return Process(launcher + runpike + argv, options); 
} 
 
//! Easy and lazy way of using @[Process.Process] that runs a process 
//! and returns a mapping with the output and exit code without 
//! having to make sure you read nonblocking yourself. 
//! 
//! @param args 
//!   Either a command line array, as the command_args 
//!   argument to @[create_process()], or a string that 
//!   will be splitted into a command line array by 
//!   calling @[split_quoted_string()] in an operating 
//!   system dependant mode. 
//! @param modifiers 
//!   It takes all the modifiers @[Process.Process] accepts, with 
//!   the exception of stdout and stderr. Each must be either absent, or 
//!   a function accepting a string; if present, the functions will be called 
//!   whenever output is made on the corresponding stream, otherwise the data 
//!   will be collected and returned in the result mapping. 
//! 
//!   If @expr{modifiers->stdin@} is set to a string it will automatically be 
//!   converted to a pipe that is fed to stdin of the started process. 
//! 
//! @seealso 
//!   @[Process.Process] @[create_process] 
//! 
//! @returns 
//!   @mapping 
//!     @member string "stdout" 
//!       Everything the process wrote on stdout, unless a stdout function was 
//!       provided. 
//!     @member string "stderr" 
//!       Everything the process wrote on stderr, similarly. 
//!     @member int "exitcode" 
//!       The process' exitcode. 
//!   @endmapping 
//! 
//! @note 
//!   As the entire output of stderr and stdout is stored in the 
//!   returned mapping it could potentially grow until memory runs out. 
//!   It is therefore advisable to set up rlimits if the output has a 
//!   potential to be very large, or else provide functions to handle 
//!   partial data. 
//! 
//! @example 
//!   Process.run( ({ "ls", "-l" }) ); 
//!   Process.run( ({ "ls", "-l" }), ([ "cwd":"/etc" ]) ); 
//!   Process.run( "ls -l" ); 
//!   Process.run( "awk -F: '{print $2}'", ([ "stdin":"foo:2\nbar:17\n" ]) ); 
mapping run(string|array(string) cmd, void|mapping modifiers) 
{ 
  string gotstdout="", gotstderr="", stdin_str; 
  int exitcode; 
 
  if(!modifiers) 
    modifiers = ([]); 
 
  if((modifiers->stdout && !callablep(modifiers->stdout)) 
    || (modifiers->stderr && !callablep(modifiers->stderr))) 
    throw( ({ "Can not redirect stdout or stderr in Process.run, " 
              "please use Process.Process instead.", backtrace() }) ); 
 
  Stdio.File mystdout = Stdio.File(); 
  Stdio.File mystderr = Stdio.File(); 
  Stdio.File mystdin; 
 
  object p; 
  if(stringp(modifiers->stdin)) 
  { 
    mystdin = Stdio.File(); 
    stdin_str = modifiers->stdin; 
    p = Process(cmd, modifiers + ([ 
                  "stdout":mystdout->pipe(), 
                  "stderr":mystderr->pipe(), 
                  "stdin":mystdin->pipe(Stdio.PROP_IPC|Stdio.PROP_REVERSE) 
                ])); 
  } 
  else 
    p = Process(cmd, modifiers + ([ 
                  "stdout":mystdout->pipe(), 
                  "stderr":mystderr->pipe(), 
                ])); 
 
#if 0 //constant(Thread.Thread) 
  // This is disabled by default since the callback alternative is 
  // much more lightweight - creating threads isn't cheap. 
  array threads = ({ 
      thread_create( lambda() { gotstdout = mystdout->read(); } ), 
      thread_create( lambda() { gotstderr = mystderr->read(); } ) 
    }); 
 
  if (mystdin) { 
    threads += ({ 
      thread_create(lambda(Stdio.File f) { f->write(stdin_str); f->close(); }, mystdin ) 
    }); 
    mystdin = 0; 
  } 
 
  exitcode = p->wait(); 
  threads->wait(); 
#else 
  Pike.SmallBackend backend = Pike.SmallBackend(); 
 
  mystdout->set_backend (backend); 
  mystderr->set_backend (backend); 
 
  mystdout->set_read_callback( lambda( mixed i, string data) { 
                                 if (modifiers->stdout) modifiers->stdout(data); 
                                 else gotstdout += data; 
                               } ); 
  mystderr->set_read_callback( lambda( mixed i, string data) { 
                                 if (modifiers->stderr) modifiers->stderr(data); 
                                 else gotstderr += data; 
                               } ); 
  mystdout->set_close_callback( lambda () { 
                                  mystdout->set_read_callback(0); 
                                  catch { mystdout->close(); }; 
                                  mystdout = 0; 
                                }); 
  mystderr->set_close_callback( lambda () { 
                                  mystderr->set_read_callback(0); 
                                  catch { mystderr->close(); }; 
                                  mystderr = 0; 
                                }); 
 
#if constant(Shuffler) 
  if (mystdin) { 
    Shuffler.Shuffler sfr = Shuffler.Shuffler(); 
    sfr->set_backend (backend); 
    Shuffler.Shuffle sf = sfr->shuffle( mystdin ); 
    sf->add_source(stdin_str); 
    sf->set_done_callback (lambda () { 
                             catch { mystdin->close(); }; 
                             mystdin = 0; 
                           }); 
    sf->start(); 
  } 
#endif 
 
  while( mystdout || mystderr || mystdin ) 
    backend( 1.0 ); 
 
  exitcode = p->wait(); 
#endif 
 
  return ([ "stdout"  : gotstdout, 
            "stderr"  : gotstderr, 
            "exitcode": exitcode   ]); 
} 
 
#if constant(exece) 
//! 
int exec(string file,string ... foo) 
{ 
  if (sizeof(file)) { 
    string path; 
 
    if(has_value(file,"/")) 
      return exece(combine_path(getcwd(),file),foo, 
                   [mapping(string:string)]getenv()); 
 
    path=[string]getenv("PATH"); 
 
    foreach(path ? path/":" : ({}) , path) 
      if(file_stat(path=combine_path(path,file))) 
        return exece(path, foo, [mapping(string:string)]getenv()); 
  } 
  return 69; 
} 
#endif 
 
protected string search_path_raw; 
protected array(string) search_path_entries=0; 
 
//! Search for the path to an executable. 
//! 
//! @param command 
//!   Executable to search for. 
//! 
//! Searches for @[command] in the directories listed in the 
//! environment variable @tt{$PATH@}. 
//! 
//! @returns 
//!   Returns the path to @[command] if found, and 
//!   @expr{0@} (zero) on failure. 
//! 
//! @note 
//!   This function is NOT thread safe if the environment 
//!   variable @tt{$PATH@} is being changed concurrently. 
//! 
//! @note 
//!   In Pike 7.8.752 and earlier the environment 
//!   variable @tt{$PATH@} was only read once. 
string search_path(string command) 
{ 
   if (command=="" || command[0]=='/') return command; 
 
   string path = getenv("PATH")||""; 
   if ((path != search_path_raw) || !search_path_entries) 
   { 
      string raw_path = path; 
#ifdef __NT__ 
      path = replace(path, "\\", "/"); 
#endif 
      array(string) e = path/path_separator - ({""}); 
 
      multiset(string) filter=(<>); 
      array(string) entries=({}); 
      foreach (e,string s) 
      { 
         string t; 
         if (s[0]=='~')  // some shells allow ~-expansion in PATH 
         { 
            if (s[0..1]=="~/" && (t=System.get_home())) 
               s=t+s[1..]; 
            else 
            { 
               // expand user? 
            } 
#ifdef __NT__ 
       } else if (sizeof(s) && (s[0] == '"') && s[-1] == '"') { 
           // Windows doesn't seem to strip quotation marks from PATH 
           // components that contain them so we need to do that here, 
           // otherwise we'll end up with a bogus path to stat on. 
           s = s[1..<1]; 
#endif /* __NT__ */ 
       } 
         if (!filter[s] /* && directory exist */ ) 
         { 
            entries += ({s}); 
            filter[s]=1; 
         } 
      } 
      // FIXME: This is NOT thread safe! 
      search_path_entries = entries; 
      search_path_raw = raw_path; 
   } 
   return locate_binary(search_path_entries, command); 
} 
 
//! 
string sh_quote(string s) 
{ 
  return replace(s, 
        ({"\\", "'", "\"", " "}), 
        ({"\\\\", "\\'", "\\\"","\\ "})); 
} 
 
array(string) split_quoted_string(string s, int(0..1)|void nt_mode) 
//! Splits the given string into a list of arguments, according to 
//! common (i.e. @expr{/bin/sh@}-based) command line quoting rules: 
//! 
//! @ul 
//! @item 
//!   Sequences of whitespace, i.e. space, tab, @expr{\n@} or 
//!   @expr{\r@}, are treated as argument separators by default. 
//! 
//! @item 
//!   Single or double quotes (@expr{'@} or @expr{"@}) can be used 
//!   around an argument to avoid whitespace splitting inside it. If 
//!   such quoted strings are used next to each other then they get 
//!   concatenated to one argument; e.g. @expr{a"b"'c'@} becomes a 
//!   single argument @expr{abc@}. 
//! 
//! @item 
//!   Backslash (@expr{\@}) can be used in front of one of the space 
//!   or quote characters mentioned above to treat them literally. 
//!   E.g. @expr{x\ y@} is a single argument with a space in the 
//!   middle, and @expr{x\"y@} is a single argument with a double 
//!   quote in the middle. 
//! 
//! @item 
//!   A backslash can also be used to quote itself; i.e. @expr{\\@} 
//!   becomes @expr{\@}. 
//! 
//! @item 
//!   Backslashes in front of other characters are removed by default. 
//!   However, if the optional @[nt_mode] flag is set then they are 
//!   retained as-is, to work better with Windows style paths. 
//! 
//! @item 
//!   Backslashes are treated literally inside quoted strings, with 
//!   the exception that @expr{\"@} is treated as a literal @expr{"@} 
//!   inside a @expr{"@}-quoted string. It's therefore possible to 
//!   include a literal @expr{"@} in a @expr{"@}-quoted string, as 
//!   opposed to @expr{'@}-quoted strings which cannot contain a 
//!   @expr{'@}. 
//! @endul 
{ 
  // Remove initial white-space. 
  sscanf(s,"%*[ \t\n\r]%s",s); 
 
  // Prefix interesting characters with NUL, 
  // and split on NUL. 
  s=replace(s, 
            ({"\"",  "'",  "\\",  " ",  "\t",  "\n",   "\r",   "\0"}), 
            ({"\0\"","\0'","\0\\","\0 ","\0\t","\0\n", "\0\r", "\0\0"})); 
  array(string) x=s/"\0"; 
  array(string) ret = ({}); 
  string last = x[0]; 
  int always_keep_last; 
 
piece_loop: 
  for(int e=1;e<sizeof(x);e++) 
  { 
    string piece = x[e]; 
    if (piece == "") { 
      // Escaped NUL. There should always be another element in x. 
      last += "\0" + x[++e]; 
      continue; 
    } 
    switch(piece[0]) 
    { 
      case '"': 
      always_keep_last = 1; 
      last+=piece[1..]; 
      while (1) { 
        if (++e == sizeof (x)) 
          break piece_loop; 
        if (has_prefix (piece = x[e], "\"")) 
          break; 
        if (piece == "") {  // Escaped NUL. 
          last += "\0" + x[++e]; 
        } 
        else { 
          if(piece == "\\" && sizeof (x) > e + 1 && has_prefix (x[e+1], "\"")) 
            piece = x[++e]; 
          last+=piece; 
        } 
      } 
      last+=piece[1..]; 
      break; 
 
      case '\'': 
      always_keep_last = 1; 
      last+=piece[1..]; 
      while (1) { 
        if (++e == sizeof (x)) 
          break piece_loop; 
        if (has_prefix (piece = x[e], "'")) 
          break; 
        if (piece == "") {  // Escaped NUL. 
          last += "\0" + x[++e]; 
        } 
        else 
          last+=piece; 
      } 
      last+=piece[1..]; 
      break; 
 
      case '\\': 
      if(sizeof(piece)>1) 
      { 
        if (nt_mode) { 
          // On NT we only escape special characters with \; 
          // other \'s we keep verbatim. 
          last+=piece; 
        } else { 
          last+=piece[1..]; 
        } 
      }else if (sizeof (x) > e + 1) { 
        // Escaped special character. 
        last+=x[++e]; 
      } 
      break; 
 
      case ' ': 
      case '\t': 
      case '\n': 
      case '\r': 
        while(sizeof(piece)==1) 
        { 
          if(e+1 < sizeof(x)) 
          { 
            if((<' ','\t','\n', '\r'>) [x[e+1][0]]) 
              piece = x[++e]; 
            else 
              break; 
          }else{ 
            break piece_loop; 
          } 
        } 
        if (sizeof (last) || always_keep_last) 
          ret += ({last}); 
        last = piece[1..]; 
      break; 
    } 
  } 
  if (sizeof (last) || always_keep_last) 
    ret += ({last}); 
  return ret; 
} 
 
Process spawn(string command, void|Stdio.Stream stdin, 
              void|Stdio.Stream stdout, void|Stdio.Stream stderr, 
              // These aren't used. Seems to be part of something unfinished. /mast 
              //function|void cleanup, mixed ... args 
             ) 
//! Spawns a process that executes @[command] as a command shell 
//! statement ("@expr{/bin/sh -c @[command]@}" for Unix, "@expr{cmd /c 
//! @[command]@}" for Windows). 
//! 
//! @param stdin 
//! @param stdout 
//! @param stderr 
//!   Stream objects to use as standard input, standard output and 
//!   standard error, respectively, for the created process. The 
//!   corresponding streams for this process are used for those that 
//!   are left out. 
//! 
//! @returns 
//!   Returns a @[Process.Process] object for the created process. 
//! 
//! @seealso 
//!   @[system], @[popen] 
{ 
  mapping(string:mixed) data=(["env":getenv()]); 
  if(stdin) data->stdin=stdin; 
  if(stdout) data->stdout=stdout; 
  if(stderr) data->stderr=stderr; 
#if defined(__NT__) 
  // if the command string command is not quoted, add double quotes to 
  // make sure it is not modified by create_process 
  if (sizeof(command) <= 1 || 
      command[0] != '\"' || command[sizeof(command)-1] != '\"') 
    command = "\"" + command + "\""; 
  return Process(({ "cmd", "/c", command }),data); 
#else /* !__NT__ */ 
  return Process(({ "/bin/sh", "-c", command }),data); 
#endif /* __NT__ */ 
} 
 
//! @decl string popen(string command) 
//! Executes @[command] as a shell statement ("@expr{/bin/sh -c 
//! @[command]@}" for Unix, "@expr{cmd /c @[command]@}" for Windows), 
//! waits until it has finished and returns the result as a string. 
//! 
//! @seealso 
//!   @[system], @[spawn] 
 
//! @decl Stdio.FILE popen(string command, string mode) 
//! Open a "process" for reading or writing.  The @[command] is executed 
//! as a shell statement ("@expr{/bin/sh -c @[command]@}" for Unix, 
//! "@expr{cmd /c @[command]@}" for Windows). The parameter @[mode] 
//! should be one of the following letters: 
//! @string 
//!   @value "r" 
//!   Open for reading.  Data written by the process to stdout 
//!   is available for read. 
//!   @value "w" 
//!   Open for writing.  Data written to the file is available 
//!   to the process on stdin. 
//! @endstring 
//! 
//! @seealso 
//!   @[system], @[spawn] 
 
variant string popen(string s) { 
   return fpopen(s)->read(); 
} 
 
variant Stdio.FILE popen(string s, string mode) { 
  return fpopen(s,mode); 
} 
 
protected Stdio.FILE fpopen(string s, string|void mode) 
{ 
  Stdio.FILE f = Stdio.FILE(); 
  if (!f) error("Popen failed. (couldn't create file)\n"); 
 
  Stdio.File p = f->pipe(); 
  if(!p) error("Popen failed. (couldn't create pipe)\n"); 
 
  if (mode == "w") 
    spawn(s, p, 0, 0, /*destruct, f*/); 
  else 
    spawn(s, 0, p, 0, /*destruct, f*/); 
  p->close(); 
  destruct(p); 
 
  return f; 
} 
 
int system(string command, void|Stdio.Stream stdin, 
           void|Stdio.Stream stdout, void|Stdio.Stream stderr) 
//! Executes @[command] as a shell statement ("@expr{/bin/sh -c 
//! @[command]@}" for Unix, "@expr{cmd /c @[command]@}" for Windows), 
//! waits until it has finished and returns its return value. 
//! 
//! @param stdin 
//! @param stdout 
//! @param stderr 
//!   Stream objects to use as standard input, standard output and 
//!   standard error, respectively, for the created process. The 
//!   corresponding streams for this process are used for those that 
//!   are left out. 
//! 
//! @seealso 
//!   @[spawn], @[popen] 
{ 
  return spawn(command, stdin, stdout, stderr)->wait(); 
} 
 
#ifndef __NT__ 
#if constant(fork) 
constant fork = predef::fork; 
#endif 
 
#if constant(exece) 
constant exece = predef::exece; 
#endif 
 
#if constant(fork) && constant(exece) 
 
//! 
class Spawn 
{ 
   object stdin; 
   object stdout; 
   object stderr; 
   array(object) fd; 
 
   object pid; 
 
   private object low_spawn(array(Stdio.File) fdp, 
                            array(Stdio.File) fd_to_close, 
                            string cmd, void|array(string) args, 
                            void|mapping(string:string) env, 
                            string|void cwd) 
   { 
      Stdio.File pie, pied; // interprocess communication 
      object pid; 
 
      pie=Stdio.File(); 
      pied=pie->pipe(); 
 
      if(!(pid=fork())) 
      { 
         mixed err=catch 
         { 
            if(cwd && !cd(cwd)) 
            { 
              error( "pike: cannot change cwd to %O: %s.\n", cwd, 
                     strerror(errno()) ); 
            } 
 
            if (sizeof(fdp)>0 && fdp[0]) fdp[0]->dup2(Stdio.File("stdin")); 
            if (sizeof(fdp)>1 && fdp[1]) fdp[1]->dup2(Stdio.File("stdout")); 
            if (sizeof(fdp)>2 && fdp[2]) fdp[2]->dup2(Stdio.File("stderr")); 
            /* dup2 fdd[3..] here FIXME FIXME */ 
            foreach (fd_to_close, Stdio.File f) 
               if (objectp(f)) { f->close(); destruct(f); } 
            pie->close(); 
            destruct(pie); 
 
            pied->set_close_on_exec(1); 
 
            if (env) 
               exece(cmd,args||({}),env); 
            else 
               exece(cmd,args||({})); 
 
            error( "pike: failed to exece %O: %s.\n", cmd, 
                   strerror(errno()) ); 
         }; 
 
         pied->write(encode_value(err)); 
         exit(1); 
      } 
 
      foreach (fdp,object f) if (objectp(f)) { f->close(); destruct(f); } 
 
      pied->close(); 
      destruct(pied); 
 
      mixed err=pie->read(); 
      if (err && err!="") throw(decode_value(err)); 
 
      pie->close(); 
      destruct(pie); 
 
      return pid; 
   } 
 
  //! 
   void create(string cmd, 
               void|array(string) args, 
               void|mapping(string:string) env, 
               string|void cwd, 
               void|array(object(Stdio.File)|void) ownpipes, 
               void|array(object(Stdio.File)|void) fds_to_close) 
   { 
      if (!ownpipes) 
      { 
         stdin=Stdio.File(); 
         stdout=Stdio.File(); 
         stderr=Stdio.File(); 
         fd=({stdin->pipe(),stdout->pipe(),stderr->pipe()}); 
         fds_to_close=({stdin,stdout,stderr}); 
      } 
      else 
      { 
         fd=ownpipes; 
         if (sizeof(fd)>0) stdin=fd[0]; else stdin=0; 
         if (sizeof(fd)>1) stdout=fd[1]; else stdout=0; 
         if (sizeof(fd)>2) stderr=fd[2]; else stderr=0; 
      } 
      pid=low_spawn(fd,fds_to_close||({}),cmd,args,env,cwd); 
   } 
 
#if constant(kill) 
   //! 
   int kill(int signal) 
   { 
      return predef::kill(pid,signal); 
   } 
#endif 
 
   //! 
   int wait() 
   { 
     return pid->wait(); 
   } 
 
   // void set_done_callback(function foo,mixed ... args); 
   // int result(); 
   // array rusage(); 
} 
#endif 
#endif 
 
#if constant(fork) || constant(System.daemon) 
private int low_daemon(int nochdir, int noclose) 
{ 
#if constant(System.daemon) 
    return System.daemon(nochdir, noclose); 
#else 
    if (fork()) 
        exit(0); 
 
#if constant(System.setsid) 
        System.setsid(); 
#endif /* System.setsid */ 
 
    if (!nochdir) 
        cd("/"); 
 
    Stdio.File fd; 
    if (!noclose && (fd = Stdio.File("/dev/null", "rw"))) 
    { 
        fd->dup2(Stdio.stdin); 
        fd->dup2(Stdio.stdout); 
        fd->dup2(Stdio.stderr); 
        if (fd->query_fd() > 2) 
            fd->close(); 
    } 
    return 0; 
#endif /* !System.daemon */ 
} 
 
void daemon(int nochdir, int noclose, 
            void|mapping(string:string|Stdio.File) modifiers) 
//! A function to run current program in the background. 
//! 
//! @param nochdir 
//!   If 0 the process will continue to run in / or the directory 
//!   dictadet by modifiers. 
//! @param noclose 
//!  If this is not 0 the process will keep current file descriptors 
//!  open. 
//! @param modifiers 
//!   Optional extra arguments. The parameters passed in this mapping 
//!   will override the arguments nochdir and noclose. 
//!   @mapping 
//!     @member string "cwd" 
//!       Change current working directory to this directory. 
//!     @member string|Stdio.File "stdin" 
//!       If this is a string this will be interpreted as a filename 
//!       pointing out a file to be used as stdandard input to the process. 
//!       If this is a Stdio.File object the process will use this as 
//!       standard input. 
//!     @member string|Stdio.File "stdout" 
//!       If this is a string this will be interpreted as a filename 
//!       pointing out a file to be used as stdandard output to the process. 
//!       If this is a Stdio.File object the process will use this as 
//!       standard output. 
//!     @member string|Stdio.File "stderr" 
//!       If this is a string this will be interpreted as a filename 
//!       pointing out a file to be used as stdandard error to the process. 
//!       If this is a Stdio.File object the process will use this as 
//!       standard error. 
//!   @endmapping 
//! 
//! @seealso 
//!   @[System.daemon] 
//! 
//! @note 
//!   This function only works on UNIX-like operating systems. 
//! 
//! @example 
//!    /* close all fd:s and cd to '/' */ 
//!   Process.daemon(0, 0); 
//! 
//!   /* Do not change working directory. Write stdout to a file called 
//!      access.log and stderr to error.log. */ 
//!   Process.daemon(1, 0, ([ "stdout": "access.log", "stderr": "error.log" ]) ); 
{ 
    array(Stdio.File) opened = ({}); 
    Stdio.File getfd(string|object f) 
    { 
        if (stringp(f)) 
        { 
            Stdio.File ret = Stdio.File(f, "crw"); 
            opened += ({ ret }); 
            return ret; 
        } 
        else if (objectp(f)) 
            return f; 
        else 
          return 0; 
    }; 
 
    if (low_daemon(nochdir, noclose) == -1) 
      error("Failed to daemonize: %s.\n", strerror(errno())); 
    if (!modifiers) 
        return; 
 
    if (modifiers["cwd"]) 
        cd(modifiers["cwd"]); 
 
    if (modifiers["stdin"]) 
        getfd(modifiers["stdin"])->dup2(Stdio.stdin); 
 
    if (modifiers["stdout"]) 
        getfd(modifiers["stdout"])->dup2(Stdio.stdout); 
 
    if (modifiers["stderr"]) 
        getfd(modifiers["stderr"])->dup2(Stdio.stderr); 
 
    opened->close(); 
} 
#endif