summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_exe_posix.c
blob: 9624f80be4a6533dcc5892ba7c98d86f75dd1e74 (plain)
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
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>

#ifdef HAVE_PRCTL
# include <sys/prctl.h>
#endif

#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif

#include <Efl.h>

#include "Ecore.h"
#include "ecore_private.h"

#define MY_CLASS ECORE_EXE_CLASS

#include "ecore_exe_private.h"

/* TODO: Something to let people build a command line and does auto escaping -
 *
 * ecore_exe_snprintf()
 *
 *   OR
 *
 * cmd = ecore_exe_comand_parameter_append(cmd, "firefox");
 * cmd = ecore_exe_comand_parameter_append(cmd, "http://www.foo.com/bar.html?baz=yes");
 * each parameter appended is one argument, and it gets escaped, quoted, and
 * appended with a preceding space.  The first is the command off course.
 */

struct _ecore_exe_dead_exe
{
   pid_t pid;
   char *cmd;
};

static inline void _ecore_exe_exec_it(const char     *exe_cmd,
                                      Ecore_Exe_Flags flags);
static Eina_Bool   _ecore_exe_data_generic_handler(void             *data,
                                                   Ecore_Fd_Handler *fd_handler,
                                                   Ecore_Exe_Flags   flags);
static Eina_Bool            _ecore_exe_data_error_handler(void             *data,
                                                          Ecore_Fd_Handler *fd_handler);
static Eina_Bool            _ecore_exe_data_read_handler(void             *data,
                                                         Ecore_Fd_Handler *fd_handler);
static Eina_Bool            _ecore_exe_data_write_handler(void             *data,
                                                          Ecore_Fd_Handler *fd_handler);
static void                 _ecore_exe_flush(Ecore_Exe *obj);
static Ecore_Exe           *_ecore_exe_is_it_alive(pid_t pid);
static Eina_Bool            _ecore_exe_make_sure_its_dead(void *data);
static Eina_Bool            _ecore_exe_make_sure_its_really_dead(void *data);
static void                 _ecore_exe_dead_attach(Ecore_Exe *obj);

static const char *shell = NULL;

/* FIXME: This errno checking stuff should be put elsewhere for everybody to use.
 * For now it lives here though, just to make testing easier.
 */
static int _ecore_exe_check_errno(int         result,
                                  const char *file,
                                  int         line);

#define E_IF_NO_ERRNO(result, foo, ok)                                                           \
  while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1);  \
          if (ok)

#define E_NO_ERRNO(result, foo, ok) \
  while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1)

#define E_IF_NO_ERRNO_NOLOOP(result, foo, ok) \
  if (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)))

static int
_ecore_exe_check_errno(int         result,
                       const char *file EINA_UNUSED,
                       int         line EINA_UNUSED)
{
   int saved_errno = errno;

   if (result == -1)
   {
      perror("*** errno reports ");
/* What is currently supported -
 *
 *   pipe
 *     EFAULT  Argument is not valid.
 *     EMFILE  Too many file descriptors used by process.
 *     ENFILE  Too many open files by system.
 *   read
 *     EAGAIN  No data now, try again.
 *     EBADF   This is not an fd that can be read.
 *     EFAULT  This is not a valid buffer.
 *     EINTR   Interupted by signal, try again.
 *     EINVAL  This is not an fd that can be read.
 *     EIO     I/O error.
 *     EISDIR  This is a directory, and cannot be read.
 *     others  Depending on what sort of thing we are reading from.
 *   close
 *     EBADF   This is not an fd that can be closed.
 *     EINTR   Interupted by signal, try again.
 *     EIO     I/O error.
 *   dup2
 *     EBADF   This is not an fd that can be dup2'ed.
 *     EBUSY   Race condition between open() and dup()
 *     EINTR   Interupted by signal, try again.
 *     EMFILE  Too many file descriptors used by process.
 *   fcntl
 *     EACCES, EAGAIN  Locked or mapped by something else, try again later.
 *     EBADF   This is not an fd that can be fcntl'ed.
 *     EDEADLK This will cause a deadlock.
 *     EFAULT  This is not a valid lock.
 *     EINTR   Interupted by signal, try again.
 *     EINVAL  This is not a valid arg.
 *     EMFILE  Too many file descriptors used by process.
 *     ENOLCK  Problem getting a lock.
 *     EPERM   Not allowed to do that.
 *   fsync
 *     EBADF   This is not an fd that is open for writing.
 *     EINVAL, EROFS  This is not an fd that can be fsynced.
 *     EIO     I/O error.
 *
 * How to use it -
 *    int ok = 0;
 *    int result;
 *
 *    E_IF_NO_ERRNO(result, foo(bar), ok)
 *      {
 *         E_IF_NO_ERRNO_NOLOOP(result, foo(bar), ok)
 *            {
 *            }
 *      }
 *
 *   if (!ok)
 *     {
 *        // Something failed, cleanup.
 *     }
 */
      switch (saved_errno)
      {
       case EACCES:
       case EAGAIN:
       case EINTR:
         { /* Not now, try later. */
            ERR("*** Must try again in %s @%u.", file, line);
            result = -1;
            break;
         }

       case EMFILE:
       case ENFILE:
       case ENOLCK:
         { /* Low on resources. */
            ERR("*** Low on resources in %s @%u.", file,
                line);
            result = 0;
            break;
         }

       case EIO:
         { /* I/O error. */
            ERR("*** I/O error in %s @%u.", file, line);
            result = 0;
            break;
         }

       case EFAULT:
       case EBADF:
       case EINVAL:
       case EROFS:
       case EISDIR:
       case EDEADLK:
       case EPERM:
       case EBUSY:
         { /* Programmer fucked up. */
            ERR("*** NAUGHTY PROGRAMMER!!!\n"
                "*** SPANK SPANK SPANK!!!\n"
                "*** Now go fix your code in %s @%u. Tut tut tut!",
                file, line);
            result = 0;
            break;
         }

       default:
         { /* Unsupported errno code, please add this one. */
            ERR("*** NAUGHTY PROGRAMMER!!!\n"
                "*** SPANK SPANK SPANK!!!\n"
                "*** Unsupported errno code %d, please add this one.\n"
                "*** Now go fix your code in %s @%u, from %s @%u. Tut tut tut!",
                saved_errno, __FILE__, __LINE__, file, line);
            result = 0;
            break;
         }
      }
   }
   else /* Everything is fine. */
     result = 1;

   errno = saved_errno;
   return result;
}

