Hacking away at stuff. Pretty soon there should be a working prototype.
This commit is contained in:
parent
38e830a79d
commit
dd485d65c5
4
Makefile
4
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:
|
||||
|
|
56
abuse.c
56
abuse.c
|
@ -1,4 +1,58 @@
|
|||
int abuse_main()
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/types.h>
|
||||
#include <nbd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
6
abuse.h
6
abuse.h
|
@ -1,14 +1,18 @@
|
|||
#ifndef ABUSE_H_INCLUDED
|
||||
#define ABUSE_H_INCLUDED
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
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 */
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "abuse.h"
|
||||
|
||||
int main()
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return abuse_main();
|
||||
return abuse_main(argc, argv, NULL);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue