polybar-dwm/src/ipc.cpp

143 lines
3.9 KiB
C++
Raw Normal View History

#include <fcntl.h>
#include <algorithm>
2017-01-14 05:02:43 +00:00
#include <cstdlib>
2017-01-14 13:57:28 +00:00
#include <cstring>
#include <vector>
#include "common.hpp"
#include "utils/file.hpp"
2017-01-14 13:57:28 +00:00
#include "utils/io.hpp"
using namespace polybar;
2017-01-14 05:02:43 +00:00
using namespace std;
2017-01-13 12:05:32 +00:00
#ifndef IPC_CHANNEL_PREFIX
#define IPC_CHANNEL_PREFIX "/tmp/polybar_mqueue."
#endif
void log(const string& msg) {
2017-01-13 12:05:32 +00:00
fprintf(stderr, "polybar-msg: %s\n", msg.c_str());
}
void log(int exit_code, const string& msg) {
2017-01-13 12:05:32 +00:00
fprintf(stderr, "polybar-msg: %s\n", msg.c_str());
exit(exit_code);
}
void usage(const string& parameters) {
2017-01-13 12:05:32 +00:00
fprintf(stderr, "Usage: polybar-msg [-p pid] %s\n", parameters.c_str());
exit(127);
}
2017-01-14 13:57:28 +00:00
void remove_pipe(const string& handle) {
if (unlink(handle.c_str()) == -1) {
log(1, "Could not remove stale ipc channel: "s + strerror(errno));
} else {
log("Removed stale ipc channel: " + handle);
}
}
bool validate_type(const string& type) {
if (type == "action") {
return true;
} else if (type == "cmd") {
return true;
} else if (type == "hook") {
return true;
} else {
return false;
}
}
int main(int argc, char** argv) {
const int E_NO_CHANNELS{2};
const int E_MESSAGE_TYPE{3};
const int E_INVALID_PID{4};
const int E_INVALID_CHANNEL{5};
const int E_WRITE{6};
vector<string> args{argv + 1, argv + argc};
string::size_type p;
int pid{0};
// If -p <pid> is passed, check if the process is running and that
// a valid channel pipe is available
if (args.size() >= 2 && args[0].compare(0, 2, "-p") == 0) {
if (!file_util::exists("/proc/" + args[1])) {
log(E_INVALID_PID, "No process with pid " + args[1]);
2017-01-14 13:57:28 +00:00
} else if (!file_util::exists(IPC_CHANNEL_PREFIX + args[1])) {
log(E_INVALID_CHANNEL, "No channel available for pid " + args[1]);
}
2017-01-14 05:02:43 +00:00
pid = atoi(args[1].c_str());
args.erase(args.begin());
args.erase(args.begin());
}
// Validate args
2017-01-14 13:57:28 +00:00
auto help = find_if(args.begin(), args.end(), [](string a) { return a == "-h" || a == "--help"; }) != args.end();
if (help || args.size() < 2) {
usage("<command=(action|cmd|hook)> <payload> [...]");
} else if (!validate_type(args[0])) {
log(E_MESSAGE_TYPE, "\"" + args[0] + "\" is not a valid type.");
}
string ipc_type{args[0]};
args.erase(args.begin());
string ipc_payload{args[0]};
args.erase(args.begin());
// Check hook specific args
if (ipc_type == "hook") {
if (args.size() != 1) {
usage("hook <module-name> <hook-index>");
} else if ((p = ipc_payload.find("module/")) != 0) {
ipc_payload = "module/" + ipc_payload + args[0];
args.erase(args.begin());
} else {
ipc_payload += args[0];
args.erase(args.begin());
}
}
// Get availble channel pipes
2017-01-14 13:57:28 +00:00
auto pipes = file_util::glob(IPC_CHANNEL_PREFIX + "*"s);
// Remove stale channel files without a running parent process
for (auto it = pipes.rbegin(); it != pipes.rend(); it++) {
if ((p = it->rfind('.')) == string::npos) {
continue;
} else if (!file_util::exists("/proc/" + it->substr(p + 1))) {
remove_pipe(*it);
pipes.erase(remove(pipes.begin(), pipes.end(), *it), pipes.end());
} else if (pid && to_string(pid) != it->substr(p + 1)) {
pipes.erase(remove(pipes.begin(), pipes.end(), *it), pipes.end());
}
}
2017-01-14 13:57:28 +00:00
if (pipes.empty()) {
log(E_NO_CHANNELS, "No active ipc channels");
}
2017-01-14 13:57:28 +00:00
int exit_status = 127;
2017-01-14 13:57:28 +00:00
// Write message to each available channel or match
// against pid if one was defined
for (auto&& channel : pipes) {
try {
file_descriptor fd(channel, O_WRONLY | O_NONBLOCK);
2017-01-13 12:05:32 +00:00
string payload{ipc_type + ':' + ipc_payload};
2017-01-13 23:27:29 +00:00
if (write(fd, payload.c_str(), payload.size()) != -1) {
2017-01-14 13:57:28 +00:00
log("Successfully wrote \"" + payload + "\" to \"" + channel + "\"");
exit_status = 0;
2017-01-13 12:05:32 +00:00
} else {
2017-01-14 13:57:28 +00:00
log(E_WRITE, "Failed to write \"" + payload + "\" to \"" + channel + "\" (err: " + strerror(errno) + ")");
}
2017-01-14 13:57:28 +00:00
} catch (const exception& err) {
remove_pipe(channel);
}
}
2017-01-14 13:57:28 +00:00
return exit_status;
}