static int run_pri = ECORE_EXE_PRIORITY_INHERIT;

void
_impl_ecore_exe_run_priority_set(int pri)
{
   run_pri = pri;
}

int
_impl_ecore_exe_run_priority_get(void)
{
   return run_pri;
}

Eo *
_impl_ecore_exe_efl_object_finalize(Eo *obj, Ecore_Exe_Data *exe)
{
   int statusPipe[2] = { -1, -1 };
   int errorPipe[2] = { -1, -1 };
   int readPipe[2] = { -1, -1 };
   int writePipe[2] = { -1, -1 };
   int n = 0;
   int ok = 1;
   int result;

   if (!exe->cmd) return NULL;

   const char *exe_cmd = exe->cmd;
   Ecore_Exe_Flags flags = exe->flags;

   if ((flags & ECORE_EXE_PIPE_AUTO) && (!(flags & ECORE_EXE_PIPE_ERROR))
       && (!(flags & ECORE_EXE_PIPE_READ)))
     /* We need something to auto pipe. */
     flags |= ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_ERROR;

   exe->child_fd_error = -1;
   exe->child_fd_read = -1;
   exe->child_fd_write = -1;
   exe->child_fd_error_x = -1;
   exe->child_fd_read_x = -1;
   exe->child_fd_write_x = -1;

   /*  Create some pipes. */
   if (ok)
   {
      E_IF_NO_ERRNO_NOLOOP(result, pipe(statusPipe), ok)
      {
      }
   }
   if (ok && (flags & ECORE_EXE_PIPE_ERROR))
   {
      E_IF_NO_ERRNO_NOLOOP(result, pipe(errorPipe), ok)
      {
         exe->child_fd_error = errorPipe[0];
         exe->child_fd_error_x = errorPipe[1];
      }
   }
   if (ok && (flags & ECORE_EXE_PIPE_READ))
   {
      E_IF_NO_ERRNO_NOLOOP(result, pipe(readPipe), ok)
      {
         exe->child_fd_read = readPipe[0];
         exe->child_fd_read_x = readPipe[1];
      }
   }
   if (ok && (flags & ECORE_EXE_PIPE_WRITE))
   {
      E_IF_NO_ERRNO_NOLOOP(result, pipe(writePipe), ok)
      {
         exe->child_fd_write = writePipe[1];
         exe->child_fd_write_x = writePipe[0];
      }
   }
   if (ok)
   {
      pid_t pid = 0;
      volatile int vfork_exec_errno = 0;

      /* FIXME: I should double check this.  After a quick look around, this is already done, but via a more modern method. */
      /* signal(SIGPIPE, SIG_IGN);    We only want EPIPE on errors */
      pid = fork();

      if (pid == -1)
      {
         ERR("Failed to fork process");
         pid = 0;
      }
      else if (pid == 0) /* child */
      {
#ifdef HAVE_SYSTEMD
         unsetenv("NOTIFY_SOCKET");
#endif
         if (run_pri != ECORE_EXE_PRIORITY_INHERIT)
         {
#ifdef PRIO_PROCESS            
            if ((run_pri >= -20) && (run_pri <= 19))
              setpriority(PRIO_PROCESS, 0, run_pri);
#else
#warning "Your OS/libc does not provide PRIO_PROCESS (and possibly setpriority())"
#warning "This is a POSIX-1.2001 standard and it is highly encouraged that you"
#warning "Have support for this"
#endif
         }
         /* dup2 STDERR, STDIN, and STDOUT.  dup2() allegedly closes the
          * second pipe if it's open. On the other hand, there was the
          * Great FD Leak Scare of '06, so let's be paranoid. */
         if (ok && (flags & ECORE_EXE_PIPE_ERROR))
         {
            E_NO_ERRNO(result, close(STDERR_FILENO), ok);
            E_NO_ERRNO(result, dup2(errorPipe[1], STDERR_FILENO), ok);
         }
         if (ok && (flags & ECORE_EXE_PIPE_READ))
         {
            E_NO_ERRNO(result, close(STDOUT_FILENO), ok);
            E_NO_ERRNO(result, dup2(readPipe[1], STDOUT_FILENO), ok);
         }
         if (ok && (flags & ECORE_EXE_PIPE_WRITE))
         {
            E_NO_ERRNO(result, close(STDIN_FILENO), ok);
            E_NO_ERRNO(result, dup2(writePipe[0], STDIN_FILENO), ok);
         }

         if (ok)
         {
            /* Setup the status pipe. */
             E_NO_ERRNO(result, close(statusPipe[0]), ok);
             E_IF_NO_ERRNO(result, eina_file_close_on_exec(statusPipe[1], EINA_TRUE), ok) /* close on exec shows success */
             {
                /* Run the actual command. */
                 _ecore_exe_exec_it(exe_cmd, flags); /* no return */
             }
         }

         /* Something went 'orribly wrong. */
         vfork_exec_errno = errno;

         /* Close the pipes. */
         if (flags & ECORE_EXE_PIPE_ERROR)
           E_NO_ERRNO(result, close(errorPipe[1]), ok);
         if (flags & ECORE_EXE_PIPE_READ)
           E_NO_ERRNO(result, close(readPipe[1]), ok);
         if (flags & ECORE_EXE_PIPE_WRITE)
           E_NO_ERRNO(result, close(writePipe[0]), ok);
         E_NO_ERRNO(result, close(statusPipe[1]), ok);

         _exit(-1);
      }
      else   /* parent */
      {
         /* Close the unused pipes. */
          E_NO_ERRNO(result, close(statusPipe[1]), ok);

          /* FIXME: after having a good look at the current e fd
           * handling, investigate fcntl(dataPipe[x], F_SETSIG, ...) */
          /* FIXME: above F_SETSIG etc. - this is async SIGIO based IO
           * which is also linux specific so we probably don't want to
           * do this as long as select() is working fine. the only time
           * we really want to think of SIGIO async IO is when it all
           * actually works basically everywhere and we can turn all
           * IO into DMA async activities (i.e. you do a read() then
           * the read is complete not on return but when you get a
           * SIGIO - the read() just starts the transfer and it is
           * completed in the background by DMA (or whatever mechanism
           * the kernel choses)) */

          /* Wait for it to start executing. */
          /* FIXME: this doesn't seem very nice - we sit and block
           * waiting on a child process... even though it's just
           * the segment between the fork() and the exec) it just feels
           * wrong */
          for (;; )
          {
             char buf;

             E_NO_ERRNO(result, read(statusPipe[0], &buf, 1), ok);
             if (result == 0)
             {
                if (vfork_exec_errno != 0)
                {
                   n = vfork_exec_errno;
                   ERR("Could not start \"%s\"", exe_cmd);
                   pid = 0;
                }
                break;
             }
          }

          /* Close the status pipe. */
          E_NO_ERRNO(result, close(statusPipe[0]), ok);
      }

      if (pid)
      {
         /* Setup the exe structure. */
          exe->start_bytes = -1;
          exe->end_bytes = -1;
          exe->start_lines = -1;
          exe->end_lines = -1;
          exe->pid = pid;
          exe->flags = flags;
          if (exe->cmd)
          {
             if (flags & ECORE_EXE_PIPE_ERROR) /* Setup the error stuff. */
             {
                E_IF_NO_ERRNO(result,
                              fcntl(exe->child_fd_error, F_SETFL,
                                    O_NONBLOCK), ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_error, EINA_TRUE),
                              ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_error_x, EINA_TRUE),
                              ok) {
                }
                {
                   exe->error_fd_handler =
                     ecore_main_fd_handler_add(exe->child_fd_error,
                                               ECORE_FD_READ,
                                               _ecore_exe_data_error_handler,
                                               obj, NULL, NULL);
                   if (!exe->error_fd_handler)
                     ok = 0;
                }
             }
             if (ok && (flags & ECORE_EXE_PIPE_READ)) /* Setup the read stuff. */
             {
                E_IF_NO_ERRNO(result,
                              fcntl(exe->child_fd_read, F_SETFL,
                                    O_NONBLOCK), ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_read, EINA_TRUE),
                              ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_read_x, EINA_TRUE),
                              ok) {
                }
                {
                   exe->read_fd_handler =
                     ecore_main_fd_handler_add(exe->child_fd_read,
                                               ECORE_FD_READ,
                                               _ecore_exe_data_read_handler,
                                               obj, NULL, NULL);
                   if (!exe->read_fd_handler)
                     ok = 0;
                }
             }
             if (ok && (flags & ECORE_EXE_PIPE_WRITE)) /* Setup the write stuff. */
             {
                E_IF_NO_ERRNO(result,
                              fcntl(exe->child_fd_write, F_SETFL,
                                    O_NONBLOCK), ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_write, EINA_TRUE),
                              ok) {
                }
                E_IF_NO_ERRNO(result,
                              eina_file_close_on_exec(exe->child_fd_write_x, EINA_TRUE),
                              ok) {
                }
                {
                   exe->write_fd_handler =
                     ecore_main_fd_handler_add(exe->child_fd_write,
                                               ECORE_FD_WRITE,
                                               _ecore_exe_data_write_handler,
                                               obj, NULL, NULL);
                   if (exe->write_fd_handler)
                     ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);  /* Nothing to write to start with. */
                   else
                     ok = 0;
                }
             }

             _ecore_exe_exes = eina_list_append(_ecore_exe_exes, obj);
             n = 0;
          }
          else
            ok = 0;
      }
      else
        ok = 0;
   }

   if (!ok) /* Something went wrong, so pull down everything. */
   {
      if (exe->pid) ecore_exe_terminate(obj);
      IF_FN_DEL(ecore_exe_free, obj);
   }
   else
   {
      Ecore_Exe_Event_Add *e;

      e = _ecore_exe_event_add_new();
      if (e)
        {
           e->exe = obj;
           /* Send the event. */
           ecore_event_add(ECORE_EXE_EVENT_ADD, e,
                           _ecore_exe_event_add_free, NULL);
        }
      /* INF("Running as %d for %s.\n", exe->pid, exe->cmd); */
   }

   errno = n;
   return obj;
}

