is_root = !getuid();
if (is_root) {
/* lttng health client socket path permissions */
- ret = chown(health_unix_sock_path, 0,
- utils_get_group_id(tracing_group_name));
+ gid_t gid;
+
+ ret = utils_get_group_id(tracing_group_name, true, &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
+
+ ret = chown(health_unix_sock_path, 0, gid);
if (ret < 0) {
ERR("Unable to set group on %s", health_unix_sock_path);
PERROR("chown");
int is_root = !getuid();
if (is_root) {
- ret = chown(rundir, 0,
- utils_get_group_id(tracing_group_name));
+ gid_t gid;
+
+ ret = utils_get_group_id(tracing_group_name, true, &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
+
+ ret = chown(rundir, 0, gid);
if (ret < 0) {
ERR("Unable to set group on %s", rundir);
PERROR("chown");
is_root = !getuid();
if (is_root) {
/* lttng health client socket path permissions */
- ret = chown(health_unix_sock_path, 0,
- utils_get_group_id(tracing_group_name));
+ gid_t gid;
+
+ ret = utils_get_group_id(tracing_group_name, true, &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
+
+ ret = chown(health_unix_sock_path, 0, gid);
if (ret < 0) {
ERR("Unable to set group on %s", health_unix_sock_path);
PERROR("chown");
}
if (is_root) {
+ gid_t gid;
+
/* lttng health client socket path permissions */
+ ret = utils_get_group_id(config.tracing_group_name.value, true,
+ &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
ret = chown(config.health_unix_sock_path.value, 0,
- utils_get_group_id(config.tracing_group_name.value));
+ gid);
if (ret < 0) {
ERR("Unable to set group on %s", config.health_unix_sock_path.value);
PERROR("chown");
int ret;
gid_t gid;
- gid = utils_get_group_id(config.tracing_group_name.value);
+ ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
/* Set lttng run dir */
ret = chown(rundir, 0, gid);
goto error;
}
if (is_root) {
- ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
+ gid_t gid;
+
+ ret = utils_get_group_id(config.tracing_group_name.value, true,
+ &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
+
+ ret = chown(path, 0, gid);
if (ret < 0) {
ERR("Unable to set group on %s", path);
PERROR("chown");
}
if (getuid() == 0) {
- ret = chown(sock_path, 0,
- utils_get_group_id(config.tracing_group_name.value));
+ gid_t gid;
+
+ ret = utils_get_group_id(config.tracing_group_name.value, true,
+ &gid);
+ if (ret) {
+ /* Default to root group. */
+ gid = 0;
+ }
+
+ ret = chown(sock_path, 0, gid);
if (ret) {
ERR("Failed to set the notification channel socket's group");
ret = -1;
#include <common/compat/getenv.h>
#include <common/compat/string.h>
#include <common/compat/dirent.h>
+#include <common/dynamic-buffer.h>
#include <lttng/constant.h>
#include "utils.h"
}
/*
- * Return the group ID matching name, else 0 if it cannot be found.
+ * Return 0 on success and set *gid to the group_ID matching the passed name.
+ * Else -1 if it cannot be found or an error occurred.
*/
LTTNG_HIDDEN
-gid_t utils_get_group_id(const char *name)
+int utils_get_group_id(const char *name, bool warn, gid_t *gid)
{
- struct group *grp;
+ static volatile int warn_once;
+ int ret;
+ long sys_len;
+ size_t len;
+ struct group grp;
+ struct group *result;
+ struct lttng_dynamic_buffer buffer;
+
+ /* Get the system limit, if it exists. */
+ sys_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (sys_len == -1) {
+ len = 1024;
+ } else {
+ len = (size_t) sys_len;
+ }
+
+ lttng_dynamic_buffer_init(&buffer);
+ ret = lttng_dynamic_buffer_set_size(&buffer, len);
+ if (ret) {
+ ERR("Failed to allocate group info buffer");
+ ret = -1;
+ goto error;
+ }
- grp = getgrnam(name);
- if (!grp) {
- static volatile int warn_once;
+ while ((ret = getgrnam_r(name, &grp, buffer.data, buffer.size, &result)) == ERANGE) {
+ const size_t new_len = 2 * buffer.size;
- if (!warn_once) {
- WARN("No tracing group detected");
- warn_once = 1;
+ /* Buffer is not big enough, increase its size. */
+ if (new_len < buffer.size) {
+ ERR("Group info buffer size overflow");
+ ret = -1;
+ goto error;
}
- return 0;
+
+ ret = lttng_dynamic_buffer_set_size(&buffer, new_len);
+ if (ret) {
+ ERR("Failed to grow group info buffer to %zu bytes",
+ new_len);
+ ret = -1;
+ goto error;
+ }
+ }
+ if (ret) {
+ PERROR("Failed to get group file entry for group name \"%s\"",
+ name);
+ ret = -1;
+ goto error;
+ }
+
+ /* Group not found. */
+ if (!result) {
+ ret = -1;
+ goto error;
}
- return grp->gr_gid;
+
+ *gid = result->gr_gid;
+ ret = 0;
+
+error:
+ if (ret && warn && !warn_once) {
+ WARN("No tracing group detected");
+ warn_once = 1;
+ }
+ lttng_dynamic_buffer_reset(&buffer);
+ return ret;
}
/*
#include <unistd.h>
#include <stdint.h>
#include <getopt.h>
+#include <stdbool.h>
+#include <sys/types.h>
#define KIBI_LOG2 10
#define MEBI_LOG2 20
char *utils_get_home_dir(void);
char *utils_get_user_home_dir(uid_t uid);
size_t utils_get_current_time_str(const char *format, char *dst, size_t len);
-gid_t utils_get_group_id(const char *name);
+int utils_get_group_id(const char *name, bool warn, gid_t *gid);
char *utils_generate_optstring(const struct option *long_options,
size_t opt_count);
int utils_create_lock_file(const char *filepath);
LTTNG_HIDDEN
int lttng_check_tracing_group(void)
{
- struct group *grp_tracing; /* no free(). See getgrnam(3) */
- gid_t *grp_list;
+ gid_t *grp_list, tracing_gid;
int grp_list_size, grp_id, i;
int ret = -1;
const char *grp_name = tracing_group;
/* Get GID of group 'tracing' */
- grp_tracing = getgrnam(grp_name);
- if (!grp_tracing) {
+ if (utils_get_group_id(grp_name, false, &tracing_gid)) {
/* If grp_tracing is NULL, the group does not exist. */
goto end;
}
}
for (i = 0; i < grp_list_size; i++) {
- if (grp_list[i] == grp_tracing->gr_gid) {
+ if (grp_list[i] == tracing_gid) {
ret = 1;
break;
}