+
+ /*
+ * Reset the dead_children flag...
+ */
+
+ dead_children = 0;
+
+ /*
+ * Collect the exit status of some children...
+ */
+
+#ifdef HAVE_WAITPID
+ while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
+#elif defined(HAVE_WAIT3)
+ while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
+#else
+ if ((pid = wait(&status)) > 0)
+#endif /* HAVE_WAITPID */
+ {
+ if (status == SIGTERM)
+ status = 0;
+
+ for (i = num_backends, backend = backends; i > 0; i --, backend ++)
+ if (backend->pid == pid)
+ break;
+
+ if (i > 0)
+ {
+ name = backend->name;
+ backend->pid = 0;
+ backend->status = status;
+
+ active_backends --;
+ }
+ else
+ name = "Unknown";
+
+ if (status)
+ {
+ if (WIFEXITED(status))
+ fprintf(stderr,
+ "ERROR: [cups-deviced] PID %d (%s) stopped with status %d!\n",
+ pid, name, WEXITSTATUS(status));
+ else
+ fprintf(stderr,
+ "ERROR: [cups-deviced] PID %d (%s) crashed on signal %d!\n",
+ pid, name, WTERMSIG(status));
+ }
+ else
+ fprintf(stderr,
+ "DEBUG: [cups-deviced] PID %d (%s) exited with no errors.\n",
+ pid, name);
+ }
+}
+
+
+/*
+ * 'sigchld_handler()' - Handle 'child' signals from old processes.
+ */
+
+static void
+sigchld_handler(int sig) /* I - Signal number */
+{
+ (void)sig;
+
+ /*
+ * Flag that we have dead children...
+ */
+
+ dead_children = 1;
+
+ /*
+ * Reset the signal handler as needed...
+ */
+
+#if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
+ signal(SIGCLD, sigchld_handler);
+#endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
+}
+
+
+/*
+ * 'start_backend()' - Run a backend to gather the available devices.
+ */
+
+static int /* O - 0 on success, -1 on error */
+start_backend(const char *name, /* I - Backend to run */
+ int root) /* I - Run as root? */
+{
+ const char *server_bin; /* CUPS_SERVERBIN environment variable */
+ char program[1024]; /* Full path to backend */
+ cupsd_backend_t *backend; /* Current backend */
+ char *argv[2]; /* Command-line arguments */
+
+
+ if (num_backends >= MAX_BACKENDS)
+ {
+ fprintf(stderr, "ERROR: Too many backends (%d)!\n", num_backends);
+ return (-1);
+ }
+
+ if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
+ server_bin = CUPS_SERVERBIN;
+
+ snprintf(program, sizeof(program), "%s/backend/%s", server_bin, name);
+
+ if (_cupsFileCheck(program, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
+ _cupsFileCheckFilter, NULL))
+ return (-1);
+
+ backend = backends + num_backends;
+
+ argv[0] = (char *)name;
+ argv[1] = NULL;
+
+ if ((backend->pipe = cupsdPipeCommand(&(backend->pid), program, argv,
+ root ? 0 : normal_user)) == NULL)
+ {
+ fprintf(stderr, "ERROR: [cups-deviced] Unable to execute \"%s\" - %s\n",
+ program, strerror(errno));
+ return (-1);
+ }
+
+ /*
+ * Fill in the rest of the backend information...
+ */
+
+ fprintf(stderr, "DEBUG: [cups-deviced] Started backend %s (PID %d)\n",
+ program, backend->pid);
+
+ backend_fds[num_backends].fd = cupsFileNumber(backend->pipe);
+ backend_fds[num_backends].events = POLLIN;
+
+ backend->name = strdup(name);
+ backend->status = 0;
+ backend->count = 0;
+
+ active_backends ++;
+ num_backends ++;
+
+ return (0);