+ int fd = -1;
+ for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
+ int flags = ai->ai_socktype;
+#ifdef SOCK_CLOEXEC
+ flags |= SOCK_CLOEXEC;
+#endif
+
+ fd = socket(ai->ai_family, flags, 0);
+ if (fd == -1)
+ continue;
+
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) != 0) {
+ WARNING("write_prometheus plugin: setsockopt(SO_REUSEADDR) failed: %s",
+ STRERRNO);
+ close(fd);
+ fd = -1;
+ continue;
+ }
+
+ if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) {
+ close(fd);
+ fd = -1;
+ continue;
+ }
+
+ if (listen(fd, /* backlog = */ 16) != 0) {
+ close(fd);
+ fd = -1;
+ continue;
+ }
+
+ char str_node[NI_MAXHOST];
+ char str_service[NI_MAXSERV];
+
+ getnameinfo(ai->ai_addr, ai->ai_addrlen, str_node, sizeof(str_node),
+ str_service, sizeof(str_service),
+ NI_NUMERICHOST | NI_NUMERICSERV);
+
+ INFO("write_prometheus plugin: Listening on [%s]:%s.", str_node,
+ str_service);
+ break;