2016-11-13 19:05:30 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2016-11-20 23:04:31 +01:00
|
|
|
#include <unistd.h>
|
2016-11-13 19:05:30 +01:00
|
|
|
|
|
|
|
#include "components/ipc.hpp"
|
|
|
|
#include "config.hpp"
|
2016-12-05 20:41:00 +01:00
|
|
|
#include "events/signal.hpp"
|
|
|
|
#include "events/signal_emitter.hpp"
|
2016-12-09 09:02:47 +01:00
|
|
|
#include "utils/factory.hpp"
|
2016-11-13 19:05:30 +01:00
|
|
|
#include "utils/file.hpp"
|
|
|
|
#include "utils/io.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
|
2016-11-19 06:22:44 +01:00
|
|
|
POLYBAR_NS
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-05 20:41:00 +01:00
|
|
|
using namespace signals::ipc;
|
|
|
|
|
2016-12-09 09:02:47 +01:00
|
|
|
/**
|
|
|
|
* Create instance
|
|
|
|
*/
|
2016-12-09 09:40:46 +01:00
|
|
|
ipc::make_type ipc::make() {
|
2016-12-09 09:02:47 +01:00
|
|
|
return factory_util::unique<ipc>(signal_emitter::make(), logger::make());
|
|
|
|
}
|
|
|
|
|
2016-11-13 19:05:30 +01:00
|
|
|
/**
|
2016-12-20 05:05:43 +01:00
|
|
|
* Construct ipc handler
|
2016-11-13 19:05:30 +01:00
|
|
|
*/
|
2016-12-20 05:05:43 +01:00
|
|
|
ipc::ipc(signal_emitter& emitter, const logger& logger) : m_sig(emitter), m_log(logger) {
|
|
|
|
m_path = string_util::replace(PATH_MESSAGING_FIFO, "%pid%", to_string(getpid()));
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
if (mkfifo(m_path.c_str(), 0666) == -1) {
|
|
|
|
throw system_error("Failed to create ipc channel");
|
|
|
|
}
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
m_log.info("Created ipc channel at: %s", m_path);
|
|
|
|
m_fd = file_util::make_file_descriptor(m_path, O_RDONLY | O_NONBLOCK);
|
|
|
|
}
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
/**
|
|
|
|
* Deconstruct ipc handler
|
|
|
|
*/
|
|
|
|
ipc::~ipc() {
|
|
|
|
if (!m_path.empty()) {
|
|
|
|
m_log.trace("ipc: Removing file handle");
|
|
|
|
unlink(m_path.c_str());
|
2016-11-13 19:05:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-20 05:05:43 +01:00
|
|
|
* Receive available ipc messages and delegate valid events
|
2016-11-13 19:05:30 +01:00
|
|
|
*/
|
2016-12-20 05:05:43 +01:00
|
|
|
void ipc::receive_message() {
|
|
|
|
m_log.info("Receiving ipc message");
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
char buffer[BUFSIZ]{'\0'};
|
|
|
|
ssize_t bytes_read{0};
|
2016-11-13 19:05:30 +01:00
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
if ((bytes_read = read(*m_fd.get(), &buffer, BUFSIZ)) == -1) {
|
|
|
|
m_log.err("Failed to read from ipc channel (err: %s)", strerror(errno));
|
2016-11-13 19:05:30 +01:00
|
|
|
}
|
|
|
|
|
2016-12-20 05:05:43 +01:00
|
|
|
if (!bytes_read) {
|
2016-11-13 19:05:30 +01:00
|
|
|
return;
|
2016-12-20 05:05:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string payload{string_util::trim(string{buffer}, '\n')};
|
|
|
|
|
|
|
|
if (payload.find(ipc_command::prefix) == 0) {
|
2016-12-05 20:41:00 +01:00
|
|
|
ipc_command msg{};
|
|
|
|
memcpy(msg.payload, &payload[0], payload.size());
|
|
|
|
m_sig.emit(process_command{move(msg)});
|
2016-11-14 09:21:18 +01:00
|
|
|
} else if (payload.find(ipc_hook::prefix) == 0) {
|
2016-12-05 20:41:00 +01:00
|
|
|
ipc_hook msg{};
|
|
|
|
memcpy(msg.payload, &payload[0], payload.size());
|
|
|
|
m_sig.emit(process_hook{move(msg)});
|
2016-11-18 18:37:52 +01:00
|
|
|
} else if (payload.find(ipc_action::prefix) == 0) {
|
2016-12-05 20:41:00 +01:00
|
|
|
ipc_action msg{};
|
|
|
|
memcpy(msg.payload, &payload[0], payload.size());
|
|
|
|
m_sig.emit(process_action{move(msg)});
|
2016-12-20 05:05:43 +01:00
|
|
|
} else if (!payload.empty()) {
|
2016-11-14 09:21:18 +01:00
|
|
|
m_log.warn("Received unknown ipc message: (payload=%s)", payload);
|
2016-11-13 19:05:30 +01:00
|
|
|
}
|
2016-12-20 05:05:43 +01:00
|
|
|
|
|
|
|
m_fd = file_util::make_file_descriptor(m_path, O_RDONLY | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file descriptor to the ipc channel
|
|
|
|
*/
|
|
|
|
int ipc::get_file_descriptor() const {
|
|
|
|
return *m_fd.get();
|
2016-11-13 19:05:30 +01:00
|
|
|
}
|
|
|
|
|
2016-11-19 06:22:44 +01:00
|
|
|
POLYBAR_NS_END
|