Eina_Bool
_impl_ecore_exe_send(Ecore_Exe  *obj EINA_UNUSED,
               Ecore_Exe_Data *exe,
               const void *data,
               int         size)
{
   void *buf;

   if (exe->child_fd_write == -1)
     {
        ERR("Ecore_Exe %p created without ECORE_EXE_PIPE_WRITE! "
              "Cannot send %d bytes from %p", exe, size, data);
        return EINA_FALSE;
     }

   buf = realloc(exe->write_data_buf, exe->write_data_size + size);
   if (!buf) return EINA_FALSE;

   exe->write_data_buf = buf;
   memcpy((char *)exe->write_data_buf + exe->write_data_size, data, size);
   exe->write_data_size += size;

   if (exe->write_fd_handler)
      ecore_main_fd_handler_active_set(exe->write_fd_handler, ECORE_FD_WRITE);

   return EINA_TRUE;
}

void
_impl_ecore_exe_auto_limits_set(Ecore_Exe *obj EINA_UNUSED,
                          Ecore_Exe_Data *exe,
                          int        start_bytes,
                          int        end_bytes,
                          int        start_lines,
                          int        end_lines)
{
   /* FIXME: sanitize the input. */
   exe->start_bytes = start_bytes;
   exe->end_bytes = end_bytes;
   exe->start_lines = start_lines;
   exe->end_lines = end_lines;

   /* FIXME: get this can of worms working.
    *
    * capture stderr & stdout internally
    *
    * raster and onefang keep moving the goal posts on this one.  It started out as
    * "show users the error output if an exe fails" and is rapidly approaching
    * "alternative method of getting the data, poll vs event driven".  Some serious
    * thinking needs to be applied to this.  Do we really want to go that far?  If
    * so, we should change the names.  The basic design will probably remain the
    * same which ever way we go.  The constant goal post moving is probably due to
    * generic design methods leading to feature creep as we inspired each other to
    * more generic designs.  It does seem like the closer we get to poll driven,
    * the more issues and corner cases there are.
    *
    * Instead of doing the usual register an event handler thing, we are ecore_exe,
    * we can take some short cuts.  Don't send the events, just leave the exe buffers
    * as is until the user asks for them, then return the event.
    *
    * start = 0,  end = 0;   clogged arteries get flushed, everything is ignored.
    * start = -1, end = -1;  clogged arteries get transferred to internal buffers.  Actually, either == -1 means buffer everything.
    * start = X,  end = 0;   buffer first X out of clogged arteries, flush and ignore rest.
    * start = 0,  end = X;   circular buffer X
    * start = X,  end = Y;   buffer first X out of clogged arteries, circular buffer Y from beginning.
    *
    * bytes vs lines, which ever one reaches the limit first.
    * Before we go beyond the start+end limit, leave the end buffer empty, and store both in the start buffer, coz they overlap.
    * After we pass the the start+end limit, insert "\n...\n" at the end of the start buffer, copy the rest to the end buffer, then store in the end buffer.
    *
    * Other issues -
    * Spank programmer for polling data if polling is not turned on.
    * Spank programmer for setting up event callbacks if polling is turned on.
    * Spank programmer for freeing the event data if it came from the event system, as that autofrees.
    * Spank the programmer if they try to set the limits bigger than what has been gathered & ignored already, coz they just lost data.
    * Spank onefang and raster for opening this can of worms.
    * Should we have separate out/err limits?
    * Should we remove from the internal buffer the data that was delivered already?
    * If so, what to do about limits, start, and end?  They could loose their meaning.
    */
}

