+ return ret;
+}
+
+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();
+
+ /* 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)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(ret));
+ DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
+ path, (int) mode, (int) uid, (int) gid);
+ strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
+ data.u.mkdir.path[PATH_MAX - 1] = '\0';
+ data.u.mkdir.mode = mode;
+
+ run_as(RUN_AS_MKDIR_RECURSIVE, &data, &ret, uid, gid);
+ errno = ret._errno;
+ return ret.u.mkdir.ret;
+}
+
+LTTNG_HIDDEN
+int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(ret));
+
+ DBG3("mkdir() %s with mode %d for uid %d and gid %d",
+ path, (int) mode, (int) uid, (int) gid);
+ strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
+ data.u.mkdir.path[PATH_MAX - 1] = '\0';
+ data.u.mkdir.mode = mode;
+ run_as(RUN_AS_MKDIR, &data, &ret, uid, gid);
+ errno = ret._errno;
+ return ret.u.mkdir.ret;
+}
+
+LTTNG_HIDDEN
+int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(ret));
+
+ DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
+ path, flags, (int) mode, (int) uid, (int) gid);
+ strncpy(data.u.open.path, path, PATH_MAX - 1);
+ data.u.open.path[PATH_MAX - 1] = '\0';
+ data.u.open.flags = flags;
+ data.u.open.mode = mode;
+ run_as(RUN_AS_OPEN, &data, &ret, uid, gid);
+ errno = ret._errno;
+ ret.u.open.ret = ret.fd;
+ return ret.u.open.ret;
+}
+
+LTTNG_HIDDEN
+int run_as_unlink(const char *path, uid_t uid, gid_t gid)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(ret));
+
+ DBG3("unlink() %s with for uid %d and gid %d",
+ path, (int) uid, (int) gid);
+ strncpy(data.u.unlink.path, path, PATH_MAX - 1);
+ data.u.unlink.path[PATH_MAX - 1] = '\0';
+ run_as(RUN_AS_UNLINK, &data, &ret, uid, gid);
+ errno = ret._errno;
+ return ret.u.unlink.ret;
+}
+
+LTTNG_HIDDEN
+int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(ret));
+
+ DBG3("rmdir_recursive() %s with for uid %d and gid %d",
+ path, (int) uid, (int) gid);
+ strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
+ data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
+ run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid);
+ errno = ret._errno;
+ return ret.u.rmdir_recursive.ret;
+}
+
+LTTNG_HIDDEN
+int run_as_extract_elf_symbol_offset(int fd, const char* function,
+ uid_t uid, gid_t gid, uint64_t *offset)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(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.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';
+
+ run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid);
+
+ errno = ret._errno;
+
+ if (ret._error) {
+ return -1;
+ }
+
+ *offset = ret.u.extract_elf_symbol_offset.offset;
+ return 0;
+}
+
+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)
+{
+ struct run_as_data data;
+ struct run_as_ret ret;
+
+ memset(&data, 0, sizeof(data));
+ memset(&ret, 0, sizeof(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.fd = fd;
+
+ strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1);
+ strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1);
+
+ data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
+ data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
+
+ run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid);
+
+ errno = ret._errno;
+
+ if (ret._error) {
+ return -1;
+ }
+
+ *num_offset = ret.u.extract_sdt_probe_offsets.num_offset;
+
+ *offsets = zmalloc(*num_offset * sizeof(uint64_t));
+ if (!*offsets) {
+ return -ENOMEM;
+ }
+
+ memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t));
+ return 0;
+}
+
+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);