2018-05-24 18:17:54 +00:00
|
|
|
/* myMPD
|
|
|
|
(c) 2018 Juergen Mang <mail@jcgames.de>
|
2018-06-21 21:29:49 +00:00
|
|
|
This project's homepage is: https://github.com/jcorporation/mympd
|
2018-05-24 18:17:54 +00:00
|
|
|
|
|
|
|
myMPD ist fork of:
|
|
|
|
|
|
|
|
ympd
|
|
|
|
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
|
|
|
|
This project's homepage is: http://www.ympd.org
|
|
|
|
|
|
|
|
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; version 2 of the License.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.,
|
|
|
|
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
2018-06-14 19:56:12 +00:00
|
|
|
#include <pwd.h>
|
2018-08-27 23:45:14 +00:00
|
|
|
#include <grp.h>
|
2018-10-15 22:34:25 +00:00
|
|
|
#include <libgen.h>
|
2019-01-05 00:01:34 +00:00
|
|
|
#include <pthread.h>
|
2019-01-05 12:29:10 +00:00
|
|
|
#include <dirent.h>
|
2018-11-26 15:55:26 +00:00
|
|
|
#include <mpd/client.h>
|
2019-01-08 00:09:43 +00:00
|
|
|
#include <stdbool.h>
|
2018-05-24 18:17:54 +00:00
|
|
|
|
2019-01-08 00:09:43 +00:00
|
|
|
#include "list.h"
|
|
|
|
#include "tiny_queue.h"
|
2019-01-05 12:29:10 +00:00
|
|
|
#include "global.h"
|
2018-05-24 18:17:54 +00:00
|
|
|
#include "mpd_client.h"
|
2019-01-11 00:38:44 +00:00
|
|
|
#include "../dist/src/mongoose/mongoose.h"
|
2019-01-05 00:01:34 +00:00
|
|
|
#include "web_server.h"
|
2019-01-08 00:09:43 +00:00
|
|
|
#include "mympd_api.h"
|
|
|
|
#include "../dist/src/inih/ini.h"
|
2019-01-11 00:38:44 +00:00
|
|
|
|
2018-05-24 18:17:54 +00:00
|
|
|
|
2018-06-12 00:07:03 +00:00
|
|
|
static void signal_handler(int sig_num) {
|
2018-08-06 21:29:20 +00:00
|
|
|
signal(sig_num, signal_handler); // Reinstantiate signal handler
|
|
|
|
s_signal_received = sig_num;
|
2018-05-24 18:17:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 00:38:44 +00:00
|
|
|
static int inihandler(void *user, const char *section, const char *name, const char* value) {
|
2018-08-17 09:52:22 +00:00
|
|
|
t_config* p_config = (t_config*)user;
|
2018-08-22 12:06:22 +00:00
|
|
|
char *crap;
|
2018-08-06 20:51:48 +00:00
|
|
|
|
2019-01-14 19:43:40 +00:00
|
|
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
2018-08-21 19:28:13 +00:00
|
|
|
|
2019-01-14 19:43:40 +00:00
|
|
|
if (MATCH("mpd", "mpdhost"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->mpdhost = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mpd", "mpdport"))
|
2018-08-22 12:06:22 +00:00
|
|
|
p_config->mpdport = strtol(value, &crap, 10);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mpd", "mpdpass"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->mpdpass = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mpd", "streamport"))
|
|
|
|
p_config->streamport = strtol(value, &crap, 10);
|
|
|
|
else if (MATCH("webserver", "webport"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->webport = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("webserver", "ssl"))
|
2018-08-06 20:51:48 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->ssl = true;
|
|
|
|
else
|
|
|
|
p_config->ssl = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("webserver", "sslport"))
|
2018-11-28 14:23:29 +00:00
|
|
|
p_config->sslport = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("webserver", "sslcert"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->sslcert = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("webserver", "sslkey"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->sslkey = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "user"))
|
2018-08-06 20:51:48 +00:00
|
|
|
p_config->user = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "coverimage"))
|
2018-10-29 19:41:04 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->coverimage = true;
|
|
|
|
else
|
|
|
|
p_config->coverimage = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "coverimagename"))
|
2018-10-29 19:41:04 +00:00
|
|
|
p_config->coverimagename = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "coverimagesize"))
|
2018-12-24 15:37:45 +00:00
|
|
|
p_config->coverimagesize = strtol(value, &crap, 10);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "varlibdir"))
|
2018-09-29 23:03:00 +00:00
|
|
|
p_config->varlibdir = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "stickers"))
|
2018-08-21 19:28:13 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->stickers = true;
|
|
|
|
else
|
|
|
|
p_config->stickers = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "smartpls"))
|
2018-09-24 20:55:53 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
2018-09-24 22:27:24 +00:00
|
|
|
p_config->smartpls = true;
|
2018-09-24 20:55:53 +00:00
|
|
|
else
|
2018-09-24 22:27:24 +00:00
|
|
|
p_config->smartpls = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "mixramp"))
|
2018-08-21 19:28:13 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->mixramp = true;
|
|
|
|
else
|
2018-09-17 17:06:06 +00:00
|
|
|
p_config->mixramp = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "taglist"))
|
2018-10-14 22:03:56 +00:00
|
|
|
p_config->taglist = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "searchtaglist"))
|
2018-10-29 18:17:04 +00:00
|
|
|
p_config->searchtaglist = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "browsetaglist"))
|
2018-10-29 18:17:04 +00:00
|
|
|
p_config->browsetaglist = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "max_elements_per_page")) {
|
2018-10-14 22:03:56 +00:00
|
|
|
p_config->max_elements_per_page = strtol(value, &crap, 10);
|
|
|
|
if (p_config->max_elements_per_page > MAX_ELEMENTS_PER_PAGE) {
|
|
|
|
printf("Setting max_elements_per_page to maximal value %d", MAX_ELEMENTS_PER_PAGE);
|
|
|
|
p_config->max_elements_per_page = MAX_ELEMENTS_PER_PAGE;
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "syscmds"))
|
2018-10-29 18:17:04 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->syscmds = true;
|
|
|
|
else
|
|
|
|
p_config->syscmds = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "localplayer"))
|
2018-10-29 18:17:04 +00:00
|
|
|
if (strcmp(value, "true") == 0)
|
|
|
|
p_config->localplayer = true;
|
|
|
|
else
|
|
|
|
p_config->localplayer = false;
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "streamurl"))
|
2018-10-29 18:17:04 +00:00
|
|
|
p_config->streamurl = strdup(value);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "last_played_count"))
|
2018-11-12 23:48:29 +00:00
|
|
|
p_config->last_played_count = strtol(value, &crap, 10);
|
2019-01-14 19:43:40 +00:00
|
|
|
else if (MATCH("mympd", "loglevel"))
|
2018-11-27 21:24:45 +00:00
|
|
|
p_config->loglevel = strtol(value, &crap, 10);
|
2019-01-19 00:20:05 +00:00
|
|
|
else if (MATCH("theme", "backgroundcolor"))
|
|
|
|
p_config->backgroundcolor = strdup(value);
|
2018-11-27 21:24:45 +00:00
|
|
|
else {
|
2019-01-14 19:43:40 +00:00
|
|
|
printf("Unkown config option: %s - %s\n", section, name);
|
2018-08-06 20:51:48 +00:00
|
|
|
return 0; /* unknown section/name, error */
|
2018-11-27 21:24:45 +00:00
|
|
|
}
|
2018-08-06 20:51:48 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:06:31 +00:00
|
|
|
int main(int argc, char **argv) {
|
2019-01-05 12:29:10 +00:00
|
|
|
s_signal_received = 0;
|
2018-10-07 21:56:11 +00:00
|
|
|
char testdirname[400];
|
2019-01-05 00:01:34 +00:00
|
|
|
mpd_client_queue = tiny_queue_create();
|
2019-01-08 00:09:43 +00:00
|
|
|
mympd_api_queue = tiny_queue_create();
|
2019-01-05 00:01:34 +00:00
|
|
|
web_server_queue = tiny_queue_create();
|
2019-01-06 09:36:34 +00:00
|
|
|
|
|
|
|
srand((unsigned int)time(NULL));
|
2018-08-06 20:51:48 +00:00
|
|
|
|
2019-01-11 00:38:44 +00:00
|
|
|
//mympd config defaults
|
|
|
|
t_config config;
|
2018-08-06 20:51:48 +00:00
|
|
|
config.mpdhost = "127.0.0.1";
|
|
|
|
config.mpdport = 6600;
|
|
|
|
config.mpdpass = NULL;
|
|
|
|
config.webport = "80";
|
|
|
|
config.ssl = true;
|
|
|
|
config.sslport = "443";
|
|
|
|
config.sslcert = "/etc/mympd/ssl/server.pem";
|
|
|
|
config.sslkey = "/etc/mympd/ssl/server.key";
|
2018-10-15 22:34:25 +00:00
|
|
|
config.user = "mympd";
|
2018-08-06 20:51:48 +00:00
|
|
|
config.streamport = 8000;
|
2018-10-29 18:17:04 +00:00
|
|
|
config.streamurl = "";
|
2018-10-29 19:41:04 +00:00
|
|
|
config.coverimage = true;
|
|
|
|
config.coverimagename = "folder.jpg";
|
2018-12-24 15:37:45 +00:00
|
|
|
config.coverimagesize = 240;
|
2018-09-29 23:03:00 +00:00
|
|
|
config.varlibdir = "/var/lib/mympd";
|
2018-08-21 19:28:13 +00:00
|
|
|
config.stickers = true;
|
|
|
|
config.mixramp = true;
|
2018-09-17 17:06:06 +00:00
|
|
|
config.taglist = "Artist,Album,AlbumArtist,Title,Track,Genre,Date,Composer,Performer";
|
2018-10-29 18:17:04 +00:00
|
|
|
config.searchtaglist = "Artist,Album,AlbumArtist,Title,Genre,Composer,Performer";
|
|
|
|
config.browsetaglist = "Artist,Album,AlbumArtist,Genre,Composer,Performer";
|
2018-09-24 22:27:24 +00:00
|
|
|
config.smartpls = true;
|
2018-10-14 22:03:56 +00:00
|
|
|
config.max_elements_per_page = 100;
|
2018-11-12 23:48:29 +00:00
|
|
|
config.last_played_count = 20;
|
2018-10-15 22:34:25 +00:00
|
|
|
char *etcdir = strdup(argv[1]);
|
2019-01-07 18:54:40 +00:00
|
|
|
config.etcdir = strdup(dirname(etcdir));
|
|
|
|
free(etcdir);
|
2018-10-29 18:17:04 +00:00
|
|
|
config.syscmds = false;
|
|
|
|
config.localplayer = true;
|
2018-11-27 21:24:45 +00:00
|
|
|
config.loglevel = 1;
|
2019-01-19 00:20:05 +00:00
|
|
|
config.backgroundcolor = "#888";
|
2018-08-06 20:51:48 +00:00
|
|
|
|
|
|
|
if (argc == 2) {
|
2019-01-11 00:38:44 +00:00
|
|
|
printf("Starting myMPD %s\n", MYMPD_VERSION);
|
|
|
|
printf("Libmpdclient %i.%i.%i\n", LIBMPDCLIENT_MAJOR_VERSION, LIBMPDCLIENT_MINOR_VERSION, LIBMPDCLIENT_PATCH_VERSION);
|
|
|
|
printf("Parsing config file: %s\n", argv[1]);
|
2018-08-06 20:51:48 +00:00
|
|
|
if (ini_parse(argv[1], inihandler, &config) < 0) {
|
2018-08-22 00:13:30 +00:00
|
|
|
printf("Can't load config file \"%s\"\n", argv[1]);
|
2018-08-06 20:51:48 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-08-21 19:28:13 +00:00
|
|
|
printf("myMPD %s\n"
|
2019-01-05 12:29:10 +00:00
|
|
|
"Copyright (C) 2018-2019 Juergen Mang <mail@jcgames.de>\n"
|
2018-08-23 09:27:12 +00:00
|
|
|
"https://github.com/jcorporation/myMPD\n"
|
|
|
|
"Built " __DATE__ " "__TIME__"\n\n"
|
|
|
|
"Usage: %s /path/to/mympd.conf\n",
|
|
|
|
MYMPD_VERSION,
|
|
|
|
argv[0]
|
2018-08-21 19:28:13 +00:00
|
|
|
);
|
2018-08-06 20:51:48 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-05-24 18:17:54 +00:00
|
|
|
}
|
2018-11-28 14:23:29 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Debug flag enabled, setting loglevel to debug\n");
|
|
|
|
config.loglevel = 3;
|
|
|
|
#endif
|
2019-01-18 21:06:29 +00:00
|
|
|
printf("Setting loglevel to %ld\n", config.loglevel);
|
|
|
|
loglevel = config.loglevel;
|
2018-05-24 18:17:54 +00:00
|
|
|
|
2018-06-14 19:56:12 +00:00
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
2019-01-05 00:01:34 +00:00
|
|
|
|
2019-01-05 12:29:10 +00:00
|
|
|
//init webserver
|
2019-01-06 09:36:34 +00:00
|
|
|
struct mg_mgr mgr;
|
2019-01-07 18:54:40 +00:00
|
|
|
if (!web_server_init(&mgr, &config)) {
|
2019-01-05 12:29:10 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-06-14 19:56:12 +00:00
|
|
|
}
|
2019-01-08 00:09:43 +00:00
|
|
|
|
2019-01-05 12:29:10 +00:00
|
|
|
//drop privileges
|
2018-08-06 20:51:48 +00:00
|
|
|
if (config.user != NULL) {
|
2019-01-11 00:38:44 +00:00
|
|
|
printf("Droping privileges to %s\n", config.user);
|
2018-06-14 19:56:12 +00:00
|
|
|
struct passwd *pw;
|
2018-08-06 20:51:48 +00:00
|
|
|
if ((pw = getpwnam(config.user)) == NULL) {
|
2018-11-28 14:23:29 +00:00
|
|
|
printf("getpwnam() failed, unknown user\n");
|
2019-01-06 09:36:34 +00:00
|
|
|
web_server_free(&mgr);
|
2018-06-14 19:56:12 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-08-27 23:45:14 +00:00
|
|
|
} else if (setgroups(0, NULL) != 0) {
|
|
|
|
printf("setgroups() failed\n");
|
2019-01-06 09:36:34 +00:00
|
|
|
web_server_free(&mgr);
|
2018-08-27 23:45:14 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-06-14 19:56:12 +00:00
|
|
|
} else if (setgid(pw->pw_gid) != 0) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("setgid() failed\n");
|
2019-01-06 09:36:34 +00:00
|
|
|
web_server_free(&mgr);
|
2018-06-14 19:56:12 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else if (setuid(pw->pw_uid) != 0) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("setuid() failed\n");
|
2019-01-06 09:36:34 +00:00
|
|
|
web_server_free(&mgr);
|
2018-06-14 19:56:12 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 22:15:54 +00:00
|
|
|
|
|
|
|
if (getuid() == 0) {
|
2018-08-23 09:27:12 +00:00
|
|
|
printf("myMPD should not be run with root privileges\n");
|
2019-01-06 09:36:34 +00:00
|
|
|
web_server_free(&mgr);
|
2018-08-23 09:27:12 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-06-21 22:15:54 +00:00
|
|
|
}
|
2018-10-07 21:21:00 +00:00
|
|
|
|
2019-01-05 12:29:10 +00:00
|
|
|
//check needed directories
|
|
|
|
if (!testdir("Document root", DOC_ROOT))
|
2018-10-07 21:56:11 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2019-01-05 12:29:10 +00:00
|
|
|
snprintf(testdirname, 400, "%s/library", DOC_ROOT);
|
2019-01-11 00:38:44 +00:00
|
|
|
if (!testdir("Link to mpd music_directory", testdirname)) {
|
|
|
|
printf("Disabling coverimage support\n");
|
2018-11-14 22:00:55 +00:00
|
|
|
config.coverimage = false;
|
|
|
|
}
|
2018-10-29 19:41:04 +00:00
|
|
|
|
2018-10-07 21:56:11 +00:00
|
|
|
snprintf(testdirname, 400, "%s/tmp", config.varlibdir);
|
|
|
|
if (!testdir("Temp dir", testdirname))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
snprintf(testdirname, 400, "%s/smartpls", config.varlibdir);
|
|
|
|
if (!testdir("Smartpls dir", testdirname))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
snprintf(testdirname, 400, "%s/state", config.varlibdir);
|
|
|
|
if (!testdir("State dir", testdirname))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2019-01-05 12:29:10 +00:00
|
|
|
//Create working threads
|
2019-01-08 00:09:43 +00:00
|
|
|
pthread_t mpd_client_thread, web_server_thread, mympd_api_thread;
|
2019-01-05 12:29:10 +00:00
|
|
|
//mpd connection
|
2019-01-11 00:38:44 +00:00
|
|
|
pthread_create(&mpd_client_thread, NULL, mpd_client_loop, &config);
|
2019-01-05 12:29:10 +00:00
|
|
|
//webserver
|
2019-01-06 09:36:34 +00:00
|
|
|
pthread_create(&web_server_thread, NULL, web_server_loop, &mgr);
|
2019-01-08 00:09:43 +00:00
|
|
|
//mympd api
|
|
|
|
pthread_create(&mympd_api_thread, NULL, mympd_api_loop, &config);
|
2019-01-05 00:01:34 +00:00
|
|
|
|
2019-01-11 00:38:44 +00:00
|
|
|
//Outsourced all work to separate threads, do nothing...
|
2019-01-05 00:25:30 +00:00
|
|
|
|
2019-01-14 19:43:40 +00:00
|
|
|
//cleanup
|
2019-01-06 09:36:34 +00:00
|
|
|
pthread_join(mpd_client_thread, NULL);
|
|
|
|
pthread_join(web_server_thread, NULL);
|
2019-01-05 12:29:10 +00:00
|
|
|
tiny_queue_free(web_server_queue);
|
|
|
|
tiny_queue_free(mpd_client_queue);
|
2019-01-08 00:09:43 +00:00
|
|
|
tiny_queue_free(mympd_api_queue);
|
2018-05-24 18:17:54 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|