Ecore_Exe_Event_Data *
_impl_ecore_exe_event_data_get(Ecore_Exe      *obj,
                         Ecore_Exe_Data *exe,
                         Ecore_Exe_Flags flags)
{
   Ecore_Exe_Event_Data *e = NULL;
   int is_buffered = 0;
   unsigned char *inbuf;
   int inbuf_num;

   /* Sort out what sort of event we are. */
   if (flags & ECORE_EXE_PIPE_READ)
   {
      flags = ECORE_EXE_PIPE_READ;
      if (exe->flags & ECORE_EXE_PIPE_READ_LINE_BUFFERED)
        is_buffered = 1;
   }
   else
   {
      flags = ECORE_EXE_PIPE_ERROR;
      if (exe->flags & ECORE_EXE_PIPE_ERROR_LINE_BUFFERED)
        is_buffered = 1;
   }

   /* Get the data. */
   if (flags & ECORE_EXE_PIPE_READ)
   {
      inbuf = exe->read_data_buf;
      inbuf_num = exe->read_data_size;
      exe->read_data_buf = NULL;
      exe->read_data_size = 0;
   }
   else
   {
      inbuf = exe->error_data_buf;
      inbuf_num = exe->error_data_size;
      exe->error_data_buf = NULL;
      exe->error_data_size = 0;
   }

   e = calloc(1, sizeof(Ecore_Exe_Event_Data));
   if (e)
   {
      e->exe = obj;
      e->data = inbuf;
      e->size = inbuf_num;

      if (is_buffered) /* Deal with line buffering. */
      {
         int max = 0;
         int count = 0;
         int i;
         int last = 0;
         char *c;

         c = (char *)inbuf;
         for (i = 0; i < inbuf_num; i++) /* Find the lines. */
         {
            if (inbuf[i] == '\n')
            {
               if (count >= max)
               {
                  Ecore_Exe_Event_Data_Line *lines;

                  /* In testing, the lines seem to arrive in batches of 500 to 1000 lines at most, roughly speaking. */
                   max += 10; /* FIXME: Maybe keep track of the largest number of lines ever sent, and add half that many instead of 10. */
                   lines = realloc(e->lines, sizeof(Ecore_Exe_Event_Data_Line) * (max + 1)); /* Allow room for the NULL termination. */
                   if (!lines)
                   {
                      ERR("Out of memory adding exe data lines to event");
                      break;
                   }
                   e->lines = lines;
               }
               /* raster said to leave the line endings as line endings, however -
                * This is line buffered mode, we are not dealing with binary here, but lines.
                * If we are not dealing with binary, we must be dealing with ASCII, unicode, or some other text format.
                * Thus the user is most likely gonna deal with this text as strings.
                * Thus the user is most likely gonna pass this data to str functions.
                * rasters way - the endings are always gonna be '\n';  onefangs way - they will always be '\0'
                * We are handing them the string length as a convenience.
                * Thus if they really want it in raw format, they can e->lines[i].line[e->lines[i].size - 1] = '\n'; easily enough.
                * In the default case, we can do this conversion quicker than the user can, as we already have the index and pointer.
                * Let's make it easy on them to use these as standard C strings.
                *
                * onefang is proud to announce that he has just set a new personal record for the
                * most over documentation of a simple assignment statement.  B-)
                */
               inbuf[i] = '\0';
               e->lines[count].line = c;
               e->lines[count].size = i - last;
               last = i + 1;
               c = (char *)&inbuf[last];
               count++;
            }
         }
         if (i > last) /* Partial line left over, save it for next time. */
         {
            if (count != 0) e->size = last;
            if (flags & ECORE_EXE_PIPE_READ)
            {
               exe->read_data_size = i - last;
               exe->read_data_buf = malloc(exe->read_data_size);
               memcpy(exe->read_data_buf, c, exe->read_data_size);
            }
            else
            {
               exe->error_data_size = i - last;
               exe->error_data_buf = malloc(exe->error_data_size);
               memcpy(exe->error_data_buf, c, exe->error_data_size);
            }
         }
         if (count == 0) /* No lines to send, cancel the event. */
         {
            _ecore_exe_event_exe_data_free(NULL, e);
            e = NULL;
         }
         else /* NULL terminate the array, so that people know where the end is. */
         {
            e->lines[count].line = NULL;
            e->lines[count].size = 0;
         }
      }
   }

   return e;
}

