From: Steinar H. Gunderson Date: Mon, 15 Apr 2013 22:32:52 +0000 (+0200) Subject: Replace all perror() calls with our own log calls. X-Git-Tag: 1.0.0~104^2 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=4856c49f1b63753ce86ad759ee649a1117628a8e Replace all perror() calls with our own log calls. --- diff --git a/acceptor.cpp b/acceptor.cpp index ecebf7e..70cd30c 100644 --- a/acceptor.cpp +++ b/acceptor.cpp @@ -10,6 +10,7 @@ #include #include "acceptor.h" +#include "log.h" #include "serverpool.h" #include "state.pb.h" @@ -28,26 +29,26 @@ int create_server_socket(int port, SocketType socket_type) 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); } @@ -57,13 +58,13 @@ int create_server_socket(int port, SocketType socket_type) addr.sin6_port = htons(port); if (bind(server_sock, reinterpret_cast(&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); } } @@ -99,7 +100,7 @@ void Acceptor::close_socket() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); } } @@ -117,7 +118,7 @@ void Acceptor::do_work() continue; } if (nfds == -1) { - perror("poll"); + log_perror("poll"); usleep(100000); continue; } @@ -131,7 +132,7 @@ void Acceptor::do_work() continue; } if (sock == -1) { - perror("accept"); + log_perror("accept"); usleep(100000); continue; } @@ -139,7 +140,7 @@ void Acceptor::do_work() // Set the socket as nonblocking. int one = 1; if (ioctl(sock, FIONBIO, &one) == -1) { - perror("FIONBIO"); + log_perror("FIONBIO"); exit(1); } diff --git a/client.cpp b/client.cpp index 3a25521..bb2d26c 100644 --- a/client.cpp +++ b/client.cpp @@ -4,6 +4,7 @@ #include #include "client.h" +#include "log.h" #include "markpool.h" #include "state.pb.h" #include "stream.h" @@ -29,12 +30,12 @@ Client::Client(int sock) socklen_t addr_len = sizeof(addr); if (getpeername(sock, reinterpret_cast(&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; @@ -64,7 +65,7 @@ Client::Client(const ClientProto &serialized, Stream *stream) } if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) { if (fwmark != 0) { - perror("setsockopt(SO_MARK)"); + log_perror("setsockopt(SO_MARK)"); } } } diff --git a/config.cpp b/config.cpp index ec9a0e1..660090d 100644 --- a/config.cpp +++ b/config.cpp @@ -26,7 +26,7 @@ bool read_config(const string &filename, vector *lines) { FILE *fp = fopen(filename.c_str(), "r"); if (fp == NULL) { - perror(filename.c_str()); + log_perror(filename.c_str()); return false; } diff --git a/httpinput.cpp b/httpinput.cpp index 0c5c620..962be2a 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -62,7 +62,7 @@ void HTTPInput::close_socket() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close()"); + log_perror("close()"); } } @@ -115,7 +115,7 @@ int HTTPInput::lookup_and_connect(const string &host, const string &port) } while (err == -1 && errno == EINTR); if (err == -1) { - perror("close"); + log_perror("close"); // Can still continue. } @@ -223,7 +223,7 @@ void HTTPInput::do_work() continue; } if (nfds == -1) { - perror("poll"); + log_perror("poll"); state = CLOSING_SOCKET; } } @@ -249,7 +249,7 @@ void HTTPInput::do_work() // 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; @@ -267,7 +267,7 @@ void HTTPInput::do_work() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("write"); + log_perror("write"); state = CLOSING_SOCKET; continue; } @@ -289,7 +289,7 @@ void HTTPInput::do_work() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("read"); + log_perror("read"); state = CLOSING_SOCKET; continue; } @@ -343,7 +343,7 @@ void HTTPInput::do_work() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("read"); + log_perror("read"); state = CLOSING_SOCKET; continue; } @@ -365,7 +365,7 @@ void HTTPInput::do_work() } while (err == -1 && errno == EINTR); if (err == -1) { - perror("close"); + log_perror("close"); } state = NOT_CONNECTED; diff --git a/log.cpp b/log.cpp index ba05a64..a4f9236 100644 --- a/log.cpp +++ b/log.cpp @@ -1,9 +1,11 @@ #include "log.h" #include +#include #include #include #include +#include #include #include @@ -110,3 +112,10 @@ void log(LogLevel log_level, const char *fmt, ...) } } } + +void log_perror(const char *msg) +{ + char errbuf[4096]; + strerror_r(errno, errbuf, sizeof(errbuf)); + log(ERROR, "%s: %s", msg, errbuf); +} diff --git a/log.h b/log.h index 7e51fcb..ce731ab 100644 --- a/log.h +++ b/log.h @@ -20,5 +20,6 @@ void start_logging(); void shut_down_logging(); void log(LogLevel log_level, const char *fmt, ...); +void log_perror(const char *msg); #endif // !defined(_LOG_H) diff --git a/main.cpp b/main.cpp index 8825631..4a5904b 100644 --- a/main.cpp +++ b/main.cpp @@ -197,14 +197,14 @@ bool dry_run_config(const std::string &argv0, const std::string &config_filename 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. @@ -221,7 +221,7 @@ bool dry_run_config(const std::string &argv0, const std::string &config_filename } while (err == -1 && errno == EINTR); if (err == -1) { - perror("waitpid()"); + log_perror("waitpid()"); return false; } @@ -402,7 +402,7 @@ start: 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); diff --git a/server.cpp b/server.cpp index 02efdbb..46bda5b 100644 --- a/server.cpp +++ b/server.cpp @@ -31,7 +31,7 @@ Server::Server() epoll_fd = epoll_create(1024); // Size argument is ignored. if (epoll_fd == -1) { - perror("epoll_fd"); + log_perror("epoll_fd"); exit(1); } } @@ -44,7 +44,7 @@ Server::~Server() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close(epoll_fd)"); + log_perror("close(epoll_fd)"); } } @@ -72,7 +72,7 @@ void Server::do_work() continue; } if (nfds == -1) { - perror("epoll_wait"); + log_perror("epoll_wait"); exit(1); } @@ -141,7 +141,7 @@ void Server::add_client(int sock) ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP; ev.data.u64 = reinterpret_cast(&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); } @@ -173,7 +173,7 @@ void Server::add_client_from_serialized(const ClientProto &client) ev.data.u64 = 0; // Keep Valgrind happy. ev.data.u64 = reinterpret_cast(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); } @@ -254,7 +254,7 @@ read_request_again: return; } if (ret == -1) { - perror("read"); + log_perror("read"); close_client(client); return; } @@ -316,7 +316,7 @@ sending_header_or_error_again: if (ret == -1) { // Error! Postcondition #1. - perror("write"); + log_perror("write"); close_client(client); return; } @@ -387,7 +387,7 @@ sending_data_again: } if (ret == -1) { // Error, close; postcondition #1. - perror("sendfile"); + log_perror("sendfile"); close_client(client); return; } @@ -435,7 +435,7 @@ int Server::parse_request(Client *client) } 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(); @@ -455,7 +455,7 @@ void Server::construct_header(Client *client) ev.data.u64 = reinterpret_cast(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); } } @@ -475,7 +475,7 @@ void Server::construct_error(Client *client, int error_code) ev.data.u64 = reinterpret_cast(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); } } @@ -490,7 +490,7 @@ void delete_from(vector *v, T elem) 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); } @@ -511,7 +511,7 @@ void Server::close_client(Client *client) } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); } clients.erase(client->sock); diff --git a/stats.cpp b/stats.cpp index f5916ed..71fb485 100644 --- a/stats.cpp +++ b/stats.cpp @@ -10,6 +10,7 @@ #include #include "client.h" +#include "log.h" #include "serverpool.h" #include "stats.h" @@ -35,14 +36,14 @@ void StatsThread::do_work() 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); @@ -61,14 +62,14 @@ void StatsThread::do_work() (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); } @@ -89,7 +90,7 @@ sleep: break; } if (nfds == -1) { - perror("poll"); + log_perror("poll"); usleep(100000); continue; } diff --git a/stream.cpp b/stream.cpp index ab10e35..2b9a695 100644 --- a/stream.cpp +++ b/stream.cpp @@ -6,6 +6,7 @@ #include #include "state.pb.h" +#include "log.h" #include "stream.h" #include "util.h" @@ -31,7 +32,7 @@ Stream::~Stream() ret = close(data_fd); } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); } } } @@ -116,7 +117,7 @@ void Stream::add_data(const char *data, ssize_t bytes) continue; } if (ret == -1) { - perror("pwrite"); + log_perror("pwrite"); // Dazed and confused, but trying to continue... break; } @@ -134,7 +135,7 @@ void Stream::add_data(const char *data, ssize_t bytes) continue; } if (ret == -1) { - perror("pwrite"); + log_perror("pwrite"); // Dazed and confused, but trying to continue... break; } diff --git a/thread.cpp b/thread.cpp index b263be6..f719eac 100644 --- a/thread.cpp +++ b/thread.cpp @@ -5,6 +5,7 @@ #include #include +#include "log.h" #include "thread.h" Thread::~Thread() {} @@ -14,7 +15,7 @@ void Thread::run() should_stop = false; int pipefd[2]; if (pipe2(pipefd, O_CLOEXEC) == -1) { - perror("pipe"); + log_perror("pipe"); exit(1); } stop_fd_read = pipefd[0]; @@ -32,7 +33,7 @@ void Thread::stop() } while (err == -1 && errno == EINTR); if (err == -1) { - perror("write"); + log_perror("write"); exit(1); } @@ -41,13 +42,13 @@ void Thread::stop() } 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); } @@ -56,7 +57,7 @@ void Thread::stop() } while (err == -1 && errno == EINTR); if (err == -1) { - perror("close"); + log_perror("close"); // Can continue (we have close-on-exec). } } diff --git a/udpinput.cpp b/udpinput.cpp index 081f631..3a8beff 100644 --- a/udpinput.cpp +++ b/udpinput.cpp @@ -61,7 +61,7 @@ void UDPInput::close_socket() } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close()"); + log_perror("close()"); } sock = -1; @@ -102,7 +102,7 @@ void UDPInput::do_work() continue; } if (nfds == -1) { - perror("poll"); + log_perror("poll"); close_socket(); continue; } @@ -114,7 +114,7 @@ void UDPInput::do_work() } while (ret == -1 && errno == EINTR); if (ret <= 0) { - perror("recv"); + log_perror("recv"); close_socket(); continue; } diff --git a/util.cpp b/util.cpp index 41c13c6..8159e38 100644 --- a/util.cpp +++ b/util.cpp @@ -14,12 +14,12 @@ int make_tempfile(const std::string &contents) 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; } @@ -28,7 +28,7 @@ int make_tempfile(const std::string &contents) while (to_write > 0) { ssize_t ret = write(fd, ptr, to_write); if (ret == -1) { - perror("write"); + log_perror("write"); close(fd); return -1; } @@ -47,7 +47,7 @@ bool read_tempfile(int fd, std::string *contents) off_t len = lseek(fd, 0, SEEK_END); if (len == -1) { - perror("lseek"); + log_perror("lseek"); ok = false; goto done; } @@ -55,7 +55,7 @@ bool read_tempfile(int fd, std::string *contents) contents->resize(len); if (lseek(fd, 0, SEEK_SET) == -1) { - perror("lseek"); + log_perror("lseek"); ok = false; goto done; } @@ -64,7 +64,7 @@ bool read_tempfile(int fd, std::string *contents) 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; } @@ -82,7 +82,7 @@ done: } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); // Can still continue. }