diff --git a/drivers/staging/blackbox/Kconfig b/drivers/staging/blackbox/Kconfig index cbfb275c52e0f315ec7c333b6c304f70fd0c3137..0e985823c2e83539023f66d06041de502dbb7f0a 100644 --- a/drivers/staging/blackbox/Kconfig +++ b/drivers/staging/blackbox/Kconfig @@ -3,7 +3,6 @@ menu "Blackbox Options" config BLACKBOX bool "Support for blackbox" - depends on !ARCH_HAS_SYSCALL_WRAPPER select STORAGE if BLACKBOX_STORAGE_MATERIAL default y help diff --git a/drivers/staging/blackbox/blackbox_common.c b/drivers/staging/blackbox/blackbox_common.c index e9329c175a6c7ab47d00a221466b0e74c28f76fe..7f658f9860ad7591f9601f034e4c78ff338b0031 100644 --- a/drivers/staging/blackbox/blackbox_common.c +++ b/drivers/staging/blackbox/blackbox_common.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include void sys_reset(void) @@ -20,7 +22,7 @@ void sys_reset(void) bbox_print_info("reset the system failed!\n"); } -void change_own_mode(char *path, int uid, int gid, int mode) +void change_own(char *path, int uid, int gid) { mm_segment_t old_fs; int ret = -1; @@ -32,90 +34,91 @@ void change_own_mode(char *path, int uid, int gid, int mode) old_fs = get_fs(); set_fs(KERNEL_DS); - ret = sys_chown(path, uid, gid); - if (ret != 0) { - bbox_print_err("sys_chown [%s] failed, ret: %d\n", path, ret); - goto __out; - } - - ret = sys_chmod(path, mode); - if (ret != 0) { - bbox_print_err("sys_chmod [%s] failed, ret: %d\n", path, ret); - goto __out; - } + ret = ksys_chown(path, uid, gid); + if (ret != 0) + bbox_print_err("ksys_chown [%s] failed, ret: %d\n", path, ret); -__out: set_fs(old_fs); } int full_write_file(const char *pfile_path, char *buf, size_t buf_size, bool is_append) { + struct file *filp = NULL; + char *pathname = NULL; mm_segment_t old_fs; - long total_to_write = (long)buf_size; - long total_write = 0; - long write_this_time; - char *ptemp = buf; - int fd = -1; + loff_t pos = 0; + int ret = -1; if (unlikely(!pfile_path || !buf)) { - bbox_print_err("fd or buf is NULL!\n"); + bbox_print_err("pfile_path or buf is NULL!\n"); return -EINVAL; } + filp = file_open(pfile_path, O_CREAT | O_RDWR | + (is_append ? O_APPEND : O_TRUNC), 0); + if (IS_ERR(filp)) { + bbox_print_err("open %s failed! [%ld]\n", pfile_path, PTR_ERR(filp)); + return -EBADF; + } + old_fs = get_fs(); set_fs(KERNEL_DS); - fd = sys_open(pfile_path, O_CREAT | O_RDWR | - (is_append ? O_APPEND : O_TRUNC), 0); - if (fd < 0) { - bbox_print_err("Create file [%s] failed! ret: %d\n", pfile_path, fd); - goto __out; - } - while (total_to_write > 0) { - write_this_time = ksys_write(fd, ptemp, total_to_write); - if (write_this_time < 0) { - bbox_print_err("%s\n", "Failed to write file!\n"); - break; - } - ptemp += write_this_time; - total_to_write -= write_this_time; - total_write += write_this_time; - } + ret = vfs_write(filp, buf, buf_size, &pos); -__out: - if (fd >= 0) { - ksys_sync(); - ksys_close(fd); - } set_fs(old_fs); - return total_write == (long)buf_size ? 0 : -1; + file_close(filp); + + if (ret < 0) { + pathname = getfullpath(filp); + bbox_print_err("write [%s] failed! [%d]\n", pathname ? pathname : "", ret); + return ret; + } + + return 0; } -static int create_new_dir(char *path) +int file_exists(const char *name) { + struct path path; int ret; - mm_segment_t old_fs; - if (unlikely(!path)) { - bbox_print_err("path is NULL!\n"); + ret = kern_path(name, LOOKUP_FOLLOW, &path); + if (ret) + return ret; + + ret = inode_permission(d_inode(path.dentry), MAY_ACCESS); + path_put(&path); + return ret; +} + +static int create_new_dir(char *name) +{ + struct dentry *dentry; + struct path path; + int ret; + + if (unlikely(!name)) { + bbox_print_err("name is NULL!\n"); return -EINVAL; } - old_fs = get_fs(); - set_fs(KERNEL_DS); - ret = sys_access(path, 0); - if (ret != 0) { - ret = sys_mkdir(path, BBOX_DIR_LIMIT); - if (ret < 0) { - bbox_print_err("Create dir [%s] failed! ret: %d\n", path, ret); - set_fs(old_fs); - return -EINVAL; - } - change_own_mode(path, AID_ROOT, AID_SYSTEM, BBOX_DIR_LIMIT); + ret = file_exists(name); + if (ret) { + dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY); + if (IS_ERR(dentry)) + return PTR_ERR(dentry); + + ret = vfs_mkdir(d_inode(path.dentry), dentry, BBOX_DIR_LIMIT); + if (ret && ret != -EEXIST) + bbox_print_err("Create dir [%s] failed! ret: %d\n", name, ret); + else + change_own(name, AID_ROOT, AID_SYSTEM); + + done_path_create(&path, dentry); } - set_fs(old_fs); return 0; } @@ -188,3 +191,85 @@ unsigned long long get_ticks(void) return (u64)uptime.tv_sec; } + +static inline struct dentry *lock_parent(struct dentry *dentry) +{ + struct dentry *dir = dget_parent(dentry); + + inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); + return dir; +} + +static inline void unlock_dir(struct dentry *dentry) +{ + inode_unlock(d_inode(dentry)); + dput(dentry); +} + +struct file *file_open(const char *filename, int open_mode, int mode) +{ + struct file *filp = NULL; + mm_segment_t old_fs; + + old_fs = get_fs(); + set_fs(KERNEL_DS); + + filp = filp_open(filename, open_mode, mode); + set_fs(old_fs); + + if (IS_ERR(filp)) + return NULL; + + return filp; +} + +void file_close(struct file *filp) +{ + if (likely(filp)) + filp_close(filp, NULL); +} + +int file_delete(struct file *filp) +{ + struct dentry *dentry = NULL; + struct dentry *parent = NULL; + int ret = 0; + + if (unlikely(!filp)) { + bbox_print_err("file is NULL!\n"); + return -EINVAL; + } + + dentry = file_dentry(filp); + parent = lock_parent(dentry); + + if (dentry->d_parent == parent) { + dget(dentry); + ret = vfs_unlink(d_inode(parent), dentry, NULL); + dput(dentry); + } + + unlock_dir(parent); + + return ret; +} + +char *getfullpath(struct file *filp) +{ + char *buf = NULL, *path = NULL; + + if (unlikely(!filp)) + return NULL; + + buf = kmalloc(PATH_MAX, GFP_KERNEL); + if (unlikely(!buf)) + return NULL; + memset(buf, 0, PATH_MAX); + + //get the path + path = d_path(&filp->f_path, buf, PATH_MAX); + + kfree(buf); + + return path; +} diff --git a/drivers/staging/blackbox/blackbox_core.c b/drivers/staging/blackbox/blackbox_core.c index a9f0f0cba691dacc470e19b38f808e6dd35f43ac..ee00a80997df1f1b07b7dcf80c9e64324f3d4958 100644 --- a/drivers/staging/blackbox/blackbox_core.c +++ b/drivers/staging/blackbox/blackbox_core.c @@ -63,7 +63,7 @@ struct error_info_to_category { static LIST_HEAD(ops_list); static DEFINE_SPINLOCK(ops_list_lock); static DEFINE_SEMAPHORE(temp_error_info_sem); -static struct error_info_to_category error_info_categorys[] = { +static struct error_info_to_category error_info_categories[] = { { MODULE_SYSTEM, {EVENT_SYSREBOOT, CATEGORY_SYSTEM_REBOOT, TOP_CATEGORY_SYSTEM_RESET} @@ -107,16 +107,15 @@ struct error_info *temp_error_info; /* ---- local function prototypes ---- */ static const char *get_top_category(const char *module, const char *event); static const char *get_category(const char *module, const char *event); -static void format_log_dir(char *buf, size_t buf_size, - const char *log_root_dir, const char event[EVENT_MAX_LEN], - const char *timestamp); -static void save_history_log(const char *log_root_dir, - struct error_info *info, const char *timestamp, int need_sys_reset); +static void format_log_dir(char *buf, size_t buf_size, const char *log_root_dir, + const char *timestamp); +static void save_history_log(const char *log_root_dir, struct error_info *info, + const char *timestamp, int need_sys_reset); +static void save_invalid_log(const struct bbox_ops *ops, const struct error_info *info); static void wait_for_log_part(void); -static void format_error_info(struct error_info *info, - const char event[EVENT_MAX_LEN], - const char module[MODULE_MAX_LEN], - const char error_desc[ERROR_DESC_MAX_LEN]); +static void format_error_info(struct error_info *info, const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char error_desc[ERROR_DESC_MAX_LEN]); static void save_last_log(void); static int save_error_log(void *pparam); @@ -126,7 +125,7 @@ static int save_error_log(void *pparam); static const char *get_top_category(const char *module, const char *event) { int i; - int count = (int)ARRAY_SIZE(error_info_categorys); + int count = (int)ARRAY_SIZE(error_info_categories); if (unlikely(!module || !event)) { bbox_print_err("module: %p, event: %p\n", module, event); @@ -134,9 +133,9 @@ static const char *get_top_category(const char *module, const char *event) } for (i = 0; i < count; i++) { - if (!strcmp(error_info_categorys[i].module, module) && - !strcmp(error_info_categorys[i].map.event, event)) { - return error_info_categorys[i].map.top_category; + if (!strcmp(error_info_categories[i].module, module) && + !strcmp(error_info_categories[i].map.event, event)) { + return error_info_categories[i].map.top_category; } } if (!strcmp(module, MODULE_SYSTEM)) @@ -148,7 +147,7 @@ static const char *get_top_category(const char *module, const char *event) static const char *get_category(const char *module, const char *event) { int i; - int count = (int)ARRAY_SIZE(error_info_categorys); + int count = (int)ARRAY_SIZE(error_info_categories); if (unlikely(!module || !event)) { bbox_print_err("module: %p, event: %p\n", module, event); @@ -156,9 +155,9 @@ static const char *get_category(const char *module, const char *event) } for (i = 0; i < count; i++) { - if (!strcmp(error_info_categorys[i].module, module) && - !strcmp(error_info_categorys[i].map.event, event)) { - return error_info_categorys[i].map.category; + if (!strcmp(error_info_categories[i].module, module) && + !strcmp(error_info_categories[i].map.event, event)) { + return error_info_categories[i].map.category; } } if (!strcmp(module, MODULE_SYSTEM)) @@ -167,25 +166,23 @@ static const char *get_category(const char *module, const char *event) return CATEGORY_SUBSYSTEM_CUSTOM; } -static void format_log_dir(char *buf, size_t buf_size, - const char *log_root_dir, const char event[EVENT_MAX_LEN], - const char *timestamp) +static void format_log_dir(char *buf, size_t buf_size, const char *log_root_dir, + const char *timestamp) { if (unlikely(!buf || buf_size == 0 || !log_root_dir || - !event || !timestamp)) { - bbox_print_err("buf: %p, buf_size: %u, log_root_dir: %p, event: %p, timestamp: %p\n", - buf, (unsigned int)buf_size, log_root_dir, event, timestamp); + !timestamp)) { + bbox_print_err("buf: %p, buf_size: %u, log_root_dir: %p, timestamp: %p\n", + buf, (unsigned int)buf_size, log_root_dir, timestamp); return; } memset(buf, 0, buf_size); - scnprintf(buf, buf_size - 1, "%s/%s_%s", log_root_dir, event, timestamp); + scnprintf(buf, buf_size - 1, "%s/%s", log_root_dir, timestamp); } -static void format_error_info(struct error_info *info, - const char event[EVENT_MAX_LEN], - const char module[MODULE_MAX_LEN], - const char error_desc[ERROR_DESC_MAX_LEN]) +static void format_error_info(struct error_info *info, const char event[EVENT_MAX_LEN], + const char module[MODULE_MAX_LEN], + const char error_desc[ERROR_DESC_MAX_LEN]) { if (unlikely(!info || !event || !module || !error_desc)) { bbox_print_err("info: %p, event: %p, module: %p, error_desc: %p\n", @@ -203,8 +200,8 @@ static void format_error_info(struct error_info *info, sizeof(info->error_desc) - 1)); } -static void save_history_log(const char *log_root_dir, - struct error_info *info, const char *timestamp, int need_sys_reset) +static void save_history_log(const char *log_root_dir, struct error_info *info, + const char *timestamp, int need_sys_reset) { char history_log_path[PATH_MAX_LEN]; char *buf; @@ -228,21 +225,31 @@ static void save_history_log(const char *log_root_dir, scnprintf(history_log_path, sizeof(history_log_path) - 1, "%s/%s", log_root_dir, HISTORY_LOG_NAME); full_write_file(history_log_path, buf, strlen(buf), 1); - change_own_mode(history_log_path, AID_ROOT, AID_SYSTEM, BBOX_FILE_LIMIT); + change_own(history_log_path, AID_ROOT, AID_SYSTEM); vfree(buf); } -static bool is_log_part_mounted(void) +static void save_invalid_log(const struct bbox_ops *ops, const struct error_info *info) { - int ret; - mm_segment_t old_fs; + char invalid_log_path[PATH_MAX_LEN]; + char timestamp[TIMESTAMP_MAX_LEN]; - old_fs = get_fs(); - set_fs(KERNEL_DS); - ret = sys_access(CONFIG_BLACKBOX_LOG_PART_REPRESENTATIVE, 0); - set_fs(old_fs); + if (unlikely(!ops || !info)) { + bbox_print_err("ops: %p, info: %p\n", ops, info); + return; + } - return ret == 0; + get_timestamp(timestamp, sizeof(timestamp)); + format_log_dir(invalid_log_path, PATH_MAX_LEN, CONFIG_BLACKBOX_LOG_PART_REPRESENTATIVE, + timestamp); + create_log_dir(invalid_log_path); + if (ops->ops.save_last_log(invalid_log_path, (struct error_info *)info) != 0) + bbox_print_err("[%s] failed to save invalid log!\n", ops->ops.module); +} + +static bool is_log_part_mounted(void) +{ + return file_exists(CONFIG_BLACKBOX_LOG_PART_REPRESENTATIVE) == 0; } static void wait_for_log_part(void) @@ -258,8 +265,7 @@ static void wait_for_log_part(void) static bool find_module_ops(struct error_info *info, struct bbox_ops **ops) { - struct list_head *cur = NULL; - struct list_head *next = NULL; + struct bbox_ops *cur = NULL; bool find_module = false; if (unlikely(!info || !ops)) { @@ -267,9 +273,9 @@ static bool find_module_ops(struct error_info *info, struct bbox_ops **ops) return find_module; } - list_for_each_safe(cur, next, &ops_list) { - *ops = list_entry(cur, struct bbox_ops, list); - if (*ops && !strcmp((*ops)->ops.module, info->module)) { + list_for_each_entry(cur, &ops_list, list) { + if (!strcmp(cur->ops.module, info->module)) { + *ops = cur; find_module = true; break; } @@ -327,7 +333,7 @@ static void save_log_without_reset(struct error_info *info) /* create log root path */ if (log_dir) { format_log_dir(log_dir, PATH_MAX_LEN, - CONFIG_BLACKBOX_LOG_ROOT_PATH, info->event, timestamp); + CONFIG_BLACKBOX_LOG_ROOT_PATH, timestamp); create_log_dir(log_dir); } else bbox_print_err("vmalloc failed!\n"); @@ -372,6 +378,7 @@ static void save_temp_error_info(const char event[EVENT_MAX_LEN], static void do_save_last_log(const struct bbox_ops *ops, const struct error_info *info) { char *log_dir = NULL; + int ret; if (unlikely(!ops || !info)) { bbox_print_err("ops: %p, info: %p\n", @@ -380,8 +387,11 @@ static void do_save_last_log(const struct bbox_ops *ops, const struct error_info } memset((void *)info, 0, sizeof(*info)); - if (ops->ops.get_last_log_info((struct error_info *)info) != 0) { + ret = ops->ops.get_last_log_info((struct error_info *)info); + if (ret) { bbox_print_err("[%s] failed to get log info!\n", ops->ops.module); + if (ret == -ENOMSG) + save_invalid_log(ops, info); return; } bbox_print_info("[%s] starts saving log!\n", ops->ops.module); @@ -396,7 +406,7 @@ static void do_save_last_log(const struct bbox_ops *ops, const struct error_info get_timestamp((char *)info->error_time, TIMESTAMP_MAX_LEN); format_log_dir(log_dir, PATH_MAX_LEN, CONFIG_BLACKBOX_LOG_ROOT_PATH, - info->event, info->error_time); + info->error_time); create_log_dir(log_dir); if (ops->ops.save_last_log(log_dir, (struct error_info *)info) == 0) save_history_log(CONFIG_BLACKBOX_LOG_ROOT_PATH, @@ -410,22 +420,14 @@ static void save_last_log(void) { unsigned long flags; struct error_info *info = NULL; - struct list_head *cur = NULL; - struct list_head *next = NULL; + struct bbox_ops *ops = NULL; info = vmalloc(sizeof(*info)); if (!info) return; spin_lock_irqsave(&ops_list_lock, flags); - list_for_each_safe(cur, next, &ops_list) { - struct bbox_ops *ops = list_entry(cur, struct bbox_ops, list); - - if (!ops) { - bbox_print_err("ops is NULL!\n"); - continue; - } - + list_for_each_entry(ops, &ops_list, list) { if (ops->ops.get_last_log_info && ops->ops.save_last_log) { spin_unlock_irqrestore(&ops_list_lock, flags); @@ -471,8 +473,6 @@ int bbox_register_module_ops(struct module_ops *ops) { struct bbox_ops *new_ops = NULL; struct bbox_ops *temp = NULL; - struct list_head *cur = NULL; - struct list_head *next = NULL; unsigned long flags; if (unlikely(!ops)) { @@ -489,8 +489,7 @@ int bbox_register_module_ops(struct module_ops *ops) if (list_empty(&ops_list)) goto __out; - list_for_each_safe(cur, next, &ops_list) { - temp = list_entry(cur, struct bbox_ops, list); + list_for_each_entry(temp, &ops_list, list) { if (!strcmp(temp->ops.module, ops->module)) { spin_unlock_irqrestore(&ops_list_lock, flags); vfree(new_ops); diff --git a/drivers/staging/blackbox/blackbox_storage.c b/drivers/staging/blackbox/blackbox_storage.c index 3e3634a6d667e1cfda71fb87842a5a385e82ac15..3f11c5f43dece6aaf8539dc6f357847405d8d2e9 100644 --- a/drivers/staging/blackbox/blackbox_storage.c +++ b/drivers/staging/blackbox/blackbox_storage.c @@ -10,6 +10,7 @@ #include #include #include +#include char *storage_material = #ifdef CONFIG_DEF_BLACKBOX_STORAGE @@ -55,7 +56,7 @@ static void do_kmsg_dump(struct kmsg_dumper *dumper, } pinfo = (struct fault_log_info *)lastlog; (void)kmsg_dump_get_buffer(dumper, true, lastlog + sizeof(*pinfo), - lastlog_len - sizeof(*pinfo), &pinfo->len); + lastlog_len - sizeof(*pinfo), (size_t *)&pinfo->len); up(&kmsg_sem); } #endif @@ -82,73 +83,49 @@ struct sys_st { static bool is_pstore_part_ready(char *pstore_file) { - mm_segment_t old_fs; - int fd = -1; - void *buf = NULL; + const char *cur_name = NULL; + struct dentry *root_dentry; + struct dentry *cur_dentry; + struct file *filp = NULL; char *full_path = NULL; - struct linux_dirent64 *dirp; - int num; - int ret = -1; struct sys_st st; + int ret = -1; - old_fs = get_fs(); - set_fs(KERNEL_DS); + if (unlikely(!pstore_file)) + return -EINVAL; + memset(pstore_file, 0, sizeof(*pstore_file)); - fd = sys_open(PSTORE_MOUNT_POINT, O_RDONLY, 0); - if (fd < 0) { - bbox_print_err("open dir [%s] failed!\n", PSTORE_MOUNT_POINT); - goto __out; + filp = file_open(PSTORE_MOUNT_POINT, O_RDONLY, 0); + if (IS_ERR(filp)) { + bbox_print_err("open %s failed! err is [%ld]\n", PSTORE_MOUNT_POINT, PTR_ERR(filp)); + return -EBADF; } - buf = vmalloc(PATH_MAX_LEN); - if (!buf) - goto __out; - full_path = vmalloc(PATH_MAX_LEN); if (!full_path) goto __out; - dirp = buf; - - num = sys_getdents64(fd, dirp, PATH_MAX_LEN); - while (num > 0) { - while (num > 0) { - if ((strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0)) { - num -= dirp->d_reclen; - dirp = (void *)dirp + dirp->d_reclen; - continue; - } - - memset(full_path, 0, PATH_MAX_LEN); - snprintf(full_path, PATH_MAX_LEN - 1, "%s%s", PSTORE_MOUNT_POINT, dirp->d_name); - - memset((void *)&st, 0, sizeof(struct sys_st)); - - ret = sys_lstat(full_path, &st.__st); - if ((ret == 0) && (S_ISREG(st.__st.st_mode)) && - (strncmp(dirp->d_name, "blackbox", strlen("blackbox")) == 0)) { - if (strcmp(full_path, pstore_file) > 0) - strncpy(pstore_file, full_path, strlen(full_path)); - bbox_print_info("get pstore file name %s %s!\n", pstore_file, - ret ? "failed" : "successfully"); - } - - num -= dirp->d_reclen; - dirp = (void *)dirp + dirp->d_reclen; - } + root_dentry = filp->f_path.dentry; + list_for_each_entry(cur_dentry, &root_dentry->d_subdirs, d_child) { + cur_name = cur_dentry->d_name.name; - dirp = buf; - memset(buf, 0, PATH_MAX_LEN); - num = sys_getdents64(fd, dirp, PATH_MAX_LEN); - } + memset(full_path, 0, PATH_MAX_LEN); + snprintf(full_path, PATH_MAX_LEN - 1, "%s%s", PSTORE_MOUNT_POINT, cur_name); + memset((void *)&st, 0, sizeof(struct sys_st)); -__out: - if (fd >= 0) - sys_close(fd); + ret = sys_lstat(full_path, &st.__st); + if (!ret && (S_ISREG(st.__st.st_mode)) && + (strncmp(cur_name, "blackbox", strlen("blackbox")) == 0)) { + if (strcmp(full_path, pstore_file) > 0) + strncpy(pstore_file, full_path, strlen(full_path)); + } + } - set_fs(old_fs); + if (strlen(pstore_file)) + bbox_print_info("get pstore file name %s successfully!\n", pstore_file); - vfree(buf); +__out: + file_close(filp); vfree(full_path); return ret == 0; @@ -157,12 +134,13 @@ static bool is_pstore_part_ready(char *pstore_file) static int get_log_by_pstore_blk(void *in, unsigned int inlen) { char pstore_file[PATH_MAX_LEN]; + struct file *filp = NULL; + char *pathname = NULL; + mm_segment_t old_fs; void *pbuf = NULL; - void *pbuf_temp = NULL; + loff_t pos = 0; static int retry; - int need_read_size = 0; - int fd = -1; - int ret = 0; + int ret = -1; memset(pstore_file, 0, PATH_MAX_LEN); while (!is_pstore_part_ready((char *)&pstore_file)) { @@ -173,46 +151,36 @@ static int get_log_by_pstore_blk(void *in, unsigned int inlen) } if (likely(in)) { - fd = sys_open(pstore_file, O_RDONLY, FILE_LIMIT); - if (fd < 0) { - bbox_print_err("%s():%d: open %s failed! [%d]\n", __func__, - __LINE__, pstore_file, fd); + filp = file_open(pstore_file, O_RDONLY, FILE_LIMIT); + if (IS_ERR(filp)) { + bbox_print_err("open %s failed! err is [%ld]\n", pstore_file, + PTR_ERR(filp)); return -EBADF; } memset(in, 0, inlen); - need_read_size = inlen; pbuf = in; - pbuf_temp = kzalloc(SZ_4K, GFP_KERNEL); - if (!pbuf_temp) - goto __out; - - while (need_read_size > 0) { - ret = sys_read(fd, pbuf_temp, SZ_4K); - if (ret < 0) { - bbox_print_err("%s():%d: read failed! [%d]\n", __func__, - __LINE__, ret); - goto __error; - } - - if (ret == 0) - break; + old_fs = get_fs(); + set_fs(KERNEL_DS); - memcpy((void *)pbuf, (const void *)pbuf_temp, ret); - pbuf += ret; - need_read_size -= ret; + ret = vfs_read(filp, pbuf, inlen, &pos); + if (ret < 0) { + pathname = getfullpath(filp); + bbox_print_err("read %s failed! err is [%d]\n", pathname ? pathname : "", + ret); + goto __error; } - kfree(pbuf_temp); - } - - sys_close(fd); - return 0; + set_fs(old_fs); + file_close(filp); + file_delete(filp); + return 0; + } + return -EBADF; __error: - kfree(pbuf_temp); -__out: - sys_close(fd); + set_fs(old_fs); + file_close(filp); return -EIO; } #endif diff --git a/include/linux/blackbox.h b/include/linux/blackbox.h index 78e2599b8185b6915da926e64021e213ca383f37..7e1ba44912d287a3ca7d9d3778fd15f411bbfd30 100644 --- a/include/linux/blackbox.h +++ b/include/linux/blackbox.h @@ -31,11 +31,15 @@ #define EVENT_HUNGTASK "HUNGTASK" #define EVENT_BOOTFAIL "BOOTFAIL" -#define bbox_print_err(format, ...) \ - pr_err("bbox: func: %s, line: %d, err: " \ - format, __func__, __LINE__, ##__VA_ARGS__) -#define bbox_print_info(format, ...) \ - pr_err("bbox: info: " format, ##__VA_ARGS__) +#define FILE_NAME(x) (strrchr(x, '/') ? (strrchr(x, '/') + 1) : x) +#define BBOX_DECORATOR_HILOG(level, fmt, args...) \ + pr_err("bbox:[%s][%s:%d] " fmt, level, FILE_NAME(__FILE__), __LINE__, ##args) + +#define bbox_print_fatal(fmt, args...) BBOX_DECORATOR_HILOG("fatal", fmt, ##args) +#define bbox_print_err(fmt, args...) BBOX_DECORATOR_HILOG("err", fmt, ##args) +#define bbox_print_warn(fmt, args...) BBOX_DECORATOR_HILOG("warn", fmt, ##args) +#define bbox_print_info(fmt, args...) BBOX_DECORATOR_HILOG("info", fmt, ##args) +#define bbox_print_debug(fmt, args...) BBOX_DECORATOR_HILOG("debug", fmt, ##args) struct error_info { char event[EVENT_MAX_LEN]; diff --git a/include/linux/blackbox_common.h b/include/linux/blackbox_common.h index 11e348e279b28a64e26650b581a0442c9c4a35ad..973a9214184dc69b07eb6e6e49e7b5ac4a1a4c8e 100644 --- a/include/linux/blackbox_common.h +++ b/include/linux/blackbox_common.h @@ -21,17 +21,24 @@ * format: * [topCategoryName],module[moduleName],category[categoryName],\ * event[eventName],time[seconds from 1970-01-01 00:00:00 UTC-tick],\ - * sysreboot[true|false],errordesc[errorDescription]\r\n + * sysreboot[true|false],errordesc[errorDescription]\n */ #define HISTORY_LOG_FORMAT "[%s],module[%s],category[%s],event[%s],"\ - "time[%s],sysreboot[%s],errdesc[%s]\r\n" -#define TIMESTAMP_FORMAT "%04d%02d%02d%02d%02d%02d_%08llu" + "time[%s],sysreboot[%s],errdesc[%s]\n" +#define TIMESTAMP_FORMAT "%04d%02d%02d%02d%02d%02d-%08llu" void sys_reset(void); -void change_own_mode(char *path, int uid, int gid, int mode); +void change_own(char *path, int uid, int gid); int full_write_file(const char *pfile_path, char *buf, size_t buf_size, bool read_file); +int file_exists(const char *name); int create_log_dir(const char *path); unsigned long long get_ticks(void); +struct file *file_open(const char *filename, int open_mode, int mode); +void file_close(struct file *filp); +ssize_t file_read(struct file *file, loff_t offset, unsigned char *data, + size_t size); +int file_delete(struct file *filp); +char *getfullpath(struct file *filp); #endif /* BLACKBOX_COMMON_H */