void
_impl_ecore_exe_efl_object_destructor(Eo *obj, Ecore_Exe_Data *exe)
{
   void *data;
   int ok = 0;
   int result;

   data = exe->data;

   if (exe->pre_free_cb)
     exe->pre_free_cb(data, obj);

   if (exe->doomsday_clock)
   {
      struct _ecore_exe_dead_exe *dead;

      ecore_timer_del(exe->doomsday_clock);
      exe->doomsday_clock = NULL;
      dead = exe->doomsday_clock_dead;
      if (dead)
      {
         IF_FREE(dead->cmd);
         free(dead);
         exe->doomsday_clock_dead = NULL;
      }
   }
   IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
   IF_FN_DEL(ecore_main_fd_handler_del, exe->read_fd_handler);
   IF_FN_DEL(ecore_main_fd_handler_del, exe->error_fd_handler);
   if (exe->child_fd_write_x != -1)
     E_NO_ERRNO(result, close(exe->child_fd_write_x), ok);
   if (exe->child_fd_read_x != -1)
     E_NO_ERRNO(result, close(exe->child_fd_read_x), ok);
   if (exe->child_fd_error_x != -1)
     E_NO_ERRNO(result, close(exe->child_fd_error_x), ok);
   if (exe->child_fd_write != -1)
     E_NO_ERRNO(result, close(exe->child_fd_write), ok);
   if (exe->child_fd_read != -1)
     E_NO_ERRNO(result, close(exe->child_fd_read), ok);
   if (exe->child_fd_error != -1)
     E_NO_ERRNO(result, close(exe->child_fd_error), ok);
   IF_FREE(exe->write_data_buf);
   IF_FREE(exe->read_data_buf);
   IF_FREE(exe->error_data_buf);
   IF_FREE(exe->cmd);

   _ecore_exe_exes = eina_list_remove(_ecore_exe_exes, obj);
   IF_FREE(exe->tag);
}

