24 [[nodiscard]]
static std::string getUserName()
29 struct passwd* pw = getpwuid(getuid());
30 return pw->pw_name ? pw->pw_name : std::string{};
34 [[nodiscard]]
static bool checkSocketDir(
zstring_view dir)
37 if (stat(dir.
c_str(), &st)) {
41 if (!S_ISDIR(st.st_mode)) {
47 if ((st.st_mode & 0777) != 0700) {
51 if (st.st_uid != getuid()) {
59 [[nodiscard]]
static bool checkSocket(
zstring_view socket)
62 if (!name.starts_with(
"socket.")) {
67 if (stat(socket.
c_str(), &st)) {
72 if (!S_ISREG(st.st_mode)) {
77 if (!S_ISSOCK(st.st_mode)) {
84 if ((st.st_mode & 0777) != 0600) {
89 if (st.st_uid != getuid()) {
99 [[nodiscard]]
static int openPort(
SOCKET listenSock)
101 const int BASE = 9938;
102 const int RANGE = 64;
106 for (
auto n :
xrange(RANGE)) {
107 int port = BASE + ((first + n) % RANGE);
108 sockaddr_in server_address;
109 memset(&server_address, 0,
sizeof(server_address));
110 server_address.sin_family = AF_INET;
111 server_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
112 server_address.sin_port = htons(port);
113 if (bind(listenSock,
reinterpret_cast<sockaddr*
>(&server_address),
114 sizeof(server_address)) != -1) {
118 throw MSXException(
"Couldn't open socket.");
122 SOCKET CliServer::createSocket()
126 if (!checkSocketDir(dir)) {
127 throw MSXException(
"Couldn't create socket directory.");
129 socketName =
strCat(dir,
"/socket.",
int(getpid()));
132 SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
136 int portNumber = openPort(sd);
142 out << portNumber <<
'\n';
145 throw MSXException(
"Couldn't write socket port file.");
149 SOCKET sd = socket(AF_UNIX, SOCK_STREAM, 0);
157 strncpy(addr.sun_path, socketName.c_str(),
sizeof(addr.sun_path) - 1);
158 addr.sun_path[
sizeof(addr.sun_path) - 1] =
'\0';
159 addr.sun_family = AF_UNIX;
161 if (bind(sd,
reinterpret_cast<sockaddr*
>(&addr),
sizeof(addr)) == -1) {
163 throw MSXException(
"Couldn't bind socket.");
165 if (chmod(socketName.c_str(), 0600) == -1) {
167 throw MSXException(
"Couldn't set socket permissions.");
171 if (!checkSocket(socketName)) {
173 throw MSXException(
"Opened socket fails sanity check.");
177 throw MSXException(
"Couldn't listen to socket: ",
sock_error());
182 void CliServer::exitAcceptLoop()
188 static void deleteSocket(
const std::string& socket)
191 auto dir = socket.
substr(0, socket.find_last_of(
'/'));
199 : commandController(commandController_)
200 , eventDistributor(eventDistributor_)
206 listenSock = createSocket();
207 thread = std::thread([
this]() { mainLoop(); });
220 deleteSocket(socketName);
224 void CliServer::mainLoop()
229 fcntl(listenSock, F_SETFL, O_NONBLOCK);
236 if (poller.
poll(listenSock)) {
240 SOCKET sd = accept(listenSock,
nullptr,
nullptr);
248 if (errno ==
one_of(EAGAIN, EWOULDBLOCK)) {
257 fcntl(sd, F_SETFL, 0);
259 cliComm.
addListener(std::make_unique<SocketConnection>(
260 commandController, eventDistributor, sd));
void printWarning(std::string_view message)
CliServer(CommandController &commandController, EventDistributor &eventDistributor, GlobalCliComm &cliComm)
void addListener(std::unique_ptr< CliListener > listener)
bool poll(int fd)
Waits for an event to occur on the given file descriptor.
bool aborted()
Returns true iff abort() was called.
void abort()
Aborts a poll in progress and any future poll attempts.
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr const char * c_str() const
constexpr zstring_view substr(size_type pos) const
void mkdir(zstring_view path, mode_t mode)
Create the specified directory.
void openofstream(std::ofstream &stream, zstring_view filename)
Open an ofstream in a platform-independent manner.
int rmdir(zstring_view path)
Call rmdir() in a platform-independent manner.
string getTempDir()
Get the name of the temp directory on the system.
string_view getFilename(string_view path)
Returns the file portion of a path name.
int unlink(zstring_view path)
Call unlink() in a platform-independent manner.
This file implemented 3 utility functions:
constexpr int OPENMSX_INVALID_SOCKET
constexpr int SOCKET_ERROR
void sock_close(SOCKET sd)
int random_int(int from, int thru)
Return a random integer in the range [from, thru] (note: closed interval).
TemporaryString tmpStrCat(Ts &&... ts)
std::string strCat(Ts &&...ts)
constexpr auto xrange(T e)