#include <unistd.h>
#include "daemon.h"
+#include "debug.h"
#define DEBUG_LOGFILE "/tmp/daemonlog.da"
#if DEBUG
static inline void close_on_exec_dup(int old, int new)
{
- dup2(old, new);
- fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
+ if (dup2(old, new) != -1)
+ fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
+ else
+ LOGE("dup2 fail\n");
}
void initialize_log(void)
default:
return "unknown error";
}
- return "unknown error";
}
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);
+ }
md5_finish(&md5_state, digest);
- close(fd);
}
static const char* basename(const char *filename)
int check_us_app_count(uint32_t app_count)
{
int res = 1;
- if ((app_count < US_APP_COUNT_MIN) ||
- (app_count > US_APP_COUNT_MAX))
- {
+ if (app_count > US_APP_COUNT_MAX) {
LOGE("wrong user space app count %u (0x%08X)\n",
(unsigned int)app_count, app_count);
res = 0;
int check_us_app_inst_func_count(uint32_t func_count)
{
int res = 1;
- if ((func_count < US_APP_INST_FUNC_MIN) ||
- (func_count > US_APP_INST_FUNC_MAX))
- {
+ if (func_count > US_APP_INST_FUNC_MAX) {
LOGE("wrong US app inst func count %u (0x%08X)\n",
(unsigned int)func_count, func_count);
res = 0;
int check_lib_inst_count(uint32_t lib_count)
{
int res = 1;
- if ((lib_count < US_APP_INST_LIB_MIN) ||
- (lib_count > US_APP_INST_LIB_MAX))
- {
+ if (lib_count > US_APP_INST_LIB_MAX) {
LOGE("wrong US app inst lib count %u (0x%08X)\n",
(unsigned int)lib_count, lib_count);
res = 0;
#define CONF_DATA_MSG_PERIOD_MIN 1
#define CONF_DATA_MSG_PERIOD_MAX 100
-#define US_APP_COUNT_MIN 0
#define US_APP_COUNT_MAX MAX_TARGET_COUNT
-#define US_APP_INST_FUNC_MIN 0
#define US_APP_INST_FUNC_MAX 100000
#define US_FUNC_ARGS "bcdxpfw"
#define US_FUNC_RETURN "vnbcdxpfw"
-#define US_APP_INST_LIB_MIN 0
#define US_APP_INST_LIB_MAX 1000
int check_app_type(uint32_t app_type);
#include <system_info.h>
#include <runtime_info.h>
#include "device_system_info.h"
+#include "debug.h"
static int is_available(const char *path)
{
- bool res;
+ bool res = 0;
- system_info_get_platform_bool(path, &res);
+ if (system_info_get_platform_bool(path, &res) != SYSTEM_INFO_ERROR_NONE) {
+ /* TODO do something */
+ LOGE("get bool value fail\n");
+ }
return res;
}
static size_t fsize(int fd)
{
struct stat buf;
- fstat(fd, &buf);
+ if (fstat(fd, &buf) != 0) {
+ LOGE("cannot get file size\n");
+ return 0;
+ }
return buf.st_size;
}
freqs[cpu_n] = 0.0;
} else {
//core enabled, get frequency
- fscanf(f, "%s", freq_str);
+ if (fscanf(f, "%s", freq_str) != 1) {
+ /* TODO return error code */
+ LOGE("scan fail\n");
+ return;
+ }
+
freqs[cpu_n] = atof(freq_str);
LOGI_th_samp("core #%d freq = %.0f\n", cpu_n, freqs[cpu_n]);
fclose(f);
return 0;
}
+/* TODO add return value to skip_lines */
static void skip_lines(FILE * fp, unsigned int count)
{
char *buffer = NULL;
size_t buflen;
unsigned int index;
- for (index = 0; index != count; ++index)
- getline(&buffer, &buflen, fp);
+ for (index = 0; index != count; ++index) {
+ if (getline(&buffer, &buflen, fp) < 0)
+ LOGE("file scan fail\n");
+ }
free(buffer);
}
-static void skip_tokens(FILE * fp, unsigned int count)
+static int skip_tokens(FILE * fp, unsigned int count)
{
+ int res;
unsigned int index;
- for (index = 0; index != count; ++index)
- fscanf(fp, "%*s");
+ for (index = 0; index != count; ++index) {
+ res = fscanf(fp, "%*s");
+ if (res == EOF && index != count - 1)
+ return -1;
+ }
+
+ return 0;
}
static void init_network_stat()
while (fscanf(fp, "%s", ifname) != EOF)
if (strcmp("lo:", ifname)) {
- fscanf(fp, "%" SCNuMAX, &irecv);
+ if (fscanf(fp, "%" SCNuMAX, &irecv) <= 0)
+ goto scan_error;
skip_tokens(fp, 7);
- fscanf(fp, "%" SCNuMAX, &isend);
+ if (fscanf(fp, "%" SCNuMAX, &isend) <= 0)
+ goto scan_error;
skip_tokens(fp, 7);
*recv += irecv;
*send += isend;
} else
skip_tokens(fp, 16);
+
+ goto exit;
+scan_error:
+ LOGE("scan fail\n");
+exit:
+ return;
}
static void peek_network_stat_diff(uint32_t *recv, uint32_t *send)
/* reset error code */
errno = 0;
/* scan partition sector size */
- fscanf(f, "%d", &res);
- /* check errors */
- int errsv = errno;
- if (errsv) {
- LOGE("scan file <%s> error: %s\n", buf,
- strerror(errsv));
- res = 0;
+ if (fscanf(f, "%d", &res) != 1) {
+ /* check errors */
+ int errsv = errno;
+ if (errsv) {
+ LOGE("scan file <%s> error: %s\n", buf,
+ strerror(errsv));
+ res = 0;
+ }
}
/* close source file */
fclose(f);
char partition[partition_name_maxlength];
uintmax_t preads, pwrites;
uintmax_t psec_read, psec_write;
- skip_tokens(fp, 2);
- fscanf(fp, "%s", partition);
+ if (skip_tokens(fp, 2) < 0)
+ goto exit;
+
+ if (fscanf(fp, "%s", partition) != 1)
+ goto scan_error;
if (*master_partition
&& !strncmp(master_partition, partition,
strlen(master_partition))) {
/* subpartition */
skip_tokens(fp, 11);
} else {
+ /* TODO add check err in skip_token func */
//1
- fscanf(fp, "%" SCNuMAX, &preads);
+ if (fscanf(fp, "%" SCNuMAX, &preads) == EOF)
+ goto scan_error;
skip_tokens(fp, 1);
//3
- fscanf(fp, "%" SCNuMAX, &psec_read);
+ if (fscanf(fp, "%" SCNuMAX, &psec_read) == EOF)
+ goto scan_error;
skip_tokens(fp, 1);
//5
- fscanf(fp, "%" SCNuMAX, &pwrites);
+ if (fscanf(fp, "%" SCNuMAX, &pwrites) == EOF)
+ goto scan_error;
skip_tokens(fp, 1);
//7
- fscanf(fp, "%" SCNuMAX, &psec_write);
+ if (fscanf(fp, "%" SCNuMAX, &psec_write) == EOF)
+ goto scan_error;
skip_tokens(fp, 4);
memcpy(master_partition, partition,
}
}
+ goto exit;
+scan_error:
+ LOGE("scan fail\n");
+exit:
+ return;
}
static void peek_disk_stat_diff(uint32_t *reads, uint32_t *bytes_reads,
for (;;) {
err = target_recv_msg(target, &log);
- if (err != 0) {
+ if ((err != 0) || (log.length >= TARGER_MSG_MAX_LEN)) {
/* disconnect */
event = EVENT_STOP;
write(target->event_fd, &event, sizeof(event));
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
//init pipe
- pipe(fd_pipe);
+ if (pipe(fd_pipe) != 0) {
+ /* TODO posible need total instrumentation stop up there */
+ LOGE("can not pipe!\n");
+ return NULL;
+ }
+
//set cleanup function
pthread_cleanup_push(transfer_thread_cleanup, (void *)fd_pipe);
}
saved_flags = fcntl(manager.buf_fd, F_GETFL);
- fcntl(manager.buf_fd, F_SETFL, saved_flags & ~O_NONBLOCK);
+ if (fcntl(manager.buf_fd, F_SETFL, saved_flags & ~O_NONBLOCK) == -1) {
+ LOGE("can not set buf_fd flags\n");
+ return -1;
+ }
if(pthread_create(&(manager.transfer_thread),
NULL,
flush_buf();
saved_flags = fcntl(manager.buf_fd, F_GETFL);
- fcntl(manager.buf_fd, F_SETFL, saved_flags | O_NONBLOCK);
+ if (fcntl(manager.buf_fd, F_SETFL, saved_flags | O_NONBLOCK) == -1) {
+ /* TODO do something on error */
+ LOGE("can not set buf_fd flags\n");
+ }
wake_up_buf();
LOGI("joining thread...\n");