+ 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;
+ return run_as(worker, RUN_AS_OPEN, &data, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_unlink(const char *path, uid_t uid, gid_t gid)
+{
+ struct run_as_worker *worker = global_worker;
+ struct run_as_data data;
+
+ DBG3("unlink() %s with for uid %d and gid %d",
+ path, uid, gid);
+ strncpy(data.u.unlink.path, path, PATH_MAX - 1);
+ data.u.unlink.path[PATH_MAX - 1] = '\0';
+ return run_as(worker, RUN_AS_UNLINK, &data, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
+{
+ struct run_as_worker *worker = global_worker;
+ struct run_as_data data;
+
+ DBG3("rmdir_recursive() %s with for uid %d and gid %d",
+ path, uid, gid);
+ strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
+ data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
+ return run_as(worker, RUN_AS_RMDIR_RECURSIVE, &data, uid, gid);
+}
+
+LTTNG_HIDDEN
+int run_as_create_worker(char *procname)
+{
+ pid_t pid;
+ int i, ret = 0;
+ ssize_t readlen;
+ struct run_as_ret recvret;
+ struct run_as_worker *worker;
+
+ if (!use_clone()) {
+ ret = 0;
+ goto end;
+ }
+ worker = zmalloc(sizeof(*worker));
+ if (!worker) {
+ ret = -ENOMEM;
+ goto end;
+ }
+ worker->procname = procname;
+ /* Create unix socket. */
+ if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
+ ret = -1;
+ goto error_sock;
+ }
+ /* Fork worker. */
+ pid = fork();
+ if (pid < 0) {
+ PERROR("fork");
+ ret = -1;
+ goto error_fork;
+ } else if (pid == 0) {
+ /* Child */
+
+ /* Just close, no shutdown. */
+ if (close(worker->sockpair[0])) {
+ PERROR("close");
+ exit(EXIT_FAILURE);
+ }
+ worker->sockpair[0] = -1;
+ ret = run_as_worker(worker);
+ if (lttcomm_close_unix_sock(worker->sockpair[1])) {
+ PERROR("close");
+ ret = -1;
+ }
+ worker->sockpair[1] = -1;
+ exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
+ } else {
+ /* Parent */
+
+ /* Just close, no shutdown. */
+ if (close(worker->sockpair[1])) {
+ PERROR("close");
+ ret = -1;
+ goto error_fork;
+ }
+ worker->sockpair[1] = -1;
+ worker->pid = pid;
+ /* Wait for worker to become ready. */
+ readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
+ &recvret, sizeof(recvret));
+ if (readlen < sizeof(recvret)) {
+ ERR("readlen: %zd", readlen);
+ PERROR("Error reading response from run_as at creation");
+ ret = -1;
+ goto error_fork;
+ }
+ global_worker = worker;
+ }
+end:
+ return ret;
+
+ /* Error handling. */
+error_fork:
+ for (i = 0; i < 2; i++) {
+ if (worker->sockpair[i] < 0) {
+ continue;
+ }
+ if (lttcomm_close_unix_sock(worker->sockpair[i])) {
+ PERROR("close");
+ }
+ worker->sockpair[i] = -1;
+ }
+error_sock:
+ free(worker);
+ return ret;
+}
+
+LTTNG_HIDDEN
+void run_as_destroy_worker(void)
+{
+ struct run_as_worker *worker = global_worker;
+ int status;
+ pid_t pid;
+
+ if (!worker) {
+ return;
+ }
+ /* Close unix socket */
+ if (lttcomm_close_unix_sock(worker->sockpair[0])) {
+ PERROR("close");
+ }
+ worker->sockpair[0] = -1;
+ /* Wait for worker. */
+ pid = waitpid(worker->pid, &status, 0);
+ if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ PERROR("wait");
+ }
+ free(worker);
+ global_worker = NULL;