Added --foreground command to musikcubed.

This commit is contained in:
casey langen 2019-01-20 11:45:55 -08:00
parent 20c7342659
commit f67f310cdc
2 changed files with 42 additions and 11 deletions

View File

@ -4,6 +4,11 @@ musikcube:
* fixed a crash in `CoreAudioOut` when parsing unnamed output devices * fixed a crash in `CoreAudioOut` when parsing unnamed output devices
(Porco-Rosso) (Porco-Rosso)
* fixed a crash in `macosmediakeys` initialization (Porco-Rosso) * fixed a crash in `macosmediakeys` initialization (Porco-Rosso)
* fixed a bug where long path names could crash on Linux systems (the-eater)
musikcubed:
* added `--foreground` command line argument that instructions the daemon to
not not `fork()`, and run in the foreground instead.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -32,14 +32,17 @@ static const short EVENT_DISPATCH = 1;
static const short EVENT_QUIT = 2; static const short EVENT_QUIT = 2;
static const pid_t NOT_RUNNING = (pid_t) -1; static const pid_t NOT_RUNNING = (pid_t) -1;
static int pipeFd[2] = { 0 }; static int pipeFd[2] = { 0 };
static bool foreground = false;
static void printHelp(); static void printHelp();
static void handleCommandLine(int argc, char** argv); static void handleCommandLine(int argc, char** argv);
static void exitIfRunning(); static void exitIfRunning();
static pid_t getDaemonPid(); static pid_t getDaemonPid();
static void startDaemon(); static void initForeground();
static void initDaemon();
static void stopDaemon(); static void stopDaemon();
static void initUtf8(); static void initUtf8();
static void run();
class EvMessageQueue: public MessageQueue { class EvMessageQueue: public MessageQueue {
public: public:
@ -62,7 +65,7 @@ class EvMessageQueue: public MessageQueue {
this->Dispatch(); this->Dispatch();
} }
static void SignalCallback(ev::sig& signal, int revents) { static void SignalQuit(ev::sig& signal, int revents) {
write(pipeFd[1], &EVENT_QUIT, sizeof(EVENT_QUIT)); write(pipeFd[1], &EVENT_QUIT, sizeof(EVENT_QUIT));
} }
@ -85,7 +88,7 @@ class EvMessageQueue: public MessageQueue {
io.start(); io.start();
sio.set(loop); sio.set(loop);
sio.set<&EvMessageQueue::SignalCallback>(); sio.set<&EvMessageQueue::SignalQuit>();
sio.start(SIGTERM); sio.start(SIGTERM);
write(pipeFd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH)); write(pipeFd[1], &EVENT_DISPATCH, sizeof(EVENT_DISPATCH));
@ -93,9 +96,6 @@ class EvMessageQueue: public MessageQueue {
loop.run(0); loop.run(0);
} }
void Quit() {
}
private: private:
ev::dynamic_loop loop; ev::dynamic_loop loop;
ev::io io; ev::io io;
@ -105,6 +105,7 @@ class EvMessageQueue: public MessageQueue {
static void printHelp() { static void printHelp() {
std::cout << "\n musikcubed:\n"; std::cout << "\n musikcubed:\n";
std::cout << " --start: start the daemon\n"; std::cout << " --start: start the daemon\n";
std::cout << " --foreground: start the in the foreground\n";
std::cout << " --stop: shut down the daemon\n"; std::cout << " --stop: shut down the daemon\n";
std::cout << " --running: check if the daemon is running\n"; std::cout << " --running: check if the daemon is running\n";
std::cout << " --version: print the version\n"; std::cout << " --version: print the version\n";
@ -117,6 +118,11 @@ static void handleCommandLine(int argc, char** argv) {
if (command == "--start") { if (command == "--start") {
return; return;
} }
else if (command == "--foreground") {
std::cout << "\n musikcubed starting in the foreground...\n\n";
::foreground = true;
return;
}
else if (command == "--stop") { else if (command == "--stop") {
stopDaemon(); stopDaemon();
} }
@ -186,7 +192,7 @@ static void exitIfRunning() {
std::cerr << "\n musikcubed is starting...\n\n"; std::cerr << "\n musikcubed is starting...\n\n";
} }
static void startDaemon() { static void initDaemon() {
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) { if (pid < 0) {
@ -221,6 +227,27 @@ static void startDaemon() {
if (lock.good()) { if (lock.good()) {
lock << std::to_string((int) getpid()); lock << std::to_string((int) getpid());
} }
debug::Start({
new debug::SimpleFileBackend()
});
}
static void initForeground() {
if (pipe(pipeFd) != 0) {
std::cerr << "\n ERROR! couldn't create pipe\n\n";
exit(EXIT_FAILURE);
}
std::ofstream lock(LOCKFILE);
if (lock.good()) {
lock << std::to_string((int) getpid());
}
debug::Start({
new debug::ConsoleBackend(),
new debug::SimpleFileBackend()
});
} }
static void initUtf8() { static void initUtf8() {
@ -230,15 +257,14 @@ static void initUtf8() {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
initUtf8();
handleCommandLine(argc, argv); handleCommandLine(argc, argv);
exitIfRunning(); exitIfRunning();
startDaemon();
initUtf8(); ::foreground ? initForeground() : initDaemon();
srand((unsigned int) time(0)); srand((unsigned int) time(0));
debug::Start();
EvMessageQueue messageQueue; EvMessageQueue messageQueue;
auto library = LibraryFactory::Default(); auto library = LibraryFactory::Default();
library->SetMessageQueue(messageQueue); library->SetMessageQueue(messageQueue);