void
_impl_ecore_exe_pause(Eo *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   kill(exe->pid, SIGSTOP);
}

void
_impl_ecore_exe_continue(Eo *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   kill(exe->pid, SIGCONT);
}

void
_impl_ecore_exe_interrupt(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   _ecore_exe_dead_attach(obj);
   kill(exe->pid, SIGINT);
}

void
_impl_ecore_exe_quit(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   _ecore_exe_dead_attach(obj);
   kill(exe->pid, SIGQUIT);
}

void
_impl_ecore_exe_terminate(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   _ecore_exe_dead_attach(obj);
   INF("Sending TERM signal to %s (%d).", exe->cmd, exe->pid);
   kill(exe->pid, SIGTERM);
}

void
_impl_ecore_exe_kill(Ecore_Exe *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   struct _ecore_exe_dead_exe *dead;
   dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
   if (dead)
   {
      dead->pid = exe->pid;
      dead->cmd = strdup(exe->cmd);
      IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
      exe->doomsday_clock =
        ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead, dead);
   }

   INF("Sending KILL signal to %s (%d).", exe->cmd, exe->pid);
   kill(exe->pid, SIGKILL);
}

void
_impl_ecore_exe_signal(Ecore_Exe *obj EINA_UNUSED,
                 Ecore_Exe_Data *exe,
                 int        num)
{
   if (num == 1)
     kill(exe->pid, SIGUSR1);
   else if (num == 2)
     kill(exe->pid, SIGUSR2);
}

void
_impl_ecore_exe_hup(Ecore_Exe *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   kill(exe->pid, SIGHUP);
}

static Ecore_Exe *
_ecore_exe_is_it_alive(pid_t pid)
{
   Ecore_Exe *exe = NULL;

   /* FIXME: There is no nice, safe, OS independent way to tell if a
    * particular PID is still alive.  I have written code to do so
    * for my urunlevel busybox applet (http://urunlevel.sourceforge.net/),
    * but it's for linux only, and still not guaranteed.
    *
    * So for now, we just check that a valid Ecore_Exe structure
    * exists for it.  Even that is not a guarantee, as the structure
    * can be freed without killing the process.
    *
    * I think we can safely put exe's into two categories, those users
    * that care about the life of the exe, and the run and forget type.
    * The run and forget type starts up the exe, then free's the
    * Ecore_Exe structure straight away.  They can never call any of
    * the functions that can call this, so we don't worry about them.
    *
    * Those user's that care about the life of exe's will keep the
    * Ecore_Exe structure around, terminate them eventually, or
    * register for exit events.  For these ones the assumption
    * that valid Ecore_Exe struct == live exe is almost valid.
    *
    * I will probably copy my urunlevel code into here someday.
    */
   exe = _ecore_exe_find(pid);

   return exe;
}

static Eina_Bool
_ecore_exe_make_sure_its_dead(void *data)
{
   struct _ecore_exe_dead_exe *dead;

   dead = data;
   if (dead)
     {
        Ecore_Exe *obj = NULL;

        if ((obj = _ecore_exe_is_it_alive(dead->pid)))
          {
             Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);
             if (exe)
               {
                  if (dead->cmd)
                    INF("Sending KILL signal to allegedly dead %s (%d).",
                        dead->cmd, dead->pid);
                  else
                    INF("Sending KILL signal to allegedly dead PID %d.",
                        dead->pid);
                  exe->doomsday_clock =
                    ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead,
                                    dead);
                  kill(dead->pid, SIGKILL);
               }
          }
        else
          {
             IF_FREE(dead->cmd);
             free(dead);
          }
     }
   return ECORE_CALLBACK_CANCEL;
}

static Eina_Bool
_ecore_exe_make_sure_its_really_dead(void *data)
{
   struct _ecore_exe_dead_exe *dead;

   dead = data;
   if (dead)
     {
        Ecore_Exe *obj = NULL;

        if ((obj = _ecore_exe_is_it_alive(dead->pid)))
          {
             Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);

             if (exe)
               {
                  ERR("RUN!  The zombie wants to eat your brains!  And your CPU!");
                  if (dead->cmd)
                    INF("%s (%d) is not really dead.", dead->cmd, dead->pid);
                  else
                    INF("PID %d is not really dead.", dead->pid);
                  exe->doomsday_clock = NULL;
               }
             IF_FREE(dead->cmd);
             free(dead);
          }
     }
   return ECORE_CALLBACK_CANCEL;
}

Ecore_Timer *
_ecore_exe_doomsday_clock_get(Ecore_Exe *obj)
{
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);
   if (!exe) return NULL;
   return exe->doomsday_clock;
}

void
_ecore_exe_doomsday_clock_set(Ecore_Exe   *obj,
                              Ecore_Timer *dc)
{
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);
   if (!exe) return;
   exe->doomsday_clock = dc;
}

