static void close_tasks_dev(void)
{
LOGI("close tasks dev (%d)\n", manager.fd.inst_tasks);
- fclose(manager.fd.inst_tasks);
+ if (manager.fd.inst_tasks != NULL) {
+ fclose(manager.fd.inst_tasks);
+ manager.fd.inst_tasks = NULL;
+ } else {
+ LOGW("manager.fd.inst_tasks alredy closed or not opened\n");
+ }
}
static int open_buf_ctl(void)
#include "daemon.h"
#include "debug.h"
+#include <errno.h>
#define DEBUG_LOGFILE "/tmp/daemonlog.da"
#if DEBUG
-static inline void close_on_exec_dup(int old, int new)
+static inline int close_on_exec_dup(int old, int new)
{
- if (dup2(old, new) != -1)
- fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
- else
+ int ret = -1;
+
+ if (dup2(old, new) != -1) {
+ unsigned long flags = fcntl(new, F_GETFD);
+ if (flags == -1) {
+ LOGE("can not get flags fd #%d <%s>\n", new,
+ strerror(errno));
+ goto err_ret;
+ }
+
+ if (fcntl(new, F_SETFD, flags | FD_CLOEXEC) == -1) {
+ LOGE("can not get flags fd #%d <%s>\n", new,
+ strerror(errno));
+ goto err_ret;
+ }
+ } else {
LOGE("dup2 fail\n");
+ goto err_ret;
+ }
+
+ /* success */
+ ret = 0;
+
+err_ret:
+ return ret;
}
-void initialize_log(void)
+int initialize_log(void)
{
+ int ret = 0;
int fd = open(DEBUG_LOGFILE, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fd != -1) {
- close_on_exec_dup(fd, 1);
- close_on_exec_dup(fd, 2);
+ if (close_on_exec_dup(fd, 1) != 0 ||
+ close_on_exec_dup(fd, 2) != 0) {
+ LOGE("duplicate fd fail\n");
+ ret = -1;
+ }
close(fd);
} else {
}
close(0);
+ return ret;
}
#else
-void initialize_log(void)
+int initialize_log(void)
{
+ return 0;
}
#endif
int fd = open(filename, O_RDONLY);
md5_init(&md5_state);
- if (fd > 0) {
+ if (fd >= 0) {
while ((size = read(fd, buffer, sizeof(buffer))) > 0)
md5_append(&md5_state, buffer, size);
close(fd);
+ } else {
+ LOGW("File does not exists <%s>\n", filename);
}
md5_finish(&md5_state, digest);
uint64_t get_total_alloc_size(void);
-void initialize_log(void);
+int initialize_log(void);
int daemonLoop(void);
void unlink_portfile(void);
if (!ensure_singleton(SINGLETON_LOCKFILE))
return 1;
- initialize_log();
+ if (initialize_log() != 0) {
+ LOGE("Init log failed. uninit\n");
+ terminate0();
+ LOGE("Daemon terminated\n");
+ exit(0);
+ }
LOGI("da_started\n");
atexit(terminate0);
FILE *f;
int cpu_n = 0;
- //clean data array
+ /* clean data array */
for (cpu_n = 0; cpu_n < num_of_cpu; cpu_n++)
freqs[cpu_n] = 0.0;
cpu_n = 0;
while (1) {
- //is CPU present
+ /* TODO for targets with 1 cpu core
+ * file "/sys/devices/system/cpu/cpu0/online" can be absent
+ * so need lookup file /sys/devices/system/cpu/online
+ * and parse it
+ */
+
+ /* is CPU present */
snprintf(filename, MIDDLE_BUFFER,
"/sys/devices/system/cpu/cpu%d/online", cpu_n);
}
fclose(f);
- //get CPU freq
+ /* get CPU freq */
snprintf(filename, MIDDLE_BUFFER,
"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", cpu_n);
f = fopen(filename, "r");
if (!f)
{
- //core is disabled
+ /* core is disabled */
LOGI_th_samp("core #%d diasabled\n", cpu_n);
freqs[cpu_n] = 0.0;
} else {
- //core enabled, get frequency
+ /* core enabled, get frequency /*/
if (fscanf(f, "%s", freq_str) != 1) {
/* TODO return error code */
- LOGE("scan fail\n");
- return;
+ freqs[cpu_n] = 0.0f;
+ LOGE("scan cpu #%d freq fail\n", cpu_n);
+ } else {
+ freqs[cpu_n] = atof(freq_str);
+ LOGI_th_samp("core #%d freq = %.0f\n", cpu_n,
+ freqs[cpu_n]);
}
-
- freqs[cpu_n] = atof(freq_str);
- LOGI_th_samp("core #%d freq = %.0f\n", cpu_n, freqs[cpu_n]);
fclose(f);
}