#include <unistd.h>
#include "acceptor.h"
+#include "log.h"
#include "serverpool.h"
#include "state.pb.h"
server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
}
if (server_sock == -1) {
- perror("socket");
+ log_perror("socket");
exit(1);
}
int one = 1;
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
- perror("setsockopt(SO_REUSEADDR)");
+ log_perror("setsockopt(SO_REUSEADDR)");
exit(1);
}
// We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
int zero = 0;
if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
- perror("setsockopt(IPV6_V6ONLY)");
+ log_perror("setsockopt(IPV6_V6ONLY)");
exit(1);
}
// Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
if (ioctl(server_sock, FIONBIO, &one) == -1) {
- perror("ioctl(FIONBIO)");
+ log_perror("ioctl(FIONBIO)");
exit(1);
}
addr.sin6_port = htons(port);
if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
- perror("bind");
+ log_perror("bind");
exit(1);
}
if (socket_type == TCP_SOCKET) {
if (listen(server_sock, 128) == -1) {
- perror("listen");
+ log_perror("listen");
exit(1);
}
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close");
+ log_perror("close");
}
}
continue;
}
if (nfds == -1) {
- perror("poll");
+ log_perror("poll");
usleep(100000);
continue;
}
continue;
}
if (sock == -1) {
- perror("accept");
+ log_perror("accept");
usleep(100000);
continue;
}
// Set the socket as nonblocking.
int one = 1;
if (ioctl(sock, FIONBIO, &one) == -1) {
- perror("FIONBIO");
+ log_perror("FIONBIO");
exit(1);
}
#include <sys/socket.h>
#include "client.h"
+#include "log.h"
#include "markpool.h"
#include "state.pb.h"
#include "stream.h"
socklen_t addr_len = sizeof(addr);
if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
- perror("getpeername");
+ log_perror("getpeername");
remote_addr = "";
} else {
char buf[INET6_ADDRSTRLEN];
if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
- perror("inet_ntop");
+ log_perror("inet_ntop");
remote_addr = "";
} else {
remote_addr = buf;
}
if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
if (fwmark != 0) {
- perror("setsockopt(SO_MARK)");
+ log_perror("setsockopt(SO_MARK)");
}
}
}
{
FILE *fp = fopen(filename.c_str(), "r");
if (fp == NULL) {
- perror(filename.c_str());
+ log_perror(filename.c_str());
return false;
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close()");
+ log_perror("close()");
}
}
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("close");
+ log_perror("close");
// Can still continue.
}
continue;
}
if (nfds == -1) {
- perror("poll");
+ log_perror("poll");
state = CLOSING_SOCKET;
}
}
// Yay, successful connect. Try to set it as nonblocking.
int one = 1;
if (ioctl(sock, FIONBIO, &one) == -1) {
- perror("ioctl(FIONBIO)");
+ log_perror("ioctl(FIONBIO)");
state = CLOSING_SOCKET;
} else {
state = SENDING_REQUEST;
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("write");
+ log_perror("write");
state = CLOSING_SOCKET;
continue;
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("read");
+ log_perror("read");
state = CLOSING_SOCKET;
continue;
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("read");
+ log_perror("read");
state = CLOSING_SOCKET;
continue;
}
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("close");
+ log_perror("close");
}
state = NOT_CONNECTED;
#include "log.h"
#include <stdio.h>
+#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <assert.h>
+#include <errno.h>
#include <string>
#include <vector>
}
}
}
+
+void log_perror(const char *msg)
+{
+ char errbuf[4096];
+ strerror_r(errno, errbuf, sizeof(errbuf));
+ log(ERROR, "%s: %s", msg, errbuf);
+}
void shut_down_logging();
void log(LogLevel log_level, const char *fmt, ...);
+void log_perror(const char *msg);
#endif // !defined(_LOG_H)
pid_t pid = fork();
switch (pid) {
case -1:
- perror("fork()");
+ log_perror("fork()");
free(argv0_copy);
free(config_filename_copy);
return false;
case 0:
// Child.
execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, NULL);
- perror(argv0_copy);
+ log_perror(argv0_copy);
_exit(1);
default:
// Parent.
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("waitpid()");
+ log_perror("waitpid()");
return false;
}
for ( ;; ) {
execlp(argv[0], argv[0], config_filename.c_str(), "--state", buf, NULL);
open_logs(config.log_destinations);
- perror("execlp");
+ log_perror("execlp");
log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv[0]);
shut_down_logging();
usleep(200000);
epoll_fd = epoll_create(1024); // Size argument is ignored.
if (epoll_fd == -1) {
- perror("epoll_fd");
+ log_perror("epoll_fd");
exit(1);
}
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close(epoll_fd)");
+ log_perror("close(epoll_fd)");
}
}
continue;
}
if (nfds == -1) {
- perror("epoll_wait");
+ log_perror("epoll_wait");
exit(1);
}
ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
ev.data.u64 = reinterpret_cast<uint64_t>(&clients[sock]);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_ADD)");
+ log_perror("epoll_ctl(EPOLL_CTL_ADD)");
exit(1);
}
ev.data.u64 = 0; // Keep Valgrind happy.
ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_ADD)");
+ log_perror("epoll_ctl(EPOLL_CTL_ADD)");
exit(1);
}
return;
}
if (ret == -1) {
- perror("read");
+ log_perror("read");
close_client(client);
return;
}
if (ret == -1) {
// Error! Postcondition #1.
- perror("write");
+ log_perror("write");
close_client(client);
return;
}
}
if (ret == -1) {
// Error, close; postcondition #1.
- perror("sendfile");
+ log_perror("sendfile");
close_client(client);
return;
}
}
if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {
if (client->fwmark != 0) {
- perror("setsockopt(SO_MARK)");
+ log_perror("setsockopt(SO_MARK)");
}
}
client->request.clear();
ev.data.u64 = reinterpret_cast<uint64_t>(client);
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_MOD)");
+ log_perror("epoll_ctl(EPOLL_CTL_MOD)");
exit(1);
}
}
ev.data.u64 = reinterpret_cast<uint64_t>(client);
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
- perror("epoll_ctl(EPOLL_CTL_MOD)");
+ log_perror("epoll_ctl(EPOLL_CTL_MOD)");
exit(1);
}
}
void Server::close_client(Client *client)
{
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
- perror("epoll_ctl(EPOLL_CTL_DEL)");
+ log_perror("epoll_ctl(EPOLL_CTL_DEL)");
exit(1);
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close");
+ log_perror("close");
}
clients.erase(client->sock);
#include <vector>
#include "client.h"
+#include "log.h"
#include "serverpool.h"
#include "stats.h"
char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
fd = mkostemp(filename, O_WRONLY);
if (fd == -1) {
- perror(filename);
+ log_perror(filename);
free(filename);
goto sleep;
}
fp = fdopen(fd, "w");
if (fp == NULL) {
- perror("fdopen");
+ log_perror("fdopen");
close(fd);
unlink(filename);
free(filename);
(long long unsigned)(client_stats[i].num_loss_events));
}
if (fclose(fp) == EOF) {
- perror("fclose");
+ log_perror("fclose");
unlink(filename);
free(filename);
goto sleep;
}
if (rename(filename, stats_file.c_str()) == -1) {
- perror("rename");
+ log_perror("rename");
unlink(filename);
}
break;
}
if (nfds == -1) {
- perror("poll");
+ log_perror("poll");
usleep(100000);
continue;
}
#include <vector>
#include "state.pb.h"
+#include "log.h"
#include "stream.h"
#include "util.h"
ret = close(data_fd);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close");
+ log_perror("close");
}
}
}
continue;
}
if (ret == -1) {
- perror("pwrite");
+ log_perror("pwrite");
// Dazed and confused, but trying to continue...
break;
}
continue;
}
if (ret == -1) {
- perror("pwrite");
+ log_perror("pwrite");
// Dazed and confused, but trying to continue...
break;
}
#include <signal.h>
#include <errno.h>
+#include "log.h"
#include "thread.h"
Thread::~Thread() {}
should_stop = false;
int pipefd[2];
if (pipe2(pipefd, O_CLOEXEC) == -1) {
- perror("pipe");
+ log_perror("pipe");
exit(1);
}
stop_fd_read = pipefd[0];
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("write");
+ log_perror("write");
exit(1);
}
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("close");
+ log_perror("close");
// Can continue (we have close-on-exec).
}
pthread_kill(worker_thread, SIGHUP);
if (pthread_join(worker_thread, NULL) == -1) {
- perror("pthread_join");
+ log_perror("pthread_join");
exit(1);
}
} while (err == -1 && errno == EINTR);
if (err == -1) {
- perror("close");
+ log_perror("close");
// Can continue (we have close-on-exec).
}
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close()");
+ log_perror("close()");
}
sock = -1;
continue;
}
if (nfds == -1) {
- perror("poll");
+ log_perror("poll");
close_socket();
continue;
}
} while (ret == -1 && errno == EINTR);
if (ret <= 0) {
- perror("recv");
+ log_perror("recv");
close_socket();
continue;
}
char filename[] = "/tmp/cubemap.XXXXXX";
int fd = mkstemp(filename);
if (fd == -1) {
- perror("mkstemp");
+ log_perror("mkstemp");
return -1;
}
if (unlink(filename) == -1) {
- perror("unlink");
+ log_perror("unlink");
// Can still continue;
}
while (to_write > 0) {
ssize_t ret = write(fd, ptr, to_write);
if (ret == -1) {
- perror("write");
+ log_perror("write");
close(fd);
return -1;
}
off_t len = lseek(fd, 0, SEEK_END);
if (len == -1) {
- perror("lseek");
+ log_perror("lseek");
ok = false;
goto done;
}
contents->resize(len);
if (lseek(fd, 0, SEEK_SET) == -1) {
- perror("lseek");
+ log_perror("lseek");
ok = false;
goto done;
}
while (has_read < len) {
ret = read(fd, &((*contents)[has_read]), len - has_read);
if (ret == -1) {
- perror("read");
+ log_perror("read");
ok = false;
goto done;
}
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
- perror("close");
+ log_perror("close");
// Can still continue.
}