13 #include <sys/resource.h>
15 #ifdef HAVE_SYS_PRCTL_H
16 # include <sys/prctl.h>
19 #ifdef HAVE_SYS_WAIT_H
20 # include <sys/wait.h>
24 #include "ecore_private.h"
26 /* FIXME: Getting respawn to work
28 * There is no way that we can do anything about the internal state info of
29 * an external exe. The same can be said about the state of user code. User
30 * code in this context means the code that is using ecore_exe to manage exe's
33 * Document that the exe must be respawnable, in other words, there is no
34 * state that it cannot regenerate by just killing it and starting it again.
35 * This includes state that the user code knows about, as the respawn is
36 * transparent to that code. On the other hand, maybe a respawn event might
37 * be useful, or maybe resend the currently non existent add event. For
38 * consistancy with ecore_con, an add event is good anyway.
40 * The Ecore_exe structure is reused for respawning, so that the (opaque)
41 * pointer held by the user remains valid. This means that the Ecore_Exe
42 * init and del functions may need to be split into two parts each to avoid
43 * duplicating code - common code part, and the rest. This implies that
44 * the unchanging members mentioned next should NEVER change.
46 * These structure members don't need to change -
47 * __list_data - we stay on the list
48 * ECORE_MAGIC - this is a constant
49 * data - passed in originally
50 * cmd - passed in originally
51 * flags - passed in originally
53 * These structure members need to change -
54 * tag - state that must be regenerated, zap it
55 * pid - it will be different
56 * child_fd_write - it will be different
57 * child_fd_read - it will be different
58 * child_fd_error - it will be different
59 * write_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
60 * read_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
61 * error_fd_handler - we cannot change the fd used by a handler, this changes coz the fd changes.
63 * Hmm, the read, write, and error buffers could be tricky.
64 * They are not atomic, and could be in a semi complete state.
65 * They fall into the "state must be regenerated" mentioned above.
66 * A respawn/add event should take care of it.
68 * These structure members need to change -
69 * write_data_buf - state that must be regenerated, zap it
70 * write_data_size - state that must be regenerated, zap it
71 * write_data_offset - state that must be regenerated, zap it
72 * read_data_buf - state that must be regenerated, zap it
73 * read_data_size - state that must be regenerated, zap it
74 * error_data_buf - state that must be regenerated, zap it
75 * error_data_size - state that must be regenerated, zap it
76 * close_write - state that must be regenerated, zap it
78 * There is the problem that an exe that fell over and needs respawning
79 * might keep falling over, keep needing to be respawned, and tie up system
80 * resources with the constant respawning. An exponentially increasing
81 * timeout (with maximum timeout) between respawns should take care of that.
82 * Although this is not a "contention for a resource" problem, the exe falling
83 * over may be, so a random element added to the timeout may help, and won't
84 * hurt. The user code may need to be informed that a timeout is in progress.
94 Ecore_Exe_Flags flags;
95 Ecore_Fd_Handler *write_fd_handler; /* the fd_handler to handle write to child - if this was used, or NULL if not */
96 Ecore_Fd_Handler *read_fd_handler; /* the fd_handler to handle read from child - if this was used, or NULL if not */
97 Ecore_Fd_Handler *error_fd_handler; /* the fd_handler to handle errors from child - if this was used, or NULL if not */
98 void *write_data_buf; /* a data buffer for data to write to the child -
99 * realloced as needed for more data and flushed when the fd handler says writes are possible
101 int write_data_size; /* the size in bytes of the data buffer */
102 int write_data_offset; /* the offset in bytes in the data buffer */
103 void *read_data_buf; /* data read from the child awating delivery to an event */
104 int read_data_size; /* data read from child in bytes */
105 void *error_data_buf; /* errors read from the child awating delivery to an event */
106 int error_data_size; /* errors read from child in bytes */
107 int child_fd_write; /* fd to write TO to send data to the child */
108 int child_fd_read; /* fd to read FROM when child has sent us (the parent) data */
109 int child_fd_error; /* fd to read FROM when child has sent us (the parent) errors */
110 int child_fd_write_x; /* fd to write TO to send data to the child */
111 int child_fd_read_x; /* fd to read FROM when child has sent us (the parent) data */
112 int child_fd_error_x; /* fd to read FROM when child has sent us (the parent) errors */
113 Eina_Bool close_stdin : 1;
115 int start_bytes, end_bytes, start_lines, end_lines; /* Number of bytes/lines to auto pipe at start/end of stdout/stderr. */
117 Ecore_Timer *doomsday_clock; /* The Timer of Death. Muahahahaha. */
118 void *doomsday_clock_dead; /* data for the doomsday clock */
120 Ecore_Exe_Cb pre_free_cb;
123 /* TODO: Something to let people build a command line and does auto escaping -
125 * ecore_exe_snprintf()
129 * cmd = ecore_exe_comand_parameter_append(cmd, "firefox");
130 * cmd = ecore_exe_comand_parameter_append(cmd, "http://www.foo.com/bar.html?baz=yes");
131 * each parameter appended is one argument, and it gets escaped, quoted, and
132 * appended with a preceding space. The first is the command off course.
135 struct _ecore_exe_dead_exe
141 static inline void _ecore_exe_exec_it(const char *exe_cmd,
142 Ecore_Exe_Flags flags);
143 static Eina_Bool _ecore_exe_data_generic_handler(void *data,
144 Ecore_Fd_Handler *fd_handler,
145 Ecore_Exe_Flags flags);
146 static Eina_Bool _ecore_exe_data_error_handler(void *data,
147 Ecore_Fd_Handler *fd_handler);
148 static Eina_Bool _ecore_exe_data_read_handler(void *data,
149 Ecore_Fd_Handler *fd_handler);
150 static Eina_Bool _ecore_exe_data_write_handler(void *data,
151 Ecore_Fd_Handler *fd_handler);
152 static void _ecore_exe_flush(Ecore_Exe *exe);
153 static void _ecore_exe_event_exe_data_free(void *data __UNUSED__,
155 static Ecore_Exe *_ecore_exe_is_it_alive(pid_t pid);
156 static Eina_Bool _ecore_exe_make_sure_its_dead(void *data);
157 static Eina_Bool _ecore_exe_make_sure_its_really_dead(void *data);
158 static Ecore_Exe_Event_Add *_ecore_exe_event_add_new(void);
159 static void _ecore_exe_event_add_free(void *data,
161 static void _ecore_exe_dead_attach(Ecore_Exe *exe);
163 EAPI int ECORE_EXE_EVENT_ADD = 0;
164 EAPI int ECORE_EXE_EVENT_DEL = 0;
165 EAPI int ECORE_EXE_EVENT_DATA = 0;
166 EAPI int ECORE_EXE_EVENT_ERROR = 0;
168 static Ecore_Exe *exes = NULL;
169 static const char *shell = NULL;
171 /* FIXME: This errno checking stuff should be put elsewhere for everybody to use.
172 * For now it lives here though, just to make testing easier.
174 static int _ecore_exe_check_errno(int result,
178 #define E_IF_NO_ERRNO(result, foo, ok) \
179 while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1); \
182 #define E_NO_ERRNO(result, foo, ok) \
183 while (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)) == -1) sleep(1)
185 #define E_IF_NO_ERRNO_NOLOOP(result, foo, ok) \
186 if (((ok) = _ecore_exe_check_errno((result) = (foo), __FILE__, __LINE__)))
189 _ecore_exe_check_errno(int result,
190 const char *file __UNUSED__,
193 int saved_errno = errno;
197 perror("*** errno reports ");
198 /* What is currently supported -
201 * EFAULT Argument is not valid.
202 * EMFILE Too many file descriptors used by process.
203 * ENFILE Too many open files by system.
205 * EAGAIN No data now, try again.
206 * EBADF This is not an fd that can be read.
207 * EFAULT This is not a valid buffer.
208 * EINTR Interupted by signal, try again.
209 * EINVAL This is not an fd that can be read.
211 * EISDIR This is a directory, and cannot be read.
212 * others Depending on what sort of thing we are reading from.
214 * EBADF This is not an fd that can be closed.
215 * EINTR Interupted by signal, try again.
218 * EBADF This is not an fd that can be dup2'ed.
219 * EBUSY Race condition between open() and dup()
220 * EINTR Interupted by signal, try again.
221 * EMFILE Too many file descriptors used by process.
223 * EACCES, EAGAIN Locked or mapped by something else, try again later.
224 * EBADF This is not an fd that can be fcntl'ed.
225 * EDEADLK This will cause a deadlock.
226 * EFAULT This is not a valid lock.
227 * EINTR Interupted by signal, try again.
228 * EINVAL This is not a valid arg.
229 * EMFILE Too many file descriptors used by process.
230 * ENOLCK Problem getting a lock.
231 * EPERM Not allowed to do that.
233 * EBADF This is not an fd that is open for writing.
234 * EINVAL, EROFS This is not an fd that can be fsynced.
241 * E_IF_NO_ERRNO(result, foo(bar), ok)
243 * E_IF_NO_ERRNO_NOLOOP(result, foo(bar), ok)
250 * // Something failed, cleanup.
258 { /* Not now, try later. */
259 ERR("*** Must try again in %s @%u.", file, line);
267 { /* Low on resources. */
268 ERR("*** Low on resources in %s @%u.", file,
276 ERR("*** I/O error in %s @%u.", file, line);
289 { /* Programmer fucked up. */
290 ERR("*** NAUGHTY PROGRAMMER!!!\n"
291 "*** SPANK SPANK SPANK!!!\n"
292 "*** Now go fix your code in %s @%u. Tut tut tut!",
299 { /* Unsupported errno code, please add this one. */
300 ERR("*** NAUGHTY PROGRAMMER!!!\n"
301 "*** SPANK SPANK SPANK!!!\n"
302 "*** Unsupported errno code %d, please add this one.\n"
303 "*** Now go fix your code in %s @%u, from %s @%u. Tut tut tut!",
304 saved_errno, __FILE__, __LINE__, file, line);
310 else /* Everything is fine. */
318 * @addtogroup Ecore_Exe_Group
323 static int run_pri = ECORE_EXE_PRIORITY_INHERIT;
326 * Sets the priority at which to launch processes
328 * This sets the priority of processes run by ecore_exe_run() and
329 * ecore_exe_pipe_run().
330 * @li On Windows, the child process is created by default with the
331 * @ref ECORE_EXE_WIN32_PRIORITY_NORMAL priority, unless the calling
332 * process is in @ref ECORE_EXE_WIN32_PRIORITY_IDLE or
333 * @ref ECORE_EXE_WIN32_PRIORITY_BELOW_NORMAL priority. In that case, the
334 * child process inherits this priority.
335 * @li On other platforms, if set to @ref ECORE_EXE_PRIORITY_INHERIT child
336 * processes inherits the priority of their parent. This is the default.
338 * @param pri value a Ecore_Exe_Win32_Priority value on Windows, -20
339 * to 19 or @ref ECORE_EXE_PRIORITY_INHERIT on other OS.
342 ecore_exe_run_priority_set(int pri)
344 EINA_MAIN_LOOP_CHECK_RETURN;
349 * Gets the priority at which to launch processes
351 * This gets ths priority of launched processes. See
352 * ecore_exe_run_priority_set() for details. This just returns the value set
355 * @return the value set by ecore_exe_run_priority_set()
358 ecore_exe_run_priority_get(void)
360 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
365 * Spawns a child process.
367 * This is now just a thin wrapper around ecore_exe_pipe_run()
368 * @note When you use this function you will have no permissions
369 * to write or read on the pipe that connects you with the spwaned process.
370 * If you need to do that use ecore_exe_pipe_run() with the
371 * appropriated flags.
373 * @param exe_cmd The command to run with @c /bin/sh.
374 * @param data Data to attach to the returned process handle.
375 * @return A process handle to the spawned process.
378 ecore_exe_run(const char *exe_cmd,
381 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
382 return ecore_exe_pipe_run(exe_cmd, 0, data);
386 * Spawns a child process with its stdin/out available for communication.
388 * This function forks and runs the given command using @c /bin/sh.
390 * Note that the process handle is only valid until a child process
391 * terminated event is received. After all handlers for the child process
392 * terminated event have been called, the handle will be freed by Ecore.
394 * This function does the same thing as ecore_exe_run(), but also makes the
395 * standard in and/or out as well as stderr from the child process available
396 * for reading or writing. To write use ecore_exe_send(). To read listen to
397 * ECORE_EXE_EVENT_DATA or ECORE_EXE_EVENT_ERROR events (set up handlers).
398 * Ecore may buffer read and error data until a newline character if asked
399 * for with the @p flags. All data will be included in the events (newlines
400 * will be replaced with NULLS if line buffered). ECORE_EXE_EVENT_DATA events
401 * will only happen if the process is run with ECORE_EXE_PIPE_READ enabled
402 * in the flags. The same with the error version. Writing will only be
403 * allowed with ECORE_EXE_PIPE_WRITE enabled in the flags.
405 * @param exe_cmd The command to run with @c /bin/sh.
406 * @param flags The flag parameters for how to deal with inter-process I/O
407 * @param data Data to attach to the returned process handle.
408 * @return A process handle to the spawned process.
411 ecore_exe_pipe_run(const char *exe_cmd,
412 Ecore_Exe_Flags flags,
415 Ecore_Exe *exe = NULL;
416 int statusPipe[2] = { -1, -1 };
417 int errorPipe[2] = { -1, -1 };
418 int readPipe[2] = { -1, -1 };
419 int writePipe[2] = { -1, -1 };
424 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
425 if (!exe_cmd) return NULL;
426 exe = calloc(1, sizeof(Ecore_Exe));
427 if (!exe) return NULL;
429 if ((flags & ECORE_EXE_PIPE_AUTO) && (!(flags & ECORE_EXE_PIPE_ERROR))
430 && (!(flags & ECORE_EXE_PIPE_READ)))
431 /* We need something to auto pipe. */
432 flags |= ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_ERROR;
434 exe->child_fd_error = -1;
435 exe->child_fd_read = -1;
436 exe->child_fd_write = -1;
437 exe->child_fd_error_x = -1;
438 exe->child_fd_read_x = -1;
439 exe->child_fd_write_x = -1;
441 /* Create some pipes. */
444 E_IF_NO_ERRNO_NOLOOP(result, pipe(statusPipe), ok)
448 if (ok && (flags & ECORE_EXE_PIPE_ERROR))
450 E_IF_NO_ERRNO_NOLOOP(result, pipe(errorPipe), ok)
452 exe->child_fd_error = errorPipe[0];
453 exe->child_fd_error_x = errorPipe[1];
456 if (ok && (flags & ECORE_EXE_PIPE_READ))
458 E_IF_NO_ERRNO_NOLOOP(result, pipe(readPipe), ok)
460 exe->child_fd_read = readPipe[0];
461 exe->child_fd_read_x = readPipe[1];
464 if (ok && (flags & ECORE_EXE_PIPE_WRITE))
466 E_IF_NO_ERRNO_NOLOOP(result, pipe(writePipe), ok)
468 exe->child_fd_write = writePipe[1];
469 exe->child_fd_write_x = writePipe[0];
475 volatile int vfork_exec_errno = 0;
477 /* FIXME: I should double check this. After a quick look around, this is already done, but via a more modern method. */
478 /* signal(SIGPIPE, SIG_IGN); We only want EPIPE on errors */
483 ERR("Failed to fork process");
486 else if (pid == 0) /* child */
488 if (run_pri != ECORE_EXE_PRIORITY_INHERIT)
490 if ((run_pri >= -20) && (run_pri <= 19))
491 setpriority(PRIO_PROCESS, 0, run_pri);
493 /* dup2 STDERR, STDIN, and STDOUT. dup2() allegedly closes the
494 * second pipe if it's open. On the other hand, there was the
495 * Great FD Leak Scare of '06, so let's be paranoid. */
496 if (ok && (flags & ECORE_EXE_PIPE_ERROR))
498 E_NO_ERRNO(result, close(STDERR_FILENO), ok);
499 E_NO_ERRNO(result, dup2(errorPipe[1], STDERR_FILENO), ok);
501 if (ok && (flags & ECORE_EXE_PIPE_READ))
503 E_NO_ERRNO(result, close(STDOUT_FILENO), ok);
504 E_NO_ERRNO(result, dup2(readPipe[1], STDOUT_FILENO), ok);
506 if (ok && (flags & ECORE_EXE_PIPE_WRITE))
508 E_NO_ERRNO(result, close(STDIN_FILENO), ok);
509 E_NO_ERRNO(result, dup2(writePipe[0], STDIN_FILENO), ok);
514 /* Setup the status pipe. */
515 E_NO_ERRNO(result, close(statusPipe[0]), ok);
516 E_IF_NO_ERRNO(result, fcntl(statusPipe[1], F_SETFD, FD_CLOEXEC), ok) /* close on exec shows success */
518 /* Run the actual command. */
519 _ecore_exe_exec_it(exe_cmd, flags); /* no return */
523 /* Something went 'orribly wrong. */
524 vfork_exec_errno = errno;
526 /* Close the pipes. */
527 if (flags & ECORE_EXE_PIPE_ERROR)
528 E_NO_ERRNO(result, close(errorPipe[1]), ok);
529 if (flags & ECORE_EXE_PIPE_READ)
530 E_NO_ERRNO(result, close(readPipe[1]), ok);
531 if (flags & ECORE_EXE_PIPE_WRITE)
532 E_NO_ERRNO(result, close(writePipe[0]), ok);
533 E_NO_ERRNO(result, close(statusPipe[1]), ok);
539 /* Close the unused pipes. */
540 E_NO_ERRNO(result, close(statusPipe[1]), ok);
542 /* FIXME: after having a good look at the current e fd
543 * handling, investigate fcntl(dataPipe[x], F_SETSIG, ...) */
544 /* FIXME: above F_SETSIG etc. - this is async SIGIO based IO
545 * which is also linux specific so we probably don't want to
546 * do this as long as select() is working fine. the only time
547 * we really want to think of SIGIO async IO is when it all
548 * actually works basically everywhere and we can turn all
549 * IO into DMA async activities (i.e. you do a read() then
550 * the read is complete not on return but when you get a
551 * SIGIO - the read() just starts the transfer and it is
552 * completed in the background by DMA (or whatever mechanism
553 * the kernel choses)) */
555 /* Wait for it to start executing. */
556 /* FIXME: this doesn't seem very nice - we sit and block
557 * waiting on a child process... even though it's just
558 * the segment between the fork() and the exec) it just feels
564 E_NO_ERRNO(result, read(statusPipe[0], &buf, 1), ok);
567 if (vfork_exec_errno != 0)
569 n = vfork_exec_errno;
570 ERR("Could not start \"%s\"", exe_cmd);
577 /* Close the status pipe. */
578 E_NO_ERRNO(result, close(statusPipe[0]), ok);
583 /* Setup the exe structure. */
584 ECORE_MAGIC_SET(exe, ECORE_MAGIC_EXE);
585 exe->start_bytes = -1;
587 exe->start_lines = -1;
591 exe->data = (void *)data;
592 if ((exe->cmd = strdup(exe_cmd)))
594 if (flags & ECORE_EXE_PIPE_ERROR) /* Setup the error stuff. */
596 E_IF_NO_ERRNO(result,
597 fcntl(exe->child_fd_error, F_SETFL,
600 E_IF_NO_ERRNO(result,
601 fcntl(exe->child_fd_error, F_SETFD,
604 E_IF_NO_ERRNO(result,
605 fcntl(exe->child_fd_error_x, F_SETFD,
609 exe->error_fd_handler =
610 ecore_main_fd_handler_add(exe->child_fd_error,
612 _ecore_exe_data_error_handler,
614 if (!exe->error_fd_handler)
618 if (ok && (flags & ECORE_EXE_PIPE_READ)) /* Setup the read stuff. */
620 E_IF_NO_ERRNO(result,
621 fcntl(exe->child_fd_read, F_SETFL,
624 E_IF_NO_ERRNO(result,
625 fcntl(exe->child_fd_read, F_SETFD,
628 E_IF_NO_ERRNO(result,
629 fcntl(exe->child_fd_read_x, F_SETFD,
633 exe->read_fd_handler =
634 ecore_main_fd_handler_add(exe->child_fd_read,
636 _ecore_exe_data_read_handler,
638 if (!exe->read_fd_handler)
642 if (ok && (flags & ECORE_EXE_PIPE_WRITE)) /* Setup the write stuff. */
644 E_IF_NO_ERRNO(result,
645 fcntl(exe->child_fd_write, F_SETFL,
648 E_IF_NO_ERRNO(result,
649 fcntl(exe->child_fd_write, F_SETFD,
652 E_IF_NO_ERRNO(result,
653 fcntl(exe->child_fd_write_x, F_SETFD,
657 exe->write_fd_handler =
658 ecore_main_fd_handler_add(exe->child_fd_write,
660 _ecore_exe_data_write_handler,
662 if (exe->write_fd_handler)
663 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0); /* Nothing to write to start with. */
669 exes = (Ecore_Exe *)eina_inlist_append(EINA_INLIST_GET(exes), EINA_INLIST_GET(exe));
679 if (!ok) /* Something went wrong, so pull down everything. */
681 if (exe->pid) ecore_exe_terminate(exe);
682 IF_FN_DEL(ecore_exe_free, exe);
686 Ecore_Exe_Event_Add *e;
688 e = _ecore_exe_event_add_new();
690 if (e) /* Send the event. */
691 ecore_event_add(ECORE_EXE_EVENT_ADD, e,
692 _ecore_exe_event_add_free, NULL);
693 /* INF("Running as %d for %s.\n", exe->pid, exe->cmd); */
701 * Defines a function to be called before really freeing the handle data.
703 * This might be useful for language bindings such as Python and Perl
704 * that need to deallocate wrappers associated with this handle.
706 * This handle should never be modified by this call. It should be
707 * considered informative only. All getters are valid when the given
708 * function is called back.
710 * @param exe The child process to attach the pre_free function.
711 * @param func The function to call before @a exe is freed.
714 ecore_exe_callback_pre_free_set(Ecore_Exe *exe,
717 EINA_MAIN_LOOP_CHECK_RETURN;
718 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
720 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE,
721 "ecore_exe_callback_pre_free_set");
724 exe->pre_free_cb = func;
728 * Sends data to the given child process which it receives on stdin.
730 * This function writes to a child processes standard in, with unlimited
731 * buffering. This call will never block. It may fail if the system runs out
734 * @param exe The child process to send to
735 * @param data The data to send
736 * @param size The size of the data to send, in bytes
737 * @return @c EINA_TRUE if successful, @c EINA_FALSE on failure.
740 ecore_exe_send(Ecore_Exe *exe,
746 EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
747 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
749 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_send");
753 if (exe->close_stdin)
755 ERR("Ecore_Exe %p stdin is closed! Cannot send %d bytes from %p",
760 if (exe->child_fd_write == -1)
762 ERR("Ecore_Exe %p created without ECORE_EXE_PIPE_WRITE! "
763 "Cannot send %d bytes from %p", exe, size, data);
767 buf = realloc(exe->write_data_buf, exe->write_data_size + size);
768 if (!buf) return EINA_FALSE;
770 exe->write_data_buf = buf;
771 memcpy((char *)exe->write_data_buf + exe->write_data_size, data, size);
772 exe->write_data_size += size;
774 if (exe->write_fd_handler)
775 ecore_main_fd_handler_active_set(exe->write_fd_handler, ECORE_FD_WRITE);
781 * The stdin of the given child process will close when the write buffer is empty.
783 * @param exe The child process
786 ecore_exe_close_stdin(Ecore_Exe *exe)
788 EINA_MAIN_LOOP_CHECK_RETURN;
789 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
791 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_close_stdin");
794 exe->close_stdin = 1;
798 * Sets the auto pipe limits for the given process handle. On Windows
799 * this function does nothing.
801 * @param exe The given process handle.
802 * @param start_bytes limit of bytes at start of output to buffer.
803 * @param end_bytes limit of bytes at end of output to buffer.
804 * @param start_lines limit of lines at start of output to buffer.
805 * @param end_lines limit of lines at end of output to buffer.
808 ecore_exe_auto_limits_set(Ecore_Exe *exe,
814 EINA_MAIN_LOOP_CHECK_RETURN;
815 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
817 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_auto_limits_set");
820 /* FIXME: sanitize the input. */
821 exe->start_bytes = start_bytes;
822 exe->end_bytes = end_bytes;
823 exe->start_lines = start_lines;
824 exe->end_lines = end_lines;
826 /* FIXME: get this can of worms working.
828 * capture stderr & stdout internally
830 * raster and onefang keep moving the goal posts on this one. It started out as
831 * "show users the error output if an exe fails" and is rapidly approaching
832 * "alternative method of getting the data, poll vs event driven". Some serious
833 * thinking needs to be applied to this. Do we really want to go that far? If
834 * so, we should change the names. The basic design will probably remain the
835 * same which ever way we go. The constant goal post moving is probably due to
836 * generic design methods leading to feature creep as we inspired each other to
837 * more generic designs. It does seem like the closer we get to poll driven,
838 * the more issues and corner cases there are.
840 * Instead of doing the usual register an event handler thing, we are ecore_exe,
841 * we can take some short cuts. Don't send the events, just leave the exe buffers
842 * as is until the user asks for them, then return the event.
844 * start = 0, end = 0; clogged arteries get flushed, everything is ignored.
845 * start = -1, end = -1; clogged arteries get transferred to internal buffers. Actually, either == -1 means buffer everything.
846 * start = X, end = 0; buffer first X out of clogged arteries, flush and ignore rest.
847 * start = 0, end = X; circular buffer X
848 * start = X, end = Y; buffer first X out of clogged arteries, circular buffer Y from beginning.
850 * bytes vs lines, which ever one reaches the limit first.
851 * Before we go beyond the start+end limit, leave the end buffer empty, and store both in the start buffer, coz they overlap.
852 * 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.
855 * Spank programmer for polling data if polling is not turned on.
856 * Spank programmer for setting up event callbacks if polling is turned on.
857 * Spank programmer for freeing the event data if it came from the event system, as that autofrees.
858 * Spank the programmer if they try to set the limits bigger than what has been gathered & ignored already, coz they just lost data.
859 * Spank onefang and raster for opening this can of worms.
860 * Should we have separate out/err limits?
861 * Should we remove from the internal buffer the data that was delivered already?
862 * If so, what to do about limits, start, and end? They could loose their meaning.
867 * Gets the auto pipe data for the given process handle
869 * @param exe The given process handle.
870 * @param flags Is this a ECORE_EXE_PIPE_READ or ECORE_EXE_PIPE_ERROR?
871 * @return The event data.
873 EAPI Ecore_Exe_Event_Data *
874 ecore_exe_event_data_get(Ecore_Exe *exe,
875 Ecore_Exe_Flags flags)
877 Ecore_Exe_Event_Data *e = NULL;
879 unsigned char *inbuf;
882 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
883 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
885 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_event_data_get");
889 /* Sort out what sort of event we are. */
890 if (flags & ECORE_EXE_PIPE_READ)
892 flags = ECORE_EXE_PIPE_READ;
893 if (exe->flags & ECORE_EXE_PIPE_READ_LINE_BUFFERED)
898 flags = ECORE_EXE_PIPE_ERROR;
899 if (exe->flags & ECORE_EXE_PIPE_ERROR_LINE_BUFFERED)
904 if (flags & ECORE_EXE_PIPE_READ)
906 inbuf = exe->read_data_buf;
907 inbuf_num = exe->read_data_size;
908 exe->read_data_buf = NULL;
909 exe->read_data_size = 0;
913 inbuf = exe->error_data_buf;
914 inbuf_num = exe->error_data_size;
915 exe->error_data_buf = NULL;
916 exe->error_data_size = 0;
919 e = calloc(1, sizeof(Ecore_Exe_Event_Data));
926 if (is_buffered) /* Deal with line buffering. */
935 for (i = 0; i < inbuf_num; i++) /* Find the lines. */
937 if (inbuf[i] == '\n')
941 /* In testing, the lines seem to arrive in batches of 500 to 1000 lines at most, roughly speaking. */
942 max += 10; /* FIXME: Maybe keep track of the largest number of lines ever sent, and add half that many instead of 10. */
943 e->lines = realloc(e->lines, sizeof(Ecore_Exe_Event_Data_Line) * (max + 1)); /* Allow room for the NULL termination. */
945 /* raster said to leave the line endings as line endings, however -
946 * This is line buffered mode, we are not dealing with binary here, but lines.
947 * If we are not dealing with binary, we must be dealing with ASCII, unicode, or some other text format.
948 * Thus the user is most likely gonna deal with this text as strings.
949 * Thus the user is most likely gonna pass this data to str functions.
950 * rasters way - the endings are always gonna be '\n'; onefangs way - they will always be '\0'
951 * We are handing them the string length as a convenience.
952 * Thus if they really want it in raw format, they can e->lines[i].line[e->lines[i].size - 1] = '\n'; easily enough.
953 * In the default case, we can do this conversion quicker than the user can, as we already have the index and pointer.
954 * Let's make it easy on them to use these as standard C strings.
956 * onefang is proud to announce that he has just set a new personal record for the
957 * most over documentation of a simple assignment statement. B-)
960 e->lines[count].line = c;
961 e->lines[count].size = i - last;
963 c = (char *)&inbuf[last];
967 if (i > last) /* Partial line left over, save it for next time. */
969 if (count != 0) e->size = last;
970 if (flags & ECORE_EXE_PIPE_READ)
972 exe->read_data_size = i - last;
973 exe->read_data_buf = malloc(exe->read_data_size);
974 memcpy(exe->read_data_buf, c, exe->read_data_size);
978 exe->error_data_size = i - last;
979 exe->error_data_buf = malloc(exe->error_data_size);
980 memcpy(exe->error_data_buf, c, exe->error_data_size);
983 if (count == 0) /* No lines to send, cancel the event. */
985 _ecore_exe_event_exe_data_free(NULL, e);
988 else /* NULL terminate the array, so that people know where the end is. */
990 e->lines[count].line = NULL;
991 e->lines[count].size = 0;
1000 * Sets the string tag for the given process handle
1002 * @param exe The given process handle.
1003 * @param tag The string tag to set on the process handle.
1006 ecore_exe_tag_set(Ecore_Exe *exe,
1009 EINA_MAIN_LOOP_CHECK_RETURN;
1010 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1012 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_tag_set");
1017 exe->tag = strdup(tag);
1023 * Retrieves the tag attached to the given process handle. There is no need to
1024 * free it as it just returns the internal pointer value. This value is only
1025 * valid as long as the @p exe is valid or until the tag is set to something
1026 * else on this @p exe.
1028 * @param exe The given process handle.
1029 * @return The string attached to @p exe. It is a handle to existing
1030 * internal string and should not be modified, use
1031 * ecore_exe_tag_set() to change it. It might be @c NULL.
1034 ecore_exe_tag_get(const Ecore_Exe *exe)
1036 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1037 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1039 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_tag_get");
1046 * Frees the given process handle.
1048 * Note that the process that the handle represents is unaffected by this
1051 * @param exe The given process handle.
1052 * @return The data attached to the handle when @ref ecore_exe_run was
1056 ecore_exe_free(Ecore_Exe *exe)
1062 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1063 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1065 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_free");
1071 if (exe->pre_free_cb)
1072 exe->pre_free_cb(data, exe);
1074 if (exe->doomsday_clock)
1076 struct _ecore_exe_dead_exe *dead;
1078 ecore_timer_del(exe->doomsday_clock);
1079 exe->doomsday_clock = NULL;
1080 dead = exe->doomsday_clock_dead;
1085 exe->doomsday_clock_dead = NULL;
1088 IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
1089 IF_FN_DEL(ecore_main_fd_handler_del, exe->read_fd_handler);
1090 IF_FN_DEL(ecore_main_fd_handler_del, exe->error_fd_handler);
1091 if (exe->child_fd_write_x != -1)
1092 E_NO_ERRNO(result, close(exe->child_fd_write_x), ok);
1093 if (exe->child_fd_read_x != -1)
1094 E_NO_ERRNO(result, close(exe->child_fd_read_x), ok);
1095 if (exe->child_fd_error_x != -1)
1096 E_NO_ERRNO(result, close(exe->child_fd_error_x), ok);
1097 if (exe->child_fd_write != -1)
1098 E_NO_ERRNO(result, close(exe->child_fd_write), ok);
1099 if (exe->child_fd_read != -1)
1100 E_NO_ERRNO(result, close(exe->child_fd_read), ok);
1101 if (exe->child_fd_error != -1)
1102 E_NO_ERRNO(result, close(exe->child_fd_error), ok);
1103 IF_FREE(exe->write_data_buf);
1104 IF_FREE(exe->read_data_buf);
1105 IF_FREE(exe->error_data_buf);
1108 exes = (Ecore_Exe *)eina_inlist_remove(EINA_INLIST_GET(exes), EINA_INLIST_GET(exe));
1109 ECORE_MAGIC_SET(exe, ECORE_MAGIC_NONE);
1116 * Frees the given event data.
1118 * @param e The given event data.
1121 ecore_exe_event_data_free(Ecore_Exe_Event_Data *e)
1130 * Retrieves the process ID of the given spawned process.
1131 * @param exe Handle to the given spawned process.
1132 * @return The process ID on success. @c -1 otherwise.
1135 ecore_exe_pid_get(const Ecore_Exe *exe)
1137 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
1138 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1140 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_pid_get");
1147 * Retrieves the command of the given spawned process.
1148 * @param exe Handle to the given spawned process.
1149 * @return The command on success, @c NULL otherwise. This string is the
1150 * pointer to the internal value and must not be modified in
1154 ecore_exe_cmd_get(const Ecore_Exe *exe)
1156 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1157 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1159 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_cmd_get");
1166 * Retrieves the data attached to the given process handle.
1167 * @param exe The given process handle.
1168 * @return The data pointer attached to @p exe Given to
1169 * ecore_exe_run() or ecore_exe_pipe_run()
1172 ecore_exe_data_get(const Ecore_Exe *exe)
1174 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1175 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1177 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_data_get");
1184 * Sets the data attached to the given process handle.
1185 * @param exe The given process handle.
1186 * @param data The pointer to attach
1187 * @return The data pointer previously attached to @p exe with
1188 * ecore_exe_run(), ecore_exe_pipe_run(), or ecore_exe_data_set()
1192 ecore_exe_data_set(Ecore_Exe *exe,
1196 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
1197 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1199 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, __func__);
1208 * Retrieves the flags attached to the given process handle.
1209 * @param exe The given process handle.
1210 * @return The flags attached to @p exe.
1212 EAPI Ecore_Exe_Flags
1213 ecore_exe_flags_get(const Ecore_Exe *exe)
1215 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
1216 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1218 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_data_get");
1225 * Pauses the given process by sending it a @c SIGSTOP signal.
1226 * @param exe Process handle to the given process.
1229 ecore_exe_pause(Ecore_Exe *exe)
1231 EINA_MAIN_LOOP_CHECK_RETURN;
1232 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1234 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_pause");
1237 kill(exe->pid, SIGSTOP);
1241 * Continues the given paused process by sending it a @c SIGCONT signal.
1242 * @param exe Process handle to the given process.
1245 ecore_exe_continue(Ecore_Exe *exe)
1247 EINA_MAIN_LOOP_CHECK_RETURN;
1248 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1250 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_continue");
1253 kill(exe->pid, SIGCONT);
1257 * Sends the given spawned process a interrupt (@c SIGINT) signal.
1258 * @param exe Process handle to the given process.
1261 ecore_exe_interrupt(Ecore_Exe *exe)
1263 EINA_MAIN_LOOP_CHECK_RETURN;
1264 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1266 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_interrupt");
1269 _ecore_exe_dead_attach(exe);
1270 kill(exe->pid, SIGINT);
1274 * Sends the given spawned process a quit (@c SIGQUIT) signal.
1275 * @param exe Process handle to the given process.
1278 ecore_exe_quit(Ecore_Exe *exe)
1280 EINA_MAIN_LOOP_CHECK_RETURN;
1281 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1283 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_quit");
1286 _ecore_exe_dead_attach(exe);
1287 kill(exe->pid, SIGQUIT);
1291 * Sends the given spawned process a terminate (@c SIGTERM) signal.
1292 * @param exe Process handle to the given process.
1295 ecore_exe_terminate(Ecore_Exe *exe)
1297 EINA_MAIN_LOOP_CHECK_RETURN;
1298 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1300 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_terminate");
1303 _ecore_exe_dead_attach(exe);
1304 INF("Sending TERM signal to %s (%d).", exe->cmd, exe->pid);
1305 kill(exe->pid, SIGTERM);
1309 * Kills the given spawned process by sending it a @c SIGKILL signal.
1310 * @param exe Process handle to the given process.
1313 ecore_exe_kill(Ecore_Exe *exe)
1315 struct _ecore_exe_dead_exe *dead;
1317 EINA_MAIN_LOOP_CHECK_RETURN;
1318 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1320 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_kill");
1324 dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
1327 dead->pid = exe->pid;
1328 dead->cmd = strdup(exe->cmd);
1329 IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
1330 exe->doomsday_clock =
1331 ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead, dead);
1334 INF("Sending KILL signal to %s (%d).", exe->cmd, exe->pid);
1335 kill(exe->pid, SIGKILL);
1339 * Sends a @c SIGUSR signal to the given spawned process.
1340 * @param exe Process handle to the given process.
1341 * @param num The number user signal to send. Must be either 1 or 2, or
1342 * the signal will be ignored.
1345 ecore_exe_signal(Ecore_Exe *exe,
1348 EINA_MAIN_LOOP_CHECK_RETURN;
1349 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1351 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_signal");
1355 kill(exe->pid, SIGUSR1);
1357 kill(exe->pid, SIGUSR2);
1361 * Sends a @c SIGHUP signal to the given spawned process.
1362 * @param exe Process handle to the given process.
1365 ecore_exe_hup(Ecore_Exe *exe)
1367 EINA_MAIN_LOOP_CHECK_RETURN;
1368 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1370 ECORE_MAGIC_FAIL(exe, ECORE_MAGIC_EXE, "ecore_exe_hup");
1373 kill(exe->pid, SIGHUP);
1381 _ecore_exe_is_it_alive(pid_t pid)
1383 Ecore_Exe *exe = NULL;
1385 /* FIXME: There is no nice, safe, OS independent way to tell if a
1386 * particular PID is still alive. I have written code to do so
1387 * for my urunlevel busybox applet (http://urunlevel.sourceforge.net/),
1388 * but it's for linux only, and still not guaranteed.
1390 * So for now, we just check that a valid Ecore_Exe structure
1391 * exists for it. Even that is not a guarantee, as the structure
1392 * can be freed without killing the process.
1394 * I think we can safely put exe's into two categories, those users
1395 * that care about the life of the exe, and the run and forget type.
1396 * The run and forget type starts up the exe, then free's the
1397 * Ecore_Exe structure straight away. They can never call any of
1398 * the functions that can call this, so we don't worry about them.
1400 * Those user's that care about the life of exe's will keep the
1401 * Ecore_Exe structure around, terminate them eventually, or
1402 * register for exit events. For these ones the assumption
1403 * that valid Ecore_Exe struct == live exe is almost valid.
1405 * I will probably copy my urunlevel code into here someday.
1407 exe = _ecore_exe_find(pid);
1410 if (!ECORE_MAGIC_CHECK(exe, ECORE_MAGIC_EXE))
1418 _ecore_exe_make_sure_its_dead(void *data)
1420 struct _ecore_exe_dead_exe *dead;
1425 Ecore_Exe *exe = NULL;
1427 if ((exe = _ecore_exe_is_it_alive(dead->pid)))
1430 INF("Sending KILL signal to allegedly dead %s (%d).",
1431 dead->cmd, dead->pid);
1433 INF("Sending KILL signal to allegedly dead PID %d.",
1435 exe->doomsday_clock =
1436 ecore_timer_add(10.0, _ecore_exe_make_sure_its_really_dead,
1438 kill(dead->pid, SIGKILL);
1446 return ECORE_CALLBACK_CANCEL;
1450 _ecore_exe_make_sure_its_really_dead(void *data)
1452 struct _ecore_exe_dead_exe *dead;
1457 Ecore_Exe *exe = NULL;
1459 if ((exe = _ecore_exe_is_it_alive(dead->pid)))
1461 ERR("RUN! The zombie wants to eat your brains! And your CPU!");
1463 INF("%s (%d) is not really dead.", dead->cmd, dead->pid);
1465 INF("PID %d is not really dead.", dead->pid);
1466 exe->doomsday_clock = NULL;
1471 return ECORE_CALLBACK_CANCEL;
1475 _ecore_exe_init(void)
1477 ECORE_EXE_EVENT_ADD = ecore_event_type_new();
1478 ECORE_EXE_EVENT_DEL = ecore_event_type_new();
1479 ECORE_EXE_EVENT_DATA = ecore_event_type_new();
1480 ECORE_EXE_EVENT_ERROR = ecore_event_type_new();
1484 _ecore_exe_shutdown(void)
1487 ecore_exe_free(exes);
1491 _ecore_exe_find(pid_t pid)
1495 EINA_INLIST_FOREACH(exes, exe)
1497 if (exe->pid == pid)
1504 _ecore_exe_doomsday_clock_get(Ecore_Exe *exe)
1506 return exe->doomsday_clock;
1510 _ecore_exe_doomsday_clock_set(Ecore_Exe *exe,
1513 exe->doomsday_clock = dc;
1517 _ecore_exe_exec_it(const char *exe_cmd,
1518 Ecore_Exe_Flags flags)
1525 /* So what is this doing?
1527 * We are trying to avoid wrapping the exe call with /bin/sh -c.
1528 * We conservatively search for certain shell meta characters,
1529 * If we don't find them, we can call the exe directly.
1531 if (!strpbrk(exe_cmd, "|&;<>()$`\\\"'*?#"))
1534 char pre_command = 1;
1537 if (!(buf = strdup(exe_cmd)))
1540 token = strtok(buf, " \t\n\v");
1543 if (token[0] == '~')
1547 if (token[0] == '[')
1549 if (strchr(token, '='))
1555 token = strtok(NULL, " \t\n\v");
1558 if ((!token) && (num_tokens))
1562 if (!(buf = strdup(exe_cmd)))
1565 token = strtok(buf, " \t\n\v");
1567 if (!(args = (char **)calloc(num_tokens + 1, sizeof(char *))))
1572 for (i = 0; i < num_tokens; i++)
1576 token = strtok(NULL, " \t\n\v");
1578 args[num_tokens] = NULL;
1582 #ifdef HAVE_SYS_PRCTL_H
1583 if ((flags & ECORE_EXE_TERM_WITH_PARENT))
1585 prctl(PR_SET_PDEATHSIG, SIGTERM);
1589 if (!(flags & ECORE_EXE_NOT_LEADER)) setsid();
1590 if ((flags & ECORE_EXE_USE_SH))
1593 execl("/bin/sh", "/bin/sh", "-c", exe_cmd, (char *)NULL);
1595 else if (use_sh) /* We have to use a shell to run this. */
1597 if (!shell) /* Find users preferred shell. */
1599 shell = getenv("SHELL");
1604 execl(shell, shell, "-c", exe_cmd, (char *)NULL);
1607 { /* We can run this directly. */
1612 ERR("arg[0] is NULL!");
1616 execvp(args[0], args);
1627 _ecore_exe_data_generic_handler(void *data,
1628 Ecore_Fd_Handler *fd_handler,
1629 Ecore_Exe_Flags flags)
1637 /* Sort out what sort of handler we are. */
1638 if (flags & ECORE_EXE_PIPE_READ)
1640 flags = ECORE_EXE_PIPE_READ;
1641 event_type = ECORE_EXE_EVENT_DATA;
1642 child_fd = exe->child_fd_read;
1646 flags = ECORE_EXE_PIPE_ERROR;
1647 event_type = ECORE_EXE_EVENT_ERROR;
1648 child_fd = exe->child_fd_error;
1652 && (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)))
1654 unsigned char *inbuf;
1657 /* Get any left over data from last time. */
1658 if (flags & ECORE_EXE_PIPE_READ)
1660 inbuf = exe->read_data_buf;
1661 inbuf_num = exe->read_data_size;
1662 exe->read_data_buf = NULL;
1663 exe->read_data_size = 0;
1667 inbuf = exe->error_data_buf;
1668 inbuf_num = exe->error_data_size;
1669 exe->error_data_buf = NULL;
1670 exe->error_data_size = 0;
1676 char buf[READBUFSIZ];
1680 if ((num = read(child_fd, buf, READBUFSIZ)) < 1)
1682 /* FIXME: SPEED/SIZE TRADE OFF - add a smaller READBUFSIZE
1683 * (currently 64k) to inbuf, use that instead of buf, and
1684 * save ourselves a memcpy(). */
1685 lost_exe = ((errno == EIO) ||
1688 (errno == EINVAL) || (errno == ENOSPC));
1689 if ((errno != EAGAIN) && (errno != EINTR))
1690 perror("_ecore_exe_generic_handler() read problem ");
1692 if (num > 0) /* data got read. */
1694 inbuf = realloc(inbuf, inbuf_num + num);
1695 memcpy(inbuf + inbuf_num, buf, num);
1699 { /* No more data to read. */
1702 Ecore_Exe_Event_Data *e;
1704 /* Stash the data away for later. */
1705 if (flags & ECORE_EXE_PIPE_READ)
1707 exe->read_data_buf = inbuf;
1708 exe->read_data_size = inbuf_num;
1712 exe->error_data_buf = inbuf;
1713 exe->error_data_size = inbuf_num;
1716 if (!(exe->flags & ECORE_EXE_PIPE_AUTO))
1718 e = ecore_exe_event_data_get(exe, flags);
1719 if (e) /* Send the event. */
1720 ecore_event_add(event_type, e,
1721 _ecore_exe_event_exe_data_free,
1727 if (flags & ECORE_EXE_PIPE_READ)
1729 if (exe->read_data_size)
1730 INF("There are %d bytes left unsent from the dead exe %s.",
1731 exe->read_data_size, exe->cmd);
1735 if (exe->error_data_size)
1736 INF("There are %d bytes left unsent from the dead exe %s.",
1737 exe->error_data_size, exe->cmd);
1739 /* Thought about this a bit. If the exe has actually
1740 * died, this won't do any harm as it must have died
1741 * recently and the pid has not had a chance to recycle.
1742 * It is also a paranoid catchall, coz the usual ecore_signal
1743 * mechenism should kick in. But let's give it a good
1744 * kick in the head anyway.
1746 ecore_exe_terminate(exe);
1753 return ECORE_CALLBACK_RENEW;
1757 _ecore_exe_data_error_handler(void *data,
1758 Ecore_Fd_Handler *fd_handler)
1760 return _ecore_exe_data_generic_handler(data, fd_handler,
1761 ECORE_EXE_PIPE_ERROR);
1765 _ecore_exe_data_read_handler(void *data,
1766 Ecore_Fd_Handler *fd_handler)
1768 return _ecore_exe_data_generic_handler(data, fd_handler,
1769 ECORE_EXE_PIPE_READ);
1773 _ecore_exe_data_write_handler(void *data,
1774 Ecore_Fd_Handler *fd_handler __UNUSED__)
1779 if ((exe->write_fd_handler) &&
1780 (ecore_main_fd_handler_active_get
1781 (exe->write_fd_handler, ECORE_FD_WRITE)))
1782 _ecore_exe_flush(exe);
1784 /* If we have sent all there is to send, and we need to close the pipe, then close it. */
1785 if ((exe->close_stdin == 1)
1786 && (exe->write_data_size == exe->write_data_offset))
1791 INF("Closing stdin for %s", exe->cmd);
1792 /* 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. */
1793 IF_FN_DEL(ecore_main_fd_handler_del, exe->write_fd_handler);
1794 if (exe->child_fd_write != -1)
1795 E_NO_ERRNO(result, close(exe->child_fd_write), ok);
1796 exe->child_fd_write = -1;
1797 IF_FREE(exe->write_data_buf);
1800 return ECORE_CALLBACK_RENEW;
1804 _ecore_exe_flush(Ecore_Exe *exe)
1808 /* check whether we need to write anything at all. */
1809 if ((exe->child_fd_write == -1) || (!exe->write_data_buf))
1811 if (exe->write_data_size == exe->write_data_offset)
1814 count = write(exe->child_fd_write,
1815 (char *)exe->write_data_buf + exe->write_data_offset,
1816 exe->write_data_size - exe->write_data_offset);
1819 if (errno == EIO || errno == EBADF || errno == EPIPE || errno == EINVAL || errno == ENOSPC) /* we lost our exe! */
1821 ecore_exe_terminate(exe);
1822 if (exe->write_fd_handler)
1823 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
1828 exe->write_data_offset += count;
1829 if (exe->write_data_offset >= exe->write_data_size) /* Nothing left to write, clean up. */
1831 exe->write_data_size = 0;
1832 exe->write_data_offset = 0;
1833 IF_FREE(exe->write_data_buf);
1834 if (exe->write_fd_handler)
1835 ecore_main_fd_handler_active_set(exe->write_fd_handler, 0);
1841 _ecore_exe_event_exe_data_free(void *data __UNUSED__,
1844 Ecore_Exe_Event_Data *e;
1847 ecore_exe_event_data_free(e);
1850 static Ecore_Exe_Event_Add *
1851 _ecore_exe_event_add_new(void)
1853 Ecore_Exe_Event_Add *e;
1855 e = calloc(1, sizeof(Ecore_Exe_Event_Add));
1860 _ecore_exe_event_add_free(void *data __UNUSED__,
1863 Ecore_Exe_Event_Add *e;
1870 _ecore_exe_event_del_new(void)
1872 Ecore_Exe_Event_Del *e;
1874 e = calloc(1, sizeof(Ecore_Exe_Event_Del));
1879 _ecore_exe_event_del_free(void *data __UNUSED__,
1882 Ecore_Exe_Event_Del *e;
1886 ecore_exe_free(e->exe);
1891 _ecore_exe_dead_attach(Ecore_Exe *exe)
1893 struct _ecore_exe_dead_exe *dead;
1895 if (exe->doomsday_clock_dead) return;
1896 dead = calloc(1, sizeof(struct _ecore_exe_dead_exe));
1899 dead->pid = exe->pid;
1900 dead->cmd = strdup(exe->cmd);
1901 IF_FN_DEL(ecore_timer_del, exe->doomsday_clock);
1902 exe->doomsday_clock =
1903 ecore_timer_add(10.0, _ecore_exe_make_sure_its_dead, dead);
1904 exe->doomsday_clock_dead = dead;