]> git.ozlabs.org Git - petitboot/blobdiff - discover/paths.c
discover/device-handler: Ensure we free unresolved boot options on remove
[petitboot] / discover / paths.c
index c352ca02e4e214f2607b32abbc6a00857e7056c4..64b218e820d2f06ec508584a7b4a2fddea0fc5b6 100644 (file)
@@ -1,5 +1,6 @@
 #define _GNU_SOURCE
 
+#include <assert.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -45,15 +46,17 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b)
 
 static char *local_name(void *ctx)
 {
-       char *tmp, *ret;
+       char *ret, tmp[] = "/tmp/pb-XXXXXX";
+       int fd;
 
-       tmp = tempnam(NULL, "pb-");
+       fd = mkstemp(tmp);
 
-       if (!tmp)
+       if (fd < 0)
                return NULL;
 
+       close(fd);
+
        ret = talloc_strdup(ctx, tmp);
-       free(tmp);
 
        return ret;
 }
@@ -79,9 +82,14 @@ static void load_url_process_exit(struct process *process)
        data = task->async_data;
        cb = task->async_cb;
 
-       result->status = process->exit_status == 0 ? LOAD_OK : LOAD_ERROR;
-       if (result->status == LOAD_ERROR)
+       if (result->status == LOAD_CANCELLED) {
+               load_url_result_cleanup_local(result);
+       } else if (process->exit_status == 0) {
+               result->status = LOAD_OK;
+       } else {
+               result->status = LOAD_ERROR;
                load_url_result_cleanup_local(result);
+       }
 
        /* The load callback may well free the ctx, which was the
         * talloc parent of the task. Therefore, we want to do our cleanup
@@ -89,6 +97,7 @@ static void load_url_process_exit(struct process *process)
         */
        process_release(process);
        talloc_free(task);
+       result->task = NULL;
 
        cb(result, data);
 }
@@ -359,6 +368,7 @@ struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
        task->url = url;
        task->async = async_cb != NULL;
        task->result = talloc_zero(ctx, struct load_url_result);
+       task->result->task = task;
        task->process = process_create(task);
        if (task->async) {
                task->async_cb = async_cb;
@@ -407,3 +417,22 @@ struct load_url_result *load_url(void *ctx, struct pb_url *url)
 {
        return load_url_async(ctx, url, NULL, NULL);
 }
+
+void load_url_async_cancel(struct load_url_result *res)
+{
+       struct load_task *task = res->task;
+
+       /* the completion callback may have already been called; this clears
+        * res->task */
+       if (!task)
+               return;
+
+       if (res->status == LOAD_CANCELLED)
+               return;
+
+       assert(task->async);
+       assert(task->process);
+
+       res->status = LOAD_CANCELLED;
+       process_stop_async(task->process);
+}