D-Bus  1.11.14
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 #if defined(__APPLE__)
49 # include <crt_externs.h>
50 # define environ (*_NSGetEnviron ())
51 #elif !HAVE_DECL_ENVIRON
52 extern char **environ;
53 #endif
54 
60 /*
61  * I'm pretty sure this whole spawn file could be made simpler,
62  * if you thought about it a bit.
63  */
64 
68 typedef enum
69 {
73 } ReadStatus;
74 
75 static ReadStatus
76 read_ints (int fd,
77  int *buf,
78  int n_ints_in_buf,
79  int *n_ints_read,
80  DBusError *error)
81 {
82  size_t bytes = 0;
83  ReadStatus retval;
84 
85  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
86 
87  retval = READ_STATUS_OK;
88 
89  while (TRUE)
90  {
91  ssize_t chunk;
92  size_t to_read;
93 
94  to_read = sizeof (int) * n_ints_in_buf - bytes;
95 
96  if (to_read == 0)
97  break;
98 
99  again:
100 
101  chunk = read (fd,
102  ((char*)buf) + bytes,
103  to_read);
104 
105  if (chunk < 0 && errno == EINTR)
106  goto again;
107 
108  if (chunk < 0)
109  {
110  dbus_set_error (error,
112  "Failed to read from child pipe (%s)",
113  _dbus_strerror (errno));
114 
115  retval = READ_STATUS_ERROR;
116  break;
117  }
118  else if (chunk == 0)
119  {
120  retval = READ_STATUS_EOF;
121  break; /* EOF */
122  }
123  else /* chunk > 0 */
124  bytes += chunk;
125  }
126 
127  *n_ints_read = (int)(bytes / sizeof(int));
128 
129  return retval;
130 }
131 
132 static ReadStatus
133 read_pid (int fd,
134  pid_t *buf,
135  DBusError *error)
136 {
137  size_t bytes = 0;
138  ReadStatus retval;
139 
140  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
141 
142  retval = READ_STATUS_OK;
143 
144  while (TRUE)
145  {
146  ssize_t chunk;
147  size_t to_read;
148 
149  to_read = sizeof (pid_t) - bytes;
150 
151  if (to_read == 0)
152  break;
153 
154  again:
155 
156  chunk = read (fd,
157  ((char*)buf) + bytes,
158  to_read);
159  if (chunk < 0 && errno == EINTR)
160  goto again;
161 
162  if (chunk < 0)
163  {
164  dbus_set_error (error,
166  "Failed to read from child pipe (%s)",
167  _dbus_strerror (errno));
168 
169  retval = READ_STATUS_ERROR;
170  break;
171  }
172  else if (chunk == 0)
173  {
174  retval = READ_STATUS_EOF;
175  break; /* EOF */
176  }
177  else /* chunk > 0 */
178  bytes += chunk;
179  }
180 
181  return retval;
182 }
183 
184 /* The implementation uses an intermediate child between the main process
185  * and the grandchild. The grandchild is our spawned process. The intermediate
186  * child is a babysitter process; it keeps track of when the grandchild
187  * exits/crashes, and reaps the grandchild.
188  *
189  * We automatically reap the babysitter process, killing it if necessary,
190  * when the DBusBabysitter's refcount goes to zero.
191  *
192  * Processes:
193  *
194  * main process
195  * | fork() A
196  * \- babysitter
197  * | fork () B
198  * \- grandchild --> exec --> spawned process
199  *
200  * IPC:
201  * child_err_report_pipe
202  * /-----------<---------<--------------\
203  * | ^
204  * v |
205  * main process babysitter grandchild
206  * ^ ^
207  * v v
208  * \-------<->-------/
209  * babysitter_pipe
210  *
211  * child_err_report_pipe is genuinely a pipe.
212  * The READ_END (also called error_pipe_from_child) is used in the main
213  * process. The WRITE_END (also called child_err_report_fd) is used in
214  * the grandchild process.
215  *
216  * On failure, the grandchild process sends CHILD_EXEC_FAILED + errno.
217  * On success, the pipe just closes (because it's close-on-exec) without
218  * sending any bytes.
219  *
220  * babysitter_pipe is mis-named: it's really a bidirectional socketpair.
221  * The [0] end (also called socket_to_babysitter) is used in the main
222  * process, the [1] end (also called parent_pipe) is used in the babysitter.
223  *
224  * If the fork() labelled B in the diagram above fails, the babysitter sends
225  * CHILD_FORK_FAILED + errno.
226  * On success, the babysitter sends CHILD_PID + the grandchild's pid.
227  * On SIGCHLD, the babysitter sends CHILD_EXITED + the exit status.
228  * The main process doesn't explicitly send anything, but when it exits,
229  * the babysitter gets POLLHUP or POLLERR.
230  */
231 
232 /* Messages from children to parents */
233 enum
234 {
235  CHILD_EXITED, /* This message is followed by the exit status int */
236  CHILD_FORK_FAILED, /* Followed by errno */
237  CHILD_EXEC_FAILED, /* Followed by errno */
238  CHILD_PID /* Followed by pid_t */
239 };
240 
244 struct DBusBabysitter
245 {
246  int refcount;
248  char *log_name;
254  pid_t sitter_pid;
262  DBusBabysitterFinishedFunc finished_cb;
263  void *finished_data;
264 
265  int errnum;
266  int status;
267  unsigned int have_child_status : 1;
268  unsigned int have_fork_errnum : 1;
269  unsigned int have_exec_errnum : 1;
270 };
271 
272 static DBusBabysitter*
273 _dbus_babysitter_new (void)
274 {
275  DBusBabysitter *sitter;
276 
277  sitter = dbus_new0 (DBusBabysitter, 1);
278  if (sitter == NULL)
279  return NULL;
280 
281  sitter->refcount = 1;
282 
283  sitter->socket_to_babysitter.fd = -1;
284  sitter->error_pipe_from_child = -1;
285 
286  sitter->sitter_pid = -1;
287  sitter->grandchild_pid = -1;
288 
289  sitter->watches = _dbus_watch_list_new ();
290  if (sitter->watches == NULL)
291  goto failed;
292 
293  return sitter;
294 
295  failed:
296  _dbus_babysitter_unref (sitter);
297  return NULL;
298 }
299 
308 {
309  _dbus_assert (sitter != NULL);
310  _dbus_assert (sitter->refcount > 0);
311 
312  sitter->refcount += 1;
313 
314  return sitter;
315 }
316 
317 static void close_socket_to_babysitter (DBusBabysitter *sitter);
318 static void close_error_pipe_from_child (DBusBabysitter *sitter);
319 
328 void
330 {
331  _dbus_assert (sitter != NULL);
332  _dbus_assert (sitter->refcount > 0);
333 
334  sitter->refcount -= 1;
335  if (sitter->refcount == 0)
336  {
337  /* If we haven't forked other babysitters
338  * since this babysitter and socket were
339  * created then this close will cause the
340  * babysitter to wake up from poll with
341  * a hangup and then the babysitter will
342  * quit itself.
343  */
344  close_socket_to_babysitter (sitter);
345 
346  close_error_pipe_from_child (sitter);
347 
348  if (sitter->sitter_pid > 0)
349  {
350  int status;
351  int ret;
352 
353  /* It's possible the babysitter died on its own above
354  * from the close, or was killed randomly
355  * by some other process, so first try to reap it
356  */
357  ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
358 
359  /* If we couldn't reap the child then kill it, and
360  * try again
361  */
362  if (ret == 0)
363  kill (sitter->sitter_pid, SIGKILL);
364 
365  if (ret == 0)
366  {
367  do
368  {
369  ret = waitpid (sitter->sitter_pid, &status, 0);
370  }
371  while (_DBUS_UNLIKELY (ret < 0 && errno == EINTR));
372  }
373 
374  if (ret < 0)
375  {
376  if (errno == ECHILD)
377  _dbus_warn ("Babysitter process not available to be reaped; should not happen");
378  else
379  _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s",
380  errno, _dbus_strerror (errno));
381  }
382  else
383  {
384  _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
385  (long) ret, (long) sitter->sitter_pid);
386 
387  if (WIFEXITED (sitter->status))
388  _dbus_verbose ("Babysitter exited with status %d\n",
389  WEXITSTATUS (sitter->status));
390  else if (WIFSIGNALED (sitter->status))
391  _dbus_verbose ("Babysitter received signal %d\n",
392  WTERMSIG (sitter->status));
393  else
394  _dbus_verbose ("Babysitter exited abnormally\n");
395  }
396 
397  sitter->sitter_pid = -1;
398  }
399 
400  if (sitter->watches)
401  _dbus_watch_list_free (sitter->watches);
402 
403  dbus_free (sitter->log_name);
404 
405  dbus_free (sitter);
406  }
407 }
408 
409 static ReadStatus
410 read_data (DBusBabysitter *sitter,
411  int fd)
412 {
413  int what;
414  int got;
415  DBusError error = DBUS_ERROR_INIT;
416  ReadStatus r;
417 
418  r = read_ints (fd, &what, 1, &got, &error);
419 
420  switch (r)
421  {
422  case READ_STATUS_ERROR:
423  _dbus_warn ("Failed to read data from fd %d: %s", fd, error.message);
424  dbus_error_free (&error);
425  return r;
426 
427  case READ_STATUS_EOF:
428  return r;
429 
430  case READ_STATUS_OK:
431  break;
432 
433  default:
434  _dbus_assert_not_reached ("invalid ReadStatus");
435  break;
436  }
437 
438  if (got == 1)
439  {
440  switch (what)
441  {
442  case CHILD_EXITED:
443  case CHILD_FORK_FAILED:
444  case CHILD_EXEC_FAILED:
445  {
446  int arg;
447 
448  r = read_ints (fd, &arg, 1, &got, &error);
449 
450  switch (r)
451  {
452  case READ_STATUS_ERROR:
453  _dbus_warn ("Failed to read arg from fd %d: %s", fd, error.message);
454  dbus_error_free (&error);
455  return r;
456  case READ_STATUS_EOF:
457  return r;
458  case READ_STATUS_OK:
459  break;
460  default:
461  _dbus_assert_not_reached ("invalid ReadStatus");
462  break;
463  }
464 
465  if (got == 1)
466  {
467  if (what == CHILD_EXITED)
468  {
469  /* Do not reset sitter->errnum to 0 here. We get here if
470  * the babysitter reports that the grandchild process has
471  * exited, and there are two ways that can happen:
472  *
473  * 1. grandchild successfully exec()s the desired process,
474  * but then the desired process exits or is terminated
475  * by a signal. The babysitter observes this and reports
476  * CHILD_EXITED.
477  *
478  * 2. grandchild fails to exec() the desired process,
479  * attempts to report the exec() failure (which
480  * we will receive as CHILD_EXEC_FAILED), and then
481  * exits itself (which will prompt the babysitter to
482  * send CHILD_EXITED). We want the CHILD_EXEC_FAILED
483  * to take precedence (and have its errno logged),
484  * which _dbus_babysitter_set_child_exit_error() does.
485  */
486  sitter->have_child_status = TRUE;
487  sitter->status = arg;
488  _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
489  WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
490  WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
491  }
492  else if (what == CHILD_FORK_FAILED)
493  {
494  sitter->have_fork_errnum = TRUE;
495  sitter->errnum = arg;
496  _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
497  }
498  else if (what == CHILD_EXEC_FAILED)
499  {
500  sitter->have_exec_errnum = TRUE;
501  sitter->errnum = arg;
502  _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
503  }
504  }
505  }
506  break;
507  case CHILD_PID:
508  {
509  pid_t pid = -1;
510 
511  r = read_pid (fd, &pid, &error);
512 
513  switch (r)
514  {
515  case READ_STATUS_ERROR:
516  _dbus_warn ("Failed to read PID from fd %d: %s", fd, error.message);
517  dbus_error_free (&error);
518  return r;
519  case READ_STATUS_EOF:
520  return r;
521  case READ_STATUS_OK:
522  break;
523  default:
524  _dbus_assert_not_reached ("invalid ReadStatus");
525  break;
526  }
527 
528  sitter->grandchild_pid = pid;
529 
530  _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
531  }
532  break;
533  default:
534  _dbus_warn ("Unknown message received from babysitter process");
535  break;
536  }
537  }
538 
539  return r;
540 }
541 
542 static void
543 close_socket_to_babysitter (DBusBabysitter *sitter)
544 {
545  _dbus_verbose ("Closing babysitter\n");
546 
547  if (sitter->sitter_watch != NULL)
548  {
549  _dbus_assert (sitter->watches != NULL);
553  sitter->sitter_watch = NULL;
554  }
555 
556  if (sitter->socket_to_babysitter.fd >= 0)
557  {
559  sitter->socket_to_babysitter.fd = -1;
560  }
561 }
562 
563 static void
564 close_error_pipe_from_child (DBusBabysitter *sitter)
565 {
566  _dbus_verbose ("Closing child error\n");
567 
568  if (sitter->error_watch != NULL)
569  {
570  _dbus_assert (sitter->watches != NULL);
573  _dbus_watch_unref (sitter->error_watch);
574  sitter->error_watch = NULL;
575  }
576 
577  if (sitter->error_pipe_from_child >= 0)
578  {
580  sitter->error_pipe_from_child = -1;
581  }
582 }
583 
584 static void
585 handle_babysitter_socket (DBusBabysitter *sitter,
586  int revents)
587 {
588  /* Even if we have POLLHUP, we want to keep reading
589  * data until POLLIN goes away; so this function only
590  * looks at HUP/ERR if no IN is set.
591  */
592  if (revents & _DBUS_POLLIN)
593  {
594  _dbus_verbose ("Reading data from babysitter\n");
595  if (read_data (sitter, sitter->socket_to_babysitter.fd) != READ_STATUS_OK)
596  close_socket_to_babysitter (sitter);
597  }
598  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
599  {
600  close_socket_to_babysitter (sitter);
601  }
602 }
603 
604 static void
605 handle_error_pipe (DBusBabysitter *sitter,
606  int revents)
607 {
608  if (revents & _DBUS_POLLIN)
609  {
610  _dbus_verbose ("Reading data from child error\n");
611  if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
612  close_error_pipe_from_child (sitter);
613  }
614  else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
615  {
616  close_error_pipe_from_child (sitter);
617  }
618 }
619 
620 /* returns whether there were any poll events handled */
621 static dbus_bool_t
622 babysitter_iteration (DBusBabysitter *sitter,
623  dbus_bool_t block)
624 {
625  DBusPollFD fds[2];
626  int i;
627  dbus_bool_t descriptors_ready;
628 
629  descriptors_ready = FALSE;
630 
631  i = 0;
632 
633  if (sitter->error_pipe_from_child >= 0)
634  {
635  fds[i].fd = sitter->error_pipe_from_child;
636  fds[i].events = _DBUS_POLLIN;
637  fds[i].revents = 0;
638  ++i;
639  }
640 
641  if (sitter->socket_to_babysitter.fd >= 0)
642  {
643  fds[i].fd = sitter->socket_to_babysitter.fd;
644  fds[i].events = _DBUS_POLLIN;
645  fds[i].revents = 0;
646  ++i;
647  }
648 
649  if (i > 0)
650  {
651  int ret;
652 
653  do
654  {
655  ret = _dbus_poll (fds, i, 0);
656  }
657  while (ret < 0 && errno == EINTR);
658 
659  if (ret == 0 && block)
660  {
661  do
662  {
663  ret = _dbus_poll (fds, i, -1);
664  }
665  while (ret < 0 && errno == EINTR);
666  }
667 
668  if (ret > 0)
669  {
670  descriptors_ready = TRUE;
671 
672  while (i > 0)
673  {
674  --i;
675  if (fds[i].fd == sitter->error_pipe_from_child)
676  handle_error_pipe (sitter, fds[i].revents);
677  else if (fds[i].fd == sitter->socket_to_babysitter.fd)
678  handle_babysitter_socket (sitter, fds[i].revents);
679  }
680  }
681  }
682 
683  return descriptors_ready;
684 }
685 
690 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter.fd >= 0 || (sitter)->error_pipe_from_child >= 0)
691 
698 void
700 {
701  /* be sure we have the PID of the child */
702  while (LIVE_CHILDREN (sitter) &&
703  sitter->grandchild_pid == -1)
704  babysitter_iteration (sitter, TRUE);
705 
706  _dbus_verbose ("Got child PID %ld for killing\n",
707  (long) sitter->grandchild_pid);
708 
709  if (sitter->grandchild_pid == -1)
710  return; /* child is already dead, or we're so hosed we'll never recover */
711 
712  kill (sitter->grandchild_pid, SIGKILL);
713 }
714 
722 {
723 
724  /* Be sure we're up-to-date */
725  while (LIVE_CHILDREN (sitter) &&
726  babysitter_iteration (sitter, FALSE))
727  ;
728 
729  /* We will have exited the babysitter when the child has exited */
730  return sitter->socket_to_babysitter.fd < 0;
731 }
732 
747  int *status)
748 {
749  if (!_dbus_babysitter_get_child_exited (sitter))
750  _dbus_assert_not_reached ("Child has not exited");
751 
752  if (!sitter->have_child_status ||
753  !(WIFEXITED (sitter->status)))
754  return FALSE;
755 
756  *status = WEXITSTATUS (sitter->status);
757  return TRUE;
758 }
759 
769 void
771  DBusError *error)
772 {
773  if (!_dbus_babysitter_get_child_exited (sitter))
774  return;
775 
776  /* Note that if exec fails, we will also get a child status
777  * from the babysitter saying the child exited,
778  * so we need to give priority to the exec error
779  */
780  if (sitter->have_exec_errnum)
781  {
783  "Failed to execute program %s: %s",
784  sitter->log_name, _dbus_strerror (sitter->errnum));
785  }
786  else if (sitter->have_fork_errnum)
787  {
789  "Failed to fork a new process %s: %s",
790  sitter->log_name, _dbus_strerror (sitter->errnum));
791  }
792  else if (sitter->have_child_status)
793  {
794  if (WIFEXITED (sitter->status))
796  "Process %s exited with status %d",
797  sitter->log_name, WEXITSTATUS (sitter->status));
798  else if (WIFSIGNALED (sitter->status))
800  "Process %s received signal %d",
801  sitter->log_name, WTERMSIG (sitter->status));
802  else
804  "Process %s exited abnormally",
805  sitter->log_name);
806  }
807  else
808  {
810  "Process %s exited, reason unknown",
811  sitter->log_name);
812  }
813 }
814 
829  DBusAddWatchFunction add_function,
830  DBusRemoveWatchFunction remove_function,
831  DBusWatchToggledFunction toggled_function,
832  void *data,
833  DBusFreeFunction free_data_function)
834 {
835  return _dbus_watch_list_set_functions (sitter->watches,
836  add_function,
837  remove_function,
838  toggled_function,
839  data,
840  free_data_function);
841 }
842 
843 static dbus_bool_t
844 handle_watch (DBusWatch *watch,
845  unsigned int condition,
846  void *data)
847 {
848  DBusBabysitter *sitter = _dbus_babysitter_ref (data);
849  int revents;
850  int fd;
851 
852  revents = 0;
853  if (condition & DBUS_WATCH_READABLE)
854  revents |= _DBUS_POLLIN;
855  if (condition & DBUS_WATCH_ERROR)
856  revents |= _DBUS_POLLERR;
857  if (condition & DBUS_WATCH_HANGUP)
858  revents |= _DBUS_POLLHUP;
859 
860  fd = dbus_watch_get_socket (watch);
861 
862  if (fd == sitter->error_pipe_from_child)
863  handle_error_pipe (sitter, revents);
864  else if (fd == sitter->socket_to_babysitter.fd)
865  handle_babysitter_socket (sitter, revents);
866 
867  while (LIVE_CHILDREN (sitter) &&
868  babysitter_iteration (sitter, FALSE))
869  ;
870 
871  /* fd.o #32992: if the handle_* methods closed their sockets, they previously
872  * didn't always remove the watches. Check that we don't regress. */
873  _dbus_assert (sitter->socket_to_babysitter.fd != -1 || sitter->sitter_watch == NULL);
874  _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
875 
876  if (_dbus_babysitter_get_child_exited (sitter) &&
877  sitter->finished_cb != NULL)
878  {
879  sitter->finished_cb (sitter, sitter->finished_data);
880  sitter->finished_cb = NULL;
881  }
882 
883  _dbus_babysitter_unref (sitter);
884  return TRUE;
885 }
886 
888 #define READ_END 0
889 
890 #define WRITE_END 1
891 
892 
893 /* Avoids a danger in re-entrant situations (calling close()
894  * on a file descriptor twice, and another module has
895  * re-opened it since the first close).
896  *
897  * This previously claimed to be relevant for threaded situations, but by
898  * trivial inspection, it is not thread-safe. It doesn't actually
899  * matter, since this module is only used in the -util variant of the
900  * library, which is only used in single-threaded situations.
901  */
902 static int
903 close_and_invalidate (int *fd)
904 {
905  int ret;
906 
907  if (*fd < 0)
908  return -1;
909  else
910  {
911  ret = _dbus_close (*fd, NULL);
912  *fd = -1;
913  }
914 
915  return ret;
916 }
917 
918 static dbus_bool_t
919 make_pipe (int p[2],
920  DBusError *error)
921 {
922  int retval;
923 
924 #ifdef HAVE_PIPE2
925  dbus_bool_t cloexec_done;
926 
927  retval = pipe2 (p, O_CLOEXEC);
928  cloexec_done = retval >= 0;
929 
930  /* Check if kernel seems to be too old to know pipe2(). We assume
931  that if pipe2 is available, O_CLOEXEC is too. */
932  if (retval < 0 && errno == ENOSYS)
933 #endif
934  {
935  retval = pipe(p);
936  }
937 
938  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
939 
940  if (retval < 0)
941  {
942  dbus_set_error (error,
944  "Failed to create pipe for communicating with child process (%s)",
945  _dbus_strerror (errno));
946  return FALSE;
947  }
948 
949 #ifdef HAVE_PIPE2
950  if (!cloexec_done)
951 #endif
952  {
955  }
956 
957  return TRUE;
958 }
959 
960 static void
961 do_write (int fd, const void *buf, size_t count)
962 {
963  size_t bytes_written;
964  int ret;
965 
966  bytes_written = 0;
967 
968  again:
969 
970  ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
971 
972  if (ret < 0)
973  {
974  if (errno == EINTR)
975  goto again;
976  else
977  {
978  _dbus_warn ("Failed to write data to pipe!");
979  exit (1); /* give up, we suck */
980  }
981  }
982  else
983  bytes_written += ret;
984 
985  if (bytes_written < count)
986  goto again;
987 }
988 
989 static void write_err_and_exit (int fd, int msg) _DBUS_GNUC_NORETURN;
990 
991 static void
992 write_err_and_exit (int fd, int msg)
993 {
994  int en = errno;
995 
996  do_write (fd, &msg, sizeof (msg));
997  do_write (fd, &en, sizeof (en));
998 
999  exit (1);
1000 }
1001 
1002 static void
1003 write_pid (int fd, pid_t pid)
1004 {
1005  int msg = CHILD_PID;
1006 
1007  do_write (fd, &msg, sizeof (msg));
1008  do_write (fd, &pid, sizeof (pid));
1009 }
1010 
1011 static void write_status_and_exit (int fd, int status) _DBUS_GNUC_NORETURN;
1012 
1013 static void
1014 write_status_and_exit (int fd, int status)
1015 {
1016  int msg = CHILD_EXITED;
1017 
1018  do_write (fd, &msg, sizeof (msg));
1019  do_write (fd, &status, sizeof (status));
1020 
1021  exit (0);
1022 }
1023 
1024 static void do_exec (int child_err_report_fd,
1025  char * const *argv,
1026  char * const *envp,
1027  DBusSpawnChildSetupFunc child_setup,
1028  void *user_data) _DBUS_GNUC_NORETURN;
1029 
1030 static void
1031 do_exec (int child_err_report_fd,
1032  char * const *argv,
1033  char * const *envp,
1034  DBusSpawnChildSetupFunc child_setup,
1035  void *user_data)
1036 {
1037 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1038  int i, max_open;
1039 #endif
1040 
1041  _dbus_verbose_reset ();
1042  _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
1043  _dbus_getpid ());
1044 
1045  if (child_setup)
1046  (* child_setup) (user_data);
1047 
1048 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1049  max_open = sysconf (_SC_OPEN_MAX);
1050 
1051  for (i = 3; i < max_open; i++)
1052  {
1053  int retval;
1054 
1055  if (i == child_err_report_fd)
1056  continue;
1057 
1058  retval = fcntl (i, F_GETFD);
1059 
1060  if (retval != -1 && !(retval & FD_CLOEXEC))
1061  _dbus_warn ("Fd %d did not have the close-on-exec flag set!", i);
1062  }
1063 #endif
1064 
1065  if (envp == NULL)
1066  {
1067  _dbus_assert (environ != NULL);
1068 
1069  envp = environ;
1070  }
1071 
1072  execve (argv[0], argv, envp);
1073 
1074  /* Exec failed */
1075  write_err_and_exit (child_err_report_fd,
1076  CHILD_EXEC_FAILED);
1077 }
1078 
1079 static void
1080 check_babysit_events (pid_t grandchild_pid,
1081  int parent_pipe,
1082  int revents)
1083 {
1084  pid_t ret;
1085  int status;
1086 
1087  do
1088  {
1089  ret = waitpid (grandchild_pid, &status, WNOHANG);
1090  /* The man page says EINTR can't happen with WNOHANG,
1091  * but there are reports of it (maybe only with valgrind?)
1092  */
1093  }
1094  while (ret < 0 && errno == EINTR);
1095 
1096  if (ret == 0)
1097  {
1098  _dbus_verbose ("no child exited\n");
1099 
1100  ; /* no child exited */
1101  }
1102  else if (ret < 0)
1103  {
1104  /* This isn't supposed to happen. */
1105  _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s",
1106  _dbus_strerror (errno));
1107  exit (1);
1108  }
1109  else if (ret == grandchild_pid)
1110  {
1111  /* Child exited */
1112  _dbus_verbose ("reaped child pid %ld\n", (long) ret);
1113 
1114  write_status_and_exit (parent_pipe, status);
1115  }
1116  else
1117  {
1118  _dbus_warn ("waitpid() reaped pid %d that we've never heard of",
1119  (int) ret);
1120  exit (1);
1121  }
1122 
1123  if (revents & _DBUS_POLLIN)
1124  {
1125  _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
1126  }
1127 
1128  if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
1129  {
1130  /* Parent is gone, so we just exit */
1131  _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
1132  exit (0);
1133  }
1134 }
1135 
1136 static int babysit_sigchld_pipe = -1;
1137 
1138 static void
1139 babysit_signal_handler (int signo)
1140 {
1141  char b = '\0';
1142  again:
1143  if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1144  if (errno == EINTR)
1145  goto again;
1146 }
1147 
1148 static void babysit (pid_t grandchild_pid,
1149  int parent_pipe) _DBUS_GNUC_NORETURN;
1150 
1151 static void
1152 babysit (pid_t grandchild_pid,
1153  int parent_pipe)
1154 {
1155  int sigchld_pipe[2];
1156 
1157  /* We don't exec, so we keep parent state, such as the pid that
1158  * _dbus_verbose() uses. Reset the pid here.
1159  */
1160  _dbus_verbose_reset ();
1161 
1162  /* I thought SIGCHLD would just wake up the poll, but
1163  * that didn't seem to work, so added this pipe.
1164  * Probably the pipe is more likely to work on busted
1165  * operating systems anyhow.
1166  */
1167  if (pipe (sigchld_pipe) < 0)
1168  {
1169  _dbus_warn ("Not enough file descriptors to create pipe in babysitter process");
1170  exit (1);
1171  }
1172 
1173  babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1174 
1175  _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1176 
1177  write_pid (parent_pipe, grandchild_pid);
1178 
1179  check_babysit_events (grandchild_pid, parent_pipe, 0);
1180 
1181  while (TRUE)
1182  {
1183  DBusPollFD pfds[2];
1184 
1185  pfds[0].fd = parent_pipe;
1186  pfds[0].events = _DBUS_POLLIN;
1187  pfds[0].revents = 0;
1188 
1189  pfds[1].fd = sigchld_pipe[READ_END];
1190  pfds[1].events = _DBUS_POLLIN;
1191  pfds[1].revents = 0;
1192 
1193  if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1194  {
1195  _dbus_warn ("_dbus_poll() error: %s", strerror (errno));
1196  exit (1);
1197  }
1198 
1199  if (pfds[0].revents != 0)
1200  {
1201  check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1202  }
1203  else if (pfds[1].revents & _DBUS_POLLIN)
1204  {
1205  char b;
1206  if (read (sigchld_pipe[READ_END], &b, 1) == -1)
1207  {
1208  /* ignore */
1209  }
1210  /* do waitpid check */
1211  check_babysit_events (grandchild_pid, parent_pipe, 0);
1212  }
1213  }
1214 
1215  exit (1);
1216 }
1217 
1244  const char *log_name,
1245  char * const *argv,
1246  char **env,
1247  DBusSpawnFlags flags,
1248  DBusSpawnChildSetupFunc child_setup,
1249  void *user_data,
1250  DBusError *error)
1251 {
1252  DBusBabysitter *sitter;
1253  int child_err_report_pipe[2] = { -1, -1 };
1254  DBusSocket babysitter_pipe[2] = { DBUS_SOCKET_INIT, DBUS_SOCKET_INIT };
1255  pid_t pid;
1256 #ifdef HAVE_SYSTEMD
1257  int fd_out = -1;
1258  int fd_err = -1;
1259 #endif
1260 
1261  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1262  _dbus_assert (argv[0] != NULL);
1263 
1264  if (sitter_p != NULL)
1265  *sitter_p = NULL;
1266 
1267  sitter = NULL;
1268 
1269  sitter = _dbus_babysitter_new ();
1270  if (sitter == NULL)
1271  {
1273  return FALSE;
1274  }
1275 
1276  sitter->log_name = _dbus_strdup (log_name);
1277  if (sitter->log_name == NULL && log_name != NULL)
1278  {
1280  goto cleanup_and_fail;
1281  }
1282 
1283  if (sitter->log_name == NULL)
1284  sitter->log_name = _dbus_strdup (argv[0]);
1285 
1286  if (sitter->log_name == NULL)
1287  {
1289  goto cleanup_and_fail;
1290  }
1291 
1292  if (!make_pipe (child_err_report_pipe, error))
1293  goto cleanup_and_fail;
1294 
1295  if (!_dbus_socketpair (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1296  goto cleanup_and_fail;
1297 
1298  /* Setting up the babysitter is only useful in the parent,
1299  * but we don't want to run out of memory and fail
1300  * after we've already forked, since then we'd leak
1301  * child processes everywhere.
1302  */
1303  sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1305  TRUE, handle_watch, sitter, NULL);
1306  if (sitter->error_watch == NULL)
1307  {
1309  goto cleanup_and_fail;
1310  }
1311 
1312  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1313  {
1314  /* we need to free it early so the destructor won't try to remove it
1315  * without it having been added, which DBusLoop doesn't allow */
1317  _dbus_watch_unref (sitter->error_watch);
1318  sitter->error_watch = NULL;
1319 
1321  goto cleanup_and_fail;
1322  }
1323 
1324  sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0].fd,
1326  TRUE, handle_watch, sitter, NULL);
1327  if (sitter->sitter_watch == NULL)
1328  {
1330  goto cleanup_and_fail;
1331  }
1332 
1333  if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1334  {
1335  /* we need to free it early so the destructor won't try to remove it
1336  * without it having been added, which DBusLoop doesn't allow */
1338  _dbus_watch_unref (sitter->sitter_watch);
1339  sitter->sitter_watch = NULL;
1340 
1342  goto cleanup_and_fail;
1343  }
1344 
1345  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1346 
1347 #ifdef HAVE_SYSTEMD
1348  if (flags & DBUS_SPAWN_REDIRECT_OUTPUT)
1349  {
1350  /* This may fail, but it's not critical.
1351  * In particular, if we were compiled with journald support but are now
1352  * running on a non-systemd system, this is going to fail, so we
1353  * have to cope gracefully. */
1354  fd_out = sd_journal_stream_fd (sitter->log_name, LOG_INFO, FALSE);
1355  fd_err = sd_journal_stream_fd (sitter->log_name, LOG_WARNING, FALSE);
1356  }
1357 #endif
1358 
1359  pid = fork ();
1360 
1361  if (pid < 0)
1362  {
1363  dbus_set_error (error,
1365  "Failed to fork (%s)",
1366  _dbus_strerror (errno));
1367  goto cleanup_and_fail;
1368  }
1369  else if (pid == 0)
1370  {
1371  /* Immediate child, this is the babysitter process. */
1372  int grandchild_pid;
1373 
1374  /* Be sure we crash if the parent exits
1375  * and we write to the err_report_pipe
1376  */
1377  signal (SIGPIPE, SIG_DFL);
1378 
1379  /* Close the parent's end of the pipes. */
1380  close_and_invalidate (&child_err_report_pipe[READ_END]);
1381  close_and_invalidate (&babysitter_pipe[0].fd);
1382 
1383  /* Create the child that will exec () */
1384  grandchild_pid = fork ();
1385 
1386  if (grandchild_pid < 0)
1387  {
1388  write_err_and_exit (babysitter_pipe[1].fd,
1389  CHILD_FORK_FAILED);
1390  _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1391  }
1392  else if (grandchild_pid == 0)
1393  {
1394 #ifdef __linux__
1395  int fd = -1;
1396 
1397 #ifdef O_CLOEXEC
1398  fd = open ("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
1399 #endif
1400 
1401  if (fd < 0)
1402  {
1403  fd = open ("/proc/self/oom_score_adj", O_WRONLY);
1405  }
1406 
1407  if (fd >= 0)
1408  {
1409  if (write (fd, "0", sizeof (char)) < 0)
1410  _dbus_warn ("writing oom_score_adj error: %s", strerror (errno));
1411  _dbus_close (fd, NULL);
1412  }
1413 #endif
1414  /* Go back to ignoring SIGPIPE, since it's evil
1415  */
1416  signal (SIGPIPE, SIG_IGN);
1417 
1418  close_and_invalidate (&babysitter_pipe[1].fd);
1419 #ifdef HAVE_SYSTEMD
1420  /* log to systemd journal if possible */
1421  if (fd_out >= 0)
1422  dup2 (fd_out, STDOUT_FILENO);
1423  if (fd_err >= 0)
1424  dup2 (fd_err, STDERR_FILENO);
1425  close_and_invalidate (&fd_out);
1426  close_and_invalidate (&fd_err);
1427 #endif
1428  do_exec (child_err_report_pipe[WRITE_END],
1429  argv,
1430  env,
1431  child_setup, user_data);
1432  _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1433  }
1434  else
1435  {
1436  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1437 #ifdef HAVE_SYSTEMD
1438  close_and_invalidate (&fd_out);
1439  close_and_invalidate (&fd_err);
1440 #endif
1441  babysit (grandchild_pid, babysitter_pipe[1].fd);
1442  _dbus_assert_not_reached ("Got to code after babysit()");
1443  }
1444  }
1445  else
1446  {
1447  /* Close the uncared-about ends of the pipes */
1448  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1449  close_and_invalidate (&babysitter_pipe[1].fd);
1450 #ifdef HAVE_SYSTEMD
1451  close_and_invalidate (&fd_out);
1452  close_and_invalidate (&fd_err);
1453 #endif
1454 
1455  sitter->socket_to_babysitter = babysitter_pipe[0];
1456  babysitter_pipe[0].fd = -1;
1457 
1458  sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1459  child_err_report_pipe[READ_END] = -1;
1460 
1461  sitter->sitter_pid = pid;
1462 
1463  if (sitter_p != NULL)
1464  *sitter_p = sitter;
1465  else
1466  _dbus_babysitter_unref (sitter);
1467 
1468  dbus_free_string_array (env);
1469 
1470  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1471 
1472  return TRUE;
1473  }
1474 
1475  cleanup_and_fail:
1476 
1477  _DBUS_ASSERT_ERROR_IS_SET (error);
1478 
1479  close_and_invalidate (&child_err_report_pipe[READ_END]);
1480  close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1481  close_and_invalidate (&babysitter_pipe[0].fd);
1482  close_and_invalidate (&babysitter_pipe[1].fd);
1483 #ifdef HAVE_SYSTEMD
1484  close_and_invalidate (&fd_out);
1485  close_and_invalidate (&fd_err);
1486 #endif
1487 
1488  if (sitter != NULL)
1489  _dbus_babysitter_unref (sitter);
1490 
1491  return FALSE;
1492 }
1493 
1494 void
1495 _dbus_babysitter_set_result_function (DBusBabysitter *sitter,
1496  DBusBabysitterFinishedFunc finished,
1497  void *user_data)
1498 {
1499  sitter->finished_cb = finished;
1500  sitter->finished_data = user_data;
1501 }
1502 
1505 void
1506 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1507 {
1508  while (LIVE_CHILDREN (sitter))
1509  babysitter_iteration (sitter, TRUE);
1510 }
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.
DBusWatch * _dbus_watch_new(DBusPollable 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
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:690
#define _DBUS_POLLHUP
Hung up.
Definition: dbus-sysdeps.h:387
unsigned int have_exec_errnum
True if we have an error code from exec()
Definition: dbus-spawn.c:269
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
DBusWatch * error_watch
Error pipe watch.
Definition: dbus-spawn.c:259
dbus_bool_t _dbus_socketpair(DBusSocket *fd1, DBusSocket *fd2, dbus_bool_t blocking, DBusError *error)
Creates pair of connect sockets (as in socketpair()).
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
Definition: dbus-sysdeps.h:120
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:266
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
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
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:379
Read succeeded.
Definition: dbus-spawn.c:70
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
short events
Events to poll for.
Definition: dbus-sysdeps.h:374
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
Definition: dbus-spawn.c:721
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
Socket interface.
Definition: dbus-sysdeps.h:149
DBusWatchList * watches
Watches.
#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:594
dbus_bool_t _dbus_babysitter_get_child_exit_status(DBusBabysitter *sitter, int *status)
Gets the exit status of the child.
Definition: dbus-spawn.c:746
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:699
Babysitter implementation details.
ReadStatus
Enumeration for status of a read()
Definition: dbus-spawn.c:68
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
pid_t sitter_pid
PID Of the babysitter.
Definition: dbus-spawn.c:254
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, const char *log_name, char *const *argv, char **env, DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
Definition: dbus-spawn.c:1243
EOF returned.
Definition: dbus-spawn.c:72
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
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:828
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:255
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
int refcount
Reference count.
Definition: dbus-spawn.c:246
unsigned int have_child_status
True if child status has been reaped.
Definition: dbus-spawn.c:267
As in POLLERR (can&#39;t watch for this, but can be present in current state passed to dbus_watch_handle(...
#define TRUE
Expands to "1".
DBusPollable fd
File descriptor.
Definition: dbus-sysdeps.h:373
#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; "something went wrong" - see the error message for more.
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:382
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:888
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_fd_set_close_on_exec(int fd)
Sets the file descriptor to be close on exec.
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:415
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
Definition: dbus-spawn.c:329
#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.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:750
char * log_name
the name under which to log messages about this process being spawned
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
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
unsigned int have_fork_errnum
True if we have an error code from fork()
Definition: dbus-spawn.c:268
#define FALSE
Expands to "0".
int error_pipe_from_child
Connection to the process that does the exec()
Definition: dbus-spawn.c:252
As in POLLIN.
DBusSocket socket_to_babysitter
Connection to the babysitter process.
#define WRITE_END
Helps remember which end of the pipe is which.
Definition: dbus-spawn.c:890
char * _dbus_strdup(const char *str)
Duplicates a string.
int errnum
Error number.
Definition: dbus-spawn.c:265
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:770
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
short revents
Events that occurred.
Definition: dbus-sysdeps.h:375
Some kind of error.
Definition: dbus-spawn.c:71
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:385
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.
Definition: dbus-spawn.c:307