+}
+
+static
+int run_as_restart_worker(struct run_as_worker *worker)
+{
+ int ret = 0;
+ char *procname = NULL;
+
+ procname = worker->procname;
+
+ /* Close socket to run_as worker process and clean up the zombie process */
+ run_as_destroy_worker_no_lock();
+
+ /* Create a new run_as worker process*/
+ ret = run_as_create_worker_no_lock(procname, NULL, NULL);
+ if (ret < 0 ) {
+ ERR("Restarting the worker process failed");
+ ret = -1;
+ goto err;
+ }
+err:
+ return ret;
+}
+
+static
+int run_as(enum run_as_cmd cmd, struct run_as_data *data,
+ struct run_as_ret *ret_value, uid_t uid, gid_t gid)
+{
+ int ret, saved_errno;
+
+ pthread_mutex_lock(&worker_lock);
+ if (use_clone()) {
+ DBG("Using run_as worker");
+
+ assert(global_worker);
+
+ ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
+ saved_errno = ret_value->_errno;
+
+ /*
+ * If the worker thread crashed the errno is set to EIO. we log
+ * the error and start a new worker process.
+ */
+ if (ret == -1 && saved_errno == EIO) {
+ DBG("Socket closed unexpectedly... "
+ "Restarting the worker process");
+ ret = run_as_restart_worker(global_worker);
+ if (ret == -1) {
+ ERR("Failed to restart worker process.");
+ goto err;
+ }
+ }
+ } else {
+ DBG("Using run_as without worker");
+ ret = run_as_noworker(cmd, data, ret_value, uid, gid);
+ }
+err:
+ pthread_mutex_unlock(&worker_lock);
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
+{
+ return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
+ uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, (int) mode, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.mkdir.path, path,
+ sizeof(data.u.mkdir.path));
+ if (ret) {
+ ERR("Failed to copy path argument of mkdirat recursive command");
+ goto error;
+ }
+ data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
+ data.u.mkdir.mode = mode;
+ data.u.mkdir.dirfd = dirfd;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
+ &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
+{
+ return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
+ uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, (int) mode, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.mkdir.path, path,
+ sizeof(data.u.mkdir.path));
+ if (ret) {
+ ERR("Failed to copy path argument of mkdirat command");
+ goto error;
+ }
+ data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
+ data.u.mkdir.mode = mode;
+ data.u.mkdir.dirfd = dirfd;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
+ &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
+ gid_t gid)
+{
+ return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
+ uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, flags, (int) mode, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
+ if (ret) {
+ ERR("Failed to copy path argument of open command");
+ goto error;
+ }
+ data.u.open.flags = flags;
+ data.u.open.mode = mode;
+ data.u.open.dirfd = dirfd;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
+ &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
+ run_as_ret.u.open.fd;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_unlink(const char *path, uid_t uid, gid_t gid)
+{
+ return run_as_unlinkat(AT_FDCWD, path, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.unlink.path, path,
+ sizeof(data.u.unlink.path));
+ if (ret) {
+ goto error;
+ }
+ data.u.unlink.dirfd = dirfd;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
+ &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
+{
+ return run_as_rmdirat(AT_FDCWD, path, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.rmdir.path, path,
+ sizeof(data.u.rmdir.path));
+ if (ret) {
+ goto error;
+ }
+ data.u.rmdir.dirfd = dirfd;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
+ &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
+{
+ return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
+}
+
+LTTNG_HIDDEN
+int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
+ dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ path, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.rmdir.path, path,
+ sizeof(data.u.rmdir.path));
+ if (ret) {
+ goto error;
+ }
+ data.u.rmdir.dirfd = dirfd;
+ data.u.rmdir.flags = flags;
+ run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
+ &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
+{
+ return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_renameat(int old_dirfd, const char *old_name,
+ int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
+ old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ old_name,
+ new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
+ new_name, (int) uid, (int) gid);
+ ret = lttng_strncpy(data.u.rename.old_path, old_name,
+ sizeof(data.u.rename.old_path));
+ if (ret) {
+ goto error;
+ }
+ ret = lttng_strncpy(data.u.rename.new_path, new_name,
+ sizeof(data.u.rename.new_path));
+ if (ret) {
+ goto error;
+ }
+
+ data.u.rename.dirfds[0] = old_dirfd;
+ data.u.rename.dirfds[1] = new_dirfd;
+ run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
+ RUN_AS_RENAME : RUN_AS_RENAMEAT,
+ &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ ret = run_as_ret.u.ret;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_extract_elf_symbol_offset(int fd, const char* function,
+ uid_t uid, gid_t gid, uint64_t *offset)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
+ "with for uid %d and gid %d", fd, function,
+ (int) uid, (int) gid);
+
+ data.u.extract_elf_symbol_offset.fd = fd;
+
+ strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
+ data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
+ ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
+ function,
+ sizeof(data.u.extract_elf_symbol_offset.function));
+ if (ret) {
+ goto error;
+ }
+
+ run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ if (run_as_ret._error) {
+ ret = -1;
+ goto error;
+ }
+
+ *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
+ const char* probe_name, uid_t uid, gid_t gid,
+ uint64_t **offsets, uint32_t *num_offset)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+
+ DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
+ "provider_name=%s with for uid %d and gid %d", fd,
+ probe_name, provider_name, (int) uid, (int) gid);
+
+ data.u.extract_sdt_probe_offsets.fd = fd;
+
+ ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
+ sizeof(data.u.extract_sdt_probe_offsets.probe_name));
+ if (ret) {
+ goto error;
+ }
+ ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
+ provider_name,
+ sizeof(data.u.extract_sdt_probe_offsets.provider_name));
+ if (ret) {
+ goto error;
+ }
+
+ run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ if (run_as_ret._error) {
+ ret = -1;
+ goto error;
+ }
+
+ *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
+ *offsets = zmalloc(*num_offset * sizeof(uint64_t));
+ if (!*offsets) {
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
+ *num_offset * sizeof(uint64_t));
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_generate_filter_bytecode(const char *filter_expression,
+ const struct lttng_credentials *creds,
+ struct lttng_bytecode **bytecode)
+{
+ int ret;
+ struct run_as_data data = {};
+ struct run_as_ret run_as_ret = {};
+ const struct lttng_bytecode *view_bytecode = NULL;
+ struct lttng_bytecode *local_bytecode = NULL;
+ const uid_t uid = lttng_credentials_get_uid(creds);
+ const gid_t gid = lttng_credentials_get_gid(creds);
+
+ DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
+ filter_expression, (int) uid, (int) gid);
+
+ ret = lttng_strncpy(data.u.generate_filter_bytecode.filter_expression, filter_expression,
+ sizeof(data.u.generate_filter_bytecode.filter_expression));
+ if (ret) {
+ goto error;
+ }
+
+ run_as(RUN_AS_GENERATE_FILTER_BYTECODE, &data, &run_as_ret, uid, gid);
+ errno = run_as_ret._errno;
+ if (run_as_ret._error) {
+ ret = -1;
+ goto error;
+ }
+
+ view_bytecode = (const struct lttng_bytecode *) run_as_ret.u.generate_filter_bytecode.bytecode;
+
+ local_bytecode = zmalloc(sizeof(*local_bytecode) + view_bytecode->len);
+ if (!local_bytecode) {
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ memcpy(local_bytecode, run_as_ret.u.generate_filter_bytecode.bytecode,
+ sizeof(*local_bytecode) + view_bytecode->len);
+ *bytecode = local_bytecode;
+error:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int run_as_create_worker(const char *procname,
+ post_fork_cleanup_cb clean_up_func,
+ void *clean_up_user_data)
+{
+ int ret;
+
+ pthread_mutex_lock(&worker_lock);
+ ret = run_as_create_worker_no_lock(procname, clean_up_func,
+ clean_up_user_data);
+ pthread_mutex_unlock(&worker_lock);
+ return ret;
+}
+
+LTTNG_HIDDEN
+void run_as_destroy_worker(void)
+{
+ pthread_mutex_lock(&worker_lock);
+ run_as_destroy_worker_no_lock();