X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=3e2db15a78a4230beaa8eb037b1763169c84df7c;hb=d3ba4d04f6c5d22ba97ea81cdca2e2acd04dfec1;hp=b60530a18304d858c7334d8257b133b158467ef5;hpb=7adc8768ee5f7276d1964da6d268effba557d3a5;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index b60530a1..3e2db15a 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -51,11 +51,19 @@ # include #endif +#if HAVE_NETINET_TCP_H +# include +#endif + /* for ntohl and htonl */ #if HAVE_ARPA_INET_H # include #endif +#ifdef HAVE_SYS_CAPABILITY_H +# include +#endif + #ifdef HAVE_LIBKSTAT extern kstat_ctl_t *kc; #endif @@ -1557,6 +1565,46 @@ int service_name_to_port_number (const char *service_name) return (-1); } /* int service_name_to_port_number */ +void set_sock_opts (int sockfd) /* {{{ */ +{ + int status; + int socktype; + + socklen_t socklen = sizeof (socklen_t); + int so_keepalive = 1; + int tcp_keepidle = ((CDTIME_T_TO_MS(plugin_get_interval()) - 1) / 100 + 1); + int tcp_keepintvl = ((CDTIME_T_TO_MS(plugin_get_interval()) - 1) / 1000 + 1); + + status = getsockopt (sockfd, SOL_SOCKET, SO_TYPE, &socktype, &socklen); + if (status != 0) + { + WARNING ("set_sock_opts: failed to determine socket type"); + return; + } + + if (socktype == SOCK_STREAM) + { + status = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, + &so_keepalive, sizeof (so_keepalive)); + if (status != 0) + WARNING ("set_sock_opts: failed to set socket keepalive flag"); + +#ifdef TCP_KEEPIDLE + status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, + &tcp_keepidle, sizeof (tcp_keepidle)); + if (status != 0) + WARNING ("set_sock_opts: failed to set socket tcp keepalive time"); +#endif + +#ifdef TCP_KEEPINTVL + status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, + &tcp_keepintvl, sizeof (tcp_keepintvl)); + if (status != 0) + WARNING ("set_sock_opts: failed to set socket tcp keepalive interval"); +#endif + } +} /* }}} void set_sock_opts */ + int strtoderive (const char *string, derive_t *ret_value) /* {{{ */ { derive_t tmp; @@ -1624,3 +1672,26 @@ void strarray_free (char **array, size_t array_len) /* {{{ */ sfree (array[i]); sfree (array); } /* }}} void strarray_free */ + +#ifdef HAVE_SYS_CAPABILITY_H +int check_capability (int capability) /* {{{ */ +{ + struct __user_cap_header_struct cap_header_data; + cap_user_header_t cap_header = &cap_header_data; + struct __user_cap_data_struct cap_data_data; + cap_user_data_t cap_data = &cap_data_data; + + cap_header->pid = getpid(); + cap_header->version = _LINUX_CAPABILITY_VERSION; + if (capget(cap_header, cap_data) < 0) + { + ERROR("check_capability: capget failed"); + return (-1); + } + + if ((cap_data->effective & (1 << capability)) == 0) + return (-1); + else + return (0); +} /* }}} int check_capability */ +#endif