2016-03-02 14:23:58 -08:00
|
|
|
#include <assert.h>
|
2016-03-02 18:49:25 -08:00
|
|
|
#include <errno.h>
|
2016-03-02 14:23:58 -08:00
|
|
|
#include <fcntl.h>
|
2016-02-29 20:49:36 -08:00
|
|
|
#include <stdlib.h>
|
2016-03-02 14:23:58 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/epoll.h>
|
2016-02-29 20:49:36 -08:00
|
|
|
#include <sys/stat.h>
|
2016-03-02 14:23:58 -08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
2016-02-29 20:49:36 -08:00
|
|
|
|
|
|
|
|
#include "flow.h"
|
2016-03-02 14:23:58 -08:00
|
|
|
#include "list.h"
|
|
|
|
|
#include "peer.h"
|
2016-02-29 20:49:36 -08:00
|
|
|
#include "receive.h"
|
|
|
|
|
#include "send.h"
|
2016-03-02 14:23:58 -08:00
|
|
|
#include "uuid.h"
|
2016-03-02 18:49:25 -08:00
|
|
|
#include "wakeup.h"
|
2016-02-29 20:49:36 -08:00
|
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
|
|
2016-03-02 14:23:58 -08:00
|
|
|
struct file {
|
|
|
|
|
struct peer peer;
|
|
|
|
|
uint8_t id[UUID_LEN];
|
|
|
|
|
char *path;
|
|
|
|
|
int flags;
|
2016-03-02 18:49:25 -08:00
|
|
|
uint32_t attempt;
|
2016-03-02 14:23:58 -08:00
|
|
|
struct flow *flow;
|
|
|
|
|
void *passthrough;
|
|
|
|
|
bool retry;
|
|
|
|
|
struct list_head file_list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct list_head file_head = LIST_HEAD_INIT(file_head);
|
|
|
|
|
|
2016-03-02 18:49:25 -08:00
|
|
|
static void file_open_wrapper(struct peer *);
|
|
|
|
|
|
2016-03-02 14:23:58 -08:00
|
|
|
static bool file_should_retry(int fd, struct file *file) {
|
|
|
|
|
// We want to retry most files, except if we're reading from a regular file
|
|
|
|
|
if (!file->flags & O_RDONLY) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Questionable heuristic time. We're going to test if the kernel driver for
|
|
|
|
|
// this fd supports polling. If it does, we assume that we have a character
|
|
|
|
|
// device, named pipe, or something else that is likely to give us different
|
|
|
|
|
// data if we read it again.
|
|
|
|
|
int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
|
assert(epoll_fd >= 0);
|
|
|
|
|
struct epoll_event ev = {
|
|
|
|
|
.events = 0,
|
|
|
|
|
.data = {
|
|
|
|
|
.ptr = NULL,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
int res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
|
|
|
|
assert(!close(epoll_fd));
|
|
|
|
|
return (res == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void file_del(struct file *file) {
|
|
|
|
|
(*file->flow->ref_count)--;
|
|
|
|
|
list_del(&file->file_list);
|
|
|
|
|
free(file->path);
|
|
|
|
|
free(file);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 18:49:25 -08:00
|
|
|
static void file_retry(struct file *file) {
|
|
|
|
|
uint32_t delay = wakeup_get_retry_delay_ms(file->attempt++);
|
|
|
|
|
fprintf(stderr, "F %s: Will retry in %ds\n", file->id, delay / 1000);
|
|
|
|
|
file->peer.event_handler = file_open_wrapper;
|
|
|
|
|
wakeup_add((struct peer *) file, delay);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 14:23:58 -08:00
|
|
|
static void file_handle_close(struct peer *peer) {
|
|
|
|
|
struct file *file = (struct file *) peer;
|
2016-03-02 18:49:25 -08:00
|
|
|
fprintf(stderr, "F %s: File closed: %s\n", file->id, file->path);
|
2016-03-02 14:23:58 -08:00
|
|
|
|
2016-03-02 18:49:25 -08:00
|
|
|
if (file->retry) {
|
|
|
|
|
file_retry(file);
|
|
|
|
|
} else {
|
|
|
|
|
file_del(file);
|
|
|
|
|
}
|
2016-03-02 14:23:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void file_open(struct file *file) {
|
2016-03-02 18:49:25 -08:00
|
|
|
fprintf(stderr, "F %s: Opening file: %s\n", file->id, file->path);
|
2016-03-02 14:23:58 -08:00
|
|
|
int fd = open(file->path, file->flags | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
2016-03-02 13:16:18 -08:00
|
|
|
if (fd == -1) {
|
2016-03-02 18:49:25 -08:00
|
|
|
fprintf(stderr, "F %s: Error opening file: %s\n", file->id, strerror(errno));
|
|
|
|
|
file_retry(file);
|
2016-03-02 13:16:18 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-03-02 14:23:58 -08:00
|
|
|
|
|
|
|
|
file->retry = file_should_retry(fd, file);
|
|
|
|
|
file->peer.event_handler = file_handle_close;
|
2016-03-02 18:49:25 -08:00
|
|
|
if (!flow_hello(fd, file->flow, file->passthrough)) {
|
|
|
|
|
fprintf(stderr, "F %s: Error writing greeting\n", file->id);
|
|
|
|
|
file_retry(file);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
file->attempt = 0;
|
2016-03-02 14:23:58 -08:00
|
|
|
file->flow->new(fd, file->passthrough, (struct peer *) file);
|
2016-03-02 18:49:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void file_open_wrapper(struct peer *peer) {
|
|
|
|
|
struct file *file = (struct file *) peer;
|
|
|
|
|
file_open(file);
|
2016-03-02 14:23:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void file_new(char *path, int flags, struct flow *flow, void *passthrough) {
|
|
|
|
|
(*flow->ref_count)++;
|
|
|
|
|
|
|
|
|
|
struct file *file = malloc(sizeof(*file));
|
|
|
|
|
assert(file);
|
|
|
|
|
file->peer.fd = -1;
|
|
|
|
|
uuid_gen(file->id);
|
|
|
|
|
file->path = strdup(path);
|
|
|
|
|
assert(file->path);
|
|
|
|
|
file->flags = flags;
|
2016-03-02 18:49:25 -08:00
|
|
|
file->attempt = 0;
|
2016-03-02 14:23:58 -08:00
|
|
|
file->flow = flow;
|
|
|
|
|
file->passthrough = passthrough;
|
|
|
|
|
|
|
|
|
|
list_add(&file->file_list, &file_head);
|
|
|
|
|
|
|
|
|
|
file_open(file);
|
2016-03-02 13:16:18 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 18:49:25 -08:00
|
|
|
void file_cleanup() {
|
|
|
|
|
struct file *iter, *next;
|
|
|
|
|
list_for_each_entry_safe(iter, next, &file_head, file_list) {
|
|
|
|
|
file_del(iter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 20:49:36 -08:00
|
|
|
void file_read_new(char *path, struct flow *flow, void *passthrough) {
|
2016-03-02 14:23:58 -08:00
|
|
|
file_new(path, O_RDONLY, flow, passthrough);
|
2016-02-29 20:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void file_write_new(char *path, struct flow *flow, void *passthrough) {
|
2016-03-02 14:23:58 -08:00
|
|
|
file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC, flow, passthrough);
|
2016-02-29 20:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void file_append_new(char *path, struct flow *flow, void *passthrough) {
|
2016-03-02 14:23:58 -08:00
|
|
|
file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW, flow, passthrough);
|
2016-02-29 20:49:36 -08:00
|
|
|
}
|