Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/194425

Change subject: Fix lint errors
......................................................................

Fix lint errors

Change-Id: Id493d0d345b16ce5f88eb07aa9f0fc209532af6b
---
M statsdlb.c
1 file changed, 127 insertions(+), 99 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/software/statsdlb 
refs/changes/25/194425/1

diff --git a/statsdlb.c b/statsdlb.c
index 8e8a250..ab8844e 100644
--- a/statsdlb.c
+++ b/statsdlb.c
@@ -7,7 +7,7 @@
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,125 +16,153 @@
  * limitations under the License.
  */
 #include <errno.h>
+#include <fcntl.h>
 #include <netdb.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <unistd.h>
 
 
-#define BUF_SIZE 65535
+static ssize_t
+get_rmem_max() {
+  int fd = open("/proc/sys/net/core/rmem_max", O_RDONLY);
+  if (fd < 0) {
+    return -1;
+  }
 
+  char buf[128];
+  int res = read(fd, buf, sizeof(buf));
+  if (res < 1) {
+    return -1;
+  }
+  buf[res] = '\0';
+
+  long long val = atoll(buf);
+  if (val < 1 || val > SIZE_MAX) {
+    return -1;
+  }
+  return (ssize_t)val;
+}
 
 /* Assign a string input to a bucket using DJB hash */
 static unsigned int
 hash_bucket(unsigned int num_buckets, const char *str, size_t n) {
-    unsigned int hash = 5381;
+  unsigned int hash = 5381;
 
-    while (n-- > 0 && str[n] != '\0') {
-        hash = ((hash << 5) + hash) ^ str[n];
-    }
+  while (n-- > 0 && str[n] != '\0') {
+    hash = ((hash << 5) + hash) ^ (unsigned char)str[n];
+  }
 
-    return hash % num_buckets;
+  return hash % num_buckets;
 }
 
 int
