D-Bus  1.6.18
dbus-spawn.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-spawn.c Wrapper around fork/exec
3  *
4  * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 
27 #include "dbus-spawn.h"
28 #include "dbus-sysdeps-unix.h"
29 #include "dbus-internals.h"
30 #include "dbus-test.h"
31 #include "dbus-protocol.h"
32 
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <sys/wait.h>
37 #include <stdlib.h>
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 
42 extern char **environ;
43 
49 /*
50  * I'm pretty sure this whole spawn file could be made simpler,
51  * if you thought about it a bit.
52  */
53 
57 typedef enum
58 {
62 } ReadStatus;
63 
64 static ReadStatus
65 read_ints (int fd,
66  int *buf,
67  int n_ints_in_buf,
68  int *n_ints_read,
69  DBusError *error)
70 {
71  size_t bytes = 0;
72  ReadStatus retval;
73 
74  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
75 
76  retval = READ_STATUS_OK;
77 
78  while (TRUE)
79  {
80  ssize_t chunk;
81  size_t to_read;
82 
83  to_read = sizeof (int) * n_ints_in_buf - bytes;
84 
85  if (to_read == 0)
86  break;
87 
88  again:
89 
90  chunk = read (fd,
91  ((char*)buf) + bytes,
92  to_read);
93 
94  if (chunk < 0 && errno == EINTR)
95  goto again;
96 
97  if (chunk < 0)
98  {
99  dbus_set_error (error,
101  "Failed to read from child pipe (%s)",
102  _dbus_strerror (errno));
103 
104  retval = READ_STATUS_ERROR;
105  break;
106  }
107  else if (chunk == 0)
108  {
109  retval = READ_STATUS_EOF;
110  break; /* EOF */
111  }
112  else /* chunk > 0 */
113  bytes += chunk;
114  }
115 
116  *n_ints_read = (int)(bytes / sizeof(int));
117 
118  return retval;
119 }
120 
121 static ReadStatus
122 read_pid (int fd,
123  pid_t *buf,
124  DBusError *error)
125 {
126  size_t bytes = 0;
127  ReadStatus retval;
128 
129  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
130 
131  retval = READ_STATUS_OK;
132 
133  while (TRUE)
134  {
135  ssize_t chunk;
136  size_t to_read;
137 
138  to_read = sizeof (pid_t) - bytes;
139 
140  if (to_read == 0)
141  break;
142 
143  again:
144 
145  chunk = read (fd,
146  ((char*)buf) + bytes,
147  to_read);
148  if (chunk < 0 && errno == EINTR)
149  goto again;
150 
151  if (chunk < 0)
152  {
153  dbus_set_error (error,
155  "Failed to read from child pipe (%s)",
156  _dbus_strerror (errno));
157 
158  retval = READ_STATUS_ERROR;
159  break;
160  }
161  else if (chunk == 0)
162  {
163  retval = READ_STATUS_EOF;
164  break; /* EOF */
165  }
166  else /* chunk > 0 */
167  bytes += chunk;
168  }
169 
170  return retval;
171 }
172 
173 /* The implementation uses an intermediate child between the main process
174  * and the grandchild. The grandchild is our spawned process. The intermediate
175  * child is a babysitter process; it keeps track of when the grandchild
176  * exits/crashes, and reaps the grandchild.
177  */
178 
179 /* Messages from children to parents */
180 enum
181 {
182  CHILD_EXITED, /* This message is followed by the exit status int */
183  CHILD_FORK_FAILED, /* Followed by errno */
184  CHILD_EXEC_FAILED, /* Followed by errno */
185  CHILD_PID /* Followed by pid_t */
186 };
187 
191 struct DBusBabysitter
192 {
193  int refcount;
195  char *executable;
200  pid_t sitter_pid;
208  DBusBabysitterFinishedFunc finished_cb;
209  void *finished_data;
210 
211  int errnum;
212  int status;
213  unsigned int have_child_status : 1;
214  unsigned int have_fork_errnum : 1;
215  unsigned int have_exec_errnum : 1;
216 };
217 
218 static DBusBabysitter*
219 _dbus_babysitter_new (void)
220 {
221  DBusBabysitter *sitter;
222 
223  sitter = dbus_new0 (DBusBabysitter, 1);
224  if (sitter == NULL)
225  return NULL;
226 
227  sitter->refcount = 1;
228 
229  sitter->socket_to_babysitter = -1;
230  sitter->error_pipe_from_child = -1;
231 
232  sitter->sitter_pid = -1;
233  sitter->grandchild_pid = -1;
234 
235  sitter->watches = _dbus_watch_list_new ();
236  if (sitter->watches == NULL)
237  goto failed;
238 
239  return sitter;
240 
241  failed:
242  _dbus_babysitter_unref (sitter);
243  return NULL;
244 }
245 
254 {
255  _dbus_assert (sitter != NULL);
256  _dbus_assert (sitter->refcount > 0);
257 
258  sitter->refcount += 1;
259 
260  return sitter;
261 }
262 
263 static void close_socket_to_babysitter (DBusBabysitter *sitter);
264 static void close_error_pipe_from_child (DBusBabysitter *sitter);
265 
274 void
276 {
277  _dbus_assert (sitter != NULL);
278  _dbus_assert (sitter->refcount > 0);
279 
280  sitter->refcount -= 1;
281  if (sitter->refcount == 0)
282  {
283  /* If we haven't forked other babysitters
284  * since this babysitter and socket were
285  * created then this close will cause the
286  * babysitter to wake up from poll with
287  * a hangup and then the babysitter will
288  * quit itself.
289  */
290  close_socket_to_babysitter (sitter);
291 
292  close_error_pipe_from_child (sitter);
293 
294  if (sitter->sitter_pid > 0)
295  {
296  int status;
297  int ret;
298 
299  /* It's possible the babysitter died on its own above
300  * from the close, or was killed randomly
301  * by some other process, so first try to reap it
302  */
303  ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
304 
305  /* If we couldn't reap the child then kill it, and
306  * try again
307  */
308  if (ret == 0)
309  kill (sitter->sitter_pid, SIGKILL);
310 
311  if (ret == 0)
312  {
313  do
314  {
315  ret = waitpid (sitter->sitter_pid, &status, 0);
316  }
317  while (_DBUS_UNLIKELY (ret < 0 && errno == EINTR));
318  }
319 
320  if (ret < 0)
321  {
322  if (errno == ECHILD)
323  _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
324  else
325  _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
326  errno, _dbus_strerror (errno));
327  }
328  else
329  {
330  _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
331  (long) ret, (long) sitter->sitter_pid);
332 
333  if (WIFEXITED (sitter->status))
334  _dbus_verbose ("Babysitter exited with status %d\n",
335  WEXITSTATUS (sitter->status));
336  else if (WIFSIGNALED (sitter->status))
337  _dbus_verbose ("Babysitter received signal %d\n",
338  WTERMSIG (sitter->status));
339  else
340  _dbus_verbose ("Babysitter exited abnormally\n");
341  }
342 
343  sitter->sitter_pid = -1;
344  }
345 
346  if (sitter->watches)
347  _dbus_watch_list_free (sitter->watches);
348 
349  dbus_free (sitter->executable);
350 
351  dbus_free (sitter);
352  }
353 }
354 
355 static ReadStatus
356 read_data (DBusBabysitter *sitter,
357  int fd)
358 {
359  int what;
360  int got;
361  DBusError error = DBUS_ERROR_INIT;
362  ReadStatus r;
363 
364  r = read_ints (fd, &what, 1, &got, &error);
365 
366  switch (r)
367  {
368  case READ_STATUS_ERROR:
369  _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
370  dbus_error_free (&error);
371  return r;
372 
373  case READ_STATUS_EOF:
374  return r;
375 
376  case READ_STATUS_OK:
377  break;
378  }
379 
380  if (got == 1)
381  {
382  switch (what)
383  {
384  case CHILD_EXITED:
385  case CHILD_FORK_FAILED:
386  case CHILD_EXEC_FAILED:
387  {
388  int arg;
389 
390  r = read_ints (fd, &arg, 1, &got, &error);
391 
392  switch (r)
393  {
394  case READ_STATUS_ERROR:
395  _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
396  dbus_error_free (&error);
397  return r;
398  case READ_STATUS_EOF:
399  return r;
400  case READ_STATUS_OK:
401  break;
402  }
403 
404  if (got == 1)
405  {
406  if (what == CHILD_EXITED)
407  {
408  sitter->have_child_status = TRUE;
409  sitter->status = arg;
410  sitter->errnum = 0;
411  _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
412  WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
413  WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
414  }
415  else if (what == CHILD_FORK_FAILED)
416  {
417  sitter->have_fork_errnum = TRUE;
418  sitter->errnum = arg;
419  _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
420  }
421  else if (what == CHILD_EXEC_FAILED)
422  {
423  sitter->have_exec_errnum = TRUE;
424  sitter->errnum = arg;
425  _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
426  }
427  }
428  }
429  break;
430  case CHILD_PID:
431  {
432  pid_t pid = -1;
433 
434  r = read_pid (fd, &pid, &error);
435 
436  switch (r)
437  {
438  case READ_STATUS_ERROR:
439  _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
440  dbus_error_free (&error);
441  return r;
442  case READ_STATUS_EOF:
443  return r;
444  case READ_STATUS_OK:
445  break;
446  }
447 
448  sitter->grandchild_pid = pid;
449 
450  _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
451  }
452  break;
453  default:
454  _dbus_warn ("Unknown message received from babysitter process\n");
455  break;
456  }
457  }
458 
459  return r;
460 }
461 
462 static void
463 close_socket_to_babysitter (DBusBabysitter *sitter)
464 {
465  _dbus_verbose ("Closing babysitter\n");
466 
467  if (sitter->sitter_watch != NULL)
468  {
469  _dbus_assert (sitter->watches != NULL);
473  sitter->sitter_watch = NULL;
474  }
475 
476  if (sitter->socket_to_babysitter >= 0)
477  {
479  sitter->socket_to_babysitter = -1;
480  }
481 }
482 
483 static void
484 close_error_pipe_from_child (DBusBabysitter *sitter)
485 {
486  _dbus_verbose ("Closing child error\n");
487 
488  if (sitter->error_watch != NULL)
489  {
490  _dbus_assert (sitter->watches != NULL);
493  _dbus_watch_unref (sitter->error_watch);
494  sitter->error_watch = NULL;
495  }
496 
497  if (sitter->error_pipe_from_child >= 0)
498  {
500  sitter->error_pipe_from_child = -1;
501  }
502 }
503 
504 static void
505 handle_babysitter_socket (DBusBabysitter *sitter,
506  int revents)
507 {
508  /* Even if we have POLLHUP, we want to keep reading
509  * data until POLLIN goes away; so this function only
510  * looks at HUP/ERR if no IN is set.
511  */
512  if (revents & _DBUS_POLLIN)
513  {
514  _dbus_verbose ("Reading data from babysitter\n");
515  if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
516  close_socket_to_babysitter (sitter);
517  }
518  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
519  {
520  close_socket_to_babysitter (sitter);
521  }
522 }
523 
524 static void
525 handle_error_pipe (DBusBabysitter *sitter,
526  int revents)
527 {
528  if (revents & _DBUS_POLLIN)
529  {
530  _dbus_verbose ("Reading data from child error\n");
531  if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
532  close_error_pipe_from_child (sitter);
533  }
534  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
535  {
536  close_error_pipe_from_child (sitter);
537  }
538 }
539 
540 /* returns whether there were any poll events handled */
541 static dbus_bool_t
542 babysitter_iteration (DBusBabysitter *sitter,
543  dbus_bool_t block)
544 {
545  DBusPollFD fds[2];
546  int i;
547  dbus_bool_t descriptors_ready;
548 
549  descriptors_ready = FALSE;
550 
551  i = 0;
552 
553  if (sitter->error_pipe_from_child >= 0)
554  {
555  fds[i].fd = sitter->error_pipe_from_child;
556  fds[i].events = _DBUS_POLLIN;
557  fds[i].revents = 0;
558  ++i;
559  }
560 
561  if (sitter->socket_to_babysitter >= 0)
562  {
563  fds[i].fd = sitter->socket_to_babysitter;
564  fds[i].events = _DBUS_POLLIN;
565  fds[i].revents = 0;
566  ++i;
567  }
568 
569  if (i > 0)
570  {
571  int ret;
572 
573  do
574  {
575  ret = _dbus_poll (fds, i, 0);
576  }
577  while (ret < 0 && errno == EINTR);
578 
579  if (ret == 0 && block)
580  {
581  do
582  {
583  ret = _dbus_poll (fds, i, -1);
584  }
585  while (ret < 0 && errno == EINTR);
586  }
587 
588  if (ret > 0)
589  {
590  descriptors_ready = TRUE;
591 
592  while (i > 0)
593  {
594  --i;
595  if (fds[i].fd == sitter->error_pipe_from_child)
596  handle_error_pipe (sitter, fds[i].revents);
597  else if (fds[i].fd == sitter->socket_to_babysitter)
598  handle_babysitter_socket (sitter, fds[i].revents);
599  }
600  }
601  }
602 
603  return descriptors_ready;
604 }
605 
610 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
611 
618 void
620 {
621  /* be sure we have the PID of the child */
622  while (LIVE_CHILDREN (sitter) &&
623  sitter->grandchild_pid == -1)
624  babysitter_iteration (sitter, TRUE);
625 
626  _dbus_verbose ("Got child PID %ld for killing\n",
627  (long) sitter->grandchild_pid);
628 
629  if (sitter->grandchild_pid == -1)
630  return; /* child is already dead, or we're so hosed we'll never recover */
631 
632  kill (sitter->grandchild_pid, SIGKILL);
633 }
634 
642 {
643 
644  /* Be sure we're up-to-date */
645  while (LIVE_CHILDREN (sitter) &&
646  babysitter_iteration (sitter, FALSE))
647  ;
648 
649  /* We will have exited the babysitter when the child has exited */
650  return sitter->socket_to_babysitter < 0;
651 }
652 
667  int *status)
668 {
669  if (!_dbus_babysitter_get_child_exited (sitter))
670  _dbus_assert_not_reached ("Child has not exited");
671 
672  if (!sitter->have_child_status ||
673  !(WIFEXITED (sitter->status)))
674  return FALSE;
675 
676  *status = WEXITSTATUS (sitter->status);
677  return TRUE;
678 }
679 
689 void
691  DBusError *error)
692 {
693  if (!_dbus_babysitter_get_child_exited (sitter))
694  return;
695 
696  /* Note that if exec fails, we will also get a child status
697  * from the babysitter saying the child exited,
698  * so we need to give priority to the exec error
699  */
700  if (sitter->have_exec_errnum)
701  {
703  "Failed to execute program %s: %s",
704  sitter->executable, _dbus_strerror (sitter->errnum));
705  }
706  else if (sitter->have_fork_errnum)
707  {
709  "Failed to fork a new process %s: %s",
710  sitter->executable, _dbus_strerror (sitter->errnum));
711  }
712  else if (sitter->have_child_status)
713  {
714  if (WIFEXITED (sitter->status))
716  "Process %s exited with status %d",
717  sitter->executable, WEXITSTATUS (sitter->status));
718  else if (WIFSIGNALED (sitter->status))
720  "Process %s received signal %d",
721  sitter->executable, WTERMSIG (sitter->status));
722  else
724  "Process %s exited abnormally",
725  sitter->executable);
726  }
727  else
728  {
730  "Process %s exited, reason unknown",
731  sitter->executable);
732  }
733 }
734 
749  DBusAddWatchFunction add_function,
750  DBusRemoveWatchFunction remove_function,
751  DBusWatchToggledFunction toggled_function,
752  void *data,
753  DBusFreeFunction free_data_function)
754 {
755  return _dbus_watch_list_set_functions (sitter->watches,
756  add_function,
757  remove_function,
758  toggled_function,
759  data,
760  free_data_function);
761 }
762 
763 static dbus_bool_t
764 handle_watch (DBusWatch *watch,
765  unsigned int condition,
766  void *data)
767 {
768  DBusBabysitter *sitter = _dbus_babysitter_ref (data);
769  int revents;
770  int fd;
771 
772  revents = 0;
773  if (condition & DBUS_WATCH_READABLE)
774  revents |= _DBUS_POLLIN;
775  if (condition & DBUS_WATCH_ERROR)
776  revents |= _DBUS_POLLERR;
777  if (condition & DBUS_WATCH_HANGUP)
778  revents |= _DBUS_POLLHUP;
779 
780  fd = dbus_watch_get_socket (watch);
781 
782  if (fd == sitter->error_pipe_from_child)
783  handle_error_pipe (sitter, revents);
784  else if (fd == sitter->socket_to_babysitter)
785  handle_babysitter_socket (sitter, revents);
786 
787  while (LIVE_CHILDREN (sitter) &&
788  babysitter_iteration (sitter, FALSE))
789  ;
790 
791  /* fd.o #32992: if the handle_* methods closed their sockets, they previously
792  * didn't always remove the watches. Check that we don't regress. */
793  _dbus_assert (sitter->socket_to_babysitter != -1 || sitter->sitter_watch == NULL);
794  _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
795 
796  if (_dbus_babysitter_get_child_exited (sitter) &&
797  sitter->finished_cb != NULL)
798  {
799  sitter->finished_cb (sitter, sitter->finished_data);
800  sitter->finished_cb = NULL;
801  }
802 
803  _dbus_babysitter_unref (sitter);
804  return TRUE;
805 }
806 
808 #define READ_END 0
809 
810 #define WRITE_END 1
811 
812 
813 /* Avoids a danger in threaded situations (calling close()
814  * on a file descriptor twice, and another thread has
815  * re-opened it since the first close)
816  */
817 static int
818 close_and_invalidate (int *fd)
819 {
820  int ret;
821 
822  if (*fd < 0)
823  return -1;
824  else
825  {
826  ret = _dbus_close_socket (*fd, NULL);
827  *fd = -1;
828  }
829 
830  return ret;
831 }
832 
833 static dbus_bool_t
834 make_pipe (int p[2],
835  DBusError *error)
836 {
837  int retval;
838 
839 #ifdef HAVE_PIPE2
840  dbus_bool_t cloexec_done;
841 
842  retval = pipe2 (p, O_CLOEXEC);
843  cloexec_done = retval >= 0;
844 
845  /* Check if kernel seems to be too old to know pipe2(). We assume
846  that if pipe2 is available, O_CLOEXEC is too. */
847  if (retval < 0 && errno == ENOSYS)
848 #endif
849  {
850  retval = pipe(p);
851  }
852 
853  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
854 
855  if (retval < 0)
856  {
857  dbus_set_error (error,
859  "Failed to create pipe for communicating with child process (%s)",
860  _dbus_strerror (errno));
861  return FALSE;
862  }
863 
864 #ifdef HAVE_PIPE2
865  if (!cloexec_done)
866 #endif
867  {
870  }
871 
872  return TRUE;
873 }
874 
875 static void
876 do_write (int fd, const void *buf, size_t count)
877 {
878  size_t bytes_written;
879  int ret;
880 
881  bytes_written = 0;
882 
883  again:
884 
885  ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
886 
887  if (ret < 0)
888  {
889  if (errno == EINTR)
890  goto again;
891  else
892  {
893  _dbus_warn ("Failed to write data to pipe!\n");
894  exit (1); /* give up, we suck */
895  }
896  }
897  else
898  bytes_written += ret;
899 
900  if (bytes_written < count)
901  goto again;
902 }
903 
904 static void
905 write_err_and_exit (int fd, int msg)
906 {
907  int en = errno;
908 
909  do_write (fd, &msg, sizeof (msg));
910  do_write (fd, &en, sizeof (en));
911 
912  exit (1);
913 }
914 
915 static void
916 write_pid (int fd, pid_t pid)
917 {
918  int msg = CHILD_PID;
919 
920  do_write (fd, &msg, sizeof (msg));
921  do_write (fd, &pid, sizeof (pid));
922 }
923 
924 static void
925 write_status_and_exit (int fd, int status)
926 {
927  int msg = CHILD_EXITED;
928 
929  do_write (fd, &msg, sizeof (msg));
930  do_write (fd, &status, sizeof (status));
931 
932  exit (0);
933 }
934 
935 static void
936 do_exec (int child_err_report_fd,
937  char **argv,
938  char **envp,
939  DBusSpawnChildSetupFunc child_setup,
940  void *user_data)
941 {
942 #ifdef DBUS_BUILD_TESTS
943  int i, max_open;
944 #endif
945 
946  _dbus_verbose_reset ();
947  _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
948  _dbus_getpid ());
949 
950  if (child_setup)
951  (* child_setup) (user_data);
952 
953 #ifdef DBUS_BUILD_TESTS
954  max_open = sysconf (_SC_OPEN_MAX);
955 
956  for (i = 3; i < max_open; i++)
957  {
958  int retval;
959 
960  if (i == child_err_report_fd)
961  continue;
962 
963  retval = fcntl (i, F_GETFD);
964 
965  if (retval != -1 && !(retval & FD_CLOEXEC))
966  _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
967  }
968 #endif
969 
970  if (envp == NULL)
971  {
972  _dbus_assert (environ != NULL);
973 
974  envp = environ;
975  }
976 
977  execve (argv[0], argv, envp);
978 
979  /* Exec failed */
980  write_err_and_exit (child_err_report_fd,
981  CHILD_EXEC_FAILED);
982 }
983 
984 static void
985 check_babysit_events (pid_t grandchild_pid,
986  int parent_pipe,
987  int revents)
988 {
989  pid_t ret;
990  int status;
991 
992  do
993  {
994  ret = waitpid (grandchild_pid, &status, WNOHANG);
995  /* The man page says EINTR can't happen with WNOHANG,
996  * but there are reports of it (maybe only with valgrind?)
997  */
998  }
999  while (ret < 0 && errno == EINTR);
1000 
1001  if (ret == 0)
1002  {
1003  _dbus_verbose ("no child exited\n");
1004 
1005  ; /* no child exited */
1006  }
1007  else if (ret < 0)
1008  {
1009  /* This isn't supposed to happen. */
1010  _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
1011  _dbus_strerror (errno));
1012  exit (1);
1013  }
1014  else if (ret == grandchild_pid)
1015  {
1016  /* Child exited */
1017  _dbus_verbose ("reaped child pid %ld\n", (long) ret);
1018 
1019  write_status_and_exit (parent_pipe, status);
1020  }
1021  else
1022  {
1023  _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
1024  (int) ret);
1025  exit (1);
1026  }
1027 
1028  if (revents & _DBUS_POLLIN)
1029  {
1030  _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
1031  }
1032 
1033  if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
1034  {
1035  /* Parent is gone, so we just exit */
1036  _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
1037  exit (0);
1038  }
1039 }
1040 
1041 static int babysit_sigchld_pipe = -1;
1042 
1043 static void
1044 babysit_signal_handler (int signo)
1045 {
1046  char b = '\0';
1047  again:
1048  if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1049  if (errno == EINTR)
1050  goto again;
1051 }
1052 
1053 static void
1054 babysit (pid_t grandchild_pid,
1055  int parent_pipe)
1056 {
1057  int sigchld_pipe[2];
1058 
1059  /* We don't exec, so we keep parent state, such as the pid that
1060  * _dbus_verbose() uses. Reset the pid here.
1061  */
1062  _dbus_verbose_reset ();
1063 
1064  /* I thought SIGCHLD would just wake up the poll, but
1065  * that didn't seem to work, so added this pipe.
1066  * Probably the pipe is more likely to work on busted
1067  * operating systems anyhow.
1068  */
1069  if (pipe (sigchld_pipe) < 0)
1070  {
1071  _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
1072  exit (1);
1073  }
1074 
1075  babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1076 
1077  _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1078 
1079  write_pid (parent_pipe, grandchild_pid);
1080 
1081  check_babysit_events (grandchild_pid, parent_pipe, 0);
1082 
1083  while (TRUE)
1084  {
1085  DBusPollFD pfds[2];
1086 
1087  pfds[0].fd = parent_pipe;
1088  pfds[0].events = _DBUS_POLLIN;
1089  pfds[0].revents = 0;
1090 
1091  pfds[1].fd = sigchld_pipe[READ_END];
1092  pfds[1].events = _DBUS_POLLIN;
1093  pfds[1].revents = 0;
1094 
1095  if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1096  {
1097  _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
1098  exit (1);
1099  }
1100 
1101  if (pfds[0].revents != 0)
1102  {
1103  check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1104  }
1105  else if (pfds[1].revents & _DBUS_POLLIN)
1106  {
1107  char b;
1108  if (read (sigchld_pipe[READ_END], &b, 1) == -1)
1109  {
1110  /* ignore */
1111  }
1112  /* do waitpid check */
1113  check_babysit_events (grandchild_pid, parent_pipe, 0);
1114  }
1115  }
1116 
1117  exit (1);
1118 }
1119 
1141  char **argv,
1142  char **env,
1143  DBusSpawnChildSetupFunc child_setup,
1144  void *user_data,
1145  DBusError *error)
1146 {
1147  DBusBabysitter *sitter;
1148  int child_err_report_pipe[2] = { -1, -1 };
1149  int babysitter_pipe[2] = { -1, -1 };
1150  pid_t pid;
1151 
1152  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1153 
1154  if (sitter_p != NULL)
1155  *sitter_p = NULL;
1156 
1157  sitter = NULL;
1158 
1159  sitter = _dbus_babysitter_new ();
1160  if (sitter == NULL)
1161  {
1163  return FALSE;
1164  }
1165 
1166  sitter->executable = _dbus_strdup (argv[0]);
1167  if (sitter->executable == NULL)
1168  {
1170  goto cleanup_and_fail;
1171  }
1172 
1173  if (!make_pipe (child_err_report_pipe, error))
1174  goto cleanup_and_fail;
1175 
1176  if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1177  goto cleanup_and_fail;
1178 
1179  /* Setting up the babysitter is only useful in the parent,
1180  * but we don't want to run out of memory and fail
1181  * after we've already forked, since then we'd leak
1182  * child processes everywhere.
1183  */
1184  sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1185  DBUS_WATCH_READABLE,
1186  TRUE, handle_watch, sitter, NULL);
1187  if (sitter->error_watch == NULL)
1188  {
1190  goto cleanup_and_fail;
1191  }
1192 
1193  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1194  {
1195  /* we need to free it early so the destructor won't try to remove it
1196  * without it having been added, which DBusLoop doesn't allow */
1198  _dbus_watch_unref (sitter->error_watch);
1199  sitter->error_watch = NULL;
1200 
1202  goto cleanup_and_fail;
1203  }
1204 
1205  sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1206  DBUS_WATCH_READABLE,
1207  TRUE, handle_watch, sitter, NULL);
1208  if (sitter->sitter_watch == NULL)
1209  {
1211  goto cleanup_and_fail;
1212  }
1213 
1214  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1215  {
1216  /* we need to free it early so the destructor won't try to remove it
1217  * without it having been added, which DBusLoop doesn't allow */
1219  _dbus_watch_unref (sitter->sitter_watch);
1220  sitter->sitter_watch = NULL;
1221 
1223  goto cleanup_and_fail;
1224  }
1225 
1226  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1227 
1228  pid = fork ();
1229 
1230  if (pid < 0)
1231  {
1232  dbus_set_error (error,
1234  "Failed to fork (%s)",
1235  _dbus_strerror (errno));
1236  goto cleanup_and_fail;
1237  }
1238  else if (pid == 0)
1239  {
1240  /* Immediate child, this is the babysitter process. */
1241  int grandchild_pid;
1242 
1243  /* Be sure we crash if the parent exits
1244  * and we write to the err_report_pipe
1245  */
1246  signal (SIGPIPE, SIG_DFL);
1247 
1248  /* Close the parent's end of the pipes. */
1249  close_and_invalidate (&child_err_report_pipe[READ_END]);
1250  close_and_invalidate (&babysitter_pipe[0]);
1251 
1252  /* Create the child that will exec () */
1253  grandchild_pid = fork ();
1254 
1255  if (grandchild_pid < 0)
1256  {
1257  write_err_and_exit (babysitter_pipe[1],
1258  CHILD_FORK_FAILED);
1259  _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1260  }
1261  else if (grandchild_pid == 0)
1262  {
1263  /* Go back to ignoring SIGPIPE, since it's evil
1264  */
1265  signal (SIGPIPE, SIG_IGN);
1266 
1267  do_exec (child_err_report_pipe[WRITE_END],
1268  argv,
1269  env,
1270  child_setup, user_data);
1271  _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1272  }
1273  else
1274  {
1275  babysit (grandchild_pid, babysitter_pipe[1]);
1276  _dbus_assert_not_reached ("Got to code after babysit()");
1277  }
1278  }
1279  else
1280  {
1281  /* Close the uncared-about ends of the pipes */
1282  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1283  close_and_invalidate (&babysitter_pipe[1]);
1284 
1285  sitter->socket_to_babysitter = babysitter_pipe[0];
1286  babysitter_pipe[0] = -1;
1287 
1288  sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1289  child_err_report_pipe[READ_END] = -1;
1290 
1291  sitter->sitter_pid = pid;
1292 
1293  if (sitter_p != NULL)
1294  *sitter_p = sitter;
1295  else
1296  _dbus_babysitter_unref (sitter);
1297 
1298  dbus_free_string_array (env);
1299 
1300  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1301 
1302  return TRUE;
1303  }
1304 
1305  cleanup_and_fail:
1306 
1307  _DBUS_ASSERT_ERROR_IS_SET (error);
1308 
1309  close_and_invalidate (&child_err_report_pipe[READ_END]);
1310  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1311  close_and_invalidate (&babysitter_pipe[0]);
1312  close_and_invalidate (&babysitter_pipe[1]);
1313 
1314  if (sitter != NULL)
1315  _dbus_babysitter_unref (sitter);
1316 
1317  return FALSE;
1318 }
1319 
1320 void
1321 _dbus_babysitter_set_result_function (DBusBabysitter *sitter,
1322  DBusBabysitterFinishedFunc finished,
1323  void *user_data)
1324 {
1325  sitter->finished_cb = finished;
1326  sitter->finished_data = user_data;
1327 }
1328 
1331 #ifdef DBUS_BUILD_TESTS
1332 
1333 static char *
1334 get_test_exec (const char *exe,
1335  DBusString *scratch_space)
1336 {
1337  const char *dbus_test_exec;
1338 
1339  dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
1340 
1341  if (dbus_test_exec == NULL)
1342  dbus_test_exec = DBUS_TEST_EXEC;
1343 
1344  if (!_dbus_string_init (scratch_space))
1345  return NULL;
1346 
1347  if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
1348  dbus_test_exec, exe, DBUS_EXEEXT))
1349  {
1350  _dbus_string_free (scratch_space);
1351  return NULL;
1352  }
1353 
1354  return _dbus_string_get_data (scratch_space);
1355 }
1356 
1357 static void
1358 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1359 {
1360  while (LIVE_CHILDREN (sitter))
1361  babysitter_iteration (sitter, TRUE);
1362 }
1363 
1364 static dbus_bool_t
1365 check_spawn_nonexistent (void *data)
1366 {
1367  char *argv[4] = { NULL, NULL, NULL, NULL };
1368  DBusBabysitter *sitter = NULL;
1369  DBusError error = DBUS_ERROR_INIT;
1370 
1371  /*** Test launching nonexistent binary */
1372 
1373  argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1374  if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1375  NULL, NULL, NULL,
1376  &error))
1377  {
1378  _dbus_babysitter_block_for_child_exit (sitter);
1379  _dbus_babysitter_set_child_exit_error (sitter, &error);
1380  }
1381 
1382  if (sitter)
1383  _dbus_babysitter_unref (sitter);
1384 
1385  if (!dbus_error_is_set (&error))
1386  {
1387  _dbus_warn ("Did not get an error launching nonexistent executable\n");
1388  return FALSE;
1389  }
1390 
1391  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1393  {
1394  _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1395  error.name, error.message);
1396  dbus_error_free (&error);
1397  return FALSE;
1398  }
1399 
1400  dbus_error_free (&error);
1401 
1402  return TRUE;
1403 }
1404 
1405 static dbus_bool_t
1406 check_spawn_segfault (void *data)
1407 {
1408  char *argv[4] = { NULL, NULL, NULL, NULL };
1409  DBusBabysitter *sitter = NULL;
1410  DBusError error = DBUS_ERROR_INIT;
1411  DBusString argv0;
1412 
1413  /*** Test launching segfault binary */
1414 
1415  argv[0] = get_test_exec ("test-segfault", &argv0);
1416 
1417  if (argv[0] == NULL)
1418  {
1419  /* OOM was simulated, never mind */
1420  return TRUE;
1421  }
1422 
1423  if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1424  NULL, NULL, NULL,
1425  &error))
1426  {
1427  _dbus_babysitter_block_for_child_exit (sitter);
1428  _dbus_babysitter_set_child_exit_error (sitter, &error);
1429  }
1430 
1431  _dbus_string_free (&argv0);
1432 
1433  if (sitter)
1434  _dbus_babysitter_unref (sitter);
1435 
1436  if (!dbus_error_is_set (&error))
1437  {
1438  _dbus_warn ("Did not get an error launching segfaulting binary\n");
1439  return FALSE;
1440  }
1441 
1442  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1444  {
1445  _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1446  error.name, error.message);
1447  dbus_error_free (&error);
1448  return FALSE;
1449  }
1450 
1451  dbus_error_free (&error);
1452 
1453  return TRUE;
1454 }
1455 
1456 static dbus_bool_t
1457 check_spawn_exit (void *data)
1458 {
1459  char *argv[4] = { NULL, NULL, NULL, NULL };
1460  DBusBabysitter *sitter = NULL;
1461  DBusError error = DBUS_ERROR_INIT;
1462  DBusString argv0;
1463 
1464  /*** Test launching exit failure binary */
1465 
1466  argv[0] = get_test_exec ("test-exit", &argv0);
1467 
1468  if (argv[0] == NULL)
1469  {
1470  /* OOM was simulated, never mind */
1471  return TRUE;
1472  }
1473 
1474  if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1475  NULL, NULL, NULL,
1476  &error))
1477  {
1478  _dbus_babysitter_block_for_child_exit (sitter);
1479  _dbus_babysitter_set_child_exit_error (sitter, &error);
1480  }
1481 
1482  _dbus_string_free (&argv0);
1483 
1484  if (sitter)
1485  _dbus_babysitter_unref (sitter);
1486 
1487  if (!dbus_error_is_set (&error))
1488  {
1489  _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1490  return FALSE;
1491  }
1492 
1493  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1495  {
1496  _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1497  error.name, error.message);
1498  dbus_error_free (&error);
1499  return FALSE;
1500  }
1501 
1502  dbus_error_free (&error);
1503 
1504  return TRUE;
1505 }
1506 
1507 static dbus_bool_t
1508 check_spawn_and_kill (void *data)
1509 {
1510  char *argv[4] = { NULL, NULL, NULL, NULL };
1511  DBusBabysitter *sitter = NULL;
1512  DBusError error = DBUS_ERROR_INIT;
1513  DBusString argv0;
1514 
1515  /*** Test launching sleeping binary then killing it */
1516 
1517  argv[0] = get_test_exec ("test-sleep-forever", &argv0);
1518 
1519  if (argv[0] == NULL)
1520  {
1521  /* OOM was simulated, never mind */
1522  return TRUE;
1523  }
1524 
1525  if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1526  NULL, NULL, NULL,
1527  &error))
1528  {
1529  _dbus_babysitter_kill_child (sitter);
1530 
1531  _dbus_babysitter_block_for_child_exit (sitter);
1532 
1533  _dbus_babysitter_set_child_exit_error (sitter, &error);
1534  }
1535 
1536  _dbus_string_free (&argv0);
1537 
1538  if (sitter)
1539  _dbus_babysitter_unref (sitter);
1540 
1541  if (!dbus_error_is_set (&error))
1542  {
1543  _dbus_warn ("Did not get an error after killing spawned binary\n");
1544  return FALSE;
1545  }
1546 
1547  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1549  {
1550  _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1551  error.name, error.message);
1552  dbus_error_free (&error);
1553  return FALSE;
1554  }
1555 
1556  dbus_error_free (&error);
1557 
1558  return TRUE;
1559 }
1560 
1562 _dbus_spawn_test (const char *test_data_dir)
1563 {
1564  if (!_dbus_test_oom_handling ("spawn_nonexistent",
1565  check_spawn_nonexistent,
1566  NULL))
1567  return FALSE;
1568 
1569  if (!_dbus_test_oom_handling ("spawn_segfault",
1570  check_spawn_segfault,
1571  NULL))
1572  return FALSE;
1573 
1574  if (!_dbus_test_oom_handling ("spawn_exit",
1575  check_spawn_exit,
1576  NULL))
1577  return FALSE;
1578 
1579  if (!_dbus_test_oom_handling ("spawn_and_kill",
1580  check_spawn_and_kill,
1581  NULL))
1582  return FALSE;
1583 
1584  return TRUE;
1585 }
1586 #endif
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
const char * message
public error message field
Definition: dbus-errors.h:51
#define DBUS_ERROR_SPAWN_FAILED
While starting a new process, something went wrong.
Implementation of DBusWatch.
Definition: dbus-watch.c:40
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_ERROR_SPAWN_EXEC_FAILED
While starting a new process, the exec() call failed.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
#define LIVE_CHILDREN(sitter)
Macro returns TRUE if the babysitter still has live sockets open to the babysitter child or the grand...
Definition: dbus-spawn.c:610
#define _DBUS_POLLHUP
Hung up.
Definition: dbus-sysdeps.h:291
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
unsigned int have_exec_errnum
True if we have an error code from exec()
Definition: dbus-spawn.c:215
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
DBusWatch * error_watch
Error pipe watch.
Definition: dbus-spawn.c:205
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
Definition: dbus-watch.c:169
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
Definition: dbus-sysdeps.h:112
#define DBUS_ERROR_SPAWN_CHILD_EXITED
While starting a new process, the child exited with a status code.
int status
Exit status code.
Definition: dbus-spawn.c:212
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int socket_to_babysitter
Connection to the babysitter process.
dbus_bool_t _dbus_full_duplex_pipe(int *fd1, int *fd2, dbus_bool_t blocking, DBusError *error)
Creates a full-duplex pipe (as in socketpair()).
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
dbus_bool_t _dbus_watch_list_add_watch(DBusWatchList *watch_list, DBusWatch *watch)
Adds a new watch to the watch list, invoking the application DBusAddWatchFunction if appropriate...
Definition: dbus-watch.c:375
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
A portable struct pollfd wrapper.
Definition: dbus-sysdeps.h:299
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:283
Read succeeded.
Definition: dbus-spawn.c:59
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
char * executable
executable name to use in error messages
short events
Events to poll for.
Definition: dbus-sysdeps.h:302
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
Definition: dbus-spawn.c:641
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:183
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, char **argv, char **env, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
Definition: dbus-spawn.c:1140
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
DBusWatchList * watches
Watches.
void _dbus_fd_set_close_on_exec(intptr_t fd)
Sets the file descriptor to be close on exec.
#define DBUS_ERROR_SPAWN_CHILD_SIGNALED
While starting a new process, the child exited on a signal.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusWatch * sitter_watch
Sitter pipe watch.
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
Definition: dbus-watch.c:564
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
Definition: dbus-string.c:437
dbus_bool_t _dbus_babysitter_get_child_exit_status(DBusBabysitter *sitter, int *status)
Gets the exit status of the child.
Definition: dbus-spawn.c:666
void _dbus_babysitter_kill_child(DBusBabysitter *sitter)
Blocks until the babysitter process gives us the PID of the spawned grandchild, then kills the spawne...
Definition: dbus-spawn.c:619
Babysitter implementation details.
ReadStatus
Enumeration for status of a read()
Definition: dbus-spawn.c:57
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
pid_t sitter_pid
PID Of the babysitter.
Definition: dbus-spawn.c:200
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1119
EOF returned.
Definition: dbus-spawn.c:61
dbus_bool_t _dbus_babysitter_set_watch_functions(DBusBabysitter *sitter, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets watch functions to notify us when the babysitter object needs to read/write file descriptors...
Definition: dbus-spawn.c:748
Object representing an exception.
Definition: dbus-errors.h:48
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
pid_t grandchild_pid
PID of the grandchild.
Definition: dbus-spawn.c:201
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
int refcount
Reference count.
int fd
File descriptor.
Definition: dbus-sysdeps.h:301
As in POLLERR (can&#39;t watch for this, but can be present in current state passed to dbus_watch_handle(...
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to &quot;1&quot;.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
As in POLLHUP (can&#39;t watch for it, but can be present in current state passed to dbus_watch_handle())...
#define DBUS_ERROR_FAILED
A generic error; &quot;something went wrong&quot; - see the error message for more.
const char * name
public error name field
Definition: dbus-errors.h:50
#define READ_END
Helps remember which end of the pipe is which.
Definition: dbus-spawn.c:808
DBusWatchList implementation details.
Definition: dbus-watch.c:214
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
Definition: dbus-spawn.c:275
dbus_bool_t _dbus_watch_list_set_functions(DBusWatchList *watch_list, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions.
Definition: dbus-watch.c:277
#define DBUS_ERROR_SPAWN_FORK_FAILED
While starting a new process, the fork() call failed.
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
DBusWatch * _dbus_watch_new(int fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:748
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
unsigned int have_fork_errnum
True if we have an error code from fork()
Definition: dbus-spawn.c:214
#define FALSE
Expands to &quot;0&quot;.
int error_pipe_from_child
Connection to the process that does the exec()
Definition: dbus-spawn.c:198
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
As in POLLIN.
#define WRITE_END
Helps remember which end of the pipe is which.
Definition: dbus-spawn.c:810
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
char * _dbus_strdup(const char *str)
Duplicates a string.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
int errnum
Error number.
Definition: dbus-spawn.c:211
void _dbus_babysitter_set_child_exit_error(DBusBabysitter *sitter, DBusError *error)
Sets the DBusError with an explanation of why the spawned child process exited (on a signal...
Definition: dbus-spawn.c:690
short revents
Events that occurred.
Definition: dbus-sysdeps.h:303
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
Some kind of error.
Definition: dbus-spawn.c:60
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application&#39;s DBusRemoveWatchFunction if appropriat...
Definition: dbus-watch.c:408
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:289
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.
Definition: dbus-spawn.c:253