27[[nodiscard]]
static std::string getUserName()
32 const struct passwd* pw = getpwuid(getuid());
33 return pw->pw_name ? pw->pw_name : std::string{};
37[[nodiscard]]
static bool checkSocketDir(
zstring_view dir)
50 if ((st->st_mode & 0777) != 0700) {
54 if (st->st_uid != getuid()) {
62[[nodiscard]]
static bool checkSocket(
zstring_view socket)
65 !name.starts_with(
"socket.")) {
80 if (!S_ISSOCK(st->st_mode)) {
87 if ((st->st_mode & 0777) != 0600) {
92 if (st->st_uid != getuid()) {
102[[nodiscard]]
static int openPort(
SOCKET listenSock)
104 const int BASE = 9938;
105 const int RANGE = 64;
109 for (
auto n :
xrange(RANGE)) {
110 int port = BASE + ((first + n) % RANGE);
111 sockaddr_in server_address;
112 memset(&server_address, 0,
sizeof(server_address));
113 server_address.sin_family = AF_INET;
114 server_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
115 server_address.sin_port = htons(port);
116 if (bind(listenSock, std::bit_cast<sockaddr*>(&server_address),
117 sizeof(server_address)) != -1) {
121 throw MSXException(
"Couldn't open socket.");
125SOCKET CliServer::createSocket()
129 if (!checkSocketDir(dir)) {
130 throw MSXException(
"Couldn't create socket directory.");
132 socketName =
strCat(dir,
"/socket.",
int(getpid()));
135 SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
139 int portNumber = openPort(sd);
145 out << portNumber <<
'\n';
148 throw MSXException(
"Couldn't write socket port file.");
152 SOCKET sd = socket(AF_UNIX, SOCK_STREAM, 0);
160 strncpy(addr.sun_path, socketName.c_str(),
sizeof(addr.sun_path) - 1);
161 addr.sun_path[
sizeof(addr.sun_path) - 1] =
'\0';
162 addr.sun_family = AF_UNIX;
164 if (bind(sd, std::bit_cast<sockaddr*>(&addr),
sizeof(addr)) == -1) {
166 throw MSXException(
"Couldn't bind socket.");
168 if (chmod(socketName.c_str(), 0600) == -1) {
170 throw MSXException(
"Couldn't set socket permissions.");
174 if (!checkSocket(socketName)) {
176 throw MSXException(
"Opened socket fails sanity check.");
180 throw MSXException(
"Couldn't listen to socket: ",
sock_error());
185void CliServer::exitAcceptLoop()
191static void deleteSocket(
const std::string& socket)
194 auto dir = socket.
substr(0, socket.find_last_of(
'/'));
202 : commandController(commandController_)
203 , eventDistributor(eventDistributor_)
207 listenSock = createSocket();
208 thread = std::thread([
this]() { mainLoop(); });
221 deleteSocket(socketName);
224void 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)
CliListener * addListener(std::unique_ptr< CliListener > listener)
bool poll(int fd)
Waits for an event to occur on the given file descriptor.
bool aborted() const
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 zstring_view substr(size_type pos) const
void mkdir(zstring_view path, mode_t mode)
Create the specified directory.
bool isRegularFile(const Stat &st)
int rmdir(zstring_view path)
Call rmdir() in a platform-independent manner.
string getTempDir()
Get the name of the temp directory on the system.
bool isDirectory(const Stat &st)
void openOfStream(std::ofstream &stream, zstring_view filename)
Open an ofstream in a platform-independent manner.
string_view getFilename(string_view path)
Returns the file portion of a path name.
std::optional< Stat > getStat(zstring_view filename)
Call stat() and return the stat structure.
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)
constexpr auto xrange(T e)