hopeful fix for random crashes - part I
[scrobby.git] / src / misc.cpp
blob92caa3af31e1053f1764e14f56b080cbb97686e2
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 <cerrno>
22 #include <cstdlib>
23 #include <cstring>
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <openssl/evp.h>
28 #include <pwd.h>
30 #include "configuration.h"
31 #include "misc.h"
33 extern ScrobbyConfig config;
35 pthread_mutex_t log_file = PTHREAD_MUTEX_INITIALIZER;
37 size_t write_data(char *buffer, size_t size, size_t nmemb, std::string data)
39 size_t result = size * nmemb;
40 data.append(buffer, result);
41 return result;
44 void ChangeToUser()
46 if (config.dedicated_user.empty() || getuid() != 0)
47 return;
49 passwd *userinfo;
50 if (!(userinfo = getpwnam(config.dedicated_user.c_str())))
52 std::cerr << "user " << config.dedicated_user << " not found!\n";
53 exit(1);
55 if (setgid(userinfo->pw_gid) == -1)
57 std::cerr << "cannot set gid for user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
58 exit(1);
60 if (setuid(userinfo->pw_uid) == -1)
62 std::cerr << "cannot change to uid of user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
63 exit(1);
65 config.user_home_folder = userinfo->pw_dir ? userinfo->pw_dir : "";
68 bool Daemonize()
70 if (daemon(0, 0) < 0)
71 return false;
73 std::ofstream f(config.file_pid.c_str(), std::ios_base::trunc);
74 if (f.is_open())
76 pid_t pid = getpid();
77 f << pid;
78 f.close();
79 return true;
81 else
82 return false;
85 void ClearCache()
87 std::ofstream f(config.file_cache.c_str(), std::ios::trunc);
88 f.close();
91 void GetCachedSongs(std::vector<std::string> &v)
93 std::ifstream f(config.file_cache.c_str());
94 if (f.is_open())
96 std::string line;
97 while (!f.eof())
99 getline(f, line);
100 if (!line.empty())
101 v.push_back(line);
106 void Cache(const std::string &s)
108 std::ofstream f(config.file_cache.c_str(), std::ios::app);
109 if (f.is_open())
111 f << s << std::endl;
112 f.close();
116 void Log(LogLevel ll, const char *format, ...)
118 if (config.log_level < ll)
119 return;
120 pthread_mutex_lock(&log_file);
121 FILE *f = fopen(config.file_log.c_str(), "a");
122 if (!f)
124 perror("Cannot open log file!\n");
125 exit(1);
127 fprintf(f, "[%s] ", DateTime().c_str());
128 va_list list;
129 va_start(list, format);
130 vfprintf(f, format, list);
131 va_end(list);
132 fprintf(f, "\n");
133 fclose(f);
134 pthread_mutex_unlock(&log_file);
137 void ignore_newlines(std::string &s)
139 for (size_t i = s.find("\n"); i != std::string::npos; i = s.find("\n"))
140 s.replace(i, 1, "");
143 std::string md5sum(const std::string &s)
145 unsigned char md_value[EVP_MAX_MD_SIZE];
146 unsigned int md_len;
148 EVP_MD_CTX mdctx;
149 EVP_DigestInit(&mdctx, EVP_md5());
150 EVP_DigestUpdate(&mdctx, s.c_str(), s.length());
151 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
152 EVP_MD_CTX_cleanup(&mdctx);
154 char result[md_len*2+1];
155 for (unsigned i = 0; i < md_len; i++)
156 sprintf(&result[i*2], "%02x", md_value[i]);
157 result[md_len*2] = 0;
159 return result;
162 std::string DateTime()
164 char result[32];
165 time_t raw;
166 tm *t;
167 time(&raw);
168 t = localtime(&raw);
169 result[strftime(result, 31, "%Y/%m/%d %X", t)] = 0;
170 return result;
173 int StrToInt(const std::string &s)
175 return atoi(s.c_str());