Main.cxx 16.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Main.hxx"
22
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "CommandLine.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "PlaylistFile.hxx"
25
#include "PlaylistGlobal.hxx"
26
#include "MusicChunk.hxx"
27
#include "StateFile.hxx"
28
#include "player/Thread.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "Mapper.hxx"
30
#include "Permission.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "Listen.hxx"
32 33
#include "client/Client.hxx"
#include "client/ClientList.hxx"
34
#include "command/AllCommands.hxx"
35
#include "Partition.hxx"
36
#include "tag/TagConfig.hxx"
37
#include "ReplayGainConfig.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "Idle.hxx"
39
#include "Log.hxx"
40
#include "LogInit.hxx"
41
#include "GlobalEvents.hxx"
Max Kellermann's avatar
Max Kellermann committed
42
#include "input/Init.hxx"
43
#include "event/Loop.hxx"
44
#include "IOThread.hxx"
45
#include "fs/AllocatedPath.hxx"
46
#include "fs/Config.hxx"
47
#include "playlist/PlaylistRegistry.hxx"
48
#include "zeroconf/ZeroconfGlue.hxx"
49
#include "decoder/DecoderList.hxx"
50
#include "AudioConfig.hxx"
51
#include "pcm/PcmConvert.hxx"
52
#include "unix/SignalHandlers.hxx"
53
#include "system/FatalError.hxx"
54
#include "util/UriUtil.hxx"
55
#include "util/Error.hxx"
56
#include "thread/Id.hxx"
57
#include "thread/Slack.hxx"
58
#include "lib/icu/Init.hxx"
59
#include "config/ConfigGlobal.hxx"
60
#include "config/Param.hxx"
61 62
#include "config/ConfigDefaults.hxx"
#include "config/ConfigOption.hxx"
63
#include "config/ConfigError.hxx"
64
#include "Stats.hxx"
Max Kellermann's avatar
Max Kellermann committed
65

66 67 68 69
#ifdef ENABLE_DAEMON
#include "unix/Daemon.hxx"
#endif

70 71
#ifdef ENABLE_DATABASE
#include "db/update/Service.hxx"
72
#include "db/Configured.hxx"
73
#include "db/DatabasePlugin.hxx"
74
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
75
#include "storage/Configured.hxx"
76
#include "storage/CompositeStorage.hxx"
77 78 79
#ifdef ENABLE_INOTIFY
#include "db/update/InotifyUpdate.hxx"
#endif
80 81
#endif

82 83 84 85
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Glue.hxx"
#endif

86
#ifdef ENABLE_SQLITE
87
#include "sticker/StickerDatabase.hxx"
88 89
#endif

90
#ifdef ENABLE_ARCHIVE
91
#include "archive/ArchiveList.hxx"
92
#endif
Max Kellermann's avatar
Max Kellermann committed
93

Max Kellermann's avatar
Max Kellermann committed
94
#ifdef ANDROID
95 96
#include "java/Global.hxx"
#include "java/File.hxx"
97
#include "android/Environment.hxx"
98
#include "android/Context.hxx"
99
#include "fs/StandardDirectory.hxx"
100
#include "fs/FileSystem.hxx"
Max Kellermann's avatar
Max Kellermann committed
101 102 103
#include "org_musicpd_Bridge.h"
#endif

104 105 106 107
#ifdef ENABLE_SYSTEMD_DAEMON
#include <systemd/sd-daemon.h>
#endif

Max Kellermann's avatar
Max Kellermann committed
108
#include <stdlib.h>
109

110
#ifdef HAVE_LOCALE_H
111 112 113
#include <locale.h>
#endif

114 115 116 117 118
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

119
#ifdef __BLOCKS__
120 121 122
#include <dispatch/dispatch.h>
#endif

123 124
#include <limits.h>

125
static constexpr unsigned DEFAULT_BUFFER_SIZE = 4096;
126
static constexpr unsigned DEFAULT_BUFFER_BEFORE_PLAY = 10;
Max Kellermann's avatar
Max Kellermann committed
127

128 129 130 131
#ifdef ANDROID
Context *context;
#endif

132
Instance *instance;
133

134 135
static StateFile *state_file;

