Merge branch 'collectd-5.6' into collectd-5.7
[collectd.git] / src / daemon / common.c
index 0aa5345..ec5c7ab 100644 (file)
@@ -271,8 +271,10 @@ ssize_t swrite(int fd, const void *buf, size_t count) {
   ptr = (const char *)buf;
   nleft = count;
 
-  if (fd < 0)
-    return (-1);
+  if (fd < 0) {
+    errno = EINVAL;
+    return errno;
+  }
 
   /* checking for closed peer connection */
   pfd.fd = fd;
@@ -281,10 +283,9 @@ ssize_t swrite(int fd, const void *buf, size_t count) {
   if (poll(&pfd, 1, 0) > 0) {
     char buffer[32];
     if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
-      // if recv returns zero (even though poll() said there is data to be
-      // read),
-      // that means the connection has been closed
-      return -1;
+      /* if recv returns zero (even though poll() said there is data to be
+       * read), that means the connection has been closed */
+      return errno ? errno : -1;
     }
   }
 
@@ -295,7 +296,7 @@ ssize_t swrite(int fd, const void *buf, size_t count) {
       continue;
 
     if (status < 0)
-      return (status);
+      return errno ? errno : status;
 
     nleft = nleft - ((size_t)status);
     ptr = ptr + ((size_t)status);
@@ -1567,16 +1568,26 @@ void strarray_free(char **array, size_t array_len) /* {{{ */
 #if HAVE_CAPABILITY
 int check_capability(int arg) /* {{{ */
 {
-  cap_value_t cap = (cap_value_t)arg;
+  cap_value_t cap_value = (cap_value_t)arg;
+  cap_t cap;
+  cap_flag_value_t cap_flag_value;
 
-  if (!CAP_IS_SUPPORTED(cap))
+  if (!CAP_IS_SUPPORTED(cap_value))
     return (-1);
 
-  int have_cap = cap_get_bound(cap);
-  if (have_cap != 1)
+  if (!(cap = cap_get_proc())) {
+    ERROR("check_capability: cap_get_proc failed.");
     return (-1);
+  }
 
-  return (0);
+  if (cap_get_flag(cap, cap_value, CAP_EFFECTIVE, &cap_flag_value) < 0) {
+    ERROR("check_capability: cap_get_flag failed.");
+    cap_free(cap);
+    return (-1);
+  }
+  cap_free(cap);
+
+  return (cap_flag_value != CAP_SET);
 } /* }}} int check_capability */
 #else
 int check_capability(__attribute__((unused)) int arg) /* {{{ */