Skip to content

Instantly share code, notes, and snippets.

@luhenry
Created March 24, 2014 16:44
Show Gist options
  • Save luhenry/9744138 to your computer and use it in GitHub Desktop.
Save luhenry/9744138 to your computer and use it in GitHub Desktop.
diff --git a/mono/utils/mono-counters-agent.c b/mono/utils/mono-counters-agent.c
index 1befb28..b3e710d 100644
--- a/mono/utils/mono-counters-agent.c
+++ b/mono/utils/mono-counters-agent.c
@@ -40,7 +40,7 @@ enum {
SRV_REMOVE_COUNTER,
SVR_SAMPLES,
};
-
+
/**
* Protocol :
* | Headers | Values | Values | ... [Infinity]
@@ -164,7 +164,7 @@ static gboolean
write_buffer_to_socket (int fd, const char* buffer, size_t size)
{
size_t sent = 0;
-
+
while (sent < size) {
ssize_t res = send (fd, buffer + sent, size - sent, 0);
if (res <= 0)
@@ -191,13 +191,44 @@ read_socket_to_buffer (int fd, char* buffer, size_t size)
}
static gboolean
-write_counter_agent_header (int socketfd, MonoCounterAgent *counter_agent)
+write_string (int socketfd, const char *str)
+{
+ int size = -1;
+
+ //Null is encoded as -1
+ if (!str)
+ return write_buffer_to_socket (socketfd, (char*)&size, 4);
+
+ size = strlen (str);
+ if (!write_buffer_to_socket (socketfd, (char*)&size, 4))
+ return FALSE;
+ if (!write_buffer_to_socket (socketfd, str, size))
+ return FALSE;
+
+ return TRUE;
+}
+
+
+static gboolean
+read_string (int socketfd, char **str)
{
- int len = strlen (counter_agent->counter->name);
+ int len;
+ if (!read_socket_to_buffer (socketfd, (char*)&len, 4))
+ return FALSE;
+
+ *str = g_malloc(len + 1);
+ if (!read_socket_to_buffer (socketfd, *str, len))
+ return FALSE;
+ (*str) [len] = '\0';
+ return TRUE;
+}
+
+static gboolean
+write_counter_agent_header (int socketfd, MonoCounterAgent *counter_agent)
+{
if (!write_buffer_to_socket(socketfd, (char*)&counter_agent->counter->category, 4) ||
- !write_buffer_to_socket (socketfd, (char*)&len, 4) ||
- !write_buffer_to_socket (socketfd, (char*) counter_agent->counter->name, len) ||
+ !write_string (socketfd, counter_agent->counter->name) ||
!write_buffer_to_socket (socketfd, (char*)&counter_agent->counter->type, 4) ||
!write_buffer_to_socket (socketfd, (char*)&counter_agent->counter->unit, 4) ||
!write_buffer_to_socket (socketfd, (char*)&counter_agent->counter->variance, 4) ||
@@ -242,40 +273,6 @@ do_hello (int socketfd)
return TRUE;
}
-static gboolean
-write_string (int socketfd, const char *str)
-{
- short size = -1;
-
- //Null is encoded as -1
- if (!str)
- return write_buffer_to_socket (socketfd, (char*)&size, 2);
-
- size = strlen (str);
- if (!write_buffer_to_socket (socketfd, (char*)&size, 2))
- return FALSE;
-
- return write_buffer_to_socket (socketfd, str, size);
-}
-
-
-static char*
-read_string (int socketfd)
-{
- int len;
- char *string;
-
- if (!read_socket_to_buffer (socketfd, (char*)&len, 4))
- return NULL;
-
- string = g_malloc(len + 1);
- if (!read_socket_to_buffer (socketfd, string, len))
- return FALSE;
- string [len] = '\0';
- return string;
-}
-
-
static int _sock;
static gboolean
write_counter (const char *category, const char *name)
@@ -316,10 +313,10 @@ do_add_counter (int socketfd)
MonoCounterCategory mcat;
MonoCounterAgent *added_counter;
- if (!(category = read_string (socketfd)))
+ if (!read_string (socketfd, &category) || !category)
goto fail;
- if (!(name = read_string (socketfd)))
+ if (!read_string (socketfd, &name) || !name)
goto fail;
mcat = mono_counters_category_name_to_id (category);
@@ -473,22 +470,22 @@ mono_counters_agent_sampling_thread (void* ptr)
fd_set socketfd_set;
gint64 last_sampling = 0, now;
char cmd;
-
+
if ((socketfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
g_warning ("mono-counters-agent: error with socket : %s", strerror (errno));
return NULL;
}
-
+
memset (&inspector_addr, 0, sizeof (inspector_addr));
-
+
inspector_addr.sin_family = AF_INET;
inspector_addr.sin_port = htons (inspector_port);
-
+
if (!inet_pton (AF_INET, inspector_ip, &inspector_addr.sin_addr)) {
g_warning ("mono-counters-agent: error with inet_pton : %s", strerror (errno));
goto cleanup;
}
-
+
if (connect(socketfd, (struct sockaddr*)&inspector_addr, sizeof(inspector_addr)) < 0) {
g_warning ("mono-counters-agent: error with connect : %s", strerror(errno));
goto cleanup;
@@ -579,10 +576,10 @@ parse_configuration (const gchar* configuration)
interval = strtoll (opt, NULL, 10);
}
}
-
+
parse_counters_names(counters_names);
parse_address(address);
-
+
g_free((void*)counters_names);
g_free((void*)address);
g_strfreev(opts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment