+ if (index >= s->fd_num)
+ return (EINVAL);
+
+ close (s->fd[index].fd);
+ s->fd[index].fd = -1;
+
+ /* When deleting the last element in the list, no memmove is necessary. */
+ if (index < (s->fd_num - 1))
+ {
+ memmove (&s->fd[index], &s->fd[index + 1],
+ sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
+ }
+
+ s->fd_num--;
+ return (0);
+} /* }}} int pb_del_socket */
+
+static int pb_add_socket (pinba_socket_t *s, /* {{{ */
+ const struct addrinfo *ai)
+{
+ int fd;
+ int tmp;
+ int status;
+
+ if (s->fd_num == PINBA_MAX_SOCKETS)
+ {
+ WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
+ "%i sockets. Please complain to the collectd developers so we can "
+ "raise the limit.", PINBA_MAX_SOCKETS);
+ return (-1);
+ }
+
+ fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (fd < 0)
+ {
+ char errbuf[1024];
+ ERROR ("pinba plugin: socket(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (0);
+ }
+
+ tmp = 1;
+ status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
+ if (status != 0)
+ {
+ char errbuf[1024];
+ WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ }
+
+ status = bind (fd, ai->ai_addr, ai->ai_addrlen);
+ if (status != 0)
+ {
+ char errbuf[1024];
+ ERROR ("pinba plugin: bind(2) failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (0);
+ }
+
+ s->fd[s->fd_num].fd = fd;
+ s->fd[s->fd_num].events = POLLIN | POLLPRI;
+ s->fd[s->fd_num].revents = 0;
+ s->fd_num++;
+
+ return (0);
+} /* }}} int pb_add_socket */
+
+static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
+ const char *service)
+{
+ pinba_socket_t *s;
+ struct addrinfo *ai_list;
+ struct addrinfo *ai_ptr;
+ struct addrinfo ai_hints;
+ int status;
+
+ memset (&ai_hints, 0, sizeof (ai_hints));
+ ai_hints.ai_flags = AI_PASSIVE;
+ ai_hints.ai_family = AF_UNSPEC;
+ ai_hints.ai_socktype = SOCK_DGRAM;
+ ai_hints.ai_addr = NULL;
+ ai_hints.ai_canonname = NULL;
+ ai_hints.ai_next = NULL;
+
+ if (node == NULL)
+ node = PINBA_DEFAULT_NODE;
+
+ if (service == NULL)
+ service = PINBA_DEFAULT_SERVICE;
+
+ ai_list = NULL;
+ status = getaddrinfo (node, service,
+ &ai_hints, &ai_list);
+ if (status != 0)
+ {
+ ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
+ gai_strerror (status));
+ return (NULL);
+ }
+ assert (ai_list != NULL);
+
+ s = malloc (sizeof (*s));
+ if (s == NULL)
+ {
+ freeaddrinfo (ai_list);
+ ERROR ("pinba plugin: malloc failed.");
+ return (NULL);
+ }
+ memset (s, 0, sizeof (*s));
+
+ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+ {
+ status = pb_add_socket (s, ai_ptr);
+ if (status != 0)
+ break;
+ } /* for (ai_list) */