136
#ifdef ENABLE_DAEMON
137

138
static bool
139
glue_daemonize_init(const struct options *options, Error &error)
140
{
141
	auto pid_file = config_get_path(ConfigOption::PID_FILE, error);
142
	if (pid_file.IsNull() && error.IsDefined())
143 144
		return false;

145 146
	daemonize_init(config_get_string(ConfigOption::USER, nullptr),
		       config_get_string(ConfigOption::GROUP, nullptr),
147
		       std::move(pid_file));
148 149 150

	if (options->kill)
		daemonize_kill();
151 152

	return true;
153 154
}

155 156
#endif

157
static bool
158
glue_mapper_init(Error &error)
159
{
160
	auto playlist_dir = config_get_path(ConfigOption::PLAYLIST_DIR, error);
161
	if (playlist_dir.IsNull() && error.IsDefined())
162
		return false;
163

164 165 166
	mapper_init(std::move(playlist_dir));
	return true;
}
167

168 169
#ifdef ENABLE_DATABASE

170 171 172
static bool
InitStorage(Error &error)
{
173
	Storage *storage = CreateConfiguredStorage(io_thread_get(), error);
174 175 176 177 178 179 180 181 182
	if (storage == nullptr)
		return !error.IsDefined();

	assert(!error.IsDefined());

	CompositeStorage *composite = new CompositeStorage();
	instance->storage = composite;
	composite->Mount("", storage);
	return true;
183 184
}

185 186 187 188 189 190
/**
 * Returns the database.  If this function returns false, this has not
 * succeeded, and the caller should create the database after the
 * process has been daemonized.
 */
static bool
191
glue_db_init_and_load(void)
192
{
193
	Error error;
194 195
	instance->database =
		CreateConfiguredDatabase(*instance->event_loop, *instance,
196
					 error);
197 198 199 200 201 202
	if (instance->database == nullptr) {
		if (error.IsDefined())
			FatalError(error);
		else
			return true;
	}
203

204 205 206
	if (instance->database->GetPlugin().flags & DatabasePlugin::FLAG_REQUIRE_STORAGE) {
		if (!InitStorage(error))
			FatalError(error);
207

208 209 210 211 212 213 214 215 216 217 218 219 220
		if (instance->storage == nullptr) {
			delete instance->database;
			instance->database = nullptr;
			LogDefault(config_domain,
				   "Found database setting without "
				   "music_directory - disabling database");
			return true;
		}
	} else {
		if (IsStorageConfigured())
			LogDefault(config_domain,
				   "Ignoring the storage configuration "
				   "because the database does not need it");
221 222
	}

223
	if (!instance->database->Open(error))
224
		FatalError(error);
225

226
	if (!instance->database->IsPlugin(simple_db_plugin))
227 228
		return true;

229
	SimpleDatabase &db = *(SimpleDatabase *)instance->database;
230
	instance->update = new UpdateService(*instance->event_loop, db,
Max Kellermann's avatar
Max Kellermann committed
231
					     static_cast<CompositeStorage &>(*instance->storage),
232
					     *instance);
233

234
	/* run database update after daemonization? */
235
	return db.FileExists();
Warren Dukes's avatar
Warren Dukes committed
236
}
Warren Dukes's avatar
Warren Dukes committed
237

238 239 240 241 242 243 244
static bool
InitDatabaseAndStorage()
{
	const bool create_db = !glue_db_init_and_load();
	return create_db;
}

245 246
#endif

247 248 249 250 251 252 253
/**
 * Configure and initialize the sticker subsystem.
 */
static void
glue_sticker_init(void)
{
#ifdef ENABLE_SQLITE
254
	Error error;
255
	auto sticker_file = config_get_path(ConfigOption::STICKER_FILE, error);
256 257 258 259 260
	if (sticker_file.IsNull()) {
		if (error.IsDefined())
			FatalError(error);
		return;
	}
261

262
	if (!sticker_global_init(std::move(sticker_file), error))
263
		FatalError(error);
264 265 266
#endif
}

