Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- removed most #ifdef's in order to get clean code; rather use flavor…
…- files to move conditional compilation logics to Makefile/configure level
- Loading branch information
Showing
12 changed files
with
364 additions
and
126 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
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,52 @@ | ||
# | ||
# This is the Makefile for the BSD flavor | ||
# | ||
|
||
CXX=c++ -Wall -O2 | ||
LD=c++ | ||
|
||
all: lhttpd frontend | ||
|
||
clean: | ||
rm -f *.o | ||
|
||
distclean: clean | ||
rm -f lhttpd | ||
|
||
lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o | ||
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o -o lhttpd -lrt | ||
|
||
|
||
frontend: lonely.o socket.o frontend-main.o log.o multicore.o rproxy.o config.o misc.o flavor.o | ||
$(LD) lonely.o socket.o frontend-main.o misc.o log.o multicore.o rproxy.o config.o flavor.o -o frontend -lrt | ||
|
||
frontend-main.o: frontend-main.cc | ||
$(CXX) -c frontend-main.cc | ||
|
||
rproxy.o: rproxy.cc rproxy.h | ||
$(CXX) -c rproxy.cc | ||
|
||
config.o: config.cc config.h | ||
$(CXX) -c config.cc | ||
|
||
multicore.o: multicore.cc multicore.h | ||
$(CXX) -c multicore.cc | ||
|
||
log.o: log.cc log.h | ||
$(CXX) -c log.cc | ||
|
||
misc.o: misc.cc misc.h | ||
$(CXX) -c misc.cc | ||
|
||
main.o: main.cc | ||
$(CXX) -c main.cc | ||
|
||
flavor.o: flavor-bsd.cc flavor.h | ||
$(CXX) -c flavor-bsd.cc -o flavor.o | ||
|
||
socket.o: socket.cc socket.h | ||
$(CXX) -c socket.cc | ||
|
||
lonely.o: lonely.cc lonely.h | ||
$(CXX) -c lonely.cc | ||
|
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,74 @@ | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <string> | ||
#include <sys/types.h> | ||
#include <sys/ioctl.h> | ||
#include <sys/mount.h> | ||
#include <sys/socket.h> | ||
#include <sys/stat.h> | ||
#include <sys/sendfile.h> | ||
#include "flavor.h" | ||
|
||
#ifndef BLKGETSIZE64 | ||
#define BLKGETSIZE64 _IOR(0x12,114,size_t) | ||
#endif | ||
|
||
namespace flavor { | ||
|
||
int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags) | ||
{ | ||
int afd = 0; | ||
|
||
if ((afd = accept(fd, saddr, slen)) < 0) | ||
return -1; | ||
if (flags == NONBLOCK) { | ||
// no error check | ||
fcntl(afd, F_SETFL, O_RDWR|O_NONBLOCK); | ||
} | ||
return afd; | ||
} | ||
|
||
bool servable_device(const struct stat &st) | ||
{ | ||
return S_ISBLK(st.st_mode); | ||
} | ||
|
||
|
||
bool servable_file(const struct stat &st) | ||
{ | ||
// no S_ISLNK() since stat() was used | ||
return S_ISBLK(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode); | ||
} | ||
|
||
|
||
int device_size(const std::string &path, size_t &size, char &sendfile) | ||
{ | ||
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY); | ||
if (fd < 0) | ||
return -1; | ||
|
||
int r = 0, saved_errno = 0; | ||
if (ioctl(fd, BLKGETSIZE64, &size) < 0) { | ||
r = -1; | ||
saved_errno = errno; | ||
} | ||
close(fd); | ||
errno = saved_errno; | ||
return r; | ||
} | ||
|
||
|
||
int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
// Linux can, unlike BSD, use sendfile() on device files, so | ||
// the last parameter is ignored | ||
ssize_t r = 0; | ||
if ((r = ::sendfile(peer, fd, offset, n)) < 0) | ||
return -1; | ||
left -= r; | ||
copied += r; | ||
return 0; | ||
} | ||
|
||
} | ||
|
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,96 @@ | ||
#include <string> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <sys/uio.h> | ||
#include <sys/disk.h> | ||
#include <sys/ioctl.h> | ||
#include "flavor.h" | ||
|
||
|
||
namespace flavor { | ||
|
||
int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags) | ||
{ | ||
int afd = 0; | ||
|
||
if ((afd = accept(fd, saddr, slen)) < 0) | ||
return -1; | ||
if (flags == NONBLOCK) { | ||
// no error check | ||
fcntl(afd, F_SETFL, O_RDWR|O_NONBLOCK); | ||
} | ||
return afd; | ||
} | ||
|
||
|
||
bool servable_device(const struct stat &st) | ||
{ | ||
return S_ISCHR(st.st_mode); | ||
} | ||
|
||
|
||
bool servable_file(const struct stat &st) | ||
{ | ||
// no S_ISLNK() since stat() was used | ||
return S_ISCHR(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode); | ||
} | ||
|
||
|
||
int device_size(const std::string &path, size_t &size, char &sendfile) | ||
{ | ||
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY); | ||
if (fd < 0) | ||
return -1; | ||
|
||
int r = 0, saved_errno = 0; | ||
if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0) { | ||
r = -1; | ||
saved_errno = errno; | ||
} | ||
close(fd); | ||
errno = saved_errno; | ||
|
||
sendfile = 0; | ||
return r; | ||
} | ||
|
||
|
||
int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile) | ||
{ | ||
off_t sbytes = 0; | ||
ssize_t r = 0; | ||
|
||
if (can_sendfile) { | ||
r = ::sendfile(fd, peer, *offset, n, NULL, &sbytes, 0); | ||
if (sbytes > 0) { | ||
*offset += sbytes; | ||
left -= sbytes; | ||
copied += sbytes; | ||
} | ||
// On FreeBSD, device files do not support sendfile() | ||
} else { | ||
char buf[n]; | ||
r = pread(fd, buf, n, *offset); | ||
if (r > 0) { | ||
// write(), not writen() | ||
r = write(cur_peer, buf, r); | ||
if (r > 0) { | ||
*offset += r; | ||
left -= r; | ||
copied += r; | ||
} | ||
} | ||
if (r <= 0) | ||
r = -1; | ||
else | ||
r = 0; | ||
} | ||
return (int)r; | ||
} | ||
|
||
|
||
} // namespace flavor | ||
|
||
|
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 <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <sys/mount.h> | ||
#include <sys/ioctl.h> | ||
#include <string> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <sys/sendfile.h> | ||
|
||
#include "flavor.h" | ||
|
||
|
||
namespace flavor { | ||
|
||
int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags) | ||
{ | ||
return accept4(fd, saddr, slen, flags == NONBLOCK ? SOCK_NONBLOCK : 0); | ||
} | ||
|
||
|
||
bool servable_device(const struct stat &st) | ||
{ | ||
return S_ISBLK(st.st_mode); | ||
} | ||
|
||
|
||
bool servable_file(const struct stat &st) | ||
{ | ||
// no S_ISLNK() since stat() was used | ||
return S_ISBLK(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode); | ||
} | ||
|
||
|
||
int device_size(const std::string &path, size_t &size, char &sendfile) | ||
{ | ||
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY); | ||
if (fd < 0) | ||
return -1; | ||
|
||
int r = 0, saved_errno = 0; | ||
if (ioctl(fd, BLKGETSIZE64, &size) < 0) { | ||
r = -1; | ||
saved_errno = errno; | ||
} | ||
close(fd); | ||
errno = saved_errno; | ||
return r; | ||
} | ||
|
||
|
||
int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile) | ||
{ | ||
// Linux can, unlike BSD, use sendfile() on device files, so | ||
// the last parameter is ignored | ||
ssize_t r = 0; | ||
if ((r = ::sendfile(peer, fd, offset, n)) < 0) | ||
return -1; | ||
left -= r; | ||
copied += r; | ||
return 0; | ||
} | ||
|
||
|
||
} // namespace flavor | ||
|
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,30 @@ | ||
#ifndef __flavor__ | ||
#define __flavor__ | ||
|
||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <string> | ||
|
||
namespace flavor { | ||
|
||
enum { | ||
NONBLOCK = 1 | ||
}; | ||
|
||
int accept(int, struct sockaddr *, socklen_t *, int); | ||
|
||
bool servable_device(const struct stat &); | ||
|
||
bool servable_file(const struct stat &); | ||
|
||
int device_size(const std::string &, size_t &, char &); | ||
|
||
// calls sendfile() if can_sendfile indicates that. returns 0 on success, -1 on error. | ||
// uses normal read/write if sendfile cannot be used. updates offset, left and copied accordingly | ||
int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile); | ||
|
||
} | ||
|
||
#endif // __flavor_h__ | ||
|
Oops, something went wrong.
You could always abuse #include to pull common C code into a separate file. Anyway, it's not a big deal for just one function.