forked from spectrumero/tnfsd
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from trekawek/events
Support native event mechanisms on BSD and Linux.
- Loading branch information
Showing
14 changed files
with
341 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bin | ||
src/*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
FROM ubuntu:latest AS build | ||
RUN apt update && apt install -y build-essential | ||
WORKDIR /src | ||
COPY src/* . | ||
RUN make clean all OS=LINUX | ||
|
||
FROM ubuntu:latest | ||
MAINTAINER Thomas Cherryhomes <[email protected]> | ||
LABEL org.opencontainers.image.authors="[email protected]" | ||
EXPOSE 16384 | ||
EXPOSE 16384/udp | ||
VOLUME /data | ||
ADD bin/tnfsd /bin | ||
RUN chmod +x /bin/tnfsd | ||
ENTRYPOINT ["/bin/tnfsd","/data"] | ||
COPY --from=build /bin/tnfsd /bin/tnfsd | ||
|
||
ENTRYPOINT ["/bin/tnfsd", "/data"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef _EVENT_H | ||
#define _EVENT_H | ||
|
||
#include <stdbool.h> | ||
|
||
#ifndef SOCKET_ERROR | ||
#define SOCKET_ERROR -1 | ||
#endif | ||
|
||
struct event_wait_res | ||
{ | ||
int *fds; | ||
int size; | ||
}; | ||
typedef struct event_wait_res event_wait_res_t; | ||
|
||
// Initializes the event queue. | ||
void tnfs_event_init(); | ||
|
||
// Registers the file descriptor to watch. | ||
bool tnfs_event_register(int fd); | ||
|
||
// Unregisters the file descriptor. | ||
void tnfs_event_unregister(int fd); | ||
|
||
// Waits for a given amount of time for an event on any registered file descriptor. | ||
// Returns the file descriptor number of 0 if timeout occurs. | ||
event_wait_res_t* tnfs_event_wait(int timeout_sec); | ||
|
||
// Returns | ||
bool tnfs_event_is_active(event_wait_res_t* res, int fds); | ||
|
||
// Closes the event queue. | ||
void tnfs_event_close(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "event.h" | ||
|
||
bool tnfs_event_is_active(event_wait_res_t* res, int fds) | ||
{ | ||
for (int i = 0; i < res->size; i++) | ||
{ | ||
if (res->fds[i] == fds) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "event.h" | ||
#include "log.h" | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/epoll.h> | ||
#include <unistd.h> | ||
|
||
#define _EVENT_MAX_FDS 4096 | ||
|
||
struct epoll_event events[_EVENT_MAX_FDS]; | ||
int epfd; | ||
event_wait_res_t wait_result; | ||
|
||
void tnfs_event_init() | ||
{ | ||
epfd = epoll_create(1); | ||
wait_result.fds = calloc(_EVENT_MAX_FDS, sizeof(int)); | ||
} | ||
|
||
bool tnfs_event_register(int fd) | ||
{ | ||
struct epoll_event ev; | ||
ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP; | ||
ev.data.fd = fd; | ||
|
||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) | ||
{ | ||
LOG("tnfs_event_register: can't register epoll descriptor\n"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void tnfs_event_unregister(int fd) | ||
{ | ||
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); | ||
} | ||
|
||
event_wait_res_t* tnfs_event_wait(int timeout_sec) | ||
{ | ||
int readyfds = epoll_wait(epfd, events, _EVENT_MAX_FDS, timeout_sec * 1000); | ||
|
||
wait_result.size = readyfds; | ||
memset(wait_result.fds, 0, _EVENT_MAX_FDS * sizeof(int)); | ||
|
||
if (readyfds == -1) | ||
{ | ||
LOG("tnfs_event_wait: epoll_wait failed\n"); | ||
wait_result.size = -1; | ||
return &wait_result; | ||
} | ||
|
||
for (int i = 0; i < readyfds; i++) | ||
{ | ||
wait_result.fds[i] = events[i].data.fd; | ||
} | ||
|
||
return &wait_result; | ||
|
||
} | ||
|
||
void tnfs_event_close() | ||
{ | ||
close(epfd); | ||
} |
Oops, something went wrong.