remove threads once again
[scrobby.git] / src / configuration.cpp
blob58e893605ff8ceaa4b417ab6eedc77f007ed89e5
1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <cstdlib>
22 #include <cstring>
23 #include <iostream>
24 #include <fstream>
26 #ifdef __linux__
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #elif defined(__FreeBSD__)
32 #include <fcntl.h>
33 #include <kvm.h>
34 #include <paths.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38 #endif
40 #ifdef HAVE_CONFIG_H
41 # include <config.h>
42 #endif
44 #include "configuration.h"
45 #include "misc.h"
47 using std::string;
49 ScrobbyConfig Config;
51 namespace
53 std::string GetLineValue(const string &line, char a = '"', char b = '"')
55 int i = 0;
56 int begin = -1, end = -1;
57 for (string::const_iterator it = line.begin(); it != line.end(); i++, it++)
59 if (*it == a || *it == b)
61 if (begin < 0)
62 begin = i+1;
63 else
64 end = i;
67 if (begin >= 0 && end >= 0)
68 return line.substr(begin, end-begin);
69 else
70 return "";
73 void HomeFolder(const ScrobbyConfig &conf, string &s)
75 if (s[0] == '~')
76 s.replace(0, 1, conf.user_home_folder);
79 LogLevel IntoLogLevel(const string &value)
81 LogLevel result = llInfo;
82 if (value == "none")
84 result = llNone;
86 else if (value == "info")
88 result = llInfo;
90 else if (value == "verbose")
92 result = llVerbose;
94 return result;
98 void ParseArgv(ScrobbyConfig &conf, int argc, char **argv)
100 for (int i = 1; i < argc; i++)
102 if (strcmp(argv[i], "--help") == 0)
104 std::cout
105 << "usage:\n"
106 << "scrobby [options] <conf file>\n"
107 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
108 << "options:\n"
109 << " --help show this help message\n"
110 << " --no-daemon do not detach from console\n"
111 << " --quiet do not log anything\n"
112 << " --verbose verbose logging\n"
113 << " --version print version information\n"
115 exit(0);
117 else if (strcmp(argv[i], "--no-daemon") == 0)
119 conf.daemonize = false;
121 else if (strcmp(argv[i], "--quiet") == 0)
123 conf.log_level = llNone;
125 else if (strcmp(argv[i], "--verbose") == 0)
127 conf.log_level = llVerbose;
129 else if (strcmp(argv[i], "--version") == 0)
131 std::cout << "scrobby - an audioscrobbler mpd client, version "VERSION"\n";
132 exit(0);
134 else
136 conf.file_config = argv[i];
141 bool CheckFiles(ScrobbyConfig &conf)
143 std::ofstream f;
144 std::ifstream g;
146 g.open(conf.file_pid.c_str());
147 if (g.is_open())
149 string strpid;
150 getline(g, strpid);
151 g.close();
152 pid_t pid = StrToInt(strpid);
153 if (pid < 1)
155 std::cerr << "pid file: " << conf.file_pid << " is invalid, trying to remove...\n";
156 if (unlink(conf.file_pid.c_str()) == 0)
158 std::cout << "pid file succesfully removed.\n";
160 else
162 std::cerr << "couldn't remove pid file.\n";
163 return false;
166 else
168 # ifdef __linux__
169 struct stat stat_pid;
170 std::ostringstream proc_file;
171 proc_file << "/proc/" << pid << std::ends;
172 if (!(stat(proc_file.str().c_str(), &stat_pid) == -1 && errno == ENOENT))
173 # elif defined(__FreeBSD__)
174 char kvm_errbuf[_POSIX2_LINE_MAX];
175 kvm_t *hkvm;
176 int cnt = 0;
177 struct kinfo_proc *proc;
179 hkvm = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, kvm_errbuf);
180 if (hkvm)
182 proc = kvm_getprocs(hkvm, KERN_PROC_PID, pid, &cnt);
183 kvm_close(hkvm);
185 else
187 std::cerr << "kvm_openfiles failed with message:" << std::endl << kvm_errbuf << std::endl;
189 if (cnt >= 1)
190 # endif
192 std::cerr << "scrobby is already running with PID " << pid << "!\n";
193 return false;
198 f.open(conf.file_pid.c_str(), std::ios_base::app);
199 if (!f.is_open())
201 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
202 return false;
204 f.close();
206 f.open(conf.file_log.c_str(), std::ios_base::app);
207 if (!f.is_open())
209 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
210 return false;
212 f.close();
214 f.open(conf.file_cache.c_str(), std::ios_base::app);
215 if (!f.is_open())
217 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
218 return false;
220 f.close();
222 return true;
225 void DefaultConfiguration(ScrobbyConfig &conf)
227 conf.user_home_folder = getenv("HOME") ? getenv("HOME") : "";
229 conf.mpd_host = "localhost";
230 conf.mpd_port = 6600;
231 conf.mpd_timeout = 15;
233 conf.file_log = "/var/log/scrobby/scrobby.log";
234 conf.file_pid = "/var/run/scrobby/scrobby.pid";
235 conf.file_cache = "/var/cache/scrobby/scrobby.cache";
237 conf.log_level = llUndefined;
238 conf.daemonize = true;
241 bool ReadConfiguration(ScrobbyConfig &conf, const string &file)
243 string line, v;
244 std::ifstream f(file.c_str());
246 if (!f.is_open())
247 return false;
249 while (!f.eof())
251 getline(f, line);
252 if (!line.empty() && line[0] != '#')
254 v = GetLineValue(line);
256 if (line.find("dedicated_user") != string::npos)
258 if (!line.empty())
259 conf.dedicated_user = v;
261 else if (line.find("mpd_host") != string::npos)
263 if (!line.empty())
264 conf.mpd_host = v;
266 else if (line.find("mpd_port") != string::npos)
268 if (!v.empty())
269 conf.mpd_port = StrToInt(v);
271 else if (line.find("mpd_timeout") != string::npos)
273 if (!v.empty())
274 conf.mpd_timeout = StrToInt(v);
276 else if (line.find("log_file") != string::npos)
278 if (!v.empty())
280 HomeFolder(conf, v);
281 conf.file_log = v;
284 else if (line.find("pid_file") != string::npos)
286 if (!v.empty())
288 HomeFolder(conf, v);
289 conf.file_pid = v;
292 else if (line.find("cache_file") != string::npos)
294 if (!v.empty())
296 HomeFolder(conf, v);
297 conf.file_cache = v;
300 else if (line.find("lastfm_user") != string::npos)
302 if (!v.empty())
303 conf.lastfm_user = v;
305 else if (line.find("lastfm_password") != string::npos)
307 if (!v.empty())
308 conf.lastfm_password = v;
310 else if (line.find("lastfm_md5_password") != string::npos)
312 if (!v.empty())
313 conf.lastfm_md5_password = v;
315 else if (line.find("log_level") != string::npos)
317 if (!v.empty() && conf.log_level == llUndefined)
318 conf.log_level = IntoLogLevel(v);
322 f.close();
323 return true;