5 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__)
7 # include <sys/resource.h>
14 #include <sys/types.h>
18 #ifdef HAVE_SYS_PRCTL_H
19 # include <sys/prctl.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
27 #include "ecore_private.h"
29 /* FIXME: Getting respawn to work
31 * There is no way that we can do anything about the internal state info of
32 * an external exe. The same can be said about the state of user code. User
33 * code in this context means the code that is using ecore_exe to manage exe's
36 * Document that the exe must be respawnable, in other words, there is no
37 * state that it cannot regenerate by just killing it and starting it again.
38 * This includes state that the user code knows about, as the respawn is
39 * transparent to that code. On the other hand, maybe a respawn event might
40 * be useful, or maybe resend the currently non existent add event. For
41 * consistancy with ecore_con, an add event is good anyway.
43 * The Ecore_exe structure is reused for respawning, so that the (opaque)
44 * pointer held by the user remains valid. This means that the Ecore_Exe
45 * init and del functions may need to be split into two parts each to avoid
46 * duplicating code - common code part, and the rest. This implies that
47 * the unchanging members mentioned next should NEVER change.
49 * These structure members don't need to change -
50 * __list_data - we stay on the list
51 * ECORE_MAGIC - this is a constant
52 * data - passed in originally
53 * cmd - passed in originally
54 * flags - passed in originally
56 * These structure members need to change -
57 * tag - state that must be regenerated, zap it
58 * pid - it will be different
59 * child_fd_write - it will be different
60 * child_fd_read - it will be different
61 * child_fd_error - it will be different
62 * write_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
63 * read_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
64 * error_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
66 * Hmm, the read, write, and error buffers could be tricky.
67 * They are not atomic, and could be in a semi complete state.
68 * They fall into the "state must be regenerated" mentioned above.
69 * A respawn/add event should take care of it.
71 * These structure members need to change -
72 * write_data_buf - state that must be regenerated, zap it
73 * write_data_size - state that must be regenerated, zap it
74 * write_data_offset - state that must be regenerated, zap it
75 * read_data_buf - state that must be regenerated, zap it
76 * read_data_size - state that must be regenerated, zap it
77 * error_data_buf - state that must be regenerated, zap it
78 * error_data_size - state that must be regenerated, zap it
79 * close_write - state that must be regenerated, zap it
81 * There is the problem that an exe that fell over and needs respawning
82 * might keep falling over, keep needing to be respawned, and tie up system
83 * resources with the constant respawning. An exponentially increasing
84 * timeout (with maximum timeout) between respawns should take care of that.
85 * Although this is not a "contention for a resource" problem, the exe falling
86 * over may be, so a random element added to the timeout may help, and won't
87 * hurt. The user code may need to be informed that a timeout is in progress.
97 Ecore_Exe_Flags flags;
98 Ecore_Fd_Handler *write_fd_handler; /* the fd_handler to handle write to child - if this was used, or NULL if not */
99 Ecore_Fd_Handler *read_fd_handler; /* the fd_handler to handle read from child - if this was used, or NULL if not */
100 Ecore_Fd_Handler *error_fd_handler; /* the fd_handler to handle errors from child - if this was used, or NULL if not */
101 void *write_data_buf; /* a data buffer for data to write to the child -
102 * realloced as needed for more data and flushed when the fd handler says writes are possible
104 int write_data_size; /* the size in bytes of the data buffer */
105 int write_data_offset; /* the offset in bytes in the data buffer */
106 void *read_data_buf; /* data read from the child awating delivery to an event */
107 int read_data_size; /* data read from child in bytes */
108 void *error_data_buf; /* errors read from the child awating delivery to an event */
109 int error_data_size; /* errors read from child in bytes */
110 int child_fd_write; /* fd to write TO to send data to the child */
111 int child_fd_read; /* fd to read FROM when child has sent us (the parent) data */
112 int child_fd_error; /* fd to read FROM when child has sent us (the parent) errors */
113 int child_fd_write_x; /* fd to write TO to send data to the child */
114 int child_fd_read_x; /* fd to read FROM when child has sent us (the parent) data */
115 int child_fd_error_x; /* fd to read FROM when child has sent us (the parent) errors */
116 Eina_Bool close_stdin : 1;
118 int start_bytes, end_bytes, start_lines, end_lines; /* Number of bytes/lines to auto pipe at start/end of stdout/stderr. */
120 Ecore_Timer *doomsday_clock; /* The Timer of Death. Muahahahaha. */
121 void *doomsday_clock_dead; /* data for the doomsday clock */
123 Ecore_Exe_Cb pre_free_cb;
126 /* TODO: Something to let people build a command line and does auto escaping -
128 * ecore_exe_snprintf()
132 * cmd = ecore_exe_comand_parameter_append(cmd, "firefox");
133 * cmd = ecore_exe_comand_parameter_append(cmd, "http://www.foo.com/bar.html?baz=yes");
134 * each parameter appended is one argument, and it gets escaped, quoted, and
135 * appended with a preceding space. The first is the command off course.
138 struct _ecore_exe_dead_exe
144 static inline void _ecore_exe_exec_it(const char *exe_cmd,
145 Ecore_Exe_Flags flags);
146 static Eina_Bool _ecore_exe_data_generic_handler(void *data,
147 Ecore_Fd_Handler *fd_handler,
148 Ecore_Exe_Flags flags);
149 static Eina_Bool _ecore_exe_data_error_handler(void *data,
150 Ecore_Fd_Handler *fd_handler);
151 static Eina_Bool _ecore_exe_data_read_handler(void *data,
152 Ecore_Fd_Handler *fd_handler);
153 static Eina_Bool _ecore_exe_data_write_handler(void *data,
154 Ecore_Fd_Handler *fd_handler);
155 static void _ecore_exe_flush(Ecore_Exe *exe);
156 static void _ecore_exe_event_exe_data_free(void *data __UNUSED__,
158 static Ecore_Exe *_ecore_exe_is_it_alive(pid_t pid);
159 static Eina_Bool _ecore_exe_make_sure_its_dead(void *data);
160 static Eina_Bool _ecore_exe_make_sure_its_really_dead(void *data);
161 static Ecore_Exe_Event_Add *_ecore_exe_event_add_new(void);
162 static void _ecore_exe_event_add_free(void *data,
164 static void _ecore_exe_dead_attach(Ecore_Exe *exe);
166 EAPI int ECORE_EXE_EVENT_ADD = 0;
167 EAPI int ECORE_EXE_EVENT_DEL = 0;
168 EAPI int ECORE_EXE_EVENT_DATA = 0;
169 EAPI int ECORE_EXE_EVENT_ERROR = 0;
171 static Ecore_Exe *exes = NULL;
172 static const char *shell = NULL;
174 /* FIXME: This errno checking stuff should be put elsewhere for everybody to use.
175 * For now it lives here though, just to make testing easier.
177 static int _ecore_exe_check_errno(int result,
181 #define E_IF_NO_ERRNO(result, foo, ok) \
182 while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1); \
185 #define E_NO_ERRNO(result, foo, ok) \
186 while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1)
188 #define E_IF_NO_ERRNO_NOLOOP(result, foo, ok) \
189 if (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)))
192 _ecore_exe_check_errno(int result,
196 int saved_errno = errno;
200 perror("*** errno reports ");
201 /* What is currently supported -
204 * EFAULT Argument is not valid.
205 * EMFILE Too many file descriptors used by process.
206 * ENFILE Too many open files by system.
208 * EAGAIN No data now, try again.
209 * EBADF This is not an fd that can be read.
210 * EFAULT This is not a valid buffer.
211 * EINTR Interupted by signal, try again.
212 * EINVAL This is not an fd that can be read.
214 * EISDIR This is a directory, and cannot be read.
215 * others Depending on what sort of thing we are reading from.
217 * EBADF This is not an fd that can be closed.
218 * EINTR Interupted by signal, try again.
221 * EBADF This is not an fd that can be dup2'ed.
222 * EBUSY Race condition between open() and dup()
223 * EINTR Interupted by signal, try again.
224 * EMFILE Too many file descriptors used by process.
226 * EACCES, EAGAIN Locked or mapped by something else, try again later.
227 * EBADF This is not an fd that can be fcntl'ed.
228 * EDEADLK This will cause a deadlock.
229 * EFAULT This is not a valid lock.
230 * EINTR Interupted by signal, try again.
231 * EINVAL This is not a valid arg.
232 * EMFILE Too many file descriptors used by process.
233 * ENOLCK Problem getting a lock.
234 * EPERM Not allowed to do that.
236 * EBADF This is not an fd that is open for writing.
237 * EINVAL, EROFS This is not an fd that can be fsynced.
244 * E_IF_NO_ERRNO(result, foo(bar), ok)
246 * E_IF_NO_ERRNO_NOLOOP(result, foo(bar), ok)
253 * // Something failed, cleanup.
261 { /* Not now, try later. */
262 ERR("*** Must try again in %s @%u.", file, line);
270 { /* Low on resources. */
271 ERR("*** Low on resources in %s @%u.", file,
279 ERR("*** I/O error in %s @%u.", file, line);
292 { /* Programmer fucked up. */
293 ERR("*** NAUGHTY PROGRAMMER!!!\n"
294 "*** SPANK SPANK SPANK!!!\n"
295 "*** Now go fix your code in %s @%u. Tut tut tut!",
302 { /* Unsupported errno code, please add this one. */
303 ERR("*** NAUGHTY PROGRAMMER!!!\n"
304 "*** SPANK SPANK SPANK!!!\n"
305 "*** Unsupported errno code %d, please add this one.\n"
306 "*** Now go fix your code in %s @%u, from %s @%u. Tut tut tut!",
307 saved_errno, __FILE__, __LINE__, file, line);
313 else /* Everything is fine. */
321 * @addtogroup Ecore_Exe_Group
326 static int run_pri = ECORE_EXE_PRIORITY_INHERIT;
329 * Sets the priority at which to launch processes
331 * This sets the priority of processes run by ecore_exe_run() and
332 * ecore_exe_pipe_run().
333 * @li On Windows, the child process is created by default with the
334 * @ref ECORE_EXE_WIN32_PRIORITY_NORMAL priority, unless the calling
335 * process is in @ref ECORE_EXE_WIN32_PRIORITY_IDLE or
336 * @ref ECORE_EXE_WIN32_PRIORITY_BELOW_NORMAL priority. In that case, the
337 * child process inherits this priority.
338 * @li On other platforms, if set to @ref ECORE_EXE_PRIORITY_INHERIT child
339 * processes inherits the priority of their parent. This is the default.
341 * @param pri value a Ecore_Exe_Win32_Priority value on Windows, -20
342 * to 19 or @ref ECORE_EXE_PRIORITY_INHERIT on other OS.
345 ecore_exe_run_priority_set(int pri)
347 EINA_MAIN_LOOP_CHECK_RETURN;
352 * Gets the priority at which to launch processes
354 * This gets ths priority of launched processes. See
355 * ecore_exe_run_priority_set() for details. This just returns the value set
358 * @return the value set by ecore_exe_run_priority_set()
361 ecore_exe_run_priority_get(void)
363 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
368 * Spawns a child process.
370 * This is now just a thin wrapper around ecore_exe_pipe_run()
371 * @note When you use this function you will have no permissions
372 * to write or read on the pipe that connects you with the spwaned process.
373 * If you need to do that use ecore_exe_pipe_run() with the
374 * appropriated flags.
376 * @param exe_cmd The command to run with @c /bin/sh.
377 * @param data Data to attach to the returned process handle.
378 * @return A process handle to the spawned process.
381 ecore_exe_run(const char *exe_cmd,
384 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
385 return ecore_exe_pipe_run(exe_cmd, 0, data);
389 * Spawns a child process with its stdin/out available for communication.
391 * This function forks and runs the given command using @c /bin/sh.
393 * Note that the process handle is only valid until a child process
394 * terminated event is received. After all handlers for the child process
395 * terminated event have been called, the handle will be freed by Ecore.
397 * This function does the same thing as ecore_exe_run(), but also makes the
398 * standard in and/or out as well as stderr from the child process available
399 * for reading or writing. To write use ecore_exe_send(). To read listen to
400 * ECORE_EXE_EVENT_DATA or ECORE_EXE_EVENT_ERROR events (set up handlers).
401 * Ecore may buffer read and error data until a newline character if asked
402 * for with the @p flags. All data will be included in the events (newlines
403 * will be replaced with NULLS if line buffered). ECORE_EXE_EVENT_DATA events
404 * will only happen if the process is run with ECORE_EXE_PIPE_READ enabled
405 * in the flags. The same with the error version. Writing will only be
406 * allowed with ECORE_EXE_PIPE_WRITE enabled in the flags.
408 * @param exe_cmd The command to run with @c /bin/sh.
409 * @param flags The flag parameters for how to deal with inter-process I/O
410 * @param data Data to attach to the returned process handle.
411 * @return A process handle to the spawned process.
414 ecore_exe_pipe_run(const char *exe_cmd,
415 Ecore_Exe_Flags flags,
418 Ecore_Exe *exe = NULL;
419 int statusPipe[2] = { -1, -1 };
420 int errorPipe[2] = { -1, -1 };
421 int readPipe[2] = { -1, -1 };
422 int writePipe[2] = { -1, -1 };
427 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
428 if (!exe_cmd) return NULL;
429 exe = calloc(1, sizeof(Ecore_Exe));
430 if (!exe) return NULL;
432 if ((flags & ECORE_EXE_PIPE_AUTO) && (!(flags & ECORE_EXE_PIPE_ERROR))
433 && (!(flags & ECORE_EXE_PIPE_READ)))
434 /* We need something to auto pipe. */
435 flags |= ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_ERROR;
437 exe->child_fd_error = -1;
438 exe->child_fd_read = -1;
439 exe->child_fd_write = -1;
440 exe->child_fd_error_x = -1;
441 exe->child_fd_read_x = -1;
442 exe->child_fd_write_x = -1;
444 /* Create some pipes. */
447 E_IF_NO_ERRNO_NOLOOP(result, pipe(statusPipe), ok)
451 if (ok && (flags & ECORE_EXE_PIPE_ERROR))
453 E_IF_NO_ERRNO_NOLOOP(result, pipe(errorPipe), ok)
455 exe->child_fd_error = errorPipe[0];
456 exe->child_fd_error_x = errorPipe[1];
459 if (ok && (flags & ECORE_EXE_PIPE_READ))
461 E_IF_NO_ERRNO_NOLOOP(result, pipe(readPipe), ok)
463 exe->child_fd_read = readPipe[0];
464 exe->child_fd_read_x = readPipe[1];
467 if (ok && (flags & ECORE_EXE_PIPE_WRITE))
469 E_IF_NO_ERRNO_NOLOOP(result, pipe(writePipe), ok)
471 exe->child_fd_write = writePipe[1];
472 exe->child_fd_write_x = writePipe[0];
478 volatile int vfork_exec_errno = 0;
480 /* FIXME: I should double check this. After a quick look around, this is already done, but via a more modern method. */
481 /* signal(SIGPIPE, SIG_IGN); We only want EPIPE on errors */
486 ERR("Failed to fork process");
489 else if (pid == 0) /* child */
491 if (run_pri != ECORE_EXE_PRIORITY_INHERIT)
493 if ((run_pri >= -20) && (run_pri <= 19))
494 setpriority(PRIO_PROCESS, 0, run_pri);
496 /* dup2 STDERR, STDIN, and STDOUT. dup2() allegedly closes the
497 * second pipe if it's open. On the other hand, there was the
498 * Great FD Leak Scare of '06, so let's be paranoid. */
499 if (ok && (flags & ECORE_EXE_PIPE_ERROR))
501 E_NO_ERRNO(result, close(STDERR_FILENO), ok);
502 E_NO_ERRNO(result, dup2(errorPipe[1], STDERR_FILENO), ok);
504 if (ok && (flags & ECORE_EXE_PIPE_READ))
506 E_NO_ERRNO(result, close(STDOUT_FILENO), ok);
507 E_NO_ERRNO(result, dup2(readPipe[1], STDOUT_FILENO), ok);
509 if (ok && (flags & ECORE_EXE_PIPE_WRITE))
511 E_NO_ERRNO(result, close(STDIN_FILENO), ok);
512 E_NO_ERRNO(result, dup2(writePipe[0], STDIN_FILENO), ok);
517 /* Setup the status pipe. */
518 E_NO_ERRNO(result, close(statusPipe[0]), ok);
519 E_IF_NO_ERRNO(result, fcntl(statusPipe[1], F_SETFD, FD_CLOEXEC), ok) /* close on exec shows success */
521 /* Run the actual command. */
522 _ecore_exe_exec_it(exe_cmd, flags); /* no return */
526 /* Something went 'orribly wrong. */
527 vfork_exec_errno = errno;
529 /* Close the pipes. */
530 if (flags & ECORE_EXE_PIPE_ERROR)
531 E_NO_ERRNO(result, close(errorPipe[1]), ok);
532 if (flags & ECORE_EXE_PIPE_READ)
533 E_NO_ERRNO(result, close(readPipe[1]), ok);
534 if (flags & ECORE_EXE_PIPE_WRITE)
535 E_NO_ERRNO(result, close(writePipe[0]), ok);
536 E_NO_ERRNO(result, close(statusPipe[1]), ok);
542 /* Close the unused pipes. */
543 E_NO_ERRNO(result, close(statusPipe[1]), ok);
545 /* FIXME: after having a good look at the current e fd
546 * handling, investigate fcntl(dataPipe[x], F_SETSIG, ...) */
547 /* FIXME: above F_SETSIG etc. - this is async SIGIO based IO
548 * which is also linux specific so we probably don't want to
549 * do this as long as select() is working fine. the only time
550 * we really want to think of SIGIO async IO is when it all
551 * actually works basically everywhere and we can turn all
552 * IO into DMA async activities (i.e. you do a read() then
553 * the read is complete not on return but when you get a
554 * SIGIO - the read() just starts the transfer and it is
555 * completed in the background by DMA (or whatever mechanism
556 * the kernel choses)) */
558 /* Wait for it to start executing. */
559 /* FIXME: this doesn't seem very nice - we sit and block
560 * waiting on a child process... even though it's just
561 * the segment between the fork() and the exec) it just feels
567 E_NO_ERRNO(result, read(statusPipe[0], &buf, 1), ok);
570 if (vfork_exec_errno != 0)
572 n = vfork_exec_errno;
573 ERR("Could not start \"%s\"", exe_cmd);
580 /* Close the status pipe. */
581 E_NO_ERRNO(result, close(statusPipe[0]), ok);
586 /* Setup the exe structure. */
587 ECORE_MAGIC_SET(exe, ECORE_MAGIC_EXE);
588 exe->start_bytes = -1;
590 exe->start_lines = -1;
594 exe->data = (void *)data;
595 if ((exe->cmd = strdup(exe_cmd)))
597 if (flags & ECORE_EXE_PIPE_ERROR) /* Setup the error stuff. */
599 E_IF_NO_ERRNO(result,
600 fcntl(exe->child_fd_error, F_SETFL,
603 E_IF_NO_ERRNO(result,
604 fcntl(exe->child_fd_error, F_SETFD,
607 E_IF_NO_ERRNO(result,
608 fcntl(exe->child_fd_error_x, F_SETFD,
612 exe->error_fd_handler =
613 ecore_main_fd_handler_add(exe->child_fd_error,
615 _ecore_exe_data_error_handler,
617 if (!exe->error_fd_handler)
621 if (ok && (flags & ECORE_EXE_PIPE_READ)) /* Setup the read stuff. */
623 E_IF_NO_ERRNO(result,
624 fcntl(exe->child_fd_read, F_SETFL,
627 E_IF_NO_ERRNO(result,
628 fcntl(exe->child_fd_read, F_SETFD,
631 E_IF_NO_ERRNO(result,
632 fcntl(exe->child_fd_read_x, F_SETFD,
636 exe->read_fd_handler =
637 ecore_main_fd_handler_add(exe->child_fd_read,
639 _ecore_exe_data_read_handler,
641 if (!exe->read_fd_handler)
645 if (ok && (flags & ECORE_EXE_PIPE_WRITE)) /* Setup the write stuff. */
647 E_IF_NO_ERRNO(result,
648 fcntl(exe->child_fd_write, F_SETFL,
651 E_IF_NO_ERRNO(result,
652 fcntl(exe->child_fd_write, F_SETFD,
655 E_IF_NO_ERRNO(result,
656 fcntl(exe->child_fd_write_x, F_SETFD,
660 exe->write_fd_handler =
661 ecore_main_fd_handler_add(exe->child_fd_write,
663 _ecore_exe_data_write_handler,
665 if (exe->write_fd_handler)
666 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0); /* Nothing to write to start with. */
672 exes = (Ecore_Exe *)eina_inlist_append(EINA_INLIST_GET(exes), EINA_INLIST_GET(exe));
682 if (!ok) /* Something went wrong, so pull down everything. */
684 if (exe->pid) ecore_exe_terminate(exe);
685 IF_FN_DEL(ecore_exe_free, exe);
689 Ecore_Exe_Event_Add *e;
691 e = _ecore_exe_event_add_new();
693 if (e) /* Send the event. */
694 ecore_event_add(ECORE_EXE_EVENT_ADD, e,
695 _ecore_exe_event_add_free, NULL);
696 /* INF("Running as %d for %s.\n", exe->pid, exe->cmd); */
704 * Defines a function to be called before really freeing the handle data.
706 * This might be useful for language bindings such as Python and Perl
707 * that need to deallocate wrappers associated with this handle.
709 * This handle should never be modified by this call. It should be
710 * considered informative only. All getters are valid when the given
711 * function is called back.
713 * @param exe The child process to attach the pre_free function.
714 * @param func The function to call before @a exe is freed.
717 ecore_exe_callback_pre_free_set(Ecore_Exe *exe,
720 EINA_MAIN_LOOP_CHECK_RETURN;
721 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
723 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE,
724 "ecore_exe_callback_pre_free_set");
727 exe->pre_free_cb = func;
731 * Sends data to the given child process which it receives on stdin.
733 * This function writes to a child processes standard in, with unlimited
734 * buffering. This call will never block. It may fail if the system runs out
737 * @param exe The child process to send to
738 * @param data The data to send
739 * @param size The size of the data to send, in bytes
740 * @return @c EINA_TRUE if successful, @c EINA_FALSE on failure.
743 ecore_exe_send(Ecore_Exe *exe,
749 EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
750 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
752 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_send");
756 if (exe->close_stdin)
758 ERR("Ecore_Exe %p stdin is closed! Cannot send %d bytes from %p",
763 if (exe->child_fd_write == -1)
765 ERR("Ecore_Exe %p created without ECORE_EXE_PIPE_WRITE! "
766 "Cannot send %d bytes from %p", exe, size, data);
770 buf = realloc(exe->write_data_buf, exe->write_data_size + size);
771 if (!buf) return EINA_FALSE;
773 exe->write_data_buf = buf;
774 memcpy((char *)exe->write_data_buf + exe->write_data_size, data, size);
775 exe->write_data_size += size;
777 if (exe->write_fd_handler)
778 ecore_main_fd_handler_active_set(exe->write_fd_handler, ECORE_FD_WRITE);
784 * The stdin of the given child process will close when the write buffer is empty.
786 * @param exe The child process
789 ecore_exe_close_stdin(Ecore_Exe *exe)
791 EINA_MAIN_LOOP_CHECK_RETURN;
792 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
794 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_close_stdin");
797 exe->close_stdin = 1;
801 * Sets the auto pipe limits for the given process handle. On Windows
802 * this function does nothing.
804 * @param exe The given process handle.
805 * @param start_bytes limit of bytes at start of output to buffer.
806 * @param end_bytes limit of bytes at end of output to buffer.
807 * @param start_lines limit of lines at start of output to buffer.
808 * @param end_lines limit of lines at end of output to buffer.
811 ecore_exe_auto_limits_set(Ecore_Exe *exe,
817 EINA_MAIN_LOOP_CHECK_RETURN;
818 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
820 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_auto_limits_set");
823 /* FIXME: sanitize the input. */
824 exe->start_bytes = start_bytes;
825 exe->end_bytes = end_bytes;
826 exe->start_lines = start_lines;
827 exe->end_lines = end_lines;
829 /* FIXME: get this can of worms working.
831 * capture stderr & stdout internally
833 * raster and onefang keep moving the goal posts on this one. It started out as
834 * "show users the error output if an exe fails" and is rapidly approaching
835 * "alternative method of getting the data, poll vs event driven". Some serious
836 * thinking needs to be applied to this. Do we really want to go that far? If
837 * so, we should change the names. The basic design will probably remain the
838 * same which ever way we go. The constant goal post moving is probably due to
839 * generic design methods leading to feature creep as we inspired each other to
840 * more generic designs. It does seem like the closer we get to poll driven,
841 * the more issues and corner cases there are.
843 * Instead of doing the usual register an event handler thing, we are ecore_exe,
844 * we can take some short cuts. Don't send the events, just leave the exe buffers
845 * as is until the user asks for them, then return the event.
847 * start = 0, end = 0; clogged arteries get flushed, everything is ignored.
848 * start = -1, end = -1; clogged arteries get transferred to internal buffers. Actually, either == -1 means buffer everything.
849 * start = X, end = 0; buffer first X out of clogged arteries, flush and ignore rest.
850 * start = 0, end = X; circular buffer X
851 * start = X, end = Y; buffer first X out of clogged arteries, circular buffer Y from beginning.
853 * bytes vs lines, which ever one reaches the limit first.
854 * Before we go beyond the start+end limit, leave the end buffer empty, and store both in the start buffer, coz they overlap.
855 * 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.
858 * Spank programmer for polling data if polling is not turned on.
859 * Spank programmer for setting up event callbacks if polling is turned on.
860 * Spank programmer for freeing the event data if it came from the event system, as that autofrees.
861 * Spank the programmer if they try to set the limits bigger than what has been gathered & ignored already, coz they just lost data.
862 * Spank onefang and raster for opening this can of worms.
863 * Should we have separate out/err limits?
864 * Should we remove from the internal buffer the data that was delivered already?
865 * If so, what to do about limits, start, and end? They could loose their meaning.
870 * Gets the auto pipe data for the given process handle
872 * @param exe The given process handle.
873 * @param flags Is this a ECORE_EXE_PIPE_READ or ECORE_EXE_PIPE_ERROR?
874 * @return The event data.
876 EAPI Ecore_Exe_Event_Data *
877 ecore_exe_event_data_get(Ecore_Exe *exe,
878 Ecore_Exe_Flags flags)
880 Ecore_Exe_Event_Data *e = NULL;
882 unsigned char *inbuf;
885 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
886 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
888 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_event_data_get");
892 /* Sort out what sort of event we are. */
893 if (flags & ECORE_EXE_PIPE_READ)
895 flags = ECORE_EXE_PIPE_READ;
896 if (exe->flags & ECORE_EXE_PIPE_READ_LINE_BUFFERED)
901 flags = ECORE_EXE_PIPE_ERROR;
902 if (exe->flags & ECORE_EXE_PIPE_ERROR_LINE_BUFFERED)
907 if (flags & ECORE_EXE_PIPE_READ)
909 inbuf = exe->read_data_buf;
910 inbuf_num = exe->read_data_size;
911 exe->read_data_buf = NULL;
912 exe->read_data_size = 0;
916 inbuf = exe->error_data_buf;
917 inbuf_num = exe->error_data_size;
918 exe->error_data_buf = NULL;
919 exe->error_data_size = 0;
922 e = calloc(1, sizeof(Ecore_Exe_Event_Data));
929 if (is_buffered) /* Deal with line buffering. */
938 for (i = 0; i < inbuf_num; i++) /* Find the lines. */
940 if (inbuf[i] == '\n')
944 /* In testing, the lines seem to arrive in batches of 500 to 1000 lines at most, roughly speaking. */
945 max += 10; /* FIXME: Maybe keep track of the largest number of lines ever sent, and add half that many instead of 10. */
946 e->lines = realloc(e->lines, sizeof(Ecore_Exe_Event_Data_Line) * (max + 1)); /* Allow room for the NULL termination. */
948 /* raster said to leave the line endings as line endings, however -
949 * This is line buffered mode, we are not dealing with binary here, but lines.
950 * If we are not dealing with binary, we must be dealing with ASCII, unicode, or some other text format.
951 * Thus the user is most likely gonna deal with this text as strings.
952 * Thus the user is most likely gonna pass this data to str functions.
953 * rasters way - the endings are always gonna be '\n'; onefangs way - they will always be '\0'
954 * We are handing them the string length as a convenience.
955 * Thus if they really want it in raw format, they can e->lines[i].line[e->lines[i].size - 1] = '\n'; easily enough.
956 * In the default case, we can do this conversion quicker than the user can, as we already have the index and pointer.
957 * Let's make it easy on them to use these as standard C strings.
959 * onefang is proud to announce that he has just set a new personal record for the
960 * most over documentation of a simple assignment statement. B-)
963 e->lines[count].line = c;
964 e->lines[count].size = i - last;
966 c = (char *)&inbuf[last];
970 if (i > last) /* Partial line left over, save it for next time. */
972 if (count != 0) e->size = last;
973 if (flags & ECORE_EXE_PIPE_READ)
975 exe->read_data_size = i - last;
976 exe->read_data_buf = malloc(exe->read_data_size);
977 memcpy(exe->read_data_buf, c, exe->read_data_size);
981 exe->error_data_size = i - last;
982 exe->error_data_buf = malloc(exe->error_data_size);
983 memcpy(exe->error_data_buf, c, exe->error_data_size);
986 if (count == 0) /* No lines to send, cancel the event. */
988 _ecore_exe_event_exe_data_free(NULL, e);
991 else /* NULL terminate the array, so that people know where the end is. */
993 e->lines[count].line = NULL;
994 e->lines[count].size = 0;
1003 * Sets the string tag for the given process handle
1005 * @param exe The given process handle.
1006 * @param tag The string tag to set on the process handle.
1009 ecore_exe_tag_set(Ecore_Exe *exe,
1012 EINA_MAIN_LOOP_CHECK_RETURN;
1013 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1015 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_tag_set");
1020 exe->tag = strdup(tag);
1026 * Retrieves the tag attached to the given process handle. There is no need to
1027 * free it as it just returns the internal pointer value. This value is only
1028 * valid as long as the @p exe is valid or until the tag is set to something
1029 * else on this @p exe.
1031 * @param exe The given process handle.
1032 * @return The string attached to @p exe. It is a handle to existing
1033 * internal string and should not be modified, use
1034 * ecore_exe_tag_set() to change it. It might be @c NULL.
1037 ecore_exe_tag_get(const Ecore_Exe *exe)
1039 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1040 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1042 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_tag_get");
1049 * Frees the given process handle.
1051 * Note that the process that the handle represents is unaffected by this
1054 * @param exe The given process handle.
1055 * @return The data attached to the handle when @ref ecore_exe_run was
1059 ecore_exe_free(Ecore_Exe *exe)
1065 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1066 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1068 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_free");
1074 if (exe->pre_free_cb)
1075 exe->pre_free_cb(data, exe);
1077 if (exe->doomsday_clock)
1079 struct _ecore_exe_dead_exe *dead;
1081 ecore_timer_del(exe->doomsday_clock);
1082 exe->doomsday_clock = NULL;
1083 dead = exe->doomsday_clock_dead;
1088 exe->doomsday_clock_dead = NULL;
1091 IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
1092 IF_FN_DEL(ecore_main_fd_handler_del, exe->read_fd_handler);
1093 IF_FN_DEL(ecore_main_fd_handler_del, exe->error_fd_handler);
1094 if (exe->child_fd_write_x != -1)
1095 E_NO_ERRNO(result, close(exe->child_fd_write_x), ok);
1096 if (exe->child_fd_read_x != -1)
1097 E_NO_ERRNO(result, close(exe->child_fd_read_x), ok);
1098 if (exe->child_fd_error_x != -1)
1099 E_NO_ERRNO(result, close(exe->child_fd_error_x), ok);
1100 if (exe->child_fd_write != -1)
1101 E_NO_ERRNO(result, close(exe->child_fd_write), ok);
1102 if (exe->child_fd_read != -1)
1103 E_NO_ERRNO(result, close(exe->child_fd_read), ok);
1104 if (exe->child_fd_error != -1)
1105 E_NO_ERRNO(result, close(exe->child_fd_error), ok);
1106 IF_FREE(exe->write_data_buf);
1107 IF_FREE(exe->read_data_buf);
1108 IF_FREE(exe->error_data_buf);
1111 exes = (Ecore_Exe *)eina_inlist_remove(EINA_INLIST_GET(exes), EINA_INLIST_GET(exe));
1112 ECORE_MAGIC_SET(exe, ECORE_MAGIC_NONE);
1119 * Frees the given event data.
1121 * @param e The given event data.
1124 ecore_exe_event_data_free(Ecore_Exe_Event_Data *e)
1133 * Retrieves the process ID of the given spawned process.
1134 * @param exe Handle to the given spawned process.
1135 * @return The process ID on success. @c -1 otherwise.
1138 ecore_exe_pid_get(const Ecore_Exe *exe)
1140 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
1141 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1143 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_pid_get");
1150 * Retrieves the command of the given spawned process.
1151 * @param exe Handle to the given spawned process.
1152 * @return The command on success, @c NULL otherwise. This string is the
1153 * pointer to the internal value and must not be modified in
1157 ecore_exe_cmd_get(const Ecore_Exe *exe)
1159 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1160 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1162 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_cmd_get");
1169 * Retrieves the data attached to the given process handle.
1170 * @param exe The given process handle.
1171 * @return The data pointer attached to @p exe Given to
1172 * ecore_exe_run() or ecore_exe_pipe_run()
1175 ecore_exe_data_get(const Ecore_Exe *exe)
1177 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1178 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1180 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_data_get");
1187 * Sets the data attached to the given process handle.
1188 * @param exe The given process handle.
1189 * @param data The pointer to attach
1190 * @return The data pointer previously attached to @p exe with
1191 * ecore_exe_run(), ecore_exe_pipe_run(), or ecore_exe_data_set()
1195 ecore_exe_data_set(Ecore_Exe *exe,
1199 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1200 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1202 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, __func__);
1211 * Retrieves the flags attached to the given process handle.
1212 * @param exe The given process handle.
1213 * @return The flags attached to @p exe.
1215 EAPI Ecore_Exe_Flags
1216 ecore_exe_flags_get(const Ecore_Exe *exe)
1218 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
1219 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1221 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_data_get");
1228 * Pauses the given process by sending it a @c SIGSTOP signal.
1229 * @param exe Process handle to the given process.
1232 ecore_exe_pause(Ecore_Exe *exe)
1234 EINA_MAIN_LOOP_CHECK_RETURN;
1235 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1237 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_pause");
1240 kill(exe->pid, SIGSTOP);
1244 * Continues the given paused process by sending it a @c SIGCONT signal.
1245 * @param exe Process handle to the given process.
1248 ecore_exe_continue(Ecore_Exe *exe)
1250 EINA_MAIN_LOOP_CHECK_RETURN;
1251 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1253 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_continue");
1256 kill(exe->pid, SIGCONT);
1260 * Sends the given spawned process a interrupt (@c SIGINT) signal.
1261 * @param exe Process handle to the given process.
1264 ecore_exe_interrupt(Ecore_Exe *exe)
1266 EINA_MAIN_LOOP_CHECK_RETURN;
1267 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1269 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_interrupt");
1272 _ecore_exe_dead_attach(exe);
1273 kill(exe->pid, SIGINT);
1277 * Sends the given spawned process a quit (@c SIGQUIT) signal.
1278 * @param exe Process handle to the given process.
1281 ecore_exe_quit(Ecore_Exe *exe)
1283 EINA_MAIN_LOOP_CHECK_RETURN;
1284 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1286 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_quit");
1289 _ecore_exe_dead_attach(exe);
1290 kill(exe->pid, SIGQUIT);
1294 * Sends the given spawned process a terminate (@c SIGTERM) signal.
1295 * @param exe Process handle to the given process.
1298 ecore_exe_terminate(Ecore_Exe *exe)
1300 EINA_MAIN_LOOP_CHECK_RETURN;
1301 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1303 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_terminate");
1306 _ecore_exe_dead_attach(exe);
1307 INF("Sending TERM signal to %s (%d).", exe->cmd, exe->pid);
1308 kill(exe->pid, SIGTERM);
1312 * Kills the given spawned process by sending it a @c SIGKILL signal.
1313 * @param exe Process handle to the given process.
1316 ecore_exe_kill(Ecore_Exe *exe)
1318 struct _ecore_exe_dead_exe *dead;
1320 EINA_MAIN_LOOP_CHECK_RETURN;
1321 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1323 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_kill");
1327 dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
1330 dead->pid = exe->pid;
1331 dead->cmd = strdup(exe->cmd);
1332 IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
1333 exe->doomsday_clock =
1334 ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead, dead);
1337 INF("Sending KILL signal to %s (%d).", exe->cmd, exe->pid);
1338 kill(exe->pid, SIGKILL);
1342 * Sends a @c SIGUSR signal to the given spawned process.
1343 * @param exe Process handle to the given process.
1344 * @param num The number user signal to send. Must be either 1 or 2, or
1345 * the signal will be ignored.
1348 ecore_exe_signal(Ecore_Exe *exe,
1351 EINA_MAIN_LOOP_CHECK_RETURN;
1352 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1354 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_signal");
1358 kill(exe->pid, SIGUSR1);
1360 kill(exe->pid, SIGUSR2);
1364 * Sends a @c SIGHUP signal to the given spawned process.
1365 * @param exe Process handle to the given process.
1368 ecore_exe_hup(Ecore_Exe *exe)
1370 EINA_MAIN_LOOP_CHECK_RETURN;
1371 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1373 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_hup");
1376 kill(exe->pid, SIGHUP);
1384 _ecore_exe_is_it_alive(pid_t pid)
1386 Ecore_Exe *exe = NULL;
1388 /* FIXME: There is no nice, safe, OS independent way to tell if a
1389 * particular PID is still alive. I have written code to do so
1390 * for my urunlevel busybox applet (http://urunlevel.sourceforge.net/),
1391 * but it's for linux only, and still not guaranteed.
1393 * So for now, we just check that a valid Ecore_Exe structure
1394 * exists for it. Even that is not a guarantee, as the structure
1395 * can be freed without killing the process.
1397 * I think we can safely put exe's into two categories, those users
1398 * that care about the life of the exe, and the run and forget type.
1399 * The run and forget type starts up the exe, then free's the
1400 * Ecore_Exe structure straight away. They can never call any of
1401 * the functions that can call this, so we don't worry about them.
1403 * Those user's that care about the life of exe's will keep the
1404 * Ecore_Exe structure around, terminate them eventually, or
1405 * register for exit events. For these ones the assumption
1406 * that valid Ecore_Exe struct == live exe is almost valid.
1408 * I will probably copy my urunlevel code into here someday.
1410 exe = _ecore_exe_find(pid);
1413 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1421 _ecore_exe_make_sure_its_dead(void *data)
1423 struct _ecore_exe_dead_exe *dead;
1428 Ecore_Exe *exe = NULL;
1430 if ((exe = _ecore_exe_is_it_alive(dead->pid)))
1433 INF("Sending KILL signal to allegedly dead %s (%d).",
1434 dead->cmd, dead->pid);
1436 INF("Sending KILL signal to allegedly dead PID %d.",
1438 exe->doomsday_clock =
1439 ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead,
1441 kill(dead->pid, SIGKILL);
1449 return ECORE_CALLBACK_CANCEL;
1453 _ecore_exe_make_sure_its_really_dead(void *data)
1455 struct _ecore_exe_dead_exe *dead;
1460 Ecore_Exe *exe = NULL;
1462 if ((exe = _ecore_exe_is_it_alive(dead->pid)))
1464 ERR("RUN! The zombie wants to eat your brains! And your CPU!");
1466 INF("%s (%d) is not really dead.", dead->cmd, dead->pid);
1468 INF("PID %d is not really dead.", dead->pid);
1469 exe->doomsday_clock = NULL;
1474 return ECORE_CALLBACK_CANCEL;
1478 _ecore_exe_init(void)
1480 ECORE_EXE_EVENT_ADD = ecore_event_type_new();
1481 ECORE_EXE_EVENT_DEL = ecore_event_type_new();
1482 ECORE_EXE_EVENT_DATA = ecore_event_type_new();
1483 ECORE_EXE_EVENT_ERROR = ecore_event_type_new();
1487 _ecore_exe_shutdown(void)
1490 ecore_exe_free(exes);
1494 _ecore_exe_find(pid_t pid)
1498 EINA_INLIST_FOREACH(exes, exe)
1500 if (exe->pid == pid)
1507 _ecore_exe_doomsday_clock_get(Ecore_Exe *exe)
1509 return exe->doomsday_clock;
1513 _ecore_exe_doomsday_clock_set(Ecore_Exe *exe,
1516 exe->doomsday_clock = dc;
1520 _ecore_exe_exec_it(const char *exe_cmd,
1521 Ecore_Exe_Flags flags)
1528 /* So what is this doing?
1530 * We are trying to avoid wrapping the exe call with /bin/sh -c.
1531 * We conservatively search for certain shell meta characters,
1532 * If we don't find them, we can call the exe directly.
1534 if (!strpbrk(exe_cmd, "|&;<>()$`\\\"'*?#"))
1537 char pre_command = 1;
1540 if (!(buf = strdup(exe_cmd)))
1543 token = strtok(buf, " \t\n\v");
1546 if (token[0] == '~')
1550 if (token[0] == '[')
1552 if (strchr(token, '='))
1558 token = strtok(NULL, " \t\n\v");
1561 if ((!token) && (num_tokens))
1565 if (!(buf = strdup(exe_cmd)))
1568 token = strtok(buf, " \t\n\v");
1570 if (!(args = (char **)calloc(num_tokens + 1, sizeof(char *))))
1575 for (i = 0; i < num_tokens; i++)
1579 token = strtok(NULL, " \t\n\v");
1581 args[num_tokens] = NULL;
1585 #ifdef HAVE_SYS_PRCTL_H
1586 if ((flags & ECORE_EXE_TERM_WITH_PARENT))
1588 prctl(PR_SET_PDEATHSIG, SIGTERM);
1592 if (!(flags & ECORE_EXE_NOT_LEADER)) setsid();
1593 if ((flags & ECORE_EXE_USE_SH))
1596 execl("/bin/sh", "/bin/sh", "-c", exe_cmd, (char *)NULL);
1598 else if (use_sh) /* We have to use a shell to run this. */
1600 if (!shell) /* Find users preferred shell. */
1602 shell = getenv("SHELL");
1607 execl(shell, shell, "-c", exe_cmd, (char *)NULL);
1610 { /* We can run this directly. */
1615 ERR("arg[0] is NULL!");
1619 execvp(args[0], args);
1630 _ecore_exe_data_generic_handler(void *data,
1631 Ecore_Fd_Handler *fd_handler,
1632 Ecore_Exe_Flags flags)
1640 /* Sort out what sort of handler we are. */
1641 if (flags & ECORE_EXE_PIPE_READ)
1643 flags = ECORE_EXE_PIPE_READ;
1644 event_type = ECORE_EXE_EVENT_DATA;
1645 child_fd = exe->child_fd_read;
1649 flags = ECORE_EXE_PIPE_ERROR;
1650 event_type = ECORE_EXE_EVENT_ERROR;
1651 child_fd = exe->child_fd_error;
1655 && (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)))
1657 unsigned char *inbuf;
1660 /* Get any left over data from last time. */
1661 if (flags & ECORE_EXE_PIPE_READ)
1663 inbuf = exe->read_data_buf;
1664 inbuf_num = exe->read_data_size;
1665 exe->read_data_buf = NULL;
1666 exe->read_data_size = 0;
1670 inbuf = exe->error_data_buf;
1671 inbuf_num = exe->error_data_size;
1672 exe->error_data_buf = NULL;
1673 exe->error_data_size = 0;
1679 char buf[READBUFSIZ];
1683 if ((num = read(child_fd, buf, READBUFSIZ)) < 1)
1685 /* FIXME: SPEED/SIZE TRADE OFF - add a smaller READBUFSIZE
1686 * (currently 64k) to inbuf, use that instead of buf, and
1687 * save ourselves a memcpy(). */
1688 lost_exe = ((errno == EIO) ||
1691 (errno == EINVAL) || (errno == ENOSPC));
1692 if ((errno != EAGAIN) && (errno != EINTR))
1693 perror("_ecore_exe_generic_handler() read problem ");
1695 if (num > 0) /* data got read. */
1697 inbuf = realloc(inbuf, inbuf_num + num);
1698 memcpy(inbuf + inbuf_num, buf, num);
1702 { /* No more data to read. */
1705 Ecore_Exe_Event_Data *e;
1707 /* Stash the data away for later. */
1708 if (flags & ECORE_EXE_PIPE_READ)
1710 exe->read_data_buf = inbuf;
1711 exe->read_data_size = inbuf_num;
1715 exe->error_data_buf = inbuf;
1716 exe->error_data_size = inbuf_num;
1719 if (!(exe->flags & ECORE_EXE_PIPE_AUTO))
1721 e = ecore_exe_event_data_get(exe, flags);
1722 if (e) /* Send the event. */
1723 ecore_event_add(event_type, e,
1724 _ecore_exe_event_exe_data_free,
1730 if (flags & ECORE_EXE_PIPE_READ)
1732 if (exe->read_data_size)
1733 INF("There are %d bytes left unsent from the dead exe %s.",
1734 exe->read_data_size, exe->cmd);
1738 if (exe->error_data_size)
1739 INF("There are %d bytes left unsent from the dead exe %s.",
1740 exe->error_data_size, exe->cmd);
1742 /* Thought about this a bit. If the exe has actually
1743 * died, this won't do any harm as it must have died
1744 * recently and the pid has not had a chance to recycle.
1745 * It is also a paranoid catchall, coz the usual ecore_signal
1746 * mechenism should kick in. But let's give it a good
1747 * kick in the head anyway.
1749 ecore_exe_terminate(exe);
1756 return ECORE_CALLBACK_RENEW;
1760 _ecore_exe_data_error_handler(void *data,
1761 Ecore_Fd_Handler *fd_handler)
1763 return _ecore_exe_data_generic_handler(data, fd_handler,
1764 ECORE_EXE_PIPE_ERROR);
1768 _ecore_exe_data_read_handler(void *data,
1769 Ecore_Fd_Handler *fd_handler)
1771 return _ecore_exe_data_generic_handler(data, fd_handler,
1772 ECORE_EXE_PIPE_READ);
1776 _ecore_exe_data_write_handler(void *data,
1777 Ecore_Fd_Handler *fd_handler __UNUSED__)
1782 if ((exe->write_fd_handler) &&
1783 (ecore_main_fd_handler_active_get
1784 (exe->write_fd_handler, ECORE_FD_WRITE)))
1785 _ecore_exe_flush(exe);
1787 /* If we have sent all there is to send, and we need to close the pipe, then close it. */
1788 if ((exe->close_stdin == 1)
1789 && (exe->write_data_size == exe->write_data_offset))
1794 INF("Closing stdin for %s", exe->cmd);
1795 /* 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. */
1796 IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
1797 if (exe->child_fd_write != -1)
1798 E_NO_ERRNO(result, close(exe->child_fd_write), ok);
1799 exe->child_fd_write = -1;
1800 IF_FREE(exe->write_data_buf);
1803 return ECORE_CALLBACK_RENEW;
1807 _ecore_exe_flush(Ecore_Exe *exe)
1811 /* check whether we need to write anything at all. */
1812 if ((exe->child_fd_write == -1) || (!exe->write_data_buf))
1814 if (exe->write_data_size == exe->write_data_offset)
1817 count = write(exe->child_fd_write,
1818 (char *)exe->write_data_buf + exe->write_data_offset,
1819 exe->write_data_size - exe->write_data_offset);
1822 if (errno == EIO || errno == EBADF || errno == EPIPE || errno == EINVAL || errno == ENOSPC) /* we lost our exe! */
1824 ecore_exe_terminate(exe);
1825 if (exe->write_fd_handler)
1826 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
1831 exe->write_data_offset += count;
1832 if (exe->write_data_offset >= exe->write_data_size) /* Nothing left to write, clean up. */
1834 exe->write_data_size = 0;
1835 exe->write_data_offset = 0;
1836 IF_FREE(exe->write_data_buf);
1837 if (exe->write_fd_handler)
1838 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
1844 _ecore_exe_event_exe_data_free(void *data __UNUSED__,
1847 Ecore_Exe_Event_Data *e;
1850 ecore_exe_event_data_free(e);
1853 static Ecore_Exe_Event_Add *
1854 _ecore_exe_event_add_new(void)
1856 Ecore_Exe_Event_Add *e;
1858 e = calloc(1, sizeof(Ecore_Exe_Event_Add));
1863 _ecore_exe_event_add_free(void *data __UNUSED__,
1866 Ecore_Exe_Event_Add *e;
1873 _ecore_exe_event_del_new(void)
1875 Ecore_Exe_Event_Del *e;
1877 e = calloc(1, sizeof(Ecore_Exe_Event_Del));
1882 _ecore_exe_event_del_free(void *data __UNUSED__,
1885 Ecore_Exe_Event_Del *e;
1889 ecore_exe_free(e->exe);
1894 _ecore_exe_dead_attach(Ecore_Exe *exe)
1896 struct _ecore_exe_dead_exe *dead;
1898 if (exe->doomsday_clock_dead) return;
1899 dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
1902 dead->pid = exe->pid;
1903 dead->cmd = strdup(exe->cmd);
1904 IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
1905 exe->doomsday_clock =
1906 ecore_timer_add(10.0, _ecore_exe_make_sure_its_dead, dead);
1907 exe->doomsday_clock_dead = dead;