- /* Can't accept pollfd less than 2 */
- if (nb_fd < 2) {
- goto end;
- }
-
- if (*pollfd) {
- /* Save pointer */
- old_pollfd = *pollfd;
- }
-
- *pollfd = malloc(nb_fd * sizeof(struct pollfd));
- if (*pollfd == NULL) {
- perror("malloc manage apps pollfd");
- goto error;
- }
-
- /* First fd is always the quit pipe */
- (*pollfd)[0].fd = thread_quit_pipe[0];
- (*pollfd)[0].events =
- POLLHUP | POLLNVAL | POLLERR | POLLIN | POLLRDHUP | POLLPRI;
-
- /* Apps command pipe */
- (*pollfd)[1].fd = apps_cmd_pipe[0];
- (*pollfd)[1].events = POLLIN;
-
- /* Start count after the two pipes below */
- count = 2;
- for (i = 2; i < old_nb_fd; i++) {
- /* Add to new pollfd */
- if (old_pollfd[i].fd != -1) {
- (*pollfd)[count].fd = old_pollfd[i].fd;
- (*pollfd)[count].events = POLLHUP | POLLNVAL | POLLERR;
- count++;
- }
-
- if (count > nb_fd) {
- ERR("Updating poll fd wrong size");
- goto error;
- }
- }
-
- if (nb_fd < 2) {
- /*
- * There should *always* be at least two fds in the pollfd. This safety
- * check make sure the poll() will actually try on those two pipes at
- * best which are the thread_quit_pipe and apps_cmd_pipe.
- */
- nb_fd = 2;
- MSG("nb_fd < 2 --> Not good! Continuing...");
- }
-
- /* Destroy old pollfd */
- free(old_pollfd);
-
- DBG("Apps cmd pollfd realloc of size %d", nb_fd);
-
-end:
- return 0;
-
-error:
- /* Destroy old pollfd */
- free(old_pollfd);
- return -1;