[icinga-checkins] icinga.org: icinga-core/mfriedrich/workers: libicinga: add set_socket_options() to workers #2954

git at icinga.org git at icinga.org
Sun Aug 5 23:32:12 CEST 2012


Module: icinga-core
Branch: mfriedrich/workers
Commit: a29a68ac16279e95627196312f826cedef2bc9f4
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=a29a68ac16279e95627196312f826cedef2bc9f4

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Sun Aug  5 14:27:48 2012 +0200

libicinga: add set_socket_options() to workers #2954

it's handy to disallow sockets to spill over into plugins, as well as
beef up the send and receive buffers somewhat.

refs #2954

---

 lib/worker.c |   19 +++++++++++++++++++
 lib/worker.h |    9 +++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/lib/worker.c b/lib/worker.c
index 1aca1c3..23fc417 100644
--- a/lib/worker.c
+++ b/lib/worker.c
@@ -589,6 +589,22 @@ static int receive_command(int sd, int events, void *discard) {
 }
 
 
+int set_socket_options(int sd, int bufsize) {
+	int ret;
+
+	ret = fcntl(sd, F_SETFD, FD_CLOEXEC);
+	ret |= fcntl(sd, F_SETFL, O_NONBLOCK);
+
+	if (!bufsize)
+		return ret;
+
+	ret |= setsockopt(sd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(int));
+	ret |= setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(int));
+
+	return ret;
+}
+
+
 static void enter_worker(int sd) {
 	/* created with socketpair(), usually */
 	master_sd = sd;
@@ -618,6 +634,9 @@ static void enter_worker(int sd) {
 	 */
 	sq = squeue_create(1024);
 
+	/* set socket opts */
+	set_socket_options(master_sd, 256 * 1024);
+
 	iobroker_register(iobs, master_sd, NULL, receive_command);
 	while (iobroker_get_num_fds(iobs) > 0) {
 		int poll_time = -1;
diff --git a/lib/worker.h b/lib/worker.h
index 4b713b8..4efa2e3 100644
--- a/lib/worker.h
+++ b/lib/worker.h
@@ -112,4 +112,13 @@ extern const char *mkstr(const char *fmt, ...);
  * @return The millisecond delta between the two structs
  */
 extern int tv_delta_msec(const struct timeval *start, const struct timeval *stop);
+
+/**
+ * Set some common socket options
+ * @param[in] sd The socket to set options for
+ * @param[in] bufsize Size to set send and receive buffers to
+ * @return 0 on success, < 0 on errors
+ */
+extern int set_socket_options(int sd, int bufsize);
+
 #endif /* INCLUDE_worker_h__ */





More information about the icinga-checkins mailing list