From dd485d65c57dca278d0019aa8720ce6603abe9bb Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 20 Sep 2011 02:30:00 -0700 Subject: [PATCH] Hacking away at stuff. Pretty soon there should be a working prototype. --- Makefile | 4 ++-- abuse.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++- abuse.h | 6 +++++- abuse_example.c | 6 ++++-- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 94f5ce1..ecbf2ec 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,10 @@ LDFLAGS := .PHONY: all clean all: $(TARGET) -$(TARGET): $(OBJS) abuse.h +$(TARGET): $(OBJS) $(CC) $(LDFLAGS) -o $@ $^ -$(OBJS): %.o: %.c +$(OBJS): %.o: %.c abuse.h $(CC) $(CFLAGS) -o $@ -c $< clean: diff --git a/abuse.c b/abuse.c index 4e343f0..5ac3f24 100644 --- a/abuse.c +++ b/abuse.c @@ -1,4 +1,58 @@ -int abuse_main() +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "abuse.h" + +int abuse_main(int argc, char *argv[], struct abuse_operations *aop) { + int sp[2]; + int nbd, sk, err, tmp_fd; + char *dev_file; + + assert(argc == 2); + dev_file = argv[1]; + + assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, sp)); + + nbd = open(dev_file, O_RDWR); + assert(nbd != -1); + + assert(ioctl(nbd, NBD_SET_SIZE, aop->size) != -1); + assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1); + + if (!fork()) { + /* The child needs to continue setting things up. */ + close(sp[0]); + sk = sp[1]; + + assert(ioctl(nbd, NBD_SET_SOCK, sk) != -1); + err = ioctl(nbd, NBD_DO_IT); + fprintf(stderr, "nbd device terminated with code %d\n", err); + + assert(ioctl(nbd, NBD_CLEAR_QUE) != -1); + assert(ioctl(nbd, NBD_CLEAR_SOCK) != -1); + + exit(0); + } + + /* The parent opens the device file at least once, to make sure the + * partition table is updated. Then it closes it and starts serving up + * requests. */ + + tmp_fd = open(dev_file, O_RDONLY); + assert(tmp_fd != -1); + close(tmp_fd); + + close(sp[1]); + sk = sp[0]; + return 0; } diff --git a/abuse.h b/abuse.h index 7bc093f..ced6ae6 100644 --- a/abuse.h +++ b/abuse.h @@ -1,14 +1,18 @@ #ifndef ABUSE_H_INCLUDED #define ABUSE_H_INCLUDED +#include + struct abuse_operations { void (*read)(); void (*write)(); void (*disc)(); void (*flush)(); void (*trim)(); + + u_int64_t size; }; -int abuse_main(); +int abuse_main(int argc, char *argv[], struct abuse_operations *aop); #endif /* ABUSE_H_INCLUDED */ diff --git a/abuse_example.c b/abuse_example.c index 5cb7a8a..42e27fc 100644 --- a/abuse_example.c +++ b/abuse_example.c @@ -1,6 +1,8 @@ +#include + #include "abuse.h" -int main() +int main(int argc, char *argv[]) { - return abuse_main(); + return abuse_main(argc, argv, NULL); }