citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 7f33d903e3a5f6fdfbd3d3eb496418ba14627941
parent cc1c37267bec140151282de0692de070b50b3096
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun, 31 Jul 2016 00:28:44 -0600

src: teach af_unix about CITRUN_SOCKET

Diffstat:
Msrc/af_unix.cc | 21+++++++++++++++------
Msrc/af_unix.h | 15+++++++--------
Msrc/term_main.cc | 5+++--
3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/src/af_unix.cc b/src/af_unix.cc @@ -17,15 +17,17 @@ #include <sys/un.h> // sockaddr_un #include <cerrno> // EWOULDBLOCK -#include <cstring> // memset, strcpy +#include <err.h> // err +#include <cstring> // memset, strlcpy #include <fcntl.h> // fcntl, F_GETFL #include <iostream> #include <stdexcept> -#include <unistd.h> // close +#include <unistd.h> // close, read #include "af_unix.h" -af_unix::af_unix() +af_unix::af_unix() : + m_socket_path("/tmp/citrun.socket") { if ((m_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) err(1, "socket"); @@ -35,7 +37,7 @@ af_unix::~af_unix() { close(m_fd); if (m_bound) - unlink("/tmp/citrun.socket"); + unlink(m_socket_path.c_str()); } af_unix::af_unix(int f) : @@ -65,13 +67,18 @@ af_unix::set_block() err(1, "fcntl(F_SETFL)"); } -void +std::string af_unix::set_listen() { struct sockaddr_un addr; std::memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; - std::strcpy(addr.sun_path, "/tmp/citrun.socket"); + + char *viewer_sock; + if ((viewer_sock = std::getenv("CITRUN_SOCKET")) != NULL) + m_socket_path = viewer_sock; + + strlcpy(addr.sun_path, m_socket_path.c_str(), sizeof(addr.sun_path)); if (bind(m_fd, (struct sockaddr *)&addr, sizeof(addr))) err(1, "bind"); @@ -81,6 +88,8 @@ af_unix::set_listen() // Size 1024 backlog if (listen(m_fd, 1024)) err(1, "listen"); + + return m_socket_path; } af_unix * diff --git a/src/af_unix.h b/src/af_unix.h @@ -1,8 +1,6 @@ #ifndef AF_UNIX_H #define AF_UNIX_H -#include <err.h> // err -#include <unistd.h> // read #include <vector> class af_unix { @@ -11,10 +9,10 @@ public: af_unix(int); ~af_unix(); - void set_listen(); - void set_block(); - void set_nonblock(); - af_unix *accept(); + std::string set_listen(); + void set_block(); + void set_nonblock(); + af_unix *accept(); template<typename T> int read_all(T &buf) @@ -25,8 +23,9 @@ public: int read_all(uint8_t *, size_t); int write_all(uint8_t *, size_t); private: - int m_fd; - int m_bound; + int m_fd; + int m_bound; + std::string m_socket_path; }; #endif diff --git a/src/term_main.cc b/src/term_main.cc @@ -4,6 +4,7 @@ #include <cassert> #include <csignal> // sigaction #include <cstdlib> // exit +#include <err.h> // errx #include <iostream> #include <ncurses.h> #include <queue> @@ -218,7 +219,7 @@ int main(int argc, char *argv[]) { af_unix listen_sock; - listen_sock.set_listen(); + std::string socket_path = listen_sock.set_listen(); initscr(); if (has_colors() == FALSE) { @@ -233,7 +234,7 @@ main(int argc, char *argv[]) init_pair(4, COLOR_CYAN, COLOR_BLACK); init_pair(5, COLOR_MAGENTA, COLOR_BLACK); - printw("Waiting for connection on /tmp/citrun.socket\n"); + printw("Waiting for connection on %s\n", socket_path.c_str()); refresh(); // Block SIGPIPE so that write() will generate errors.