267
static bool
268
glue_state_file_init(Error &error)
269
{
270
	auto path_fs = config_get_path(ConfigOption::STATE_FILE, error);
271 272 273 274 275 276 277 278 279 280 281 282 283 284
	if (path_fs.IsNull()) {
		if (error.IsDefined())
			return false;

#ifdef ANDROID
		const auto cache_dir = GetUserCacheDir();
		if (cache_dir.IsNull())
			return true;

		path_fs = AllocatedPath::Build(cache_dir, "state");
#else
		return true;
#endif
	}
285

286 287 288
	const unsigned interval =
		config_get_unsigned(ConfigOption::STATE_FILE_INTERVAL,
				    StateFile::DEFAULT_INTERVAL);
289 290

	state_file = new StateFile(std::move(path_fs), interval,
291 292
				   *instance->partition,
				   *instance->event_loop);
293
	state_file->Read();
294
	return true;
295 296
}

297 298 299 300 301
/**
 * Windows-only initialization of the Winsock2 library.
 */
static void winsock_init(void)
{
302
#ifdef WIN32
303 304
	WSADATA sockinfo;

305
	int retval = WSAStartup(MAKEWORD(2, 2), &sockinfo);
306
	if(retval != 0)
307 308
		FormatFatalError("Attempt to open Winsock2 failed; error code %d",
				 retval);
309 310

	if (LOBYTE(sockinfo.wVersion) != 2)
311 312
		FatalError("We use Winsock2 but your version is either too new "
			   "or old; please install Winsock 2.x");
313
#endif
314
}
315

Max Kellermann's avatar
Max Kellermann committed
316 317 318 319 320 321
/**
 * Initialize the decoder and player core, including the music pipe.
 */
static void
initialize_decoder_and_player(void)
{
322
	const struct config_param *param;
Max Kellermann's avatar
Max Kellermann committed
323

324
	size_t buffer_size;
325
	param = config_get_param(ConfigOption::AUDIO_BUFFER_SIZE);
326
	if (param != nullptr) {
327
		char *test;
328
		long tmp = strtol(param->value.c_str(), &test, 10);
329
		if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
330 331
			FormatFatalError("buffer size \"%s\" is not a "
					 "positive integer, line %i",
332
					 param->value.c_str(), param->line);
333
		buffer_size = tmp;
Max Kellermann's avatar
Max Kellermann committed
334 335 336 337 338
	} else
		buffer_size = DEFAULT_BUFFER_SIZE;

	buffer_size *= 1024;

339
	const unsigned buffered_chunks = buffer_size / CHUNK_SIZE;
Max Kellermann's avatar
Max Kellermann committed
340 341

	if (buffered_chunks >= 1 << 15)
342 343
		FormatFatalError("buffer size \"%lu\" is too big",
				 (unsigned long)buffer_size);
Max Kellermann's avatar
Max Kellermann committed
344

345
	float perc;
346
	param = config_get_param(ConfigOption::BUFFER_BEFORE_PLAY);
347
	if (param != nullptr) {
348
		char *test;
349
		perc = strtod(param->value.c_str(), &test);
Max Kellermann's avatar
Max Kellermann committed
350
		if (*test != '%' || perc < 0 || perc > 100) {
351 352 353
			FormatFatalError("buffered before play \"%s\" is not "
					 "a positive percentage and less "
					 "than 100 percent, line %i",
354
					 param->value.c_str(), param->line);
Max Kellermann's avatar
Max Kellermann committed
355 356 357 358
		}
	} else
		perc = DEFAULT_BUFFER_BEFORE_PLAY;

359
	unsigned buffered_before_play = (perc / 100) * buffered_chunks;
Max Kellermann's avatar
Max Kellermann committed
360 361 362
	if (buffered_before_play > buffered_chunks)
		buffered_before_play = buffered_chunks;

363
	const unsigned max_length =
364
		config_get_positive(ConfigOption::MAX_PLAYLIST_LENGTH,
365 366
				    DEFAULT_PLAYLIST_MAX_LENGTH);

367 368 369 370
	instance->partition = new Partition(*instance,
					    max_length,
					    buffered_chunks,
					    buffered_before_play);
Max Kellermann's avatar
Max Kellermann committed
371 372
}

373
/**
374
 * Handler for GlobalEvents::IDLE.
375 376 377
 */
