hopeful fix for random crashes - part I
[scrobby.git] / src / configuration.cpp
blob676dc9d357838e677d9f61988c0a62f6074de79f
1 /***************************************************************************
2 * Copyright (C) 2008 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 namespace {
50 std::string GetLineValue(const string &line, char a = '"', char b = '"')
52 int i = 0;
53 int begin = -1, end = -1;
54 for (string::const_iterator it = line.begin(); it != line.end(); i++, it++)
56 if (*it == a || *it == b)
58 if (begin < 0)
59 begin = i+1;
60 else
61 end = i;
64 if (begin >= 0 && end >= 0)
65 return line.substr(begin, end-begin);
66 else
67 return "";
70 void HomeFolder(const ScrobbyConfig &conf, string &s)
72 if (s[0] == '~')
73 s.replace(0, 1, conf.user_home_folder);
76 LogLevel IntoLogLevel(const string &value)
78 LogLevel result = llInfo;
79 if (value == "none")
81 result = llNone;
83 else if (value == "info")
85 result = llInfo;
87 else if (value == "verbose")
89 result = llVerbose;
91 return result;
95 void ParseArgv(ScrobbyConfig &conf, int argc, char **argv)
97 for (int i = 1; i < argc; i++)
99 if (strcmp(argv[i], "--help") == 0)
101 std::cout
102 << "usage:\n"
103 << "scrobby [options] <conf file>\n"
104 << "scrobby [options] (search for ~/.scrobbyconf, then /etc/scrobby.conf)\n\n"
105 << "options:\n"
106 << " --help show this help message\n"
107 << " --no-daemon do not detach from console\n"
108 << " --quiet do not log anything\n"
109 << " --verbose verbose logging\n"
110 << " --version print version information\n"
112 exit(0);
114 else if (strcmp(argv[i], "--no-daemon") == 0)
116 conf.daemonize = false;
118 else if (strcmp(argv[i], "--quiet") == 0)
120 conf.log_level = llNone;
122 else if (strcmp(argv[i], "--verbose") == 0)
124 conf.log_level = llVerbose;
126 else if (strcmp(argv[i], "--version") == 0)
128 std::cout << "scrobby - an audioscrobbler mpd client, version "VERSION"\n";
129 exit(0);
131 else
133 conf.file_config = argv[i];
138 bool CheckFiles(ScrobbyConfig &conf)
140 std::ofstream f;
141 std::ifstream g;
143 g.open(conf.file_pid.c_str());
144 if (g.is_open())
146 string strpid;
147 getline(g, strpid);
148 g.close();
149 pid_t pid = StrToInt(strpid);
150 if (pid < 1)
152 std::cerr << "pid file: " << conf.file_pid << " is invalid, trying to remove...\n";
153 if (unlink(conf.file_pid.c_str()) == 0)
155 std::cout << "pid file succesfully removed.\n";
157 else
159 std::cerr << "couldn't remove pid file.\n";
160 return false;
163 else
165 # ifdef __linux__
166 struct stat stat_pid;
167 std::ostringstream proc_file;
168 proc_file << "/proc/" << pid << std::ends;
169 if (!(stat(proc_file.str().c_str(), &stat_pid) == -1 && errno == ENOENT))
170 # elif defined(__FreeBSD__)
171 char kvm_errbuf[_POSIX2_LINE_MAX];
172 kvm_t *hkvm;
173 int cnt = 0;
174 struct kinfo_proc *proc;
176 hkvm = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, kvm_errbuf);
177 if (hkvm)
179 proc = kvm_getprocs(hkvm, KERN_PROC_PID, pid, &cnt);
180 kvm_close(hkvm);
182 else
184 std::cerr << "kvm_openfiles failed with message:" << std::endl << kvm_errbuf << std::endl;
186 if (cnt >= 1)
187 # endif
189 std::cerr << "scrobby is already running with PID " << pid << "!\n";
190 return false;
195 f.open(conf.file_pid.c_str(), std::ios_base::app);
196 if (!f.is_open())
198 std::cerr << "Cannot create/open pid file: " << conf.file_pid << std::endl;
199 return false;
201 f.close();
203 f.open(conf.file_log.c_str(), std::ios_base::app);
204 if (!f.is_open())
206 std::cerr << "Cannot create/open log file: " << conf.file_log << std::endl;
207 return false;
209 f.close();
211 f.open(conf.file_cache.c_str(), std::ios_base::app);
212 if (!f.is_open())
214 std::cerr << "Cannot create/open cache file: " << conf.file_cache << std::endl;
215 return false;
217 f.close();
219 return true;
222 void DefaultConfiguration(ScrobbyConfig &conf)
224 conf.user_home_folder = getenv("HOME") ? getenv("HOME") : "";
226 conf.mpd_host = "localhost";
227 conf.mpd_port = 6600;
228 conf.mpd_timeout = 15;
230 conf.file_log = "/var/log/scrobby/scrobby.log";
231 conf.file_pid = "/var/run/scrobby/scrobby.pid";
232 conf.file_cache = "/var/cache/scrobby/scrobby.cache";
234 conf.log_level = llUndefined;
235 conf.daemonize = true;
238 bool ReadConfiguration(ScrobbyConfig &conf, const string &file)
240 string line, v;
241 std::ifstream f(file.c_str());
243 if (!f.is_open())
244 return false;
246 while (!f.eof())
248 getline(f, line);
249 if (!line.empty() && line[0] != '#')
251 v = GetLineValue(line);
253 if (line.find("dedicated_user") != string::npos)
255 if (!line.empty())
256 conf.dedicated_user = v;
258 else if (line.find("mpd_host") != string::npos)
260 if (!line.empty())
261 conf.mpd_host = v;
263 else if (line.find("mpd_port") != string::npos)
265 if (!v.empty())
266 conf.mpd_port = StrToInt(v);
268 else if (line.find("mpd_timeout") != string::npos)
270 if (!v.empty())
271 conf.mpd_timeout = StrToInt(v);
273 else if (line.find("log_file") != string::npos)
275 if (!v.empty())
277 HomeFolder(conf, v);
278 conf.file_log = v;
281 else if (line.find("pid_file") != string::npos)
283 if (!v.empty())
285 HomeFolder(conf, v);
286 conf.file_pid = v;
289 else if (line.find("cache_file") != string::npos)
291 if (!v.empty())
293 HomeFolder(conf, v);
294 conf.file_cache = v;
297 else if (line.find("lastfm_user") != string::npos)
299 if (!v.empty())
300 conf.lastfm_user = v;
302 else if (line.find("lastfm_password") != string::npos)
304 if (!v.empty())
305 conf.lastfm_password = v;
307 else if (line.find("lastfm_md5_password") != string::npos)
309 if (!v.empty())
310 conf.lastfm_md5_password = v;
312 else if (line.find("log_level") != string::npos)
314 if (!v.empty() && conf.log_level == llUndefined)
315 conf.log_level = IntoLogLevel(v);
319 f.close();
320 return true;