-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <talloc/talloc.h>
#include <system/system.h>
static char *local_name(void *ctx)
{
- char *tmp, *ret;
+ char *ret, tmp[] = "/tmp/pb-XXXXXX";
+ mode_t oldmask;
+ int fd;
- tmp = tempnam(NULL, "pb-");
+ oldmask = umask(0644);
+ fd = mkstemp(tmp);
+ umask(oldmask);
- if (!tmp)
+ if (fd < 0)
return NULL;
+ close(fd);
+
ret = talloc_strdup(ctx, tmp);
- free(tmp);
return ret;
}
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_ok(process)) {
+ 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
*/
process_release(process);
talloc_free(task);
+ result->task = NULL;
cb(result, data);
}
task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
} else {
rc = process_run_sync(task->process);
- task->result->status = rc ? LOAD_ERROR : LOAD_OK;
+ if (rc || !process_exit_ok(task->process))
+ task->result->status = LOAD_ERROR;
+ else
+ task->result->status = LOAD_OK;
process_release(task->process);
task->process = NULL;
}
const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
struct process *process;
enum tftp_type type;
+ int rc;
process = process_create(ctx);
process->path = pb_system_apps.tftp;
process->argv = argv;
process->keep_stdout = true;
- process_run_sync(process);
+ process->add_stderr = true;
+ rc = process_run_sync(process);
- if (!process->stdout_buf || process->stdout_len == 0) {
+ if (rc || !process->stdout_buf || process->stdout_len == 0) {
pb_log("Can't check TFTP client type!\n");
type = TFTP_TYPE_BROKEN;
pb_system_apps.wget,
"-O",
NULL, /* 2: local file */
- NULL,
- NULL,
+ NULL, /* 3 (optional): --quiet */
+ NULL, /* 4 (optional): --no-check-certificate */
+ NULL, /* 5: URL */
NULL,
};
int i;
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;
{
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);
+}