/* Copyright Statement: * * * * This software/firmware and related documentation ("MediaTek Software") are * * protected under relevant copyright laws. The information contained herein * * is confidential and proprietary to MediaTek Inc. and/or its licensors. * * Without the prior written permission of MediaTek inc. and/or its licensors, * * any reproduction, modification, use or disclosure of MediaTek Software, * * and information contained herein, in whole or in part, shall be strictly prohibited. * * * * MediaTek Inc. (C) 2016. All rights reserved. * * * * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES * * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") * * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON * * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, * * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. * * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE * * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR * * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH * * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES * * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES * * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK * * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR * * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND * * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, * * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, * * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO * * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. * * * * The following software/firmware and/or related documentation ("MediaTek Software") * * have been modified by MediaTek Inc. All revisions are subject to any receiver's * * applicable license agreements with MediaTek Inc. * */ #define LOG_TAG "mtk_bt_snoop" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "bt_target.h" #include #include #include #include "log_tree_mgr.h" #include "log_writer.h" #include "logs_stats.h" #include "osi/include/log.h" #include "osi/include/osi.h" #include "osi/include/thread.h" #include "snoop_log_config.h" #include "mtk_util.h" #include "osi/include/properties.h" #include #include #include #include #define BTSNOOP_SAVELAST_PROPERTY "persist.bluetooth.btsnoopsavelast" extern std::string get_btsnoop_log_path(bool filtered); extern std::string get_btsnoop_last_log_path(std::string btsnoop_path); static void mtk_twrite_rename_history_file(const char * log_path); static bool mtk_is_btsnoop_savelast(void) { char btsnoop_savelast[PROPERTY_VALUE_MAX] = {0}; osi_property_get(BTSNOOP_SAVELAST_PROPERTY, btsnoop_savelast, "false"); return strncmp(btsnoop_savelast, "true", 4) == 0; } static uint64_t mtk_readability_timestamp(void) { const int buf_size = 24; char buffer[buf_size]; char buffer2[buf_size]; struct timeval tv; time_t curtime = time(NULL); struct tm * ltime = localtime(&curtime); strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", ltime); gettimeofday(&tv, NULL); snprintf(buffer2, sizeof(buffer2), "%s", buffer); return strtoull(buffer2, NULL, 10); } static void mtk_twrite_rename_history_file(const char * log_path) { char dir_path[PROPERTY_VALUE_MAX] = {0}; char file_path[PROPERTY_VALUE_MAX] = {0}; char *dir_name = NULL; char *file_name = NULL; char full_path[PROPERTY_VALUE_MAX] = {0}; char last_path[PROPERTY_VALUE_MAX] = {0}; uint64_t read_ts = mtk_readability_timestamp(); int exist = 0; strncpy(dir_path, log_path, PROPERTY_VALUE_MAX - 1); dir_path[PROPERTY_VALUE_MAX - 1] = '\0'; strncpy(file_path, log_path, PROPERTY_VALUE_MAX - 1); file_path[PROPERTY_VALUE_MAX - 1] = '\0'; dir_name = dirname(dir_path); file_name = basename(file_path); if (dir_name == NULL || file_name == NULL) { return; } LOG_INFO("%s dir_name=%s, file_nanme=%s", __func__, dir_name, file_name); DIR *p_dir = opendir(dir_name); if (p_dir != NULL) { int32_t max_idx = 0; uint32_t total_cnt = 0; exist = 0; struct stat buf; struct dirent *p_file; while ((p_file = readdir(p_dir)) != NULL) { if (strncmp(p_file->d_name, "..", 2) == 0 || strncmp(p_file->d_name, ".", 1) == 0) { continue; } /* if the hci log size is <=16, it's not complete and nonsense, remove */ /* if it's history log */ if ((strstr(p_file->d_name, file_name) != NULL) && (strstr(p_file->d_name, "last") != NULL)){ memset(full_path, 0, sizeof(full_path)); snprintf(full_path, sizeof(full_path), "%s/%s", dir_name, p_file->d_name); if (stat(full_path, &buf) == 0) { /* 16 is hci log header */ if (buf.st_size <= 16) { LOG_INFO("log %s size %d, remove", p_file->d_name, (int32_t)buf.st_size); if (remove(full_path)) { LOG_ERROR("%s can't remove", full_path); } else { LOG_INFO("%s removed", full_path); } continue; } } } /* count the last file number */ if ((strstr(p_file->d_name, file_name) != NULL) && (strstr(p_file->d_name, "last") != NULL)){ int32_t idx = 0; int32_t ret = sscanf(p_file->d_name, "%*[^.].%*[^.].%d.%*[^.]", &idx); if (ret != 0) { if (max_idx < idx) { max_idx = idx; } } total_cnt++; } if (strncmp(p_file->d_name, file_name, strlen(p_file->d_name)) == 0) { exist = 1; } } LOG_INFO("%s last_cnt=%d, max_idx=%d", __func__, total_cnt, max_idx); if (0 == total_cnt) max_idx = 0; else max_idx++; if (exist) { memset(full_path, 0, sizeof(full_path)); snprintf(full_path, sizeof(full_path), "%s/%s", dir_name, file_name); snprintf(last_path, PROPERTY_VALUE_MAX, "%s/%s.%d.last.%" PRIu64, dir_name, file_name, max_idx, read_ts); LOG_INFO("%s rename '%s' to '%s'", __func__, full_path, last_path); if (rename(full_path, last_path) && errno != ENOENT) { LOG_ERROR("%s unable to rename '%s' to '%s': %s", __func__, full_path, last_path, strerror(errno)); } } closedir(p_dir); } return; } void mtk_twrite_remove_history_file(const char * log_path) { char dir_path[PROPERTY_VALUE_MAX] = {0}; char file_path[PROPERTY_VALUE_MAX] = {0}; char *dir_name = NULL; char *file_name = NULL; char full_path[PROPERTY_VALUE_MAX] = {0}; strncpy(dir_path, log_path, PROPERTY_VALUE_MAX - 1); dir_path[PROPERTY_VALUE_MAX - 1] = '\0'; strncpy(file_path, log_path, PROPERTY_VALUE_MAX - 1); file_path[PROPERTY_VALUE_MAX - 1] = '\0'; dir_name = dirname(dir_path); file_name = basename(file_path); if (dir_name == NULL || file_name == NULL) { return; } LOG_INFO("%s log_path=%s, dir_name=%s, file_name=%s", __func__, log_path, dir_name, file_name); DIR *p_dir = opendir(dir_name); if (p_dir != NULL) { struct dirent *p_file; while ((p_file = readdir(p_dir)) != NULL) { if (strncmp(p_file->d_name, "..", 2) == 0 || strncmp(p_file->d_name, ".", 1) == 0) { continue; } if (strstr(p_file->d_name, file_name) != NULL) { memset(full_path, 0, sizeof(full_path)); snprintf(full_path, sizeof(full_path), "%s/%s", dir_name, p_file->d_name); if (remove(full_path)) { LOG_ERROR("%s can't remove", full_path); } else { LOG_INFO("%s removed", full_path); } } } closedir(p_dir); } return; } void mtk_open_next_snoop_file(bool is_boot, int &logfile_fd, bool &is_btsnoop_filtered, int32_t &packet_counter) { packet_counter = 0; if (logfile_fd != INVALID_FD) { close(logfile_fd); logfile_fd = INVALID_FD; } auto log_path = get_btsnoop_log_path(is_btsnoop_filtered); auto last_log_path = get_btsnoop_last_log_path(log_path); if (mtk_is_btsnoop_savelast() || (false == is_boot)) { mtk_twrite_rename_history_file(log_path.c_str()); } else { mtk_twrite_rename_history_file(log_path.c_str()); } mode_t prevmask = umask(0); logfile_fd = open(log_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); umask(prevmask); if (logfile_fd == INVALID_FD) { LOG(ERROR) << __func__ << ": unable to open '" << log_path << "' : " << strerror(errno); return; } write(logfile_fd, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16); }