D-Bus 1.14.10
dbus-spawn-unix.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-spawn-unix.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#if defined(DBUS_WIN) || !defined(DBUS_UNIX)
28#error "This file only makes sense on Unix OSs"
29#endif
30
31#include "dbus-spawn.h"
32#include "dbus-sysdeps-unix.h"
33#include "dbus-internals.h"
34#include "dbus-test.h"
35#include "dbus-protocol.h"
36
37#include <unistd.h>
38#include <fcntl.h>
39#include <signal.h>
40#include <sys/wait.h>
41#include <stdio.h>
42#include <stdlib.h>
43#ifdef HAVE_ERRNO_H
44#include <errno.h>
45#endif
46#ifdef HAVE_SYSTEMD
47#ifdef HAVE_SYSLOG_H
48#include <syslog.h>
49#endif
50#include <systemd/sd-journal.h>
51#endif
52
53#if defined(__APPLE__)
54# include <crt_externs.h>
55# define environ (*_NSGetEnviron ())
56#elif !HAVE_DECL_ENVIRON
57extern char **environ;
58#endif
59
64
65/*
66 * I'm pretty sure this whole spawn file could be made simpler,
67 * if you thought about it a bit.
68 */
69
79
80static ReadStatus
81read_ints (int fd,
82 int *buf,
83 int n_ints_in_buf,
84 int *n_ints_read,
85 DBusError *error)
86{
87 size_t bytes = 0;
88 ReadStatus retval;
89
90 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
91
92 retval = READ_STATUS_OK;
93
94 while (TRUE)
95 {
96 ssize_t chunk;
97 size_t to_read;
98
99 to_read = sizeof (int) * n_ints_in_buf - bytes;
100
101 if (to_read == 0)
102 break;
103
104 again:
105
106 chunk = read (fd,
107 ((char*)buf) + bytes,
108 to_read);
109
110 if (chunk < 0 && errno == EINTR)
111 goto again;
112
113 if (chunk < 0)
114 {
115 dbus_set_error (error,
117 "Failed to read from child pipe (%s)",
118 _dbus_strerror (errno));
119
120 retval = READ_STATUS_ERROR;
121 break;
122 }
123 else if (chunk == 0)
124 {
125 retval = READ_STATUS_EOF;
126 break; /* EOF */
127 }
128 else /* chunk > 0 */
129 bytes += chunk;
130 }
131
132 *n_ints_read = (int)(bytes / sizeof(int));
133
134 return retval;
135}
136
137static ReadStatus
138read_pid (int fd,
139 pid_t *buf,
140 DBusError *error)
141{
142 size_t bytes = 0;
143 ReadStatus retval;
144
145 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
146
147 retval = READ_STATUS_OK;
148
149 while (TRUE)
150 {
151 ssize_t chunk;
152 size_t to_read;
153
154 to_read = sizeof (pid_t) - bytes;
155
156 if (to_read == 0)
157 break;
158
159 again:
160
161 chunk = read (fd,
162 ((char*)buf) + bytes,
163 to_read);
164 if (chunk < 0 && errno == EINTR)
165 goto again;
166
167 if (chunk < 0)
168 {
169 dbus_set_error (error,
171 "Failed to read from child pipe (%s)",
172 _dbus_strerror (errno));
173
174 retval = READ_STATUS_ERROR;
175 break;
176 }
177 else if (chunk == 0)
178 {
179 retval = READ_STATUS_EOF;
180 break; /* EOF */
181 }
182 else /* chunk > 0 */
183 bytes += chunk;
184 }
185
186 return retval;
187}
188
189/* The implementation uses an intermediate child between the main process
190 * and the grandchild. The grandchild is our spawned process. The intermediate
191 * child is a babysitter process; it keeps track of when the grandchild
192 * exits/crashes, and reaps the grandchild.
193 *
194 * We automatically reap the babysitter process, killing it if necessary,
195 * when the DBusBabysitter's refcount goes to zero.
196 *
197 * Processes:
198 *
199 * main process
200 * | fork() A
201 * \- babysitter
202 * | fork () B
203 * \- grandchild --> exec --> spawned process
204 *
205 * IPC:
206 * child_err_report_pipe
207 * /-----------<---------<--------------\
208 * | ^
209 * v |
210 * main process babysitter grandchild
211 * ^ ^
212 * v v
213 * \-------<->-------/
214 * babysitter_pipe
215 *
216 * child_err_report_pipe is genuinely a pipe.
217 * The READ_END (also called error_pipe_from_child) is used in the main
218 * process. The WRITE_END (also called child_err_report_fd) is used in
219 * the grandchild process.
220 *
221 * On failure, the grandchild process sends CHILD_EXEC_FAILED + errno.
222 * On success, the pipe just closes (because it's close-on-exec) without
223 * sending any bytes.
224 *
225 * babysitter_pipe is mis-named: it's really a bidirectional socketpair.
226 * The [0] end (also called socket_to_babysitter) is used in the main
227 * process, the [1] end (also called parent_pipe) is used in the babysitter.
228 *
229 * If the fork() labelled B in the diagram above fails, the babysitter sends
230 * CHILD_FORK_FAILED + errno.
231 * On success, the babysitter sends CHILD_PID + the grandchild's pid.
232 * On SIGCHLD, the babysitter sends CHILD_EXITED + the exit status.
233 * The main process doesn't explicitly send anything, but when it exits,
234 * the babysitter gets POLLHUP or POLLERR.
235 */
236
237/* Messages from children to parents */
238enum
239{
240 CHILD_EXITED, /* This message is followed by the exit status int */
241 CHILD_FORK_FAILED, /* Followed by errno */
242 CHILD_EXEC_FAILED, /* Followed by errno */
243 CHILD_PID /* Followed by pid_t */
244};
245
250{
252
253 char *log_name;
255
258
261
263
266
267 DBusBabysitterFinishedFunc finished_cb;
268 void *finished_data;
269
270 int errnum;
271 int status;
272 unsigned int have_child_status : 1;
273 unsigned int have_fork_errnum : 1;
274 unsigned int have_exec_errnum : 1;
275};
276
277static DBusBabysitter*
278_dbus_babysitter_new (void)
279{
280 DBusBabysitter *sitter;
281
282 sitter = dbus_new0 (DBusBabysitter, 1);
283 if (sitter == NULL)
284 return NULL;
285
286 sitter->refcount = 1;
287
288 sitter->socket_to_babysitter.fd = -1;
289 sitter->error_pipe_from_child = -1;
290
291 sitter->sitter_pid = -1;
292 sitter->grandchild_pid = -1;
293
294 sitter->watches = _dbus_watch_list_new ();
295 if (sitter->watches == NULL)
296 goto failed;
297
298 return sitter;
299
300 failed:
301 _dbus_babysitter_unref (sitter);
302 return NULL;
303}
304
313{
314 _dbus_assert (sitter != NULL);
315 _dbus_assert (sitter->refcount > 0);
316
317 sitter->refcount += 1;
318
319 return sitter;
320}
321
322static void close_socket_to_babysitter (DBusBabysitter *sitter);
323static void close_error_pipe_from_child (DBusBabysitter *sitter);
324
333void
335{
336 _dbus_assert (sitter != NULL);
337 _dbus_assert (sitter->refcount > 0);
338
339 sitter->refcount -= 1;
340 if (sitter->refcount == 0)
341 {
342 /* If we haven't forked other babysitters
343 * since this babysitter and socket were
344 * created then this close will cause the
345 * babysitter to wake up from poll with
346 * a hangup and then the babysitter will
347 * quit itself.
348 */
349 close_socket_to_babysitter (sitter);
350
351 close_error_pipe_from_child (sitter);
352
353 if (sitter->sitter_pid > 0)
354 {
355 int status;
356 int ret;
357
358 /* It's possible the babysitter died on its own above
359 * from the close, or was killed randomly
360 * by some other process, so first try to reap it
361 */
362 ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
363
364 /* If we couldn't reap the child then kill it, and
365 * try again
366 */
367 if (ret == 0)
368 kill (sitter->sitter_pid, SIGKILL);
369
370 if (ret == 0)
371 {
372 do
373 {
374 ret = waitpid (sitter->sitter_pid, &status, 0);
375 }
376 while (_DBUS_UNLIKELY (ret < 0 && errno == EINTR));
377 }
378
379 if (ret < 0)
380 {
381 if (errno == ECHILD)
382 _dbus_warn ("Babysitter process not available to be reaped; should not happen");
383 else
384 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s",
385 errno, _dbus_strerror (errno));
386 }
387 else
388 {
389 _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
390 (long) ret, (long) sitter->sitter_pid);
391
392 if (WIFEXITED (sitter->status))
393 _dbus_verbose ("Babysitter exited with status %d\n",
394 WEXITSTATUS (sitter->status));
395 else if (WIFSIGNALED (sitter->status))
396 _dbus_verbose ("Babysitter received signal %d\n",
397 WTERMSIG (sitter->status));
398 else
399 _dbus_verbose ("Babysitter exited abnormally\n");
400 }
401
402 sitter->sitter_pid = -1;
403 }
404
405 if (sitter->watches)
407
408 dbus_free (sitter->log_name);
409
410 dbus_free (sitter);
411 }
412}
413
414static ReadStatus
415read_data (DBusBabysitter *sitter,
416 int fd)
417{
418 int what;
419 int got;
421 ReadStatus r;
422
423 r = read_ints (fd, &what, 1, &got, &error);
424
425 switch (r)
426 {
428 _dbus_warn ("Failed to read data from fd %d: %s", fd, error.message);
429 dbus_error_free (&error);
430 return r;
431
432 case READ_STATUS_EOF:
433 return r;
434
435 case READ_STATUS_OK:
436 break;
437
438 default:
439 _dbus_assert_not_reached ("invalid ReadStatus");
440 break;
441 }
442
443 if (got == 1)
444 {
445 switch (what)
446 {
447 case CHILD_EXITED:
448 case CHILD_FORK_FAILED:
449 case CHILD_EXEC_FAILED:
450 {
451 int arg;
452
453 r = read_ints (fd, &arg, 1, &got, &error);
454
455 switch (r)
456 {
458 _dbus_warn ("Failed to read arg from fd %d: %s", fd, error.message);
459 dbus_error_free (&error);
460 return r;
461 case READ_STATUS_EOF:
462 return r;
463 case READ_STATUS_OK:
464 break;
465 default:
466 _dbus_assert_not_reached ("invalid ReadStatus");
467 break;
468 }
469
470 if (got == 1)
471 {
472 if (what == CHILD_EXITED)
473 {
474 /* Do not reset sitter->errnum to 0 here. We get here if
475 * the babysitter reports that the grandchild process has
476 * exited, and there are two ways that can happen:
477 *
478 * 1. grandchild successfully exec()s the desired process,
479 * but then the desired process exits or is terminated
480 * by a signal. The babysitter observes this and reports
481 * CHILD_EXITED.
482 *
483 * 2. grandchild fails to exec() the desired process,
484 * attempts to report the exec() failure (which
485 * we will receive as CHILD_EXEC_FAILED), and then
486 * exits itself (which will prompt the babysitter to
487 * send CHILD_EXITED). We want the CHILD_EXEC_FAILED
488 * to take precedence (and have its errno logged),
489 * which _dbus_babysitter_set_child_exit_error() does.
490 */
491 sitter->have_child_status = TRUE;
492 sitter->status = arg;
493 _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
494 WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
495 WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
496 }
497 else if (what == CHILD_FORK_FAILED)
498 {
499 sitter->have_fork_errnum = TRUE;
500 sitter->errnum = arg;
501 _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
502 }
503 else if (what == CHILD_EXEC_FAILED)
504 {
505 sitter->have_exec_errnum = TRUE;
506 sitter->errnum = arg;
507 _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
508 }
509 }
510 }
511 break;
512 case CHILD_PID:
513 {
514 pid_t pid = -1;
515
516 r = read_pid (fd, &pid, &error);
517
518 switch (r)
519 {
521 _dbus_warn ("Failed to read PID from fd %d: %s", fd, error.message);
522 dbus_error_free (&error);
523 return r;
524 case READ_STATUS_EOF:
525 return r;
526 case READ_STATUS_OK:
527 break;
528 default:
529 _dbus_assert_not_reached ("invalid ReadStatus");
530 break;
531 }
532
533 sitter->grandchild_pid = pid;
534
535 _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
536 }
537 break;
538 default:
539 _dbus_warn ("Unknown message received from babysitter process");
540 break;
541 }
542 }
543
544 return r;
545}
546
547static void
548close_socket_to_babysitter (DBusBabysitter *sitter)
549{
550 _dbus_verbose ("Closing babysitter\n");
551
552 if (sitter->sitter_watch != NULL)
553 {
554 _dbus_assert (sitter->watches != NULL);
560 sitter->sitter_watch = NULL;
561 }
562
563 if (sitter->socket_to_babysitter.fd >= 0)
564 {
566 sitter->socket_to_babysitter.fd = -1;
567 }
568}
569
570static void
571close_error_pipe_from_child (DBusBabysitter *sitter)
572{
573 _dbus_verbose ("Closing child error\n");
574
575 if (sitter->error_watch != NULL)
576 {
577 _dbus_assert (sitter->watches != NULL);
583 sitter->error_watch = NULL;
584 }
585
586 if (sitter->error_pipe_from_child >= 0)
587 {
589 sitter->error_pipe_from_child = -1;
590 }
591}
592
593static void
594handle_babysitter_socket (DBusBabysitter *sitter,
595 int revents)
596{
597 /* Even if we have POLLHUP, we want to keep reading
598 * data until POLLIN goes away; so this function only
599 * looks at HUP/ERR if no IN is set.
600 */
601 if (revents & _DBUS_POLLIN)
602 {
603 _dbus_verbose ("Reading data from babysitter\n");
604 if (read_data (sitter, sitter->socket_to_babysitter.fd) != READ_STATUS_OK)
605 close_socket_to_babysitter (sitter);
606 }
607 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
608 {
609 close_socket_to_babysitter (sitter);
610 }
611}
612
613static void
614handle_error_pipe (DBusBabysitter *sitter,
615 int revents)
616{
617 if (revents & _DBUS_POLLIN)
618 {
619 _dbus_verbose ("Reading data from child error\n");
620 if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
621 close_error_pipe_from_child (sitter);
622 }
623 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
624 {
625 close_error_pipe_from_child (sitter);
626 }
627}
628
629/* returns whether there were any poll events handled */
630static dbus_bool_t
631babysitter_iteration (DBusBabysitter *sitter,
632 dbus_bool_t block)
633{
634 DBusPollFD fds[2];
635 int i;
636 dbus_bool_t descriptors_ready;
637
638 descriptors_ready = FALSE;
639
640 i = 0;
641
642 if (sitter->error_pipe_from_child >= 0)
643 {
644 fds[i].fd = sitter->error_pipe_from_child;
645 fds[i].events = _DBUS_POLLIN;
646 fds[i].revents = 0;
647 ++i;
648 }
649
650 if (sitter->socket_to_babysitter.fd >= 0)
651 {
652 fds[i].fd = sitter->socket_to_babysitter.fd;
653 fds[i].events = _DBUS_POLLIN;
654 fds[i].revents = 0;
655 ++i;
656 }
657
658 if (i > 0)
659 {
660 int ret;
661
662 do
663 {
664 ret = _dbus_poll (fds, i, 0);
665 }
666 while (ret < 0 && errno == EINTR);
667
668 if (ret == 0 && block)
669 {
670 do
671 {
672 ret = _dbus_poll (fds, i, -1);
673 }
674 while (ret < 0 && errno == EINTR);
675 }
676
677 if (ret > 0)
678 {
679 descriptors_ready = TRUE;
680
681 while (i > 0)
682 {
683 --i;
684 if (fds[i].fd == sitter->error_pipe_from_child)
685 handle_error_pipe (sitter, fds[i].revents);
686 else if (fds[i].fd == sitter->socket_to_babysitter.fd)
687 handle_babysitter_socket (sitter, fds[i].revents);
688 }
689 }
690 }
691
692 return descriptors_ready;
693}
694
699#define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter.fd >= 0 || (sitter)->error_pipe_from_child >= 0)
700
707void
709{
710 /* be sure we have the PID of the child */
711 while (LIVE_CHILDREN (sitter) &&
712 sitter->grandchild_pid == -1)
713 babysitter_iteration (sitter, TRUE);
714
715 _dbus_verbose ("Got child PID %ld for killing\n",
716 (long) sitter->grandchild_pid);
717
718 if (sitter->grandchild_pid == -1)
719 return; /* child is already dead, or we're so hosed we'll never recover */
720
721 kill (sitter->grandchild_pid, SIGKILL);
722}
723
731{
732
733 /* Be sure we're up-to-date */
734 while (LIVE_CHILDREN (sitter) &&
735 babysitter_iteration (sitter, FALSE))
736 ;
737
738 /* We will have exited the babysitter when the child has exited */
739 return sitter->socket_to_babysitter.fd < 0;
740}
741
756 int *status)
757{
759 _dbus_assert_not_reached ("Child has not exited");
760
761 if (!sitter->have_child_status ||
762 !(WIFEXITED (sitter->status)))
763 return FALSE;
764
765 *status = WEXITSTATUS (sitter->status);
766 return TRUE;
767}
768
778void
780 DBusError *error)
781{
783 return;
784
785 /* Note that if exec fails, we will also get a child status
786 * from the babysitter saying the child exited,
787 * so we need to give priority to the exec error
788 */
789 if (sitter->have_exec_errnum)
790 {
792 "Failed to execute program %s: %s",
793 sitter->log_name, _dbus_strerror (sitter->errnum));
794 }
795 else if (sitter->have_fork_errnum)
796 {
798 "Failed to fork a new process %s: %s",
799 sitter->log_name, _dbus_strerror (sitter->errnum));
800 }
801 else if (sitter->have_child_status)
802 {
803 if (WIFEXITED (sitter->status))
805 "Process %s exited with status %d",
806 sitter->log_name, WEXITSTATUS (sitter->status));
807 else if (WIFSIGNALED (sitter->status))
809 "Process %s received signal %d",
810 sitter->log_name, WTERMSIG (sitter->status));
811 else
813 "Process %s exited abnormally",
814 sitter->log_name);
815 }
816 else
817 {
819 "Process %s exited, reason unknown",
820 sitter->log_name);
821 }
822}
823
838 DBusAddWatchFunction add_function,
839 DBusRemoveWatchFunction remove_function,
840 DBusWatchToggledFunction toggled_function,
841 void *data,
842 DBusFreeFunction free_data_function)
843{
845 add_function,
846 remove_function,
847 toggled_function,
848 data,
849 free_data_function);
850}
851
852static dbus_bool_t
853handle_watch (DBusWatch *watch,
854 unsigned int condition,
855 void *data)
856{
857 DBusBabysitter *sitter = _dbus_babysitter_ref (data);
858 int revents;
859 int fd;
860
861 revents = 0;
862 if (condition & DBUS_WATCH_READABLE)
863 revents |= _DBUS_POLLIN;
864 if (condition & DBUS_WATCH_ERROR)
865 revents |= _DBUS_POLLERR;
866 if (condition & DBUS_WATCH_HANGUP)
867 revents |= _DBUS_POLLHUP;
868
869 fd = dbus_watch_get_socket (watch);
870
871 if (fd == sitter->error_pipe_from_child)
872 handle_error_pipe (sitter, revents);
873 else if (fd == sitter->socket_to_babysitter.fd)
874 handle_babysitter_socket (sitter, revents);
875
876 while (LIVE_CHILDREN (sitter) &&
877 babysitter_iteration (sitter, FALSE))
878 ;
879
880 /* fd.o #32992: if the handle_* methods closed their sockets, they previously
881 * didn't always remove the watches. Check that we don't regress. */
882 _dbus_assert (sitter->socket_to_babysitter.fd != -1 || sitter->sitter_watch == NULL);
883 _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
884
886 sitter->finished_cb != NULL)
887 {
888 sitter->finished_cb (sitter, sitter->finished_data);
889 sitter->finished_cb = NULL;
890 }
891
892 _dbus_babysitter_unref (sitter);
893 return TRUE;
894}
895
897#define READ_END 0
899#define WRITE_END 1
900
901
902/* Avoids a danger in re-entrant situations (calling close()
903 * on a file descriptor twice, and another module has
904 * re-opened it since the first close).
905 *
906 * This previously claimed to be relevant for threaded situations, but by
907 * trivial inspection, it is not thread-safe. It doesn't actually
908 * matter, since this module is only used in the -util variant of the
909 * library, which is only used in single-threaded situations.
910 */
911static int
912close_and_invalidate (int *fd)
913{
914 int ret;
915
916 if (*fd < 0)
917 return -1;
918 else
919 {
920 ret = _dbus_close (*fd, NULL);
921 *fd = -1;
922 }
923
924 return ret;
925}
926
927static dbus_bool_t
928make_pipe (int p[2],
929 DBusError *error)
930{
931 int retval;
932
933#ifdef HAVE_PIPE2
934 dbus_bool_t cloexec_done;
935
936 retval = pipe2 (p, O_CLOEXEC);
937 cloexec_done = retval >= 0;
938
939 /* Check if kernel seems to be too old to know pipe2(). We assume
940 that if pipe2 is available, O_CLOEXEC is too. */
941 if (retval < 0 && errno == ENOSYS)
942#endif
943 {
944 retval = pipe(p);
945 }
946
947 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
948
949 if (retval < 0)
950 {
951 dbus_set_error (error,
953 "Failed to create pipe for communicating with child process (%s)",
954 _dbus_strerror (errno));
955 return FALSE;
956 }
957
958#ifdef HAVE_PIPE2
959 if (!cloexec_done)
960#endif
961 {
964 }
965
966 return TRUE;
967}
968
969static void
970do_write (int fd, const void *buf, size_t count)
971{
972 size_t bytes_written;
973 int ret;
974
975 bytes_written = 0;
976
977 again:
978
979 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
980
981 if (ret < 0)
982 {
983 if (errno == EINTR)
984 goto again;
985 else
986 {
987 _dbus_warn ("Failed to write data to pipe!");
988 exit (1); /* give up, we suck */
989 }
990 }
991 else
992 bytes_written += ret;
993
994 if (bytes_written < count)
995 goto again;
996}
997
998static void write_err_and_exit (int fd, int msg) _DBUS_GNUC_NORETURN;
999
1000static void
1001write_err_and_exit (int fd, int msg)
1002{
1003 int en = errno;
1004
1005 do_write (fd, &msg, sizeof (msg));
1006 do_write (fd, &en, sizeof (en));
1007
1008 exit (1);
1009}
1010
1011static void
1012write_pid (int fd, pid_t pid)
1013{
1014 int msg = CHILD_PID;
1015
1016 do_write (fd, &msg, sizeof (msg));
1017 do_write (fd, &pid, sizeof (pid));
1018}
1019
1020static void write_status_and_exit (int fd, int status) _DBUS_GNUC_NORETURN;
1021
1022static void
1023write_status_and_exit (int fd, int status)
1024{
1025 int msg = CHILD_EXITED;
1026
1027 do_write (fd, &msg, sizeof (msg));
1028 do_write (fd, &status, sizeof (status));
1029
1030 exit (0);
1031}
1032
1033static void do_exec (int child_err_report_fd,
1034 char * const *argv,
1035 char * const *envp,
1036 DBusSpawnChildSetupFunc child_setup,
1037 void *user_data) _DBUS_GNUC_NORETURN;
1038
1039static void
1040do_exec (int child_err_report_fd,
1041 char * const *argv,
1042 char * const *envp,
1043 DBusSpawnChildSetupFunc child_setup,
1044 void *user_data)
1045{
1046#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1047 int i, max_open;
1048#endif
1049
1050 _dbus_verbose_reset ();
1051 _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
1052 _dbus_getpid ());
1053
1054 if (child_setup)
1055 (* child_setup) (user_data);
1056
1057#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1058 max_open = sysconf (_SC_OPEN_MAX);
1059
1060 for (i = 3; i < max_open; i++)
1061 {
1062 int retval;
1063
1064 if (i == child_err_report_fd)
1065 continue;
1066
1067 retval = fcntl (i, F_GETFD);
1068
1069 if (retval != -1 && !(retval & FD_CLOEXEC))
1070 {
1071 char description[256] = { 0 };
1072 char proc_self_fd[256] = { 0 };
1073 size_t description_length = sizeof (description) - 1;
1074
1075 snprintf (proc_self_fd, sizeof (proc_self_fd) - 1,
1076 "/proc/self/fd/%d", i);
1077 proc_self_fd[sizeof (proc_self_fd) - 1] = '\0';
1078
1079 if (readlink (proc_self_fd, description, description_length) <= 0)
1080 snprintf (description, sizeof (description) - 1, "(unknown)");
1081
1082 description[sizeof (description) - 1] = '\0';
1083 _dbus_warn ("Fd %d \"%s\" did not have the close-on-exec flag set!",
1084 i, description);
1085 }
1086 }
1087#endif
1088
1089 if (envp == NULL)
1090 {
1091 _dbus_assert (environ != NULL);
1092
1093 envp = environ;
1094 }
1095
1096 execve (argv[0], argv, envp);
1097
1098 /* Exec failed */
1099 write_err_and_exit (child_err_report_fd,
1100 CHILD_EXEC_FAILED);
1101}
1102
1103static void
1104check_babysit_events (pid_t grandchild_pid,
1105 int parent_pipe,
1106 int revents)
1107{
1108 pid_t ret;
1109 int status;
1110
1111 do
1112 {
1113 ret = waitpid (grandchild_pid, &status, WNOHANG);
1114 /* The man page says EINTR can't happen with WNOHANG,
1115 * but there are reports of it (maybe only with valgrind?)
1116 */
1117 }
1118 while (ret < 0 && errno == EINTR);
1119
1120 if (ret == 0)
1121 {
1122 _dbus_verbose ("no child exited\n");
1123
1124 ; /* no child exited */
1125 }
1126 else if (ret < 0)
1127 {
1128 /* This isn't supposed to happen. */
1129 _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s",
1130 _dbus_strerror (errno));
1131 exit (1);
1132 }
1133 else if (ret == grandchild_pid)
1134 {
1135 /* Child exited */
1136 _dbus_verbose ("reaped child pid %ld\n", (long) ret);
1137
1138 write_status_and_exit (parent_pipe, status);
1139 }
1140 else
1141 {
1142 _dbus_warn ("waitpid() reaped pid %d that we've never heard of",
1143 (int) ret);
1144 exit (1);
1145 }
1146
1147 if (revents & _DBUS_POLLIN)
1148 {
1149 _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
1150 }
1151
1152 if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
1153 {
1154 /* Parent is gone, so we just exit */
1155 _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
1156 exit (0);
1157 }
1158}
1159
1160/* Only used in a single-threaded child process, does not need to be
1161 * thread-safe */
1162static int babysit_sigchld_pipe = -1;
1163
1164static void
1165babysit_signal_handler (int signo)
1166{
1167 /* Signal handlers that might set errno must save and restore the errno
1168 * that the interrupted function might have been relying on. */
1169 int saved_errno = errno;
1170 char b = '\0';
1171
1172 again:
1173 if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1174 if (errno == EINTR)
1175 goto again;
1176
1177 errno = saved_errno;
1178}
1179
1180static void babysit (pid_t grandchild_pid,
1181 int parent_pipe) _DBUS_GNUC_NORETURN;
1182
1183static void
1184babysit (pid_t grandchild_pid,
1185 int parent_pipe)
1186{
1187 int sigchld_pipe[2];
1188
1189 /* We don't exec, so we keep parent state, such as the pid that
1190 * _dbus_verbose() uses. Reset the pid here.
1191 */
1192 _dbus_verbose_reset ();
1193
1194 /* I thought SIGCHLD would just wake up the poll, but
1195 * that didn't seem to work, so added this pipe.
1196 * Probably the pipe is more likely to work on busted
1197 * operating systems anyhow.
1198 */
1199 if (pipe (sigchld_pipe) < 0)
1200 {
1201 _dbus_warn ("Not enough file descriptors to create pipe in babysitter process");
1202 exit (1);
1203 }
1204
1205 babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1206
1207 _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1208
1209 write_pid (parent_pipe, grandchild_pid);
1210
1211 check_babysit_events (grandchild_pid, parent_pipe, 0);
1212
1213 while (TRUE)
1214 {
1215 DBusPollFD pfds[2];
1216
1217 pfds[0].fd = parent_pipe;
1218 pfds[0].events = _DBUS_POLLIN;
1219 pfds[0].revents = 0;
1220
1221 pfds[1].fd = sigchld_pipe[READ_END];
1222 pfds[1].events = _DBUS_POLLIN;
1223 pfds[1].revents = 0;
1224
1225 if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1226 {
1227 _dbus_warn ("_dbus_poll() error: %s", strerror (errno));
1228 exit (1);
1229 }
1230
1231 if (pfds[0].revents != 0)
1232 {
1233 check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1234 }
1235 else if (pfds[1].revents & _DBUS_POLLIN)
1236 {
1237 char b;
1238 if (read (sigchld_pipe[READ_END], &b, 1) == -1)
1239 {
1240 /* ignore */
1241 }
1242 /* do waitpid check */
1243 check_babysit_events (grandchild_pid, parent_pipe, 0);
1244 }
1245 }
1246
1247 exit (1);
1248}
1249
1276 const char *log_name,
1277 char * const *argv,
1278 char * const *env,
1279 DBusSpawnFlags flags,
1280 DBusSpawnChildSetupFunc child_setup,
1281 void *user_data,
1282 DBusError *error)
1283{
1284 DBusBabysitter *sitter;
1285 int child_err_report_pipe[2] = { -1, -1 };
1286 DBusSocket babysitter_pipe[2] = { DBUS_SOCKET_INIT, DBUS_SOCKET_INIT };
1287 pid_t pid;
1288 int fd_out = -1;
1289 int fd_err = -1;
1290
1291 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1292 _dbus_assert (argv[0] != NULL);
1293
1294 if (sitter_p != NULL)
1295 *sitter_p = NULL;
1296
1297 sitter = NULL;
1298
1299 sitter = _dbus_babysitter_new ();
1300 if (sitter == NULL)
1301 {
1303 return FALSE;
1304 }
1305
1306 sitter->log_name = _dbus_strdup (log_name);
1307 if (sitter->log_name == NULL && log_name != NULL)
1308 {
1310 goto cleanup_and_fail;
1311 }
1312
1313 if (sitter->log_name == NULL)
1314 sitter->log_name = _dbus_strdup (argv[0]);
1315
1316 if (sitter->log_name == NULL)
1317 {
1319 goto cleanup_and_fail;
1320 }
1321
1322 if (!make_pipe (child_err_report_pipe, error))
1323 goto cleanup_and_fail;
1324
1325 if (!_dbus_socketpair (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1326 goto cleanup_and_fail;
1327
1328 /* Setting up the babysitter is only useful in the parent,
1329 * but we don't want to run out of memory and fail
1330 * after we've already forked, since then we'd leak
1331 * child processes everywhere.
1332 */
1333 sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1335 TRUE, handle_watch, sitter, NULL);
1336 if (sitter->error_watch == NULL)
1337 {
1339 goto cleanup_and_fail;
1340 }
1341
1342 _dbus_watch_ref (sitter->error_watch);
1343
1344 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1345 {
1346 /* we need to free it early so the destructor won't try to remove it
1347 * without it having been added, which DBusLoop doesn't allow */
1351 sitter->error_watch = NULL;
1352
1354 goto cleanup_and_fail;
1355 }
1356
1358
1359 sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0].fd,
1361 TRUE, handle_watch, sitter, NULL);
1362 if (sitter->sitter_watch == NULL)
1363 {
1365 goto cleanup_and_fail;
1366 }
1367
1368 _dbus_watch_ref (sitter->sitter_watch);
1369
1370 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1371 {
1372 /* we need to free it early so the destructor won't try to remove it
1373 * without it having been added, which DBusLoop doesn't allow */
1377 sitter->sitter_watch = NULL;
1378
1380 goto cleanup_and_fail;
1381 }
1382
1384
1385 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1386
1387 if (flags & DBUS_SPAWN_SILENCE_OUTPUT)
1388 {
1389 fd_out = open ("/dev/null", O_RDONLY);
1390
1391 if (fd_out < 0)
1392 {
1393 dbus_set_error (error, _dbus_error_from_errno (errno),
1394 "Failed to open /dev/null: %s",
1395 _dbus_strerror (errno));
1396 goto cleanup_and_fail;
1397 }
1398
1400
1401 fd_err = _dbus_dup (fd_out, error);
1402
1403 if (fd_err < 0)
1404 goto cleanup_and_fail;
1405 }
1406#ifdef HAVE_SYSTEMD
1407 else if (flags & DBUS_SPAWN_REDIRECT_OUTPUT)
1408 {
1409 /* This may fail, but it's not critical.
1410 * In particular, if we were compiled with journald support but are now
1411 * running on a non-systemd system, this is going to fail, so we
1412 * have to cope gracefully. */
1413 fd_out = sd_journal_stream_fd (sitter->log_name, LOG_INFO, FALSE);
1414 fd_err = sd_journal_stream_fd (sitter->log_name, LOG_WARNING, FALSE);
1415 }
1416#endif
1417
1418 /* Make sure our output buffers aren't redundantly printed by both the
1419 * parent and the child */
1420 fflush (stdout);
1421 fflush (stderr);
1422
1423 pid = fork ();
1424
1425 if (pid < 0)
1426 {
1427 dbus_set_error (error,
1429 "Failed to fork (%s)",
1430 _dbus_strerror (errno));
1431 goto cleanup_and_fail;
1432 }
1433 else if (pid == 0)
1434 {
1435 /* Immediate child, this is the babysitter process. */
1436 int grandchild_pid;
1437
1438 /* Be sure we crash if the parent exits
1439 * and we write to the err_report_pipe
1440 */
1441 signal (SIGPIPE, SIG_DFL);
1442
1443 /* Close the parent's end of the pipes. */
1444 close_and_invalidate (&child_err_report_pipe[READ_END]);
1445 close_and_invalidate (&babysitter_pipe[0].fd);
1446
1447 fflush (stdout);
1448 fflush (stderr);
1449
1450 /* Create the child that will exec () */
1451 grandchild_pid = fork ();
1452
1453 if (grandchild_pid < 0)
1454 {
1455 write_err_and_exit (babysitter_pipe[1].fd,
1456 CHILD_FORK_FAILED);
1457 _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1458 }
1459 else if (grandchild_pid == 0)
1460 {
1461 /* This might not succeed in a dbus-daemon that started as root
1462 * and dropped privileges, so don't log an error on failure.
1463 * (Also, we can't safely log errors here anyway, because logging
1464 * is not async-signal safe). */
1466
1467 /* Go back to ignoring SIGPIPE, since it's evil
1468 */
1469 signal (SIGPIPE, SIG_IGN);
1470
1471 close_and_invalidate (&babysitter_pipe[1].fd);
1472
1473 /* Redirect stdout, stderr to systemd Journal or /dev/null
1474 * as requested, if possible */
1475 if (fd_out >= 0)
1476 dup2 (fd_out, STDOUT_FILENO);
1477 if (fd_err >= 0)
1478 dup2 (fd_err, STDERR_FILENO);
1479 close_and_invalidate (&fd_out);
1480 close_and_invalidate (&fd_err);
1481
1482 do_exec (child_err_report_pipe[WRITE_END],
1483 argv,
1484 env,
1485 child_setup, user_data);
1486 _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1487 }
1488 else
1489 {
1490 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1491 close_and_invalidate (&fd_out);
1492 close_and_invalidate (&fd_err);
1493 babysit (grandchild_pid, babysitter_pipe[1].fd);
1494 _dbus_assert_not_reached ("Got to code after babysit()");
1495 }
1496 }
1497 else
1498 {
1499 /* Close the uncared-about ends of the pipes */
1500 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1501 close_and_invalidate (&babysitter_pipe[1].fd);
1502 close_and_invalidate (&fd_out);
1503 close_and_invalidate (&fd_err);
1504
1505 sitter->socket_to_babysitter = babysitter_pipe[0];
1506 babysitter_pipe[0].fd = -1;
1507
1508 sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1509 child_err_report_pipe[READ_END] = -1;
1510
1511 sitter->sitter_pid = pid;
1512
1513 if (sitter_p != NULL)
1514 *sitter_p = sitter;
1515 else
1516 _dbus_babysitter_unref (sitter);
1517
1518 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1519
1520 return TRUE;
1521 }
1522
1523 cleanup_and_fail:
1524
1525 _DBUS_ASSERT_ERROR_IS_SET (error);
1526
1527 close_and_invalidate (&child_err_report_pipe[READ_END]);
1528 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1529 close_and_invalidate (&babysitter_pipe[0].fd);
1530 close_and_invalidate (&babysitter_pipe[1].fd);
1531 close_and_invalidate (&fd_out);
1532 close_and_invalidate (&fd_err);
1533
1534 if (sitter != NULL)
1535 _dbus_babysitter_unref (sitter);
1536
1537 return FALSE;
1538}
1539
1540void
1541_dbus_babysitter_set_result_function (DBusBabysitter *sitter,
1542 DBusBabysitterFinishedFunc finished,
1543 void *user_data)
1544{
1545 sitter->finished_cb = finished;
1546 sitter->finished_data = user_data;
1547}
1548
1550
1551void
1552_dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1553{
1554 while (LIVE_CHILDREN (sitter))
1555 babysitter_iteration (sitter, TRUE);
1556}
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition dbus-errors.h:62
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
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...
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_babysitter_get_child_exit_status(DBusBabysitter *sitter, int *status)
Gets the exit status of the child.
#define READ_END
Helps remember which end of the pipe is which.
#define WRITE_END
Helps remember which end of the pipe is which.
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
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.
char * _dbus_strdup(const char *str)
Duplicates a string.
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,...
ReadStatus
Enumeration for status of a read()
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
void _dbus_babysitter_kill_child(DBusBabysitter *sitter)
Blocks until the babysitter process gives us the PID of the spawned grandchild, then kills the spawne...
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, const char *log_name, char *const *argv, char *const *env, DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
#define LIVE_CHILDREN(sitter)
Macro returns TRUE if the babysitter still has live sockets open to the babysitter child or the grand...
@ READ_STATUS_OK
Read succeeded.
@ READ_STATUS_EOF
EOF returned.
@ READ_STATUS_ERROR
Some kind of error.
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:49
#define TRUE
Expands to "1".
Definition dbus-macros.h:39
#define FALSE
Expands to "0".
Definition dbus-macros.h:42
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition dbus-memory.h:63
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:58
#define DBUS_ERROR_SPAWN_CHILD_EXITED
While starting a new process, the child exited with a status code.
#define DBUS_ERROR_SPAWN_CHILD_SIGNALED
While starting a new process, the child exited on a signal.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_SPAWN_EXEC_FAILED
While starting a new process, the exec() call failed.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define DBUS_ERROR_SPAWN_FAILED
While starting a new process, something went wrong.
#define DBUS_ERROR_SPAWN_FORK_FAILED
While starting a new process, the fork() call failed.
dbus_bool_t _dbus_reset_oom_score_adj(const char **error_str_p)
If the current process has been protected from the Linux OOM killer (the oom_score_adj process parame...
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
int _dbus_dup(int fd, DBusError *error)
Duplicates a file descriptor.
void _dbus_fd_set_close_on_exec(int fd)
Sets the file descriptor to be close on exec.
#define _DBUS_POLLERR
Error condition.
dbus_bool_t _dbus_socketpair(DBusSocket *fd1, DBusSocket *fd2, dbus_bool_t blocking, DBusError *error)
Creates pair of connect sockets (as in socketpair()).
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
#define _DBUS_POLLHUP
Hung up.
#define _DBUS_POLLIN
There is data to read.
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:35
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:381
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition dbus-watch.c:232
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition dbus-watch.c:249
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:295
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
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
Definition dbus-watch.c:124
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition dbus-watch.c:138
void _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:414
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_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:593
Babysitter implementation details.
DBusWatch * sitter_watch
Sitter pipe watch.
unsigned int have_exec_errnum
True if we have an error code from exec()
DBusSocket socket_to_babysitter
Connection to the babysitter process.
int status
Exit status code.
unsigned int have_child_status
True if child status has been reaped.
char * log_name
the name under which to log messages about this process being spawned
pid_t sitter_pid
PID Of the babysitter.
int errnum
Error number.
int error_pipe_from_child
Connection to the process that does the exec()
int refcount
Reference count.
DBusWatchList * watches
Watches.
DBusWatch * error_watch
Error pipe watch.
pid_t grandchild_pid
PID of the grandchild.
unsigned int have_fork_errnum
True if we have an error code from fork()
Object representing an exception.
Definition dbus-errors.h:49
const char * message
public error message field
Definition dbus-errors.h:51
short events
Events to poll for.
short revents
Events that occurred.
DBusPollable fd
File descriptor.
Socket interface.
DBusWatchList implementation details.
Definition dbus-watch.c:215
Implementation of DBusWatch.
Definition dbus-watch.c:41