1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
27 #include <openssl/evp.h>
30 #include "configuration.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
);
46 if (config
.dedicated_user
.empty() || getuid() != 0)
50 if (!(userinfo
= getpwnam(config
.dedicated_user
.c_str())))
52 std::cerr
<< "user " << config
.dedicated_user
<< " not found!\n";
55 if (setgid(userinfo
->pw_gid
) == -1)
57 std::cerr
<< "cannot set gid for user " << config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
60 if (setuid(userinfo
->pw_uid
) == -1)
62 std::cerr
<< "cannot change to uid of user " << config
.dedicated_user
<< ": " << strerror(errno
) << std::endl
;
72 std::ofstream
f(config
.file_pid
.c_str(), std::ios_base::app
);
86 std::ofstream
f(config
.file_cache
.c_str(), std::ios::trunc
);
90 void GetCachedSongs(std::vector
<std::string
> &v
)
92 std::ifstream
f(config
.file_cache
.c_str());
105 void Cache(const std::string
&s
)
107 std::ofstream
f(config
.file_cache
.c_str(), std::ios::app
);
115 void Log(LogLevel ll
, const char *format
, ...)
117 if (config
.log_level
< ll
)
119 pthread_mutex_lock(&log_file
);
120 FILE *f
= fopen(config
.file_log
.c_str(), "a");
123 perror("Cannot open log file!\n");
126 fprintf(f
, "[%s] ", DateTime().c_str());
128 va_start(list
, format
);
129 vfprintf(f
, format
, list
);
133 pthread_mutex_unlock(&log_file
);
136 void ignore_newlines(std::string
&s
)
138 for (size_t i
= s
.find("\n"); i
!= std::string::npos
; i
= s
.find("\n"))
142 std::string
md5sum(const std::string
&s
)
144 unsigned char md_value
[EVP_MAX_MD_SIZE
];
148 EVP_DigestInit(&mdctx
, EVP_md5());
149 EVP_DigestUpdate(&mdctx
, s
.c_str(), s
.length());
150 EVP_DigestFinal_ex(&mdctx
, md_value
, &md_len
);
151 EVP_MD_CTX_cleanup(&mdctx
);
153 char result
[md_len
*2+1];
154 for (unsigned i
= 0; i
< md_len
; i
++)
155 sprintf(&result
[i
*2], "%02x", md_value
[i
]);
156 result
[md_len
*2] = 0;
161 std::string
DateTime()
168 result
[strftime(result
, 31, "%Y/%m/%d %X", t
)] = 0;
172 int StrToInt(const std::string
&s
)
174 return atoi(s
.c_str());