Set up process limits and socket timeouts

(Until we have non-blocking sockets + poll())
This commit is contained in:
sin 2014-08-21 23:09:03 +01:00 committed by FRIGN
parent 4f18d89f22
commit de74d9b8fe

19
quark.c
View file

@ -14,6 +14,7 @@
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/resource.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -23,6 +24,7 @@ char *argv0;
#define LENGTH(x) (sizeof x / sizeof x[0]) #define LENGTH(x) (sizeof x / sizeof x[0])
#define MAXBUFLEN 1024 #define MAXBUFLEN 1024
#define NPROCS 512
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
#define HttpOk "200 OK" #define HttpOk "200 OK"
@ -495,6 +497,7 @@ invalid_request:
void void
serve(int fd) { serve(int fd) {
int result; int result;
struct timeval tv;
socklen_t salen; socklen_t salen;
struct sockaddr sa; struct sockaddr sa;
@ -521,6 +524,14 @@ serve(int fd) {
break; break;
} }
/* If we haven't received any data within this period, close the
* socket to avoid spamming the process table */
tv.tv_sec = 30;
tv.tv_usec = 0;
if (setsockopt(req.fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
logerrmsg("error\tsetsockopt SO_RCVTIMEO failed: %s\n",
strerror(errno));
result = request(); result = request();
shutdown(req.fd, SHUT_RD); shutdown(req.fd, SHUT_RD);
status = -1; status = -1;
@ -562,6 +573,7 @@ main(int argc, char *argv[]) {
struct addrinfo hints, *ai = NULL; struct addrinfo hints, *ai = NULL;
struct passwd *upwd = NULL; struct passwd *upwd = NULL;
struct group *gpwd = NULL; struct group *gpwd = NULL;
struct rlimit rlim;
int i; int i;
ARGBEGIN { ARGBEGIN {
@ -647,6 +659,13 @@ main(int argc, char *argv[]) {
goto err; goto err;
} }
rlim.rlim_cur = NPROCS;
rlim.rlim_max = NPROCS;
if (setrlimit(RLIMIT_NPROC, &rlim) == -1) {
logerrmsg("error\tsetrlimit RLIMIT_NPROC: %s\n", strerror(errno));
goto err;
}
if (chdir(docroot) == -1) { if (chdir(docroot) == -1) {
logerrmsg("error\tchdir %s: %s\n", docroot, strerror(errno)); logerrmsg("error\tchdir %s: %s\n", docroot, strerror(errno));
goto err; goto err;