#include <stdint.h>
#include <sys/types.h>
+#include <linux/limits.h>
#include "da_inst.h"
#include "da_protocol.h"
#include "da_protocol_inst.h"
#include "debug.h"
-
+#include "daemon.h"
struct lib_list_t *new_lib_inst_list = NULL;
char *packed_app_list = NULL;
char *packed_lib_list = NULL;
+static struct _msg_target_t *lib_maps_message = NULL;
+static pthread_mutex_t lib_inst_list_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t lib_maps_message_mutex = PTHREAD_MUTEX_INITIALIZER;
+
uint32_t libs_count;
//----------------------------------- lists ----------------------------------
return 1;
}
+static void lock_lib_maps_message()
+{
+ pthread_mutex_lock(&lib_inst_list_mutex);
+}
+
+static void unlock_lib_maps_message()
+{
+ pthread_mutex_unlock(&lib_inst_list_mutex);
+}
+
+static void lock_lib_list()
+{
+ pthread_mutex_lock(&lib_maps_message_mutex);
+}
+
+static void unlock_lib_list()
+{
+ pthread_mutex_unlock(&lib_maps_message_mutex);
+}
+
+static void generate_maps_inst_msg(struct user_space_inst_t *us_inst)
+{
+ lock_lib_maps_message();
+
+ struct lib_list_t *lib = us_inst->lib_inst_list;
+ struct app_list_t *app = us_inst->app_inst_list;
+ char *p, *resolved;
+ uint32_t total_maps_count = 0;
+ uint32_t total_len = sizeof(total_maps_count) + sizeof(*lib_maps_message);
+ char real_path_buf[PATH_MAX];
+
+ /* total message len calculate */
+ while (lib != NULL) {
+ p = lib->lib->bin_path;
+ total_len += strlen(p) + 1;
+ total_maps_count++;
+ LOGI("lib #%u <%s>\n", total_maps_count, p);
+ lib = (struct lib_list_t *)lib->next;
+ }
+
+ while (app != NULL) {
+ p = app->app->exe_path;
+ resolved = realpath((const char *)p, real_path_buf);
+ if (resolved != NULL) {
+ total_len += strlen(p) + 1;
+ total_maps_count++;
+ LOGI("app #%u <%s>\n", total_maps_count, resolved);
+ } else {
+ LOGE("cannot resolve bin path <%s>", p);
+ }
+
+ app = (struct app_list_t *)app->next;
+ }
+
+ if (lib_maps_message != NULL)
+ free(lib_maps_message);
+
+ lib_maps_message = malloc(total_len);
+ lib_maps_message->type = MSG_MAPS_INST_LIST;
+ lib_maps_message->length = total_len;
+
+ /* pack data */
+ p = lib_maps_message->data;
+ pack_int32(p, total_maps_count);
+
+ lib = us_inst->lib_inst_list;
+ while (lib != NULL) {
+ pack_str(p, lib->lib->bin_path);
+ lib = (struct lib_list_t *)lib->next;
+ }
+
+ app = us_inst->app_inst_list;
+ while (app != NULL) {
+ resolved = realpath(app->app->exe_path, real_path_buf);
+ if (resolved != NULL)
+ pack_str(p, real_path_buf);
+ app = (struct app_list_t *)app->next;
+ }
+
+ LOGI("total_len = %u\n", total_len);
+ print_buf((char *)lib_maps_message, total_len, "lib_maps_message");
+
+ unlock_lib_maps_message();
+}
+
+void send_maps_inst_msg_to(int sock)
+{
+ lock_lib_maps_message();
+ send_msg_to_target(sock, (struct msg_target_t *)lib_maps_message);
+ unlock_lib_maps_message();
+}
+
+static void send_maps_inst_msg_to_all_targets()
+{
+ lock_lib_maps_message();
+ send_msg_to_all_targets(lib_maps_message);
+ unlock_lib_maps_message();
+}
+
//-----------------------------------------------------------------------------
struct app_info_t *app_info_get_first(struct app_list_t **app_list)
{
int msg_start(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
struct msg_t **msg, enum ErrorCode *err)
{
+ int res = 0;
char *p = NULL;
*msg = NULL;
+
+ /* lock list access */
+ lock_lib_list();
+
if (!parse_app_inst_list(data, &us_inst->app_num, &us_inst->app_inst_list)) {
*err = ERR_WRONG_MESSAGE_FORMAT;
LOGE("parse app inst\n");
- return 1;
+ res = 1;
+ goto msg_start_exit;
}
generate_msg(msg, us_inst->lib_inst_list, us_inst->app_inst_list);
pack_int32(p, NMSG_START);
} else {
*err = ERR_CANNOT_START_PROFILING;
- return 1;
+ res = 1;
+ goto msg_start_exit;
}
- return 0;
+
+ generate_maps_inst_msg(us_inst);
+
+msg_start_exit:
+ /* unlock list access */
+ unlock_lib_list();
+
+ return res;
}
int msg_swap_inst_add(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
struct msg_t **msg, enum ErrorCode *err)
{
+ int res = 0;
uint32_t lib_num = 0;
char *p = NULL;
*err = ERR_UNKNOWN;
+ /* lock list access */
+ lock_lib_list();
+
if (!parse_lib_inst_list(data, &lib_num, &us_inst->lib_inst_list)) {
*err = ERR_WRONG_MESSAGE_FORMAT;
LOGE("parse lib inst list fail\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_add_exit;
}
// rm probes from new if its presents in cur
if (!resolve_collisions_for_add_msg(&us_inst->lib_inst_list, &new_lib_inst_list)) {
LOGE("resolve collision\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_add_exit;
};
// generate msg to send
cmp_libs))
{
LOGE("data move\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_add_exit;
};
// free new_list
free_data_list((struct data_list_t **)&new_lib_inst_list);
new_lib_inst_list = NULL;
*err = ERR_NO;
- return 0;
+
+ generate_maps_inst_msg(us_inst);
+ send_maps_inst_msg_to_all_targets();
+
+msg_swap_inst_add_exit:
+ /* unlock list access */
+ unlock_lib_list();
+
+ return res;
}
int msg_swap_inst_remove(struct msg_buf_t *data, struct user_space_inst_t *us_inst,
struct msg_t **msg, enum ErrorCode *err)
{
+ int res = 0;
uint32_t lib_num = 0;
char *p = NULL;
*err = ERR_UNKNOWN;
+ /* lock list access */
+ lock_lib_list();
+
if (!parse_lib_inst_list(data, &lib_num, &new_lib_inst_list)) {
*err = ERR_WRONG_MESSAGE_FORMAT;
LOGE("parse lib inst\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_remove_exit;
}
if (!resolve_collisions_for_rm_msg(&us_inst->lib_inst_list, &new_lib_inst_list)) {
LOGE("resolve collisions\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_remove_exit;
}
if (us_inst->app_inst_list != NULL) {
if (!generate_msg(msg, new_lib_inst_list, us_inst->app_inst_list)) {
LOGE("generate msg\n");
- return 1;
+ res = 1;
+ goto msg_swap_inst_remove_exit;
}
p = (char *)*msg;
pack_int32(p, NMSG_SWAP_INST_ADD);
free_data_list((struct data_list_t **)&new_lib_inst_list);
new_lib_inst_list = NULL;
*err = ERR_NO;
- return 0;
+
+ generate_maps_inst_msg(us_inst);
+ send_maps_inst_msg_to_all_targets();
+
+msg_swap_inst_remove_exit:
+ /* unlock list access */
+ unlock_lib_list();
+
+ return res;
}
void msg_swap_free_all_data(struct user_space_inst_t *us_inst)
return -(err_code != ERR_NO);
}
-static int process_msg_get_screenshot(struct msg_buf_t *msg_control)
+int send_msg_to_target(int sock, struct msg_target_t *msg)
+{
+ if (sock != -1) {
+ if (send(sock, msg, sizeof(struct _msg_target_t) + msg->length, MSG_NOSIGNAL) == -1)
+ LOGE("fail to send data to target socket(%d)\n", sock);
+ else
+ return 1;
+ }
+ return 0;
+}
+
+int send_msg_to_all_targets(struct msg_target_t *msg)
{
int target_index;
int sock;
+ for (target_index = 0; target_index < MAX_TARGET_COUNT; target_index++) {
+ sock = manager.target[target_index].socket;
+ if (sock != -1) {
+ if (send(sock, msg, sizeof(struct _msg_target_t) + msg->length, MSG_NOSIGNAL) == -1)
+ LOGE("fail to send data to target index(%d)\n",
+ target_index);
+ else
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int process_msg_get_screenshot(struct msg_buf_t *msg_control)
+{
uint32_t log_len;
- msg_target_t sendlog;
+ struct msg_target_t sendlog;
enum ErrorCode err_code = ERR_UNKNOWN;
// send config message to target process
sendlog.length = 0;
log_len = sizeof(sendlog.type) + sizeof(sendlog.length) + sendlog.length;
- for (target_index = 0; target_index < MAX_TARGET_COUNT; target_index++) {
- sock = manager.target[target_index].socket;
- if (sock != -1) {
- if (send(sock, &sendlog, log_len, MSG_NOSIGNAL) == -1) {
- LOGE("fail to send data to target index(%d)\n",
- target_index);
- } else {
- err_code = ERR_NO;
- break;
- }
- }
- }
+ if (send_msg_to_all_targets(&sendlog) == 1)
+ err_code = ERR_NO;
return -(err_code != ERR_NO);
}
enum ErrorCode error_code = ERR_NO;
int target_index;
- msg_target_t sendlog;
+ struct msg_target_t sendlog;
LOGI("MY HANDLE %s (%X)\n", msg_ID_str(msg->id), msg->id);
init_parse_control(&msg_control, msg);