fusefs: support unix-domain sockets
Also, fix the teardown of the Fifo.read_write test Sponsored by: The FreeBSD Foundation
This commit is contained in:
parent
be59a9a0dc
commit
309be7b892
@ -548,7 +548,7 @@ fuse_vnop_create(struct vop_create_args *ap)
|
||||
}
|
||||
bzero(&fdi, sizeof(fdi));
|
||||
|
||||
if ((vap->va_type != VREG))
|
||||
if ((vap->va_type != VREG && vap->va_type != VSOCK))
|
||||
return (EINVAL);
|
||||
|
||||
if (!fsess_isimpl(mp, FUSE_CREATE)) {
|
||||
@ -583,7 +583,7 @@ fuse_vnop_create(struct vop_create_args *ap)
|
||||
|
||||
feo = fdip->answ;
|
||||
|
||||
if ((err = fuse_internal_checkentry(feo, VREG))) {
|
||||
if ((err = fuse_internal_checkentry(feo, vap->va_type))) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -603,7 +603,7 @@ fuse_vnop_create(struct vop_create_args *ap)
|
||||
goto out;
|
||||
foo = fdip2->answ;
|
||||
}
|
||||
err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, VREG);
|
||||
err = fuse_vnode_get(mp, feo, feo->nodeid, dvp, vpp, cnp, vap->va_type);
|
||||
if (err) {
|
||||
struct fuse_release_in *fri;
|
||||
uint64_t nodeid = feo->nodeid;
|
||||
|
@ -30,6 +30,8 @@
|
||||
|
||||
extern "C" {
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
}
|
||||
|
||||
#include "mockfs.hh"
|
||||
@ -40,13 +42,14 @@ using namespace testing;
|
||||
class Create: public FuseTest {
|
||||
public:
|
||||
|
||||
void expect_create(const char *relpath, ProcessMockerT r)
|
||||
void expect_create(const char *relpath, mode_t mode, ProcessMockerT r)
|
||||
{
|
||||
EXPECT_CALL(*m_mock, process(
|
||||
ResultOf([=](auto in) {
|
||||
const char *name = (const char*)in->body.bytes +
|
||||
sizeof(fuse_open_in);
|
||||
return (in->header.opcode == FUSE_CREATE &&
|
||||
in->body.open.mode == mode &&
|
||||
(0 == strcmp(relpath, name)));
|
||||
}, Eq(true)),
|
||||
_)
|
||||
@ -63,14 +66,15 @@ TEST_F(Create, attr_cache)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnImmediate([=](auto in __unused, auto out) {
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
@ -98,10 +102,10 @@ TEST_F(Create, eexist)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnErrno(EEXIST));
|
||||
expect_create(RELPATH, mode, ReturnErrno(EEXIST));
|
||||
EXPECT_NE(0, open(FULLPATH, O_CREAT | O_EXCL, mode));
|
||||
EXPECT_EQ(EEXIST, errno);
|
||||
}
|
||||
@ -114,12 +118,12 @@ TEST_F(Create, Enosys)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnErrno(ENOSYS));
|
||||
expect_create(RELPATH, mode, ReturnErrno(ENOSYS));
|
||||
|
||||
EXPECT_CALL(*m_mock, process(
|
||||
ResultOf([=](auto in) {
|
||||
@ -133,7 +137,7 @@ TEST_F(Create, Enosys)
|
||||
_)
|
||||
).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, entry);
|
||||
out->body.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.entry.attr.mode = mode;
|
||||
out->body.entry.nodeid = ino;
|
||||
out->body.entry.entry_valid = UINT64_MAX;
|
||||
out->body.entry.attr_valid = UINT64_MAX;
|
||||
@ -162,7 +166,7 @@ TEST_F(Create, entry_cache_negative)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
/*
|
||||
@ -174,9 +178,10 @@ TEST_F(Create, entry_cache_negative)
|
||||
|
||||
/* create will first do a LOOKUP, adding a negative cache entry */
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(ReturnNegativeCache(&entry_valid));
|
||||
expect_create(RELPATH, ReturnImmediate([=](auto in __unused, auto out) {
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
@ -194,7 +199,7 @@ TEST_F(Create, entry_cache_negative_purge)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
struct timespec entry_valid = {.tv_sec = TIME_T_MAX, .tv_nsec = 0};
|
||||
@ -205,9 +210,10 @@ TEST_F(Create, entry_cache_negative_purge)
|
||||
.RetiresOnSaturation();
|
||||
|
||||
/* Then the CREATE should purge the negative cache entry */
|
||||
expect_create(RELPATH, ReturnImmediate([=](auto in __unused, auto out) {
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
}));
|
||||
@ -230,10 +236,10 @@ TEST_F(Create, eperm)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnErrno(EPERM));
|
||||
expect_create(RELPATH, mode, ReturnErrno(EPERM));
|
||||
|
||||
EXPECT_NE(0, open(FULLPATH, O_CREAT | O_EXCL, mode));
|
||||
EXPECT_EQ(EPERM, errno);
|
||||
@ -243,14 +249,15 @@ TEST_F(Create, ok)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0755;
|
||||
mode_t mode = S_IFREG | 0755;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnImmediate([=](auto in __unused, auto out) {
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
@ -261,6 +268,34 @@ TEST_F(Create, ok)
|
||||
/* Deliberately leak fd. close(2) will be tested in release.cc */
|
||||
}
|
||||
|
||||
/* Create a unix-domain socket */
|
||||
TEST_F(Create, socket)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_sock";
|
||||
const char RELPATH[] = "some_sock";
|
||||
mode_t mode = S_IFSOCK | 0755;
|
||||
struct sockaddr_un sa;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
}));
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ASSERT_LE(0, fd) << strerror(errno);
|
||||
sa.sun_family = AF_UNIX;
|
||||
strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
|
||||
ASSERT_EQ(0, bind(fd, (struct sockaddr*)&sa, sizeof(sa)))
|
||||
<< strerror(errno);
|
||||
}
|
||||
|
||||
/*
|
||||
* A regression test for a bug that affected old FUSE implementations:
|
||||
* open(..., O_WRONLY | O_CREAT, 0444) should work despite the seeming
|
||||
@ -273,14 +308,15 @@ TEST_F(Create, wronly_0444)
|
||||
{
|
||||
const char FULLPATH[] = "mountpoint/some_file.txt";
|
||||
const char RELPATH[] = "some_file.txt";
|
||||
mode_t mode = 0444;
|
||||
mode_t mode = S_IFREG | 0444;
|
||||
uint64_t ino = 42;
|
||||
int fd;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
expect_create(RELPATH, ReturnImmediate([=](auto in __unused, auto out) {
|
||||
expect_create(RELPATH, mode,
|
||||
ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = S_IFREG | mode;
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
|
@ -30,6 +30,8 @@
|
||||
|
||||
extern "C" {
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <fcntl.h>
|
||||
}
|
||||
|
||||
@ -44,8 +46,21 @@ const char MESSAGE[] = "Hello, World!\n";
|
||||
const int msgsize = sizeof(MESSAGE);
|
||||
|
||||
class Fifo: public FuseTest {
|
||||
public:
|
||||
pthread_t m_child;
|
||||
|
||||
Fifo(): m_child(NULL) {};
|
||||
|
||||
void TearDown() {
|
||||
if (m_child != NULL) {
|
||||
pthread_join(m_child, NULL);
|
||||
}
|
||||
FuseTest::TearDown();
|
||||
}
|
||||
};
|
||||
|
||||
class Socket: public Fifo {};
|
||||
|
||||
/* Writer thread */
|
||||
static void* writer(void* arg) {
|
||||
ssize_t sent = 0;
|
||||
@ -70,7 +85,6 @@ static void* writer(void* arg) {
|
||||
*/
|
||||
TEST_F(Fifo, read_write)
|
||||
{
|
||||
pthread_t th0;
|
||||
mode_t mode = S_IFIFO | 0755;
|
||||
const int bufsize = 80;
|
||||
char message[bufsize];
|
||||
@ -82,7 +96,7 @@ TEST_F(Fifo, read_write)
|
||||
|
||||
fd = open(FULLPATH, O_RDWR);
|
||||
ASSERT_LE(0, fd) << strerror(errno);
|
||||
ASSERT_EQ(0, pthread_create(&th0, NULL, writer, &fd))
|
||||
ASSERT_EQ(0, pthread_create(&m_child, NULL, writer, &fd))
|
||||
<< strerror(errno);
|
||||
while (recvd < msgsize) {
|
||||
r = read(fd, message + recvd, bufsize - recvd);
|
||||
@ -94,3 +108,98 @@ TEST_F(Fifo, read_write)
|
||||
|
||||
/* Deliberately leak fd */
|
||||
}
|
||||
|
||||
/* Writer thread */
|
||||
static void* socket_writer(void* arg __unused) {
|
||||
ssize_t sent = 0;
|
||||
int fd, err;
|
||||
struct sockaddr_un sa;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return (void*)(intptr_t)errno;
|
||||
}
|
||||
sa.sun_family = AF_UNIX;
|
||||
strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
|
||||
err = connect(fd, (struct sockaddr*)&sa, sizeof(sa));
|
||||
if (err < 0) {
|
||||
perror("connect");
|
||||
return (void*)(intptr_t)errno;
|
||||
}
|
||||
|
||||
while (sent < msgsize) {
|
||||
ssize_t r;
|
||||
|
||||
r = write(fd, MESSAGE + sent, msgsize - sent);
|
||||
if (r < 0)
|
||||
return (void*)(intptr_t)errno;
|
||||
else
|
||||
sent += r;
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reading and writing unix-domain sockets works. None of the I/O actually
|
||||
* goes through FUSE.
|
||||
*/
|
||||
TEST_F(Socket, read_write)
|
||||
{
|
||||
mode_t mode = S_IFSOCK | 0755;
|
||||
const int bufsize = 80;
|
||||
char message[bufsize];
|
||||
struct sockaddr_un sa;
|
||||
ssize_t recvd = 0, r;
|
||||
uint64_t ino = 42;
|
||||
int fd, connected;
|
||||
Sequence seq;
|
||||
|
||||
EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
|
||||
EXPECT_CALL(*m_mock, process(
|
||||
ResultOf([=](auto in) {
|
||||
return (in->header.opcode == FUSE_CREATE);
|
||||
}, Eq(true)),
|
||||
_)
|
||||
).InSequence(seq)
|
||||
.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, create);
|
||||
out->body.create.entry.attr.mode = mode;
|
||||
out->body.create.entry.nodeid = ino;
|
||||
out->body.create.entry.entry_valid = UINT64_MAX;
|
||||
out->body.create.entry.attr_valid = UINT64_MAX;
|
||||
})));
|
||||
EXPECT_LOOKUP(1, RELPATH)
|
||||
.InSequence(seq)
|
||||
.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto out) {
|
||||
SET_OUT_HEADER_LEN(out, entry);
|
||||
out->body.entry.attr.mode = mode;
|
||||
out->body.entry.nodeid = ino;
|
||||
out->body.entry.attr.nlink = 1;
|
||||
out->body.entry.attr_valid = UINT64_MAX;
|
||||
out->body.entry.entry_valid = UINT64_MAX;
|
||||
})));
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ASSERT_LE(0, fd) << strerror(errno);
|
||||
sa.sun_family = AF_UNIX;
|
||||
strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
|
||||
ASSERT_EQ(0, bind(fd, (struct sockaddr*)&sa, sizeof(sa)))
|
||||
<< strerror(errno);
|
||||
listen(fd, 5);
|
||||
ASSERT_EQ(0, pthread_create(&m_child, NULL, socket_writer, NULL))
|
||||
<< strerror(errno);
|
||||
connected = accept(fd, 0, 0);
|
||||
ASSERT_LE(0, connected) << strerror(errno);
|
||||
|
||||
while (recvd < msgsize) {
|
||||
r = read(connected, message + recvd, bufsize - recvd);
|
||||
ASSERT_LE(0, r) << strerror(errno);
|
||||
ASSERT_LT(0, r) << "unexpected EOF";
|
||||
recvd += r;
|
||||
}
|
||||
ASSERT_STREQ(message, MESSAGE);
|
||||
|
||||
/* Deliberately leak fd */
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user