static void
idle_event_emitted(void)
378
{
Max Kellermann's avatar
Max Kellermann committed
379
	/* send "idle" notifications to all subscribed
380
	   clients */
381
	unsigned flags = idle_get();
382
	if (flags != 0)
383
		instance->client_list->IdleAdd(flags);
384

385 386
	if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
	    state_file != nullptr)
387
		state_file->CheckModified();
388 389
}

390 391
#ifdef WIN32

392
/**
393
 * Handler for GlobalEvents::SHUTDOWN.
394 395 396 397
 */
static void
shutdown_event_emitted(void)
{
398
	instance->event_loop->Break();
399 400
}

401 402
#endif

Max Kellermann's avatar
Max Kellermann committed
403 404
#ifndef ANDROID

405
int main(int argc, char *argv[])
406 407 408 409 410 411 412 413
{
#ifdef WIN32
	return win32_main(argc, argv);
#else
	return mpd_main(argc, argv);
#endif
}

Max Kellermann's avatar
Max Kellermann committed
414 415
#endif

416 417
static int mpd_main_after_fork(struct options);

418 419 420
#ifdef ANDROID
static inline
#endif
421
int mpd_main(int argc, char *argv[])
422
{
Max Kellermann's avatar
Max Kellermann committed
423
	struct options options;
424
	Error error;
Warren Dukes's avatar
Warren Dukes committed
425

426
#ifdef ENABLE_DAEMON
427
	daemonize_close_stdin();
428
#endif
429

430
#ifndef ANDROID
431
#ifdef HAVE_LOCALE_H
432 433
	/* initialize locale */
	setlocale(LC_CTYPE,"");
434
	setlocale(LC_COLLATE, "");
435
#endif
436
#endif
Max Kellermann's avatar
Max Kellermann committed
437

438
	if (!IcuInit(error)) {
439 440 441 442
		LogError(error);
		return EXIT_FAILURE;
	}

443
	winsock_init();
444
	io_thread_init();
445
	config_global_init();
Warren Dukes's avatar
Warren Dukes committed
446

447
	try {
448
#ifdef ANDROID
449 450
		(void)argc;
		(void)argv;
451

452 453 454 455
		const auto sdcard = Environment::getExternalStorageDirectory();
		if (!sdcard.IsNull()) {
			const auto config_path =
				AllocatedPath::Build(sdcard, "mpd.conf");
456 457
			if (FileExists(config_path))
				ReadConfigFile(config_path);
458
		}
459
#else
460 461 462 463 464 465 466
		if (!parse_cmdline(argc, argv, &options, error)) {
			LogError(error);
			return EXIT_FAILURE;
		}
#endif
	} catch (const std::exception &e) {
		LogError(e);
467 468
		return EXIT_FAILURE;
	}
Warren Dukes's avatar
Warren Dukes committed
469

470
#ifdef ENABLE_DAEMON
471
	if (!glue_daemonize_init(&options, error)) {
472
		LogError(error);
473 474
		return EXIT_FAILURE;
	}
475
#endif
476

Max Kellermann's avatar
Max Kellermann committed
477
	stats_global_init();
478
	TagLoadConfig();
479

480
	if (!log_init(options.verbose, options.log_stderr, error)) {
481
		LogError(error);
482 483
		return EXIT_FAILURE;
	}
484

485
	instance = new Instance();
486
	instance->event_loop = new EventLoop();
487

488 489 490 491 492 493 494 495 496 497 498 499 500
#ifdef ENABLE_NEIGHBOR_PLUGINS
	instance->neighbors = new NeighborGlue();
	if (!instance->neighbors->Init(io_thread_get(), *instance, error)) {
		LogError(error);
		return EXIT_FAILURE;
	}

	if (instance->neighbors->IsEmpty()) {
		delete instance->neighbors;
		instance->neighbors = nullptr;
	}
#endif

501 502
	const unsigned max_clients =
		config_get_positive(ConfigOption::MAX_CONN, 10);
503
	instance->client_list = new ClientList(max_clients);
504

505 506
	initialize_decoder_and_player();

507 508
	if (!listen_global_init(*instance->event_loop, *instance->partition,
				error)) {
509
		LogError(error);
510 511
		return EXIT_FAILURE;
	}
512

513
#ifdef ENABLE_DAEMON
514
	daemonize_set_user();
515
	daemonize_begin(options.daemon);
516
#endif
Warren Dukes's avatar
Warren Dukes committed
517

518
#ifdef __BLOCKS__
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	/* Runs the OS X native event loop in the main thread, and runs
	   the rest of mpd_main on a new thread. This lets CoreAudio receive
	   route change notifications (e.g. plugging or unplugging headphones).
	   All hardware output on OS X ultimately uses CoreAudio internally.
	   This must be run after forking; if dispatch is called before forking,
	   the child process will have a broken internal dispatch state. */
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		exit(mpd_main_after_fork(options));
	});
	dispatch_main();
	return EXIT_FAILURE; // unreachable, because dispatch_main never returns
