Fix deamon mode on macos
This commit is contained in:

committed by
Max Kellermann

parent
407db96d4a
commit
e3cf9bb0a1
@ -437,6 +437,12 @@ if is_windows
|
|||||||
]
|
]
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if is_darwin
|
||||||
|
sources += [
|
||||||
|
'src/apple/AppleMain.cxx',
|
||||||
|
]
|
||||||
|
endif
|
||||||
|
|
||||||
if not is_android
|
if not is_android
|
||||||
sources += [
|
sources += [
|
||||||
'src/CommandLine.cxx',
|
'src/CommandLine.cxx',
|
||||||
|
@ -677,6 +677,8 @@ try {
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return win32_main(argc, argv);
|
return win32_main(argc, argv);
|
||||||
|
#elif __APPLE__
|
||||||
|
return apple_main(argc, argv);
|
||||||
#else
|
#else
|
||||||
return mpd_main(argc, argv);
|
return mpd_main(argc, argv);
|
||||||
#endif
|
#endif
|
||||||
|
12
src/Main.hxx
12
src/Main.hxx
@ -61,4 +61,16 @@ win32_app_stopping();
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If program is run as deamon on macos, fork very early to avoid objc runtime issues,
|
||||||
|
* and then calls mpd_main() with specified arguments.
|
||||||
|
* If program is run as a regular application calls mpd_main() immediately.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
apple_main(int argc, char *argv[]);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
38
src/apple/AppleMain.cxx
Normal file
38
src/apple/AppleMain.cxx
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
// Copyright The Music Player Daemon Project
|
||||||
|
|
||||||
|
#include "Main.hxx"
|
||||||
|
#include "Instance.hxx"
|
||||||
|
#include "CommandLine.hxx"
|
||||||
|
#include "net/Init.hxx"
|
||||||
|
#include "config/Data.hxx"
|
||||||
|
|
||||||
|
static int service_argc;
|
||||||
|
static char **service_argv;
|
||||||
|
|
||||||
|
int apple_main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
service_argc = argc;
|
||||||
|
service_argv = argv;
|
||||||
|
|
||||||
|
#ifdef ENABLE_DAEMON
|
||||||
|
CommandLineOptions options;
|
||||||
|
ConfigData raw_config;
|
||||||
|
|
||||||
|
ParseCommandLine(argc, argv, options, raw_config);
|
||||||
|
|
||||||
|
if (options.daemon) {
|
||||||
|
// Fork before any Objective-C runtime initializations
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0)
|
||||||
|
throw MakeErrno("fork() failed");
|
||||||
|
|
||||||
|
if (pid > 0) {
|
||||||
|
// Parent process: exit immediately
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return mpd_main(argc, argv);
|
||||||
|
}
|
@ -134,6 +134,8 @@ daemonize_begin(bool detach)
|
|||||||
|
|
||||||
/* move to a child process */
|
/* move to a child process */
|
||||||
|
|
||||||
|
#ifndef __APPLE__
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
throw MakeErrno("fork() failed");
|
throw MakeErrno("fork() failed");
|
||||||
@ -178,6 +180,8 @@ daemonize_begin(bool detach)
|
|||||||
WCOREDUMP(status) ? " (core dumped)" : "");
|
WCOREDUMP(status) ? " (core dumped)" : "");
|
||||||
|
|
||||||
std::exit(WEXITSTATUS(status));
|
std::exit(WEXITSTATUS(status));
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Reference in New Issue
Block a user