static inline void
_ecore_exe_exec_it(const char     *exe_cmd,
                   Ecore_Exe_Flags flags)
{
   char use_sh = 1;
   char *buf = NULL;
   char **args = NULL;
   int save_errno = 0;

   /* So what is this doing?
    *
    * We are trying to avoid wrapping the exe call with /bin/sh -c.
    * We conservatively search for certain shell meta characters,
    * If we don't find them, we can call the exe directly.
    */
   if (!strpbrk(exe_cmd, "|&;<>()$`\\\"'*?#"))
   {
      char *token;
      char pre_command = 1;
      int num_tokens = 0;
      int len;

      len = strlen(exe_cmd);
      buf = alloca(len + 1);
      strcpy(buf, exe_cmd);
      buf[len] = 0;

      token = strtok(buf, " \t\n\v");
      while (token)
      {
         if (token[0] == '~')
           break;
         if (pre_command)
         {
            if (token[0] == '[')
              break;
            if (strchr(token, '='))
              break;
            else
              pre_command = 0;
         }
         num_tokens++;
         token = strtok(NULL, " \t\n\v");
      }
      if ((!token) && (num_tokens))
      {
         int i = 0;

         len = strlen(exe_cmd);
         buf = alloca(len + 1);
         strcpy(buf, exe_cmd);
         buf[len] = 0;

         token = strtok(buf, " \t\n\v");
         use_sh = 0;
         args = alloca((num_tokens + 1) * sizeof(char *));
         for (i = 0; i < num_tokens; i++)
         {
            if (token)
              args[i] = token;
            token = strtok(NULL, " \t\n\v");
         }
         args[num_tokens] = NULL;
      }
   }

#ifdef HAVE_PRCTL
   if ((flags & ECORE_EXE_TERM_WITH_PARENT))
     {
        prctl(PR_SET_PDEATHSIG, SIGTERM);
     }
#endif

   if (!(flags & ECORE_EXE_NOT_LEADER)) setsid();
   if ((flags & ECORE_EXE_USE_SH))
   {
      errno = 0;
      execl("/bin/sh", "/bin/sh", "-c", exe_cmd, (char *)NULL);
   }
   else if (use_sh) /* We have to use a shell to run this. */
   {
      if (!shell) /* Find users preferred shell. */
      {
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
         if (getuid() == geteuid())
#endif
           shell = getenv("SHELL");
         if (!shell)
           shell = "/bin/sh";
      }
      errno = 0;
      execl(shell, shell, "-c", exe_cmd, (char *)NULL);
   }
   else
   { /* We can run this directly. */
      if (!args)
      {
         ERR("arg[0] is NULL!");
         return;
      }
      errno = 0;
      if (args[0]) execvp(args[0], args);
   }

   save_errno = errno;
   errno = save_errno;
   return;
}

static Eina_Bool
_ecore_exe_data_generic_handler(void             *data,
                                Ecore_Fd_Handler *fd_handler,
                                Ecore_Exe_Flags   flags)
{
   Ecore_Exe *obj = data;
   int child_fd;
   int event_type;
   const Efl_Event_Description *eo_event = NULL;

   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);
   if (!exe) return EINA_FALSE;

   /* Sort out what sort of handler we are. */
   if (flags & ECORE_EXE_PIPE_READ)
   {
      flags = ECORE_EXE_PIPE_READ;
      event_type = ECORE_EXE_EVENT_DATA;
      eo_event = ECORE_EXE_EVENT_DATA_GET;
      child_fd = exe->child_fd_read;
   }
   else
   {
      flags = ECORE_EXE_PIPE_ERROR;
      event_type = ECORE_EXE_EVENT_ERROR;
      eo_event = ECORE_EXE_EVENT_DATA_ERROR;
      child_fd = exe->child_fd_error;
   }

   if ((fd_handler)
       && (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)))
   {
      unsigned char *inbuf, *temp;
      int inbuf_num;

      /* Get any left over data from last time. */
      if (flags & ECORE_EXE_PIPE_READ)
      {
         inbuf = exe->read_data_buf;
         inbuf_num = exe->read_data_size;
         exe->read_data_buf = NULL;
         exe->read_data_size = 0;
      }
      else
      {
         inbuf = exe->error_data_buf;
         inbuf_num = exe->error_data_size;
         exe->error_data_buf = NULL;
         exe->error_data_size = 0;
      }

      for (;; )
      {
         int num, lost_exe;
         char buf[READBUFSIZ];

         lost_exe = 0;
         errno = 0;
         if ((num = read(child_fd, buf, READBUFSIZ)) < 1)
         {
            /* FIXME: SPEED/SIZE TRADE OFF - add a smaller READBUFSIZE
             * (currently 64k) to inbuf, use that instead of buf, and
             * save ourselves a memcpy(). */
              lost_exe = ((errno == EIO) ||
                          (errno == EBADF) ||
                          (errno == EPIPE) ||
                          (errno == EINVAL) || (errno == ENOSPC));
              if ((errno != EAGAIN) && (errno != EINTR))
                perror("_ecore_exe_generic_handler() read problem ");
         }
         if (num > 0) /* data got read. */
         {
            temp = inbuf;
            inbuf = realloc(inbuf, inbuf_num + num);
            if (inbuf)
              {
                 memcpy(inbuf + inbuf_num, buf, num);
                 inbuf_num += num;
              }
            else // realloc fails and returns NULL.
              {
                 ERR("Out of memory in exe generic data handler");
                 inbuf = temp;
              }
         }
         else
         { /* No more data to read. */
            if (inbuf)
            {
               Ecore_Exe_Event_Data *e;

               /* Stash the data away for later. */
               if (flags & ECORE_EXE_PIPE_READ)
               {
                  exe->read_data_buf = inbuf;
                  exe->read_data_size = inbuf_num;
               }
               else
               {
                  exe->error_data_buf = inbuf;
                  exe->error_data_size = inbuf_num;
               }

               if (!(exe->flags & ECORE_EXE_PIPE_AUTO))
               {
                  e = ecore_exe_event_data_get(obj, flags);
                  if (e) /* Send the event. */
                    {
                       ecore_event_add(event_type, e,
                             _ecore_exe_event_exe_data_free,
                             NULL);
                       efl_event_callback_call(obj, eo_event, e);
                    }
               }
            }
            if (lost_exe)
            {
               if (flags & ECORE_EXE_PIPE_READ)
               {
                  if (exe->read_data_size)
                    INF("There are %d bytes left unsent from the dead exe %s.",
                        exe->read_data_size, exe->cmd);
               }
               else
               {
                  if (exe->error_data_size)
                    INF("There are %d bytes left unsent from the dead exe %s.",
                        exe->error_data_size, exe->cmd);
               }
               /* Thought about this a bit.  If the exe has actually
                * died, this won't do any harm as it must have died
                * recently and the pid has not had a chance to recycle.
                * It is also a paranoid catchall, coz the usual ecore_signal
                * mechanism should kick in.  But let's give it a good
                * kick in the head anyway.
                */
               ecore_exe_terminate(obj);
            }
            break;
         }
      }
   }

   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