#else
	return mpd_main_after_fork(options);
#endif
}

static int mpd_main_after_fork(struct options options)
536
try {
537 538
	Error error;

539
	GlobalEvents::Initialize(*instance->event_loop);
540
	GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted);
541
#ifdef WIN32
542
	GlobalEvents::Register(GlobalEvents::SHUTDOWN, shutdown_event_emitted);
543
#endif
544

545 546 547 548
	if (!ConfigureFS(error)) {
		LogError(error);
		return EXIT_FAILURE;
	}
549

550
	if (!glue_mapper_init(error)) {
551
		LogError(error);
552 553 554
		return EXIT_FAILURE;
	}

555
	initPermissions();
556
	playlist_global_init();
557
	spl_global_init();
558
#ifdef ENABLE_ARCHIVE
559
	archive_plugin_init_all();
560
#endif
561

562
	if (!pcm_convert_global_init(error)) {
563
		LogError(error);
564 565 566
		return EXIT_FAILURE;
	}

567
	decoder_plugin_init_all();
568

569
#ifdef ENABLE_DATABASE
570
	const bool create_db = InitDatabaseAndStorage();
571
#endif
572

573
	glue_sticker_init();
574

Max Kellermann's avatar
Max Kellermann committed
575
	command_init();
576
	initAudioConfig();
577
	instance->partition->outputs.Configure(*instance->event_loop,
578
					       instance->partition->pc);
579
	client_manager_init();
580
	replay_gain_global_init();
581

582
	if (!input_stream_global_init(error)) {
583
		LogError(error);
584 585 586
		return EXIT_FAILURE;
	}

587
	playlist_list_global_init();
588

589
#ifdef ENABLE_DAEMON
590
	daemonize_commit();
591
#endif
592

593
#ifndef ANDROID
594
	setup_log_output(options.log_stderr);
Warren Dukes's avatar
Warren Dukes committed
595

596
	SignalHandlersInit(*instance->event_loop);
597
#endif
598

599
	io_thread_start();
600

601 602 603 604 605 606
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr &&
	    !instance->neighbors->Open(error))
		FatalError(error);
#endif

607
	ZeroconfInit(*instance->event_loop);
608

609
	StartPlayerThread(instance->partition->pc);
610

611
#ifdef ENABLE_DATABASE
612
	if (create_db) {
613 614
		/* the database failed to load: recreate the
		   database */
615
		unsigned job = instance->update->Enqueue("", true);
616
		if (job == 0)
617
			FatalError("directory update failed");
618
	}
619
#endif
620

621
	if (!glue_state_file_init(error)) {
622
		LogError(error);
623 624
		return EXIT_FAILURE;
	}
625

626
	instance->partition->outputs.SetReplayGainMode(replay_gain_get_real_mode(instance->partition->playlist.queue.random));
627

628
#ifdef ENABLE_DATABASE
629
	if (config_get_bool(ConfigOption::AUTO_UPDATE, false)) {
630
#ifdef ENABLE_INOTIFY
631
		if (instance->storage != nullptr &&
632
		    instance->update != nullptr)
633 634 635
			mpd_inotify_init(*instance->event_loop,
					 *instance->storage,
					 *instance->update,
636
					 config_get_unsigned(ConfigOption::AUTO_UPDATE_DEPTH,
637
							     INT_MAX));
638
#else
639
		FormatWarning(config_domain,
640
			      "inotify: auto_update was disabled. enable during compilation phase");
641
#endif
642
	}
