Skip to content

Commit e3cf9bb

Browse files
CamilleScholtzMaxKellermann
authored andcommitted
Fix deamon mode on macos
1 parent 407db96 commit e3cf9bb

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ if is_windows
437437
]
438438
endif
439439

440+
if is_darwin
441+
sources += [
442+
'src/apple/AppleMain.cxx',
443+
]
444+
endif
445+
440446
if not is_android
441447
sources += [
442448
'src/CommandLine.cxx',

src/Main.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,8 @@ try {
677677

678678
#ifdef _WIN32
679679
return win32_main(argc, argv);
680+
#elif __APPLE__
681+
return apple_main(argc, argv);
680682
#else
681683
return mpd_main(argc, argv);
682684
#endif

src/Main.hxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ win32_app_stopping();
6161

6262
#endif
6363

64+
#ifdef __APPLE__
65+
66+
/**
67+
* If program is run as deamon on macos, fork very early to avoid objc runtime issues,
68+
* and then calls mpd_main() with specified arguments.
69+
* If program is run as a regular application calls mpd_main() immediately.
70+
*/
71+
int
72+
apple_main(int argc, char *argv[]);
73+
74+
#endif
75+
6476
#endif

src/apple/AppleMain.cxx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright The Music Player Daemon Project
3+
4+
#include "Main.hxx"
5+
#include "Instance.hxx"
6+
#include "CommandLine.hxx"
7+
#include "net/Init.hxx"
8+
#include "config/Data.hxx"
9+
10+
static int service_argc;
11+
static char **service_argv;
12+
13+
int apple_main(int argc, char *argv[])
14+
{
15+
service_argc = argc;
16+
service_argv = argv;
17+
18+
#ifdef ENABLE_DAEMON
19+
CommandLineOptions options;
20+
ConfigData raw_config;
21+
22+
ParseCommandLine(argc, argv, options, raw_config);
23+
24+
if (options.daemon) {
25+
// Fork before any Objective-C runtime initializations
26+
pid_t pid = fork();
27+
if (pid < 0)
28+
throw MakeErrno("fork() failed");
29+
30+
if (pid > 0) {
31+
// Parent process: exit immediately
32+
_exit(0);
33+
}
34+
}
35+
#endif
36+
37+
return mpd_main(argc, argv);
38+
}

src/unix/Daemon.cxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ daemonize_begin(bool detach)
134134

135135
/* move to a child process */
136136

137+
#ifndef __APPLE__
138+
137139
pid_t pid = fork();
138140
if (pid < 0)
139141
throw MakeErrno("fork() failed");
@@ -178,6 +180,8 @@ daemonize_begin(bool detach)
178180
WCOREDUMP(status) ? " (core dumped)" : "");
179181

180182
std::exit(WEXITSTATUS(status));
183+
184+
#endif
181185
}
182186

183187
void

0 commit comments

Comments
 (0)