X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=45f9d533540c7e07626674242990cd7502c1729d;hb=08cf386cb8aa711ef4043d9139d464b38188a44b;hp=c6559a804e3b003c45e2566c466c7f5413b2d6f3;hpb=8737bfd2e356a35bbef30ed2185c566cd61bca0b;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index c6559a80..45f9d533 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -60,6 +60,10 @@ # include #endif +#ifdef HAVE_SYS_CAPABILITY_H +# include +#endif + #ifdef HAVE_LIBKSTAT extern kstat_ctl_t *kc; #endif @@ -1668,3 +1672,52 @@ 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) /* {{{ */ +{ +#ifdef _LINUX_CAPABILITY_VERSION_3 + cap_user_header_t cap_header = calloc(sizeof (*cap_header), 1); + if (cap_header == NULL) + { + ERROR("check_capability: calloc failed"); + return (-1); + } + + cap_user_data_t cap_data = calloc(sizeof (*cap_data), 1); + if (cap_data == NULL) + { + ERROR("check_capability: calloc failed"); + sfree(cap_header); + return (-1); + } + + cap_header->pid = getpid(); + cap_header->version = _LINUX_CAPABILITY_VERSION; + if (capget(cap_header, cap_data) < 0) + { + ERROR("check_capability: capget failed"); + sfree(cap_header); + sfree(cap_data); + return (-1); + } + + if ((cap_data->effective & (1 << capability)) == 0) + { + sfree(cap_header); + sfree(cap_data); + return (-1); + } + else + { + sfree(cap_header); + sfree(cap_data); + return (0); + } +#else + WARNING ("check_capability: unsupported capability implementation. " + "Some plugin(s) may require elevated privileges to work properly."); + return (0); +#endif /* _LINUX_CAPABILITY_VERSION_3 */ +} /* }}} int check_capability */ +#endif /* HAVE_SYS_CAPABILITY_H */