643
#endif
644

645 646
	config_global_check();

647 648
	/* enable all audio outputs (if not already done by
	   playlist_state_restore() */
649
	instance->partition->pc.LockUpdateAudio();
650

651 652 653
#ifdef WIN32
	win32_app_started();
#endif
654

655 656 657 658
	/* the MPD frontend does not care about timer slack; set it to
	   a huge value to allow the kernel to reduce CPU wakeups */
	SetThreadTimerSlackMS(100);

659 660 661 662
#ifdef ENABLE_SYSTEMD_DAEMON
	sd_notify(0, "READY=1");
#endif

663
	/* run the main loop */
664
	instance->event_loop->Run();
665

666 667 668 669
#ifdef WIN32
	win32_app_stopping();
#endif

670 671
	/* cleanup */

672
#if defined(ENABLE_DATABASE) && defined(ENABLE_INOTIFY)
673
	mpd_inotify_finish();
674 675 676

	if (instance->update != nullptr)
		instance->update->CancelAllAsync();
677
#endif
678

679 680 681 682 683
	if (state_file != nullptr) {
		state_file->Write();
		delete state_file;
	}

684
	instance->partition->pc.Kill();
685
	ZeroconfDeinit();
Max Kellermann's avatar
Max Kellermann committed
686
	listen_global_finish();
687
	delete instance->client_list;
688

689 690 691 692 693 694 695
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (instance->neighbors != nullptr) {
		instance->neighbors->Close();
		delete instance->neighbors;
	}
#endif

696
#ifdef ENABLE_DATABASE
697
	delete instance->update;
698 699 700 701 702

	if (instance->database != nullptr) {
		instance->database->Close();
		delete instance->database;
	}
703 704

	delete instance->storage;
705
#endif
706

707 708 709 710
#ifdef ENABLE_SQLITE
	sticker_global_finish();
#endif

711
	GlobalEvents::Deinitialize();
712

713
	playlist_list_global_finish();
714
	input_stream_global_finish();
715 716

#ifdef ENABLE_DATABASE
717
	mapper_finish();
718 719
#endif

720 721
	DeinitFS();

722
	delete instance->partition;
Max Kellermann's avatar
Max Kellermann committed
723
	command_finish();
724
	decoder_plugin_deinit_all();
725
#ifdef ENABLE_ARCHIVE
726
	archive_plugin_deinit_all();
727
#endif
728
	config_global_finish();
729
	io_thread_deinit();
730
#ifndef ANDROID
731
	SignalHandlersFinish();
732
#endif
733
	delete instance->event_loop;
734
	delete instance;
735
	instance = nullptr;
736 737

#ifdef ENABLE_DAEMON
738
	daemonize_finish();
739
#endif
740

741 742 743
#ifdef WIN32
	WSACleanup();
#endif
744

745
	IcuFinish();
746

747
	log_deinit();
748
	return EXIT_SUCCESS;
749 750 751
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
Warren Dukes's avatar
Warren Dukes committed
752
}
Max Kellermann's avatar
Max Kellermann committed
753 754 755 756 757

#ifdef ANDROID

gcc_visibility_default
JNIEXPORT void JNICALL
758
Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context)
Max Kellermann's avatar
Max Kellermann committed
759
{
760 761
	Java::Init(env);
	Java::File::Initialise(env);
762
	Environment::Initialise(env);
763

764 765
	context = new Context(env, _context);

Max Kellermann's avatar
Max Kellermann committed
766
	mpd_main(0, nullptr);
767

768
	delete context;
769
	Environment::Deinitialise(env);
Max Kellermann's avatar
Max Kellermann committed
770 771
}

772 773 774 775 776 777 778 779
gcc_visibility_default
JNIEXPORT void JNICALL
Java_org_musicpd_Bridge_shutdown(JNIEnv *, jclass)
{
	if (instance != nullptr)
		instance->event_loop->Break();
}

Max Kellermann's avatar
Max Kellermann committed
780
#endif