_ecore_exe_data_error_handler(void             *data,
                              Ecore_Fd_Handler *fd_handler)
{
   return _ecore_exe_data_generic_handler(data, fd_handler,
                                          ECORE_EXE_PIPE_ERROR);
}

static Eina_Bool
_ecore_exe_data_read_handler(void             *data,
                             Ecore_Fd_Handler *fd_handler)
{
   return _ecore_exe_data_generic_handler(data, fd_handler,
                                          ECORE_EXE_PIPE_READ);
}

static Eina_Bool
_ecore_exe_data_write_handler(void             *data,
                              Ecore_Fd_Handler *fd_handler EINA_UNUSED)
{
   Ecore_Exe *obj = data;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);

   if (!exe) return EINA_FALSE;

   if ((exe->write_fd_handler) &&
       (ecore_main_fd_handler_active_get
        (exe->write_fd_handler, ECORE_FD_WRITE)))
     _ecore_exe_flush(obj);

   /* If we have sent all there is to send, and we need to close the pipe, then close it. */
   if ((exe->close_stdin == 1) &&
       (exe->write_data_size == exe->write_data_offset))
     {
        int ok = 0;
        int result;

        INF("Closing stdin for %s", exe->cmd);
        /* if (exe->child_fd_write != -1)  E_NO_ERRNO(result, fsync(exe->child_fd_write), ok);   This a) doesn't work, and b) isn't needed. */
        IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
        if (exe->child_fd_write != -1)
          E_NO_ERRNO(result, close(exe->child_fd_write), ok);
        exe->child_fd_write = -1;
        IF_FREE(exe->write_data_buf);
     }

   return ECORE_CALLBACK_RENEW;
}

static void
_ecore_exe_flush(Ecore_Exe *obj)
{
   int count;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);

   if (!exe) return;
   /* check whether we need to write anything at all. */
   if ((exe->child_fd_write == -1) || (!exe->write_data_buf)) return;
   if (exe->write_data_size == exe->write_data_offset) return;

   count = write(exe->child_fd_write,
                 (char *)exe->write_data_buf + exe->write_data_offset,
                 exe->write_data_size - exe->write_data_offset);
   if (count < 1)
     {
        if (errno == EIO || errno == EBADF || errno == EPIPE || errno == EINVAL || errno == ENOSPC) /* we lost our exe! */
          {
             ecore_exe_terminate(obj);
             if (exe->write_fd_handler)
               ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
          }
     }
   else
     {
        exe->write_data_offset += count;
        if (exe->write_data_offset >= exe->write_data_size) /* Nothing left to write, clean up. */
          {
             exe->write_data_size = 0;
             exe->write_data_offset = 0;
             IF_FREE(exe->write_data_buf);
             if (exe->write_fd_handler)
               ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
          }
     }
}

static void
_ecore_exe_dead_attach(Ecore_Exe *obj)
{
   struct _ecore_exe_dead_exe *dead;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, MY_CLASS);

   if (!exe) return;
   if (exe->doomsday_clock_dead) return;
   dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
   if (dead)
     {
        dead->pid = exe->pid;
        if (exe->cmd) dead->cmd = strdup(exe->cmd);
        IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
        exe->doomsday_clock =
          ecore_timer_add(10.0, _ecore_exe_make_sure_its_dead, dead);
        exe->doomsday_clock_dead = dead;
     }
}