D-Bus  1.8.16
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 #ifdef HAVE_SYSTEMD
42 #ifdef HAVE_SYSLOG_H
43 #include <syslog.h>
44 #endif
45 #include <systemd/sd-journal.h>
46 #endif
47 
48 extern char **environ;
49 
55 /*
56  * I'm pretty sure this whole spawn file could be made simpler,
57  * if you thought about it a bit.
58  */
59 
63 typedef enum
64 {
68 } ReadStatus;
69 
70 static ReadStatus
71 read_ints (int fd,
72  int *buf,
73  int n_ints_in_buf,
74  int *n_ints_read,
75  DBusError *error)
76 {
77  size_t bytes = 0;
78  ReadStatus retval;
79 
80  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
81 
82  retval = READ_STATUS_OK;
83 
84  while (TRUE)
85  {
86  ssize_t chunk;
87  size_t to_read;
88 
89  to_read = sizeof (int) * n_ints_in_buf - bytes;
90 
91  if (to_read == 0)
92  break;
93 
94  again:
95 
96  chunk = read (fd,
97  ((char*)buf) + bytes,
98  to_read);
99 
100  if (chunk < 0 && errno == EINTR)
101  goto again;
102 
103  if (chunk < 0)
104  {
105  dbus_set_error (error,
107  "Failed to read from child pipe (%s)",
108  _dbus_strerror (errno));
109 
110  retval = READ_STATUS_ERROR;
111  break;
112  }
113  else if (chunk == 0)
114  {
115  retval = READ_STATUS_EOF;
116  break; /* EOF */
117  }
118  else /* chunk > 0 */
119  bytes += chunk;
120  }
121 
122  *n_ints_read = (int)(bytes / sizeof(int));
123 
124  return retval;
125 }
126 
127 static ReadStatus
128 read_pid (int fd,
129  pid_t *buf,
130  DBusError *error)
131 {
132  size_t bytes = 0;
133  ReadStatus retval;
134 
135  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
136 
137  retval = READ_STATUS_OK;
138 
139  while (TRUE)
140  {
141  ssize_t chunk;
142  size_t to_read;
143 
144  to_read = sizeof (pid_t) - bytes;
145 
146  if (to_read == 0)
147  break;
148 
149  again:
150 
151  chunk = read (fd,
152  ((char*)buf) + bytes,
153  to_read);
154  if (chunk < 0 && errno == EINTR)
155  goto again;
156 
157  if (chunk < 0)
158  {
159  dbus_set_error (error,
161  "Failed to read from child pipe (%s)",
162  _dbus_strerror (errno));
163 
164  retval = READ_STATUS_ERROR;
165  break;
166  }
167  else if (chunk == 0)
168  {
169  retval = READ_STATUS_EOF;
170  break; /* EOF */
171  }
172  else /* chunk > 0 */
173  bytes += chunk;
174  }
175 
176  return retval;
177 }
178 
179 /* The implementation uses an intermediate child between the main process
180  * and the grandchild. The grandchild is our spawned process. The intermediate
181  * child is a babysitter process; it keeps track of when the grandchild
182  * exits/crashes, and reaps the grandchild.
183  *
184  * We automatically reap the babysitter process, killing it if necessary,
185  * when the DBusBabysitter's refcount goes to zero.
186  *
187  * Processes:
188  *
189  * main process
190  * | fork() A
191  * \- babysitter
192  * | fork () B
193  * \- grandchild --> exec --> spawned process
194  *
195  * IPC:
196  * child_err_report_pipe
197  * /-----------<---------<--------------\
198  * | ^
199  * v |
200  * main process babysitter grandchild
201  * ^ ^
202  * v v
203  * \-------<->-------/
204  * babysitter_pipe
205  *
206  * child_err_report_pipe is genuinely a pipe.
207  * The READ_END (also called error_pipe_from_child) is used in the main
208  * process. The WRITE_END (also called child_err_report_fd) is used in
209  * the grandchild process.
210  *
211  * On failure, the grandchild process sends CHILD_EXEC_FAILED + errno.
212  * On success, the pipe just closes (because it's close-on-exec) without
213  * sending any bytes.
214  *
215  * babysitter_pipe is mis-named: it's really a bidirectional socketpair.
216  * The [0] end (also called socket_to_babysitter) is used in the main
217  * process, the [1] end (also called parent_pipe) is used in the babysitter.
218  *
219  * If the fork() labelled B in the diagram above fails, the babysitter sends
220  * CHILD_FORK_FAILED + errno.
221  * On success, the babysitter sends CHILD_PID + the grandchild's pid.
222  * On SIGCHLD, the babysitter sends CHILD_EXITED + the exit status.
223  * The main process doesn't explicitly send anything, but when it exits,
224  * the babysitter gets POLLHUP or POLLERR.
225  */
226 
227 /* Messages from children to parents */
228 enum
229 {
230  CHILD_EXITED, /* This message is followed by the exit status int */
231  CHILD_FORK_FAILED, /* Followed by errno */
232  CHILD_EXEC_FAILED, /* Followed by errno */
233  CHILD_PID /* Followed by pid_t */
234 };
235 
239 struct DBusBabysitter
240 {
241  int refcount;
243  char *log_name;
249  pid_t sitter_pid;
257  DBusBabysitterFinishedFunc finished_cb;
258  void *finished_data;
259 
260  int errnum;
261  int status;
262  unsigned int have_child_status : 1;
263  unsigned int have_fork_errnum : 1;
264  unsigned int have_exec_errnum : 1;
265 };
266 
267 static DBusBabysitter*
268 _dbus_babysitter_new (void)
269 {
270  DBusBabysitter *sitter;
271 
272  sitter = dbus_new0 (DBusBabysitter, 1);
273  if (sitter == NULL)
274  return NULL;
275 
276  sitter->refcount = 1;
277 
278  sitter->socket_to_babysitter = -1;
279  sitter->error_pipe_from_child = -1;
280 
281  sitter->sitter_pid = -1;
282  sitter->grandchild_pid = -1;
283 
284  sitter->watches = _dbus_watch_list_new ();
285  if (sitter->watches == NULL)
286  goto failed;
287 
288  return sitter;
289 
290  failed:
291  _dbus_babysitter_unref (sitter);
292  return NULL;
293 }
294 
303 {
304  _dbus_assert (sitter != NULL);
305  _dbus_assert (sitter->refcount > 0);
306 
307  sitter->refcount += 1;
308 
309  return sitter;
310 }
311 
312 static void close_socket_to_babysitter (DBusBabysitter *sitter);
313 static void close_error_pipe_from_child (DBusBabysitter *sitter);
314 
323 void
325 {
326  _dbus_assert (sitter != NULL);
327  _dbus_assert (sitter->refcount > 0);
328 
329  sitter->refcount -= 1;
330  if (sitter->refcount == 0)
331  {
332  /* If we haven't forked other babysitters
333  * since this babysitter and socket were
334  * created then this close will cause the
335  * babysitter to wake up from poll with
336  * a hangup and then the babysitter will
337  * quit itself.
338  */
339  close_socket_to_babysitter (sitter);
340 
341  close_error_pipe_from_child (sitter);
342 
343  if (sitter->sitter_pid > 0)
344  {
345  int status;
346  int ret;
347 
348  /* It's possible the babysitter died on its own above
349  * from the close, or was killed randomly
350  * by some other process, so first try to reap it
351  */
352  ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
353 
354  /* If we couldn't reap the child then kill it, and
355  * try again
356  */
357  if (ret == 0)
358  kill (sitter->sitter_pid, SIGKILL);
359 
360  if (ret == 0)
361  {
362  do
363  {
364  ret = waitpid (sitter->sitter_pid, &status, 0);
365  }
366  while (_DBUS_UNLIKELY (ret < 0 && errno == EINTR));
367  }
368 
369  if (ret < 0)
370  {
371  if (errno == ECHILD)
372  _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
373  else
374  _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
375  errno, _dbus_strerror (errno));
376  }
377  else
378  {
379  _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
380  (long) ret, (long) sitter->sitter_pid);
381 
382  if (WIFEXITED (sitter->status))
383  _dbus_verbose ("Babysitter exited with status %d\n",
384  WEXITSTATUS (sitter->status));
385  else if (WIFSIGNALED (sitter->status))
386  _dbus_verbose ("Babysitter received signal %d\n",
387  WTERMSIG (sitter->status));
388  else
389  _dbus_verbose ("Babysitter exited abnormally\n");
390  }
391 
392  sitter->sitter_pid = -1;
393  }
394 
395  if (sitter->watches)
396  _dbus_watch_list_free (sitter->watches);
397 
398  dbus_free (sitter->log_name);
399 
400  dbus_free (sitter);
401  }
402 }
403 
404 static ReadStatus
405 read_data (DBusBabysitter *sitter,
406  int fd)
407 {
408  int what;
409  int got;
410  DBusError error = DBUS_ERROR_INIT;
411  ReadStatus r;
412 
413  r = read_ints (fd, &what, 1, &got, &error);
414 
415  switch (r)
416  {
417  case READ_STATUS_ERROR:
418  _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
419  dbus_error_free (&error);
420  return r;
421 
422  case READ_STATUS_EOF:
423  return r;
424 
425  case READ_STATUS_OK:
426  break;
427  }
428 
429  if (got == 1)
430  {
431  switch (what)
432  {
433  case CHILD_EXITED:
434  case CHILD_FORK_FAILED:
435  case CHILD_EXEC_FAILED:
436  {
437  int arg;
438 
439  r = read_ints (fd, &arg, 1, &got, &error);
440 
441  switch (r)
442  {
443  case READ_STATUS_ERROR:
444  _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
445  dbus_error_free (&error);
446  return r;
447  case READ_STATUS_EOF:
448  return r;
449  case READ_STATUS_OK:
450  break;
451  }
452 
453  if (got == 1)
454  {
455  if (what == CHILD_EXITED)
456  {
457  sitter->have_child_status = TRUE;
458  sitter->status = arg;
459  sitter->errnum = 0;
460  _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
461  WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
462  WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
463  }
464  else if (what == CHILD_FORK_FAILED)
465  {
466  sitter->have_fork_errnum = TRUE;
467  sitter->errnum = arg;
468  _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
469  }
470  else if (what == CHILD_EXEC_FAILED)
471  {
472  sitter->have_exec_errnum = TRUE;
473  sitter->errnum = arg;
474  _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
475  }
476  }
477  }
478  break;
479  case CHILD_PID:
480  {
481  pid_t pid = -1;
482 
483  r = read_pid (fd, &pid, &error);
484 
485  switch (r)
486  {
487  case READ_STATUS_ERROR:
488  _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
489  dbus_error_free (&error);
490  return r;
491  case READ_STATUS_EOF:
492  return r;
493  case READ_STATUS_OK:
494  break;
495  }
496 
497  sitter->grandchild_pid = pid;
498 
499  _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
500  }
501  break;
502  default:
503  _dbus_warn ("Unknown message received from babysitter process\n");
504  break;
505  }
506  }
507 
508  return r;
509 }
510 
511 static void
512 close_socket_to_babysitter (DBusBabysitter *sitter)
513 {
514  _dbus_verbose ("Closing babysitter\n");
515 
516  if (sitter->sitter_watch != NULL)
517  {
518  _dbus_assert (sitter->watches != NULL);
522  sitter->sitter_watch = NULL;
523  }
524 
525  if (sitter->socket_to_babysitter >= 0)
526  {
528  sitter->socket_to_babysitter = -1;
529  }
530 }
531 
532 static void
533 close_error_pipe_from_child (DBusBabysitter *sitter)
534 {
535  _dbus_verbose ("Closing child error\n");
536 
537  if (sitter->error_watch != NULL)
538  {
539  _dbus_assert (sitter->watches != NULL);
542  _dbus_watch_unref (sitter->error_watch);
543  sitter->error_watch = NULL;
544  }
545 
546  if (sitter->error_pipe_from_child >= 0)
547  {
549  sitter->error_pipe_from_child = -1;
550  }
551 }
552 
553 static void
554 handle_babysitter_socket (DBusBabysitter *sitter,
555  int revents)
556 {
557  /* Even if we have POLLHUP, we want to keep reading
558  * data until POLLIN goes away; so this function only
559  * looks at HUP/ERR if no IN is set.
560  */
561  if (revents & _DBUS_POLLIN)
562  {
563  _dbus_verbose ("Reading data from babysitter\n");
564  if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
565  close_socket_to_babysitter (sitter);
566  }
567  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
568  {
569  close_socket_to_babysitter (sitter);
570  }
571 }
572 
573 static void
574 handle_error_pipe (DBusBabysitter *sitter,
575  int revents)
576 {
577  if (revents & _DBUS_POLLIN)
578  {
579  _dbus_verbose ("Reading data from child error\n");
580  if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
581  close_error_pipe_from_child (sitter);
582  }
583  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
584  {
585  close_error_pipe_from_child (sitter);
586  }
587 }
588 
589 /* returns whether there were any poll events handled */
590 static dbus_bool_t
591 babysitter_iteration (DBusBabysitter *sitter,
592  dbus_bool_t block)
593 {
594  DBusPollFD fds[2];
595  int i;
596  dbus_bool_t descriptors_ready;
597 
598  descriptors_ready = FALSE;
599 
600  i = 0;
601 
602  if (sitter->error_pipe_from_child >= 0)
603  {
604  fds[i].fd = sitter->error_pipe_from_child;
605  fds[i].events = _DBUS_POLLIN;
606  fds[i].revents = 0;
607  ++i;
608  }
609 
610  if (sitter->socket_to_babysitter >= 0)
611  {
612  fds[i].fd = sitter->socket_to_babysitter;
613  fds[i].events = _DBUS_POLLIN;
614  fds[i].revents = 0;
615  ++i;
616  }
617 
618  if (i > 0)
619  {
620  int ret;
621 
622  do
623  {
624  ret = _dbus_poll (fds, i, 0);
625  }
626  while (ret < 0 && errno == EINTR);
627 
628  if (ret == 0 && block)
629  {
630  do
631  {
632  ret = _dbus_poll (fds, i, -1);
633  }
634  while (ret < 0 && errno == EINTR);
635  }
636 
637  if (ret > 0)
638  {
639  descriptors_ready = TRUE;
640 
641  while (i > 0)
642  {
643  --i;
644  if (fds[i].fd == sitter->error_pipe_from_child)
645  handle_error_pipe (sitter, fds[i].revents);
646  else if (fds[i].fd == sitter->socket_to_babysitter)
647  handle_babysitter_socket (sitter, fds[i].revents);
648  }
649  }
650  }
651 
652  return descriptors_ready;
653 }
654 
659 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
660 
667 void
669 {
670  /* be sure we have the PID of the child */
671  while (LIVE_CHILDREN (sitter) &&
672  sitter->grandchild_pid == -1)
673  babysitter_iteration (sitter, TRUE);
674 
675  _dbus_verbose ("Got child PID %ld for killing\n",
676  (long) sitter->grandchild_pid);
677 
678  if (sitter->grandchild_pid == -1)
679  return; /* child is already dead, or we're so hosed we'll never recover */
680 
681  kill (sitter->grandchild_pid, SIGKILL);
682 }
683 
691 {
692 
693  /* Be sure we're up-to-date */
694  while (LIVE_CHILDREN (sitter) &&
695  babysitter_iteration (sitter, FALSE))
696  ;
697 
698  /* We will have exited the babysitter when the child has exited */
699  return sitter->socket_to_babysitter < 0;
700 }
701 
716  int *status)
717 {
718  if (!_dbus_babysitter_get_child_exited (sitter))
719  _dbus_assert_not_reached ("Child has not exited");
720 
721  if (!sitter->have_child_status ||
722  !(WIFEXITED (sitter->status)))
723  return FALSE;
724 
725  *status = WEXITSTATUS (sitter->status);
726  return TRUE;
727 }
728 
738 void
740  DBusError *error)
741 {
742  if (!_dbus_babysitter_get_child_exited (sitter))
743  return;
744 
745  /* Note that if exec fails, we will also get a child status
746  * from the babysitter saying the child exited,
747  * so we need to give priority to the exec error
748  */
749  if (sitter->have_exec_errnum)
750  {
752  "Failed to execute program %s: %s",
753  sitter->log_name, _dbus_strerror (sitter->errnum));
754  }
755  else if (sitter->have_fork_errnum)
756  {
758  "Failed to fork a new process %s: %s",
759  sitter->log_name, _dbus_strerror (sitter->errnum));
760  }
761  else if (sitter->have_child_status)
762  {
763  if (WIFEXITED (sitter->status))
765  "Process %s exited with status %d",
766  sitter->log_name, WEXITSTATUS (sitter->status));
767  else if (WIFSIGNALED (sitter->status))
769  "Process %s received signal %d",
770  sitter->log_name, WTERMSIG (sitter->status));
771  else
773  "Process %s exited abnormally",
774  sitter->log_name);
775  }
776  else
777  {
779  "Process %s exited, reason unknown",
780  sitter->log_name);
781  }
782 }
783 
798  DBusAddWatchFunction add_function,
799  DBusRemoveWatchFunction remove_function,
800  DBusWatchToggledFunction toggled_function,
801  void *data,
802  DBusFreeFunction free_data_function)
803 {
804  return _dbus_watch_list_set_functions (sitter->watches,
805  add_function,
806  remove_function,
807  toggled_function,
808  data,
809  free_data_function);
810 }
811 
812 static dbus_bool_t
813 handle_watch (DBusWatch *watch,
814  unsigned int condition,
815  void *data)
816 {
817  DBusBabysitter *sitter = _dbus_babysitter_ref (data);
818  int revents;
819  int fd;
820 
821  revents = 0;
822  if (condition & DBUS_WATCH_READABLE)
823  revents |= _DBUS_POLLIN;
824  if (condition & DBUS_WATCH_ERROR)
825  revents |= _DBUS_POLLERR;
826  if (condition & DBUS_WATCH_HANGUP)
827  revents |= _DBUS_POLLHUP;
828 
829  fd = dbus_watch_get_socket (watch);
830 
831  if (fd == sitter->error_pipe_from_child)
832  handle_error_pipe (sitter, revents);
833  else if (fd == sitter->socket_to_babysitter)
834  handle_babysitter_socket (sitter, revents);
835 
836  while (LIVE_CHILDREN (sitter) &&
837  babysitter_iteration (sitter, FALSE))
838  ;
839 
840  /* fd.o #32992: if the handle_* methods closed their sockets, they previously
841  * didn't always remove the watches. Check that we don't regress. */
842  _dbus_assert (sitter->socket_to_babysitter != -1 || sitter->sitter_watch == NULL);
843  _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
844 
845  if (_dbus_babysitter_get_child_exited (sitter) &&
846  sitter->finished_cb != NULL)
847  {
848  sitter->finished_cb (sitter, sitter->finished_data);
849  sitter->finished_cb = NULL;
850  }
851 
852  _dbus_babysitter_unref (sitter);
853  return TRUE;
854 }
855 
857 #define READ_END 0
858 
859 #define WRITE_END 1
860 
861 
862 /* Avoids a danger in re-entrant situations (calling close()
863  * on a file descriptor twice, and another module has
864  * re-opened it since the first close).
865  *
866  * This previously claimed to be relevant for threaded situations, but by
867  * trivial inspection, it is not thread-safe. It doesn't actually
868  * matter, since this module is only used in the -util variant of the
869  * library, which is only used in single-threaded situations.
870  */
871 static int
872 close_and_invalidate (int *fd)
873 {
874  int ret;
875 
876  if (*fd < 0)
877  return -1;
878  else
879  {
880  ret = _dbus_close_socket (*fd, NULL);
881  *fd = -1;
882  }
883 
884  return ret;
885 }
886 
887 static dbus_bool_t
888 make_pipe (int p[2],
889  DBusError *error)
890 {
891  int retval;
892 
893 #ifdef HAVE_PIPE2
894  dbus_bool_t cloexec_done;
895 
896  retval = pipe2 (p, O_CLOEXEC);
897  cloexec_done = retval >= 0;
898 
899  /* Check if kernel seems to be too old to know pipe2(). We assume
900  that if pipe2 is available, O_CLOEXEC is too. */
901  if (retval < 0 && errno == ENOSYS)
902 #endif
903  {
904  retval = pipe(p);
905  }
906 
907  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
908 
909  if (retval < 0)
910  {
911  dbus_set_error (error,
913  "Failed to create pipe for communicating with child process (%s)",
914  _dbus_strerror (errno));
915  return FALSE;
916  }
917 
918 #ifdef HAVE_PIPE2
919  if (!cloexec_done)
920 #endif
921  {
924  }
925 
926  return TRUE;
927 }
928 
929 static void
930 do_write (int fd, const void *buf, size_t count)
931 {
932  size_t bytes_written;
933  int ret;
934 
935  bytes_written = 0;
936 
937  again:
938 
939  ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
940 
941  if (ret < 0)
942  {
943  if (errno == EINTR)
944  goto again;
945  else
946  {
947  _dbus_warn ("Failed to write data to pipe!\n");
948  exit (1); /* give up, we suck */
949  }
950  }
951  else
952  bytes_written += ret;
953 
954  if (bytes_written < count)
955  goto again;
956 }
957 
958 static void
959 write_err_and_exit (int fd, int msg)
960 {
961  int en = errno;
962 
963  do_write (fd, &msg, sizeof (msg));
964  do_write (fd, &en, sizeof (en));
965 
966  exit (1);
967 }
968 
969 static void
970 write_pid (int fd, pid_t pid)
971 {
972  int msg = CHILD_PID;
973 
974  do_write (fd, &msg, sizeof (msg));
975  do_write (fd, &pid, sizeof (pid));
976 }
977 
978 static void
979 write_status_and_exit (int fd, int status)
980 {
981  int msg = CHILD_EXITED;
982 
983  do_write (fd, &msg, sizeof (msg));
984  do_write (fd, &status, sizeof (status));
985 
986  exit (0);
987 }
988 
989 static void
990 do_exec (int child_err_report_fd,
991  char **argv,
992  char **envp,
993  DBusSpawnChildSetupFunc child_setup,
994  void *user_data)
995 {
996 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
997  int i, max_open;
998 #endif
999 
1000  _dbus_verbose_reset ();
1001  _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
1002  _dbus_getpid ());
1003 
1004  if (child_setup)
1005  (* child_setup) (user_data);
1006 
1007 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1008  max_open = sysconf (_SC_OPEN_MAX);
1009 
1010  for (i = 3; i < max_open; i++)
1011  {
1012  int retval;
1013 
1014  if (i == child_err_report_fd)
1015  continue;
1016 
1017  retval = fcntl (i, F_GETFD);
1018 
1019  if (retval != -1 && !(retval & FD_CLOEXEC))
1020  _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
1021  }
1022 #endif
1023 
1024  if (envp == NULL)
1025  {
1026  _dbus_assert (environ != NULL);
1027 
1028  envp = environ;
1029  }
1030 
1031  execve (argv[0], argv, envp);
1032 
1033  /* Exec failed */
1034  write_err_and_exit (child_err_report_fd,
1035  CHILD_EXEC_FAILED);
1036 }
1037 
1038 static void
1039 check_babysit_events (pid_t grandchild_pid,
1040  int parent_pipe,
1041  int revents)
1042 {
1043  pid_t ret;
1044  int status;
1045 
1046  do
1047  {
1048  ret = waitpid (grandchild_pid, &status, WNOHANG);
1049  /* The man page says EINTR can't happen with WNOHANG,
1050  * but there are reports of it (maybe only with valgrind?)
1051  */
1052  }
1053  while (ret < 0 && errno == EINTR);
1054 
1055  if (ret == 0)
1056  {
1057  _dbus_verbose ("no child exited\n");
1058 
1059  ; /* no child exited */
1060  }
1061  else if (ret < 0)
1062  {
1063  /* This isn't supposed to happen. */
1064  _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
1065  _dbus_strerror (errno));
1066  exit (1);
1067  }
1068  else if (ret == grandchild_pid)
1069  {
1070  /* Child exited */
1071  _dbus_verbose ("reaped child pid %ld\n", (long) ret);
1072 
1073  write_status_and_exit (parent_pipe, status);
1074  }
1075  else
1076  {
1077  _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
1078  (int) ret);
1079  exit (1);
1080  }
1081 
1082  if (revents & _DBUS_POLLIN)
1083  {
1084  _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
1085  }
1086 
1087  if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
1088  {
1089  /* Parent is gone, so we just exit */
1090  _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
1091  exit (0);
1092  }
1093 }
1094 
1095 static int babysit_sigchld_pipe = -1;
1096 
1097 static void
1098 babysit_signal_handler (int signo)
1099 {
1100  char b = '\0';
1101  again:
1102  if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1103  if (errno == EINTR)
1104  goto again;
1105 }
1106 
1107 static void
1108 babysit (pid_t grandchild_pid,
1109  int parent_pipe)
1110 {
1111  int sigchld_pipe[2];
1112 
1113  /* We don't exec, so we keep parent state, such as the pid that
1114  * _dbus_verbose() uses. Reset the pid here.
1115  */
1116  _dbus_verbose_reset ();
1117 
1118  /* I thought SIGCHLD would just wake up the poll, but
1119  * that didn't seem to work, so added this pipe.
1120  * Probably the pipe is more likely to work on busted
1121  * operating systems anyhow.
1122  */
1123  if (pipe (sigchld_pipe) < 0)
1124  {
1125  _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
1126  exit (1);
1127  }
1128 
1129  babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1130 
1131  _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1132 
1133  write_pid (parent_pipe, grandchild_pid);
1134 
1135  check_babysit_events (grandchild_pid, parent_pipe, 0);
1136 
1137  while (TRUE)
1138  {
1139  DBusPollFD pfds[2];
1140 
1141  pfds[0].fd = parent_pipe;
1142  pfds[0].events = _DBUS_POLLIN;
1143  pfds[0].revents = 0;
1144 
1145  pfds[1].fd = sigchld_pipe[READ_END];
1146  pfds[1].events = _DBUS_POLLIN;
1147  pfds[1].revents = 0;
1148 
1149  if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1150  {
1151  _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
1152  exit (1);
1153  }
1154 
1155  if (pfds[0].revents != 0)
1156  {
1157  check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1158  }
1159  else if (pfds[1].revents & _DBUS_POLLIN)
1160  {
1161  char b;
1162  if (read (sigchld_pipe[READ_END], &b, 1) == -1)
1163  {
1164  /* ignore */
1165  }
1166  /* do waitpid check */
1167  check_babysit_events (grandchild_pid, parent_pipe, 0);
1168  }
1169  }
1170 
1171  exit (1);
1172 }
1173 
1195  const char *log_name,
1196  char **argv,
1197  char **env,
1198  DBusSpawnChildSetupFunc child_setup,
1199  void *user_data,
1200  DBusError *error)
1201 {
1202  DBusBabysitter *sitter;
1203  int child_err_report_pipe[2] = { -1, -1 };
1204  int babysitter_pipe[2] = { -1, -1 };
1205  pid_t pid;
1206 #ifdef HAVE_SYSTEMD
1207  int fd_out = -1;
1208  int fd_err = -1;
1209 #endif
1210 
1211  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1212  _dbus_assert (argv[0] != NULL);
1213 
1214  if (sitter_p != NULL)
1215  *sitter_p = NULL;
1216 
1217  sitter = NULL;
1218 
1219  sitter = _dbus_babysitter_new ();
1220  if (sitter == NULL)
1221  {
1223  return FALSE;
1224  }
1225 
1226  sitter->log_name = _dbus_strdup (log_name);
1227  if (sitter->log_name == NULL && log_name != NULL)
1228  {
1230  goto cleanup_and_fail;
1231  }
1232 
1233  if (sitter->log_name == NULL)
1234  sitter->log_name = _dbus_strdup (argv[0]);
1235 
1236  if (sitter->log_name == NULL)
1237  {
1239  goto cleanup_and_fail;
1240  }
1241 
1242  if (!make_pipe (child_err_report_pipe, error))
1243  goto cleanup_and_fail;
1244 
1245  if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1246  goto cleanup_and_fail;
1247 
1248  /* Setting up the babysitter is only useful in the parent,
1249  * but we don't want to run out of memory and fail
1250  * after we've already forked, since then we'd leak
1251  * child processes everywhere.
1252  */
1253  sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1254  DBUS_WATCH_READABLE,
1255  TRUE, handle_watch, sitter, NULL);
1256  if (sitter->error_watch == NULL)
1257  {
1259  goto cleanup_and_fail;
1260  }
1261 
1262  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1263  {
1264  /* we need to free it early so the destructor won't try to remove it
1265  * without it having been added, which DBusLoop doesn't allow */
1267  _dbus_watch_unref (sitter->error_watch);
1268  sitter->error_watch = NULL;
1269 
1271  goto cleanup_and_fail;
1272  }
1273 
1274  sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1275  DBUS_WATCH_READABLE,
1276  TRUE, handle_watch, sitter, NULL);
1277  if (sitter->sitter_watch == NULL)
1278  {
1280  goto cleanup_and_fail;
1281  }
1282 
1283  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1284  {
1285  /* we need to free it early so the destructor won't try to remove it
1286  * without it having been added, which DBusLoop doesn't allow */
1288  _dbus_watch_unref (sitter->sitter_watch);
1289  sitter->sitter_watch = NULL;
1290 
1292  goto cleanup_and_fail;
1293  }
1294 
1295  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1296 
1297 #ifdef HAVE_SYSTEMD
1298  /* This may fail, but it's not critical.
1299  * In particular, if we were compiled with journald support but are now
1300  * running on a non-systemd system, this is going to fail, so we
1301  * have to cope gracefully. */
1302  fd_out = sd_journal_stream_fd (sitter->log_name, LOG_INFO, FALSE);
1303  fd_err = sd_journal_stream_fd (sitter->log_name, LOG_WARNING, FALSE);
1304 #endif
1305 
1306  pid = fork ();
1307 
1308  if (pid < 0)
1309  {
1310  dbus_set_error (error,
1312  "Failed to fork (%s)",
1313  _dbus_strerror (errno));
1314  goto cleanup_and_fail;
1315  }
1316  else if (pid == 0)
1317  {
1318  /* Immediate child, this is the babysitter process. */
1319  int grandchild_pid;
1320 
1321  /* Be sure we crash if the parent exits
1322  * and we write to the err_report_pipe
1323  */
1324  signal (SIGPIPE, SIG_DFL);
1325 
1326  /* Close the parent's end of the pipes. */
1327  close_and_invalidate (&child_err_report_pipe[READ_END]);
1328  close_and_invalidate (&babysitter_pipe[0]);
1329 
1330  /* Create the child that will exec () */
1331  grandchild_pid = fork ();
1332 
1333  if (grandchild_pid < 0)
1334  {
1335  write_err_and_exit (babysitter_pipe[1],
1336  CHILD_FORK_FAILED);
1337  _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1338  }
1339  else if (grandchild_pid == 0)
1340  {
1341  /* Go back to ignoring SIGPIPE, since it's evil
1342  */
1343  signal (SIGPIPE, SIG_IGN);
1344 
1345  close_and_invalidate (&babysitter_pipe[1]);
1346 #ifdef HAVE_SYSTEMD
1347  /* log to systemd journal if possible */
1348  if (fd_out >= 0)
1349  dup2 (fd_out, STDOUT_FILENO);
1350  if (fd_err >= 0)
1351  dup2 (fd_err, STDERR_FILENO);
1352  close_and_invalidate (&fd_out);
1353  close_and_invalidate (&fd_err);
1354 #endif
1355  do_exec (child_err_report_pipe[WRITE_END],
1356  argv,
1357  env,
1358  child_setup, user_data);
1359  _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1360  }
1361  else
1362  {
1363  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1364 #ifdef HAVE_SYSTEMD
1365  close_and_invalidate (&fd_out);
1366  close_and_invalidate (&fd_err);
1367 #endif
1368  babysit (grandchild_pid, babysitter_pipe[1]);
1369  _dbus_assert_not_reached ("Got to code after babysit()");
1370  }
1371  }
1372  else
1373  {
1374  /* Close the uncared-about ends of the pipes */
1375  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1376  close_and_invalidate (&babysitter_pipe[1]);
1377 #ifdef HAVE_SYSTEMD
1378  close_and_invalidate (&fd_out);
1379  close_and_invalidate (&fd_err);
1380 #endif
1381 
1382  sitter->socket_to_babysitter = babysitter_pipe[0];
1383  babysitter_pipe[0] = -1;
1384 
1385  sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1386  child_err_report_pipe[READ_END] = -1;
1387 
1388  sitter->sitter_pid = pid;
1389 
1390  if (sitter_p != NULL)
1391  *sitter_p = sitter;
1392  else
1393  _dbus_babysitter_unref (sitter);
1394 
1395  dbus_free_string_array (env);
1396 
1397  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1398 
1399  return TRUE;
1400  }
1401 
1402  cleanup_and_fail:
1403 
1404  _DBUS_ASSERT_ERROR_IS_SET (error);
1405 
1406  close_and_invalidate (&child_err_report_pipe[READ_END]);
1407  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1408  close_and_invalidate (&babysitter_pipe[0]);
1409  close_and_invalidate (&babysitter_pipe[1]);
1410 #ifdef HAVE_SYSTEMD
1411  close_and_invalidate (&fd_out);
1412  close_and_invalidate (&fd_err);
1413 #endif
1414 
1415  if (sitter != NULL)
1416  _dbus_babysitter_unref (sitter);
1417 
1418  return FALSE;
1419 }
1420 
1421 void
1422 _dbus_babysitter_set_result_function (DBusBabysitter *sitter,
1423  DBusBabysitterFinishedFunc finished,
1424  void *user_data)
1425 {
1426  sitter->finished_cb = finished;
1427  sitter->finished_data = user_data;
1428 }
1429 
1432 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1433 
1434 static char *
1435 get_test_exec (const char *exe,
1436  DBusString *scratch_space)
1437 {
1438  const char *dbus_test_exec;
1439 
1440  dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
1441 
1442  if (dbus_test_exec == NULL)
1443  dbus_test_exec = DBUS_TEST_EXEC;
1444 
1445  if (!_dbus_string_init (scratch_space))
1446  return NULL;
1447 
1448  if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
1449  dbus_test_exec, exe, DBUS_EXEEXT))
1450  {
1451  _dbus_string_free (scratch_space);
1452  return NULL;
1453  }
1454 
1455  return _dbus_string_get_data (scratch_space);
1456 }
1457 
1458 static void
1459 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1460 {
1461  while (LIVE_CHILDREN (sitter))
1462  babysitter_iteration (sitter, TRUE);
1463 }
1464 
1465 static dbus_bool_t
1466 check_spawn_nonexistent (void *data)
1467 {
1468  char *argv[4] = { NULL, NULL, NULL, NULL };
1469  DBusBabysitter *sitter = NULL;
1470  DBusError error = DBUS_ERROR_INIT;
1471 
1472  /*** Test launching nonexistent binary */
1473 
1474  argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1475  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent", argv,
1476  NULL, NULL, NULL,
1477  &error))
1478  {
1479  _dbus_babysitter_block_for_child_exit (sitter);
1480  _dbus_babysitter_set_child_exit_error (sitter, &error);
1481  }
1482 
1483  if (sitter)
1484  _dbus_babysitter_unref (sitter);
1485 
1486  if (!dbus_error_is_set (&error))
1487  {
1488  _dbus_warn ("Did not get an error launching nonexistent executable\n");
1489  return FALSE;
1490  }
1491 
1492  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1494  {
1495  _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1496  error.name, error.message);
1497  dbus_error_free (&error);
1498  return FALSE;
1499  }
1500 
1501  dbus_error_free (&error);
1502 
1503  return TRUE;
1504 }
1505 
1506 static dbus_bool_t
1507 check_spawn_segfault (void *data)
1508 {
1509  char *argv[4] = { NULL, NULL, NULL, NULL };
1510  DBusBabysitter *sitter = NULL;
1511  DBusError error = DBUS_ERROR_INIT;
1512  DBusString argv0;
1513 
1514  /*** Test launching segfault binary */
1515 
1516  argv[0] = get_test_exec ("test-segfault", &argv0);
1517 
1518  if (argv[0] == NULL)
1519  {
1520  /* OOM was simulated, never mind */
1521  return TRUE;
1522  }
1523 
1524  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_segfault", argv,
1525  NULL, NULL, NULL,
1526  &error))
1527  {
1528  _dbus_babysitter_block_for_child_exit (sitter);
1529  _dbus_babysitter_set_child_exit_error (sitter, &error);
1530  }
1531 
1532  _dbus_string_free (&argv0);
1533 
1534  if (sitter)
1535  _dbus_babysitter_unref (sitter);
1536 
1537  if (!dbus_error_is_set (&error))
1538  {
1539  _dbus_warn ("Did not get an error launching segfaulting binary\n");
1540  return FALSE;
1541  }
1542 
1543  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1545  {
1546  _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1547  error.name, error.message);
1548  dbus_error_free (&error);
1549  return FALSE;
1550  }
1551 
1552  dbus_error_free (&error);
1553 
1554  return TRUE;
1555 }
1556 
1557 static dbus_bool_t
1558 check_spawn_exit (void *data)
1559 {
1560  char *argv[4] = { NULL, NULL, NULL, NULL };
1561  DBusBabysitter *sitter = NULL;
1562  DBusError error = DBUS_ERROR_INIT;
1563  DBusString argv0;
1564 
1565  /*** Test launching exit failure binary */
1566 
1567  argv[0] = get_test_exec ("test-exit", &argv0);
1568 
1569  if (argv[0] == NULL)
1570  {
1571  /* OOM was simulated, never mind */
1572  return TRUE;
1573  }
1574 
1575  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_exit", argv,
1576  NULL, NULL, NULL,
1577  &error))
1578  {
1579  _dbus_babysitter_block_for_child_exit (sitter);
1580  _dbus_babysitter_set_child_exit_error (sitter, &error);
1581  }
1582 
1583  _dbus_string_free (&argv0);
1584 
1585  if (sitter)
1586  _dbus_babysitter_unref (sitter);
1587 
1588  if (!dbus_error_is_set (&error))
1589  {
1590  _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1591  return FALSE;
1592  }
1593 
1594  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1596  {
1597  _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1598  error.name, error.message);
1599  dbus_error_free (&error);
1600  return FALSE;
1601  }
1602 
1603  dbus_error_free (&error);
1604 
1605  return TRUE;
1606 }
1607 
1608 static dbus_bool_t
1609 check_spawn_and_kill (void *data)
1610 {
1611  char *argv[4] = { NULL, NULL, NULL, NULL };
1612  DBusBabysitter *sitter = NULL;
1613  DBusError error = DBUS_ERROR_INIT;
1614  DBusString argv0;
1615 
1616  /*** Test launching sleeping binary then killing it */
1617 
1618  argv[0] = get_test_exec ("test-sleep-forever", &argv0);
1619 
1620  if (argv[0] == NULL)
1621  {
1622  /* OOM was simulated, never mind */
1623  return TRUE;
1624  }
1625 
1626  if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_and_kill", argv,
1627  NULL, NULL, NULL,
1628  &error))
1629  {
1630  _dbus_babysitter_kill_child (sitter);
1631 
1632  _dbus_babysitter_block_for_child_exit (sitter);
1633 
1634  _dbus_babysitter_set_child_exit_error (sitter, &error);
1635  }
1636 
1637  _dbus_string_free (&argv0);
1638 
1639  if (sitter)
1640  _dbus_babysitter_unref (sitter);
1641 
1642  if (!dbus_error_is_set (&error))
1643  {
1644  _dbus_warn ("Did not get an error after killing spawned binary\n");
1645  return FALSE;
1646  }
1647 
1648  if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1650  {
1651  _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1652  error.name, error.message);
1653  dbus_error_free (&error);
1654  return FALSE;
1655  }
1656 
1657  dbus_error_free (&error);
1658 
1659  return TRUE;
1660 }
1661 
1663 _dbus_spawn_test (const char *test_data_dir)
1664 {
1665  if (!_dbus_test_oom_handling ("spawn_nonexistent",
1666  check_spawn_nonexistent,
1667  NULL))
1668  return FALSE;
1669 
1670  if (!_dbus_test_oom_handling ("spawn_segfault",
1671  check_spawn_segfault,
1672  NULL))
1673  return FALSE;
1674 
1675  if (!_dbus_test_oom_handling ("spawn_exit",
1676  check_spawn_exit,
1677  NULL))
1678  return FALSE;
1679 
1680  if (!_dbus_test_oom_handling ("spawn_and_kill",
1681  check_spawn_and_kill,
1682  NULL))
1683  return FALSE;
1684 
1685  return TRUE;
1686 }
1687 #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:659
#define _DBUS_POLLHUP
Hung up.
Definition: dbus-sysdeps.h:302
unsigned int have_exec_errnum
True if we have an error code from exec()
Definition: dbus-spawn.c:264
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
DBusWatch * error_watch
Error pipe watch.
Definition: dbus-spawn.c:254
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:110
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
#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:261
#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:376
void dbus_error_free(DBusError *error)
Frees an error that'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:310
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:294
Read succeeded.
Definition: dbus-spawn.c:65
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.
short events
Events to poll for.
Definition: dbus-sysdeps.h:313
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
Definition: dbus-spawn.c:690
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:185
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:586
dbus_bool_t _dbus_babysitter_get_child_exit_status(DBusBabysitter *sitter, int *status)
Gets the exit status of the child.
Definition: dbus-spawn.c:715
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:668
Babysitter implementation details.
ReadStatus
Enumeration for status of a read()
Definition: dbus-spawn.c:63
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, const char *log_name, char **argv, char **env, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
Definition: dbus-spawn.c:1194
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
pid_t sitter_pid
PID Of the babysitter.
Definition: dbus-spawn.c:249
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:1096
EOF returned.
Definition: dbus-spawn.c:67
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:797
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:250
#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:312
As in POLLERR (can'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 "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
const char * name
public error name field
Definition: dbus-errors.h:50
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
#define READ_END
Helps remember which end of the pipe is which.
Definition: dbus-spawn.c:857
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
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:324
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:296
#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:749
char * log_name
the name under which to log messages about this process being spawned
#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:263
#define FALSE
Expands to "0".
int error_pipe_from_child
Connection to the process that does the exec()
Definition: dbus-spawn.c:247
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
As in POLLIN.
#define WRITE_END
Helps remember which end of the pipe is which.
Definition: dbus-spawn.c:859
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:260
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:739
short revents
Events that occurred.
Definition: dbus-sysdeps.h:314
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:66
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application's DBusRemoveWatchFunction if appropriat...
Definition: dbus-watch.c:409
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:300
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.
Definition: dbus-spawn.c:302