-main(int argc, char **argv) {
-    int num_dests = argc - 2;
-    if (num_dests < 1) {
-        fprintf(stderr, "Usage: %s SRC_PORT DST_PORT1 [DST_PORT2 ...]\n",
-                argv[0]);
+main(int argc, char *const argv[]) {
+  if (argc < 3) {
+    fprintf(stderr, "Usage: %s SRC_PORT DST_PORT1 [DST_PORT2 ...]\n",
+        argv[0]);
+    exit(1);
+  }
+  unsigned int num_dests = (unsigned int)argc - 2;
+  ssize_t buf_size = get_rmem_max();
+  if (buf_size < 1) {
+    buf_size = 66560;
+  }
+
+  char *stat;
+  if ((stat = calloc(1, (unsigned int)buf_size)) == NULL) {
+    perror("calloc");
+    exit(1);
+  }
+
+  struct addrinfo hints;
+  memset(&hints, 0, sizeof(hints));
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_socktype = (int) SOCK_DGRAM;
+  hints.ai_flags = AI_PASSIVE;
+
+  /* Server address */
+  struct addrinfo *src;
+  if (getaddrinfo(NULL, argv[1], &hints, &src) != 0) {
+    perror("getaddrinfo");
+    exit(1);
+  }
+
+  /* Backend addresses */
+  struct addrinfo **dests;
+  if ((dests = calloc(num_dests, sizeof(*dests))) == NULL) {
+    perror("calloc");
+    exit(1);
+  }
+
+  unsigned int i;
+  struct addrinfo *dest;
+  for (i = 0; i < num_dests; i++) {
+    if (getaddrinfo(NULL, *(argv + i + 2), &hints, &dest) != 0) {
+      perror("getaddrinfo");
+      exit(1);
+    }
+    dests[i] = dest;
+  }
+
+  int sockfd = socket(src->ai_family, src->ai_socktype, src->ai_protocol);
+  if (bind(sockfd, src->ai_addr, src->ai_addrlen) == -1) {
+    perror("bind");
+    exit(1);
+  }
+
+  /* Set SO_REUSEADDR, so we can restart quickly */
+  int on = 1;
+  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
+    perror("setsockopt");
+    exit(1);
+  }
+
+  char *buf;
+  if ((buf = calloc(1, (unsigned int)buf_size)) == NULL) {
+    perror("calloc");
+    exit(1);
+  }
+
+  for (;;) {
+    ssize_t n = recv(sockfd, buf, (unsigned int)buf_size - 1, 0);
+    if (n < 0 && errno != EINTR) {
+      perror("recv");
+      exit(1);
+    }
+    if (n < 1) {
+      continue;
+    }
+    buf[n] = '\0';
+
+    char *rest = buf;
+    char *token;
+    while ((token = strsep(&rest, "\n")) != NULL) {
+      int stat_len = snprintf(stat, (unsigned int)buf_size, "%s\n", token);
+      if (stat_len < 0) {
+        perror("snprintf");
         exit(1);
-    }
+      }
 
-    char stat[BUF_SIZE];
-    memset(stat, '\0', sizeof(stat));
+      if (stat_len < 4 || stat_len >= buf_size) {
+        continue;
+      }
 
-    struct addrinfo hints;
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = AF_UNSPEC;
-    hints.ai_socktype = SOCK_DGRAM;
-    hints.ai_flags = AI_PASSIVE;
+      /* Bucket on the basis of the metric name, not the metric value. */
+      size_t name_len = strcspn(stat, ":|");
+      if (name_len == (unsigned int)stat_len) {
+        continue;
+      }
+      unsigned int dest_idx = hash_bucket(num_dests, stat, name_len);
+      dest = dests[dest_idx];
 
-    /* Server address */
-    struct addrinfo *src;
-    if (getaddrinfo(NULL, argv[1], &hints, &src) != 0) {
-        perror("getaddrinfo");
+      if (sendto(sockfd, stat, (unsigned int)stat_len, 0, dest->ai_addr,
+                 dest->ai_addrlen) == -1 && errno != EINTR) {
+        perror("sendto");
         exit(1);
+      }
     }
-
-    /* Backend addresses */
-    struct addrinfo **dests;
-    if ((dests = malloc(num_dests * sizeof(*dests))) == NULL) {
-        perror("malloc");
-        exit(1);
-    }
-
-    int i;
-    struct addrinfo *dest;
-    for (i = 0; i < num_dests; i++) {
-        if (getaddrinfo(NULL, argv[i + 2], &hints, &dest) != 0) {
-            perror("getaddrinfo");
-            exit(1);
-        }
-        dests[i] = dest;
-    }
-
-    int sockfd = socket(src->ai_family, src->ai_socktype, src->ai_protocol);
-    if (bind(sockfd, src->ai_addr, src->ai_addrlen) == -1) {
-        perror("bind");
-        exit(1);
-    }
-
-    /* Set SO_REUSEADDR, so we can restart quickly */
-    int on = 1;
-    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
-        perror("setsockopt");
-        exit(1);
-    }
-
-    char buf[BUF_SIZE];
-
-    for (;;) {
-        int n = recv(sockfd, buf, sizeof(buf) - 1, 0);
-        if (n < 0 && errno != EINTR) {
-            perror("recv");
-            exit(1);
-        }
-        if (n < 1) {
-            continue;
-        }
-        buf[n] = '\0';
-
-        char *rest = buf;
-        char *token;
-        while ((token = strsep(&rest, "\n")) != NULL) {
-            int stat_len = snprintf(stat, sizeof(stat), "%s\n", token);
-            if (stat_len < 0) {
-                perror("snprintf");
-                exit(1);
-            }
-
-            if (stat_len < 4 || stat_len >= sizeof(stat)) {
-                continue;
-            }
-
-            /* Bucket on the basis of the metric name, not the metric value. */
-            size_t name_len = strcspn(stat, ":|");
-            if (name_len == stat_len) {
-                continue;
-            }
-            unsigned int dest_idx = hash_bucket(num_dests, stat, name_len);
-            dest = dests[dest_idx];
-
-            if (sendto(sockfd, stat, stat_len, 0, dest->ai_addr,
-                       dest->ai_addrlen) == -1 && errno != EINTR) {
-                perror("sendto");
-                exit(1);
-            }
-        }
-    }
+  }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/194425
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id493d0d345b16ce5f88eb07aa9f0fc209532af6b
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/statsdlb
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to