if (manager.fd.inst_tasks == NULL) {
manager.fd.inst_tasks = fopen(INST_PID_FILENAME, "re");
if (manager.fd.inst_tasks == NULL) {
- LOGE("Cannot open tasks dev: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot open tasks dev: %s\n", buf);
return 1;
}
LOGI("tasks dev opened: %s, %d\n", BUF_FILENAME, manager.buf_fd);
{
manager.buf_fd = open(BUF_FILENAME, O_RDONLY | O_CLOEXEC);
if (manager.buf_fd == -1) {
- LOGE("Cannot open buffer: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot open buffer: %s\n", buf);
return 1;
}
LOGI("buffer opened: %s, %d\n", BUF_FILENAME, manager.buf_fd);
manager.user_ev_fd = open(USER_EVENT_FILENAME, O_WRONLY | O_CLOEXEC);
if (manager.user_ev_fd == -1) {
- LOGE("Cannot open user event sysfs file: %s\b", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot open user event sysfs file: %s\n", buf);
return 1;
}
LOGI("user event sysfs file opened: %s, %d\n", USER_EVENT_FILENAME,
}
if (ioctl(manager.buf_fd, SWAP_DRIVER_BUFFER_INITIALIZE, &init) == -1) {
- LOGE("Cannot init buffer: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot init buffer: %s\n", buf);
return 1;
}
if (open_tasks_dev() != 0) {
- LOGE("Cannot open tasks: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot open tasks: %s\n", buf);
return 1;
}
void exit_buf(void)
{
LOGI("Uninit driver (%d)\n", manager.buf_fd);
- if (ioctl(manager.buf_fd, SWAP_DRIVER_BUFFER_UNINITIALIZE) == -1)
- LOGW("Cannot uninit driver: %s\n", strerror(errno));
+ if (ioctl(manager.buf_fd, SWAP_DRIVER_BUFFER_UNINITIALIZE) == -1) {
+ GETSTRERROR(errno, buf);
+ LOGW("Cannot uninit driver: %s\n", buf);
+ }
close_buf_ctl();
close_tasks_dev();
void flush_buf(void)
{
- if (ioctl(manager.buf_fd, SWAP_DRIVER_FLUSH_BUFFER) == -1)
- LOGW("Cannot send flush to driver: %s\n", strerror(errno));
+ if (ioctl(manager.buf_fd, SWAP_DRIVER_FLUSH_BUFFER) == -1) {
+ GETSTRERROR(errno, buf);
+ LOGW("Cannot send flush to driver: %s\n", buf);
+ }
}
void wake_up_buf(void)
{
- if (ioctl(manager.buf_fd, SWAP_DRIVER_WAKE_UP) == -1)
- LOGW("Cannot send wake up to driver: %s\n", strerror(errno));
+ if (ioctl(manager.buf_fd, SWAP_DRIVER_WAKE_UP) == -1) {
+ GETSTRERROR(errno, buf);
+ LOGW("Cannot send wake up to driver: %s\n", buf);
+ }
}
int write_to_buf(struct msg_data_t *msg)
uint32_t total_len = MSG_DATA_HDR_LEN + msg->len;
if (write(manager.user_ev_fd, msg, total_len) == -1) {
+ GETSTRERROR(errno, buf);
LOGE("write to buf (user_ev_fd=%d, msg=%p, len=%d) %s\n",
- manager.user_ev_fd, msg, total_len, strerror(errno));
+ manager.user_ev_fd, msg, total_len, buf);
return 1;
}
return 0;
}
-#define print_feature(f,in,to,delim) \
- if (f & in) { \
- sprintf(to, dstr(f) delim ); \
- to+=strlen( dstr(f) delim ); \
+#define print_feature(f,in,to,delim) \
+ if (f & in) { \
+ if (strlen(dstr(f) delim) + 1 < buflen ) { \
+ lenin = snprintf(to, buflen, dstr(f) delim ); \
+ to += lenin; \
+ buflen -= lenin; \
+ } else { \
+ goto err_exit; \
+ } \
}
#define print_feature_0(f) print_feature(f, feature0, to, ", \n\t")
-static void feature_code_str(uint64_t feature0, uint64_t feature1, char *to)
+static void feature_code_str(uint64_t feature0, uint64_t feature1, char *to,
+ uint32_t buflen)
{
+ int lenin = 0;
print_feature_0(FL_FUNCTION_PROFILING);
print_feature_0(FL_MEMORY_ALLOC_PROBING);
print_feature_0(FL_FILE_API_PROBING);
print_feature_0(FL_SYSTEM_NETWORK);
print_feature_0(FL_SYSTEM_DEVICE);
print_feature_0(FL_SYSTEM_ENERGY);
+
+ goto exit;
+err_exit:
+ LOGE("Not enought mem to print\n");
+exit:
+ return;
}
printBuf((char *)msg, msg->len + sizeof (*msg));
if (send(manager.host.control_socket,
msg, MSG_CMD_HDR_LEN + msg->len, MSG_NOSIGNAL) == -1) {
- LOGE("Cannot send reply : %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot send reply : %s\n", buf);
return -1;
}
if (send(manager.host.control_socket, msg,
loglen, MSG_NOSIGNAL) == -1) {
- LOGE("Cannot send reply: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot send reply: %s\n", buf);
free(msg);
return 1;
}
s += len;
for (i = 0; i!= 16; ++i) {
- sprintf(s, "%02x", ba->digest[i]);
+ snprintf(s, 2, "%02x", ba->digest[i]);
s += 2;
}
*s = '\0';
int f;
ssize_t count;
- sprintf(buf, "/proc/%u/cmdline", pid);
+ snprintf(buf, sizeof(buf), "/proc/%u/cmdline", pid);
f = open(buf, O_RDONLY);
if (f != -1) {
count = read(f, buf, sizeof(buf));
sendACKToHost(msg->id, ERR_NO, 0, 0);
// send config message to target process
sendlog.type = MSG_OPTION;
- sendlog.length = sprintf(sendlog.data, "%llu",
- (unsigned long long) prof_session.conf.use_features0);
+ sendlog.length = snprintf(sendlog.data, sizeof(sendlog.data),
+ "%llu",
+ (unsigned long long)
+ prof_session.conf.use_features0) + 1;
target_send_msg_to_all(&sendlog);
break;
case NMSG_BINARY_INFO:
{
char buf[1024];
memset(&buf[0], 0, 1024);
- feature_code_str(conf->use_features0, conf->use_features1, buf);
+ feature_code_str(conf->use_features0, conf->use_features1, buf, sizeof(buf));
LOGI("conf = \n");
LOGI("\tuse_features = 0x%016LX : 0x%016LX \n(\t%s)\n",
conf->use_features0, conf->use_features1, buf);
}
// remove previous screen capture files
remove_indir(SCREENSHOT_DIR);
- if (mkdir(SCREENSHOT_DIR, 0777) == -1 && errno != EEXIST)
- LOGW("Failed to create directory for screenshot : %s\n",
- strerror(errno));
+ if (mkdir(SCREENSHOT_DIR, 0777) == -1 && errno != EEXIST) {
+ GETSTRERROR(errno, buf);
+ LOGW("Failed to create directory for screenshot : %s\n", buf);
+ }
set_label_for_all(SCREENSHOT_DIR);
char fname[64];
char buf[lpad_path_len];
- sprintf(fname, "/proc/%d/cmdline", pid);
+ snprintf(fname, sizeof(fname), "/proc/%d/cmdline", pid);
if (-1 == file2str(fname, buf, lpad_path_len))
return lpad_pid;
if (err == 0) {
/* send config message to target process */
log.type = MSG_OPTION;
- log.length = sprintf(log.data, "%llu\0",
- prof_session.conf.use_features0) + 1;
+ log.length = snprintf(log.data, sizeof(log.data), "%llu",
+ prof_session.conf.use_features0) + 1;
if (target_send_msg(target, &log) != 0)
LOGE("fail to send data to target %p\n", target);
#endif
#ifdef DEBUG
+#define GETSTRERROR(err_code, buf) \
+ char buf[256]; \
+ strerror_r(err_code, buf, sizeof(buf))
+
#define LOGE(...) do_log("ERR", __func__, __LINE__, __VA_ARGS__)
#define LOGW(...) do_log("WRN", __func__, __LINE__, __VA_ARGS__)
#endif
#else
+ #define GETSTRERROR(...)
#define LOGI(...)
#define LOGI_(...)
#define LOGE(...)
static void suffix_filename(char buf[PATH_MAX], const char *filename)
{
char adj_filename[PATH_MAX];
- sprintf(adj_filename, "%s.exe", filename);
+ snprintf(adj_filename, sizeof(adj_filename), "%s.exe", filename);
const char *use_filename = exist(adj_filename) ? adj_filename
: filename;
- strcpy(buf, use_filename);
+ size_t len = strlen(use_filename) + 1;
+ strncpy(buf, use_filename, len);
+ if (len > strlen(buf) + 1)
+ LOGE("too small buf <%s>\n", buf);
}
void get_build_dir(char builddir[PATH_MAX], const char *filename)
char **name_arr;
size_t bytes_count;
- sprintf(command, "/sys/class/input/%s/device/name", inputname);
+ if (snprintf(command, sizeof(command), "/sys/class/input/%s/device/name",
+ inputname) >= sizeof(command))
+ LOGE("too small buffer\n");
// run command
cmd_fp = fopen(command, "r");
if (cmd_fp == NULL)
DIR *dp;
struct dirent *d;
int count = 0;
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
+
dp = opendir("/sys/class/input");
if (dp == NULL)
goto exit;
- while ((d = readdir(dp)) != NULL) {
+ while ((readdir_r(dp, dirent_r, &d) == 0) && d) {
if (!strncmp(d->d_name, "event", 5)) {
// start with "event"
// event file
if (!check_input(d->d_name, input_id)) {
- sprintf(dev[count].fileName,
- "/dev/input/%s", d->d_name);
+ snprintf(dev[count].fileName, MAX_FILENAME,
+ "/dev/input/%s", d->d_name);
dev[count].fd = open(dev[count].fileName,
O_RDWR | O_NONBLOCK);
count++;
{
LOGI("write to device\n");
if (ioctl(manager.buf_fd, SWAP_DRIVER_MSG, msg) == -1) {
- LOGE("write to device: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("write to device: %s\n", buf);
return 1;
}
return 0;
memset(&serverAddrUn, '\0', sizeof(serverAddrUn));
serverAddrUn.sun_family = AF_UNIX;
- sprintf(serverAddrUn.sun_path, "%s", UDS_NAME);
+ snprintf(serverAddrUn.sun_path, sizeof(serverAddrUn.sun_path), "%s", UDS_NAME);
if (-1 == bind(manager.target_server_socket, (struct sockaddr*) &serverAddrUn,
sizeof(serverAddrUn)))
DIR *dir_info;
struct dirent *dir_entry;
char fullpath[PATH_MAX];
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
+
dir_info = opendir(BRIGHTNESS_PARENT_DIR);
if (dir_info != NULL) {
- while ((dir_entry = readdir(dir_info)) != NULL) {
+ while ((readdir_r(dir_info, dirent_r, &dir_entry) == 0) && dir_entry) {
if (strcmp(dir_entry->d_name, ".") == 0 ||
strcmp(dir_entry->d_name, "..") == 0)
continue;
else { /* first directory */
- sprintf(fullpath,
- BRIGHTNESS_PARENT_DIR "/%s/"
- BRIGHTNESS_FILENAME,
- dir_entry->d_name);
+ snprintf(fullpath, sizeof(fullpath),
+ BRIGHTNESS_PARENT_DIR "/%s/"
+ BRIGHTNESS_FILENAME,
+ dir_entry->d_name);
get_file_status(&manager.fd.brightness,
fullpath);
}
{
int maxbrightnessfd = -1;
static int max_brightness = -1;
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
if (__builtin_expect(max_brightness < 0, 0)) {
#ifdef DEVICE_ONLY
dir_info = opendir(BRIGHTNESS_PARENT_DIR);
if (dir_info != NULL) {
- while((dir_entry = readdir(dir_info)) != NULL) {
+ while ((readdir_r(dir_info, dirent_r, &dir_entry) == 0) && dir_entry) {
if (strcmp(dir_entry->d_name, ".") == 0 ||
strcmp(dir_entry->d_name, "..") == 0)
continue;
else { /* first */
- sprintf(fullpath,
- BRIGHTNESS_PARENT_DIR "/%s/" MAX_BRIGHTNESS_FILENAME,
- dir_entry->d_name);
+ snprintf(fullpath, sizeof(fullpath),
+ BRIGHTNESS_PARENT_DIR "/%s/" MAX_BRIGHTNESS_FILENAME,
+ dir_entry->d_name);
max_brightness = get_file_status(&maxbrightnessfd, fullpath);
}
}
char *abuf, *bbuf;
// read from stat file
- sprintf(filename, "%s/stat", path);
+ snprintf(filename, sizeof(filename), "%s/stat", path);
fd = open(filename, O_RDONLY, 0);
if(unlikely(fd == -1)){
P->sh_mem = 0;
// read from smaps file
- sprintf(filename, "%s/smaps", path);
+ snprintf(filename, sizeof(filename), "%s/smaps", path);
fp = fopen(filename, "r");
if(fp == NULL){
continue;
}
- sprintf(buf, "/proc/%d", pidarray[i]);
+ snprintf(buf, sizeof(buf), "/proc/%d", pidarray[i]);
if (unlikely(stat(buf, &sb) == -1)) // cannot access anymore
{
del_node(prochead, pidarray[i]);
}
}
- sprintf(filename, CPUNUM_OF_FREQ);
+ snprintf(filename, sizeof(filename), CPUNUM_OF_FREQ);
// update cpu frequency information
for(i = 0; i < num_of_cpu; i++)
{
unsigned int tid;
procNode *node = NULL;
procNode **thread_prochead = NULL;
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
- sprintf(path, "/proc/%d/task", pid);
+ snprintf(path, sizeof(path), "/proc/%d/task", pid);
if(!(taskdir = opendir(path)))
{
}
thread_prochead = (procNode **)&(node->thread_prochead);
- while((entry = readdir(taskdir)) != NULL)
- {
+ while ((readdir_r(taskdir, dirent_r, &entry) == 0) && entry) {
if(*entry->d_name > '0' && *entry->d_name <= '9')
{
tid = atoi(entry->d_name);
- sprintf(buf, "/proc/%d/task/%d", pid, tid);
+ snprintf(buf, sizeof(buf), "/proc/%d/task/%d", pid, tid);
if(unlikely(stat(buf, &sb) == -1))
{
// overall information getter functions
// ========================================================================
-static int get_device_network_type(char* buf, int buflen)
+#define print_to_buf(buf, buflen, str) \
+do { \
+ if (strlen(str) <= buflen) { \
+ lenin = snprintf(buf, buflen, "CDMA,"); \
+ len += lenin; \
+ buflen -= lenin; \
+ } else { \
+ LOGE("can not pack <%s>\n", str); \
+ goto exit; \
+ } \
+} while(1)
+
+static int get_device_network_type(char* buf, size_t buflen)
{
- int len = 0;
+ int len = 0, lenin = 0;
+
if (is_cdma_available())
- len += sprintf(buf + len, "CDMA,");
+ print_to_buf(buf, buflen, "CDMA,");
if (is_edge_available())
- len += sprintf(buf + len, "EDGE,");
+ print_to_buf(buf, buflen, "EDGE,");
if (is_gprs_available())
- len += sprintf(buf + len, "GPRS,");
+ print_to_buf(buf, buflen, "GPRS,");
if (is_gsm_available())
- len += sprintf(buf + len, "GSM,");
+ print_to_buf(buf, buflen, "GSM,");
if (is_hsdpa_available())
- len += sprintf(buf + len, "HSDPA,");
+ print_to_buf(buf, buflen, "HSDPA,");
if (is_hspa_available())
- len += sprintf(buf + len, "HSPA,");
+ print_to_buf(buf, buflen, "HSPA,");
if (is_hsupa_available())
- len += sprintf(buf + len, "HSUPA,");
+ print_to_buf(buf, buflen, "HSUPA,");
if (is_umts_available())
- len += sprintf(buf + len, "UMTS,");
+ print_to_buf(buf, buflen, "UMTS,");
if (is_lte_available())
- len += sprintf(buf + len, "LTE,");
+ print_to_buf(buf, buflen, "LTE,");
if (len != 0)
buf[--len] = 0;
+exit:
return len;
}
uint32_t res = 0;
/* prepare partition sector size file name */
- sprintf(buf, "/sys/block/%s/queue/hw_sector_size", partition_name);
+ snprintf(buf, MAX_FILENAME, "/sys/block/%s/queue/hw_sector_size", partition_name);
f = fopen(buf, "r");
/* check file */
/* check errors */
int errsv = errno;
if (errsv) {
- LOGE("scan file <%s> error: %s\n", buf,
- strerror(errsv));
+ GETSTRERROR(errsv, errno_buf);
+ LOGE("scan file <%s> error: %s\n", buf, errno_buf);
res = 0;
}
}
DIR *d = opendir("/proc");
struct dirent *dirent;
int count = 0, i = 0;
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
if (!d) {
- LOGW("Cannot open /proc dir (%s)\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGW("Cannot open /proc dir (%s)\n", buf);
return 0;
}
- while ((dirent = readdir(d)) && (count < n)) {
+ while ((readdir_r(d, dirent_r, &dirent) == 0) && dirent) {
if (dirent->d_type == DT_DIR) {
char *tmp;
pid_t pid = strtol(dirent->d_name, &tmp, 10);
fd_pipe[1], NULL,
BUF_SIZE, 0);
if (nrd == -1) {
- if (errno == EAGAIN) {
+ int errsv = errno;
+ if (errsv == EAGAIN) {
LOGI("No more data to read\n");
break;
}
- LOGE("Cannot splice read: %s\n", strerror(errno));
+ GETSTRERROR(errsv, err_buf);
+ LOGE("Cannot splice read: %s\n", err_buf);
goto thread_exit;
}
nrd, 0);
if (nwr == -1) {
- LOGE("Cannot splice write: %s\n", strerror(errno));
+ GETSTRERROR(errno, buf);
+ LOGE("Cannot splice write: %s\n", buf);
goto thread_exit;
}
if (nwr != nrd) {
return -1;
}
- sprintf(command, "%s %s", DA_PRELOAD_TIZEN, exec_path);
+ snprintf(command, sizeof(command), "%s %s", DA_PRELOAD_TIZEN, exec_path);
LOGI("cmd: %s\n", command);
pid = fork();
char cmdline[PATH_MAX];
DIR *proc;
FILE *fp;
+ static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
+ static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
struct dirent *entry;
int found, len = strlen(path);
pid_t pid = 0;
if (!proc)
goto out;
- while ((entry = readdir(proc)) != NULL) {
+ while ((readdir_r(proc, dirent_r, &entry) == 0) && entry) {
pid = (pid_t)atoi(entry->d_name);
if (pid == 0)
continue;
return pkg_pid;
}
-static int find_alternative_bin_path(const char *binary_path, char *alter_bin_path) {
+static int find_alternative_bin_path(const char *binary_path, char *alter_bin_path,
+ size_t buflen)
+{
// alternative path may be /opt/apps/... or /opt/usr/apps/...)
+ char *add_fname;
+ char *p;
if (strncmp(binary_path, APPDIR1, strlen(APPDIR1)) == 0) {
- strcpy(alter_bin_path, APPDIR2);
- strcat(alter_bin_path, binary_path + strlen(APPDIR1));
+ strncpy(alter_bin_path, APPDIR2, buflen);
+ buflen -= strlen(alter_bin_path);
+ add_fname = binary_path + strlen(APPDIR1);
+ strncat(alter_bin_path, add_fname, buflen);
} else if (strncmp(binary_path, APPDIR2, strlen(APPDIR2)) == 0) {
- strcpy(alter_bin_path, APPDIR1);
- strcat(alter_bin_path, binary_path + strlen(APPDIR2));
+ strncpy(alter_bin_path, APPDIR1, buflen);
+ buflen -= strlen(alter_bin_path);
+ add_fname = binary_path + strlen(APPDIR2);
+ strncat(alter_bin_path, add_fname, buflen);
} else {
return 1;
}
pkg_pid = get_pid_by_path(binary_path);
if (pkg_pid == 0) {
- if (find_alternative_bin_path(binary_path, alter_bin_path) == 0)
+ if (find_alternative_bin_path(binary_path, alter_bin_path, PATH_MAX) == 0)
pkg_pid = get_pid_by_path(alter_bin_path);
}
if (pkg_pid != 0) {
if (kill(pkg_pid, SIGTERM) == -1) {
- LOGE("cannot kill %d -%d err<%s>\n", pkg_pid, SIGKILL,
- strerror(errno));
+ GETSTRERROR(errno, err_buf);
+ LOGE("cannot kill %d -%d errno<%s>\n", pkg_pid, SIGKILL,
+ err_buf);
return -1;
} else {
// we need sleep up there because kill() function
tPath[tlen - 4] = '\0';
}
- sprintf(cmdPath, "/proc/%d/cmdline", pid);
+ snprintf(cmdPath, sizeof(cmdPath), "/proc/%d/cmdline", pid);
if((fp = fopen(cmdPath, "r")) == NULL)
{
resolved[0] = 0;
//try resolve <path>.exe
- sprintf(tmp_path, "%s.exe", path);
+ snprintf(tmp_path, sizeof(tmp_path), "%s.exe", path);
if ((res = realpath(tmp_path, resolved)) == NULL) {
//try to resolve path <path>
res = realpath(path, resolved);