From 8cc59474c77d0c4fa8b66c5cb9c6893c7a626497 Mon Sep 17 00:00:00 2001 From: lyj_love_code Date: Mon, 4 Jul 2022 16:11:57 +0800 Subject: [PATCH] add hisysevent kernel interface ohos inclusion category: feature issue: #I5F6WP CVE: NA ----------------- hisysevent is used to provide kernel event logging interface. Signed-off-by: lyj_love_code --- drivers/staging/Kconfig | 2 + drivers/staging/Makefile | 1 + drivers/staging/hisysevent/Kconfig | 6 + drivers/staging/hisysevent/Makefile | 2 + .../staging/hisysevent/hiview_hisysevent.c | 536 ++++++++++++++++++ drivers/staging/zerohung/zrhung_event.c | 283 +-------- include/dfx/hiview_hisysevent.h | 32 ++ 7 files changed, 593 insertions(+), 269 deletions(-) create mode 100644 drivers/staging/hisysevent/Kconfig create mode 100644 drivers/staging/hisysevent/Makefile create mode 100644 drivers/staging/hisysevent/hiview_hisysevent.c create mode 100644 include/dfx/hiview_hisysevent.h diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig index d9ace574ad9d..6efe436abd9b 100644 --- a/drivers/staging/Kconfig +++ b/drivers/staging/Kconfig @@ -122,6 +122,8 @@ source "drivers/staging/hilog/Kconfig" source "drivers/staging/hievent/Kconfig" +source "drivers/staging/hisysevent/Kconfig" + source "drivers/staging/zerohung/Kconfig" source "drivers/staging/hungtask/Kconfig" diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile index 3883281dbe7a..9dacda71f751 100644 --- a/drivers/staging/Makefile +++ b/drivers/staging/Makefile @@ -51,6 +51,7 @@ obj-$(CONFIG_WFX) += wfx/ obj-y += hikey9xx/ obj-$(CONFIG_HILOG) += hilog/ obj-$(CONFIG_HIEVENT) += hievent/ +obj-$(CONFIG_HISYSEVENT) += hisysevent/ obj-$(CONFIG_DFX_ZEROHUNG) += zerohung/ obj-$(CONFIG_DFX_HUNGTASK) += hungtask/ obj-$(CONFIG_BLACKBOX) += blackbox/ diff --git a/drivers/staging/hisysevent/Kconfig b/drivers/staging/hisysevent/Kconfig new file mode 100644 index 000000000000..a40621cb846a --- /dev/null +++ b/drivers/staging/hisysevent/Kconfig @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +config HISYSEVENT + tristate "Enable hisysevent" + depends on HIEVENT + help + Say Y here to enable hisysevent feature support. diff --git a/drivers/staging/hisysevent/Makefile b/drivers/staging/hisysevent/Makefile new file mode 100644 index 000000000000..f3b6c929612c --- /dev/null +++ b/drivers/staging/hisysevent/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_HISYSEVENT) += hiview_hisysevent.o diff --git a/drivers/staging/hisysevent/hiview_hisysevent.c b/drivers/staging/hisysevent/hiview_hisysevent.c new file mode 100644 index 000000000000..e35ebfd1ebb0 --- /dev/null +++ b/drivers/staging/hisysevent/hiview_hisysevent.c @@ -0,0 +1,536 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 Huawei Technologies Co., Ltd. All rights reserved. + */ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define pr_fmt(fmt) "hisysevent: " fmt + +#define PARAM_INT_MAX_LEN 20 // 20 = 19 (max len) + 1 ('\0') +#define PARAM_DBL_MAX_LEN 18 // 18 = 17 (max sign digits) + 1 ('\0') +#define PARAM_STR_MAX_LEN (8 * 1024) // 8KB + +#define MAX_DOMAIN_LENGTH 16 +#define MAX_EVENT_NAME_LENGTH 32 +#define MAX_PARAM_NAME_LENGTH 48 +#define MAX_PARAM_NUMBER 128 + +#define HISYSEVENT_WRITER_DEV "/dev/bbox" +#define HISYSEVENT_INFO_BUF_LEN (384 * 1024) // 384KB, max length of event + +#define MINUTE_TO_SECS 60 +#define SEC_TO_MILLISEC 1000 +#define MILLISEC_TO_NANOSEC (1000 * 1000) +#define TIME_ZONE_LEN 6 + +#define BUF_POINTER_FORWARD \ + do { \ + if (tmp_len >= 0 && tmp_len < len) { \ + tmp += tmp_len; \ + len -= tmp_len; \ + } else { \ + pr_err("string over length"); \ + tmp += len; \ + len = 0; \ + } \ + } while (0) + +static int CHECK_CODE = 0x7BCDABCD; + +struct hisysevent_payload { + /* key of the event param */ + char *key; + + /* value of the event param */ + char *value; + + /* next param */ + struct hisysevent_payload *next; +}; + +/* hisysevent struct */ +struct hiview_hisysevent { + /* event domain */ + char *domain; + + /* event name */ + char *name; + + /* event type */ + int type; + + /* event time */ + long long time; + + /* time zone */ + char *tz; + + /* process id */ + int pid; + + /* thread id */ + int tid; + + /* user id */ + int uid; + + /* payload linked list */ + struct hisysevent_payload *head; + + /* length of payload */ + int payload_cnt; +}; + +static struct hisysevent_payload *hisysevent_payload_create(void) +{ + struct hisysevent_payload *payload = NULL; + + payload = kmalloc(sizeof(*payload), GFP_KERNEL); + if (!payload) + return NULL; + + payload->key = NULL; + payload->value = NULL; + payload->next = NULL; + return payload; +} + +static void hisysevent_payload_destroy(struct hisysevent_payload *p) +{ + if (!p) + return; + + kfree(p->value); + kfree(p->key); + kfree(p); +} + +static void +hisysevent_add_payload(struct hiview_hisysevent *event, struct hisysevent_payload *payload) +{ + if (!event->head) { + event->head = payload; + } else { + struct hisysevent_payload *temp = event->head; + + while (temp->next) + temp = temp->next; + temp->next = payload; + } +} + +static struct hisysevent_payload * +hisysevent_get_payload(struct hiview_hisysevent *event, const char *key) +{ + struct hisysevent_payload *p = event->head; + + if (!key) + return NULL; + + while (p) { + if (p->key && strcmp(p->key, key) == 0) + return p; + p = p->next; + } + + return NULL; +} + +static struct hisysevent_payload * +hisysevent_get_or_create_payload(struct hiview_hisysevent *event, const char *key) +{ + struct hisysevent_payload *payload = hisysevent_get_payload(event, key); + + if (payload) { + kfree(payload->value); + return payload; + } + + payload = hisysevent_payload_create(); + if (!payload) + return NULL; + + payload->key = kstrdup(key, GFP_ATOMIC); + if (!payload->key) { + hisysevent_payload_destroy(payload); + return NULL; + } + + hisysevent_add_payload(event, payload); + return payload; +} + +static int json_add_number(char *json, size_t len, const char *key, long long num) +{ + return snprintf(json, len, "\"%s\":%lld,", key, num); +} + +static int json_add_string(char *json, size_t len, const char *key, const char *str) +{ + return snprintf(json, len, "\"%s\":%s,", key, str); +} + +static int json_add_string2(char *json, size_t len, const char *key, const char *str) +{ + return snprintf(json, len, "\"%s\":\"%s\",", key, str); +} + +static int hisysevent_convert_base(const struct hiview_hisysevent *event, char **buf, int len) +{ + int tmp_len = 0; + char *tmp = *buf; + + tmp_len = json_add_string2(tmp, len, "domain_", event->domain); + BUF_POINTER_FORWARD; + tmp_len = json_add_string2(tmp, len, "name_", event->name); + BUF_POINTER_FORWARD; + tmp_len = json_add_number(tmp, len, "type_", event->type); + BUF_POINTER_FORWARD; + tmp_len = json_add_number(tmp, len, "time_", event->time); + BUF_POINTER_FORWARD; + tmp_len = json_add_string2(tmp, len, "tz_", event->tz); + BUF_POINTER_FORWARD; + tmp_len = json_add_number(tmp, len, "pid_", event->pid); + BUF_POINTER_FORWARD; + tmp_len = json_add_number(tmp, len, "tid_", event->tid); + BUF_POINTER_FORWARD; + tmp_len = json_add_number(tmp, len, "uid_", event->uid); + BUF_POINTER_FORWARD; + *buf = tmp; + return len; +} + +static int hisysevent_convert_payload(struct hisysevent_payload *payload, char **buf, int len) +{ + int tmp_len = 0; + char *tmp = *buf; + struct hisysevent_payload *tmp_payload = payload; + + while (tmp_payload) { + if (!tmp_payload->key || !tmp_payload->value) { + pr_err("drop invalid payload"); + tmp_payload = tmp_payload->next; + continue; + } + tmp_len = json_add_string(tmp, len, tmp_payload->key, tmp_payload->value); + BUF_POINTER_FORWARD; + tmp_payload = tmp_payload->next; + } + *buf = tmp; + return len; +} + +static int hisysevent_convert_json(const struct hiview_hisysevent *event, char **buf_ptr) +{ + char *tmp; + int tmp_len = 0; + int buf_len = HISYSEVENT_INFO_BUF_LEN + 1; + int len = buf_len; + char *buf = vmalloc(buf_len); + + if (!buf) + return -ENOMEM; + memset(buf, 0, buf_len); + + tmp = buf; + tmp_len = snprintf(tmp, len, "%c", '{'); + BUF_POINTER_FORWARD; + + len = hisysevent_convert_base(event, &tmp, len); + if (!event->head) + goto convert_end; + len = hisysevent_convert_payload(event->head, &tmp, len); + +convert_end: + tmp_len = snprintf(tmp - 1, len, "%c", '}'); + BUF_POINTER_FORWARD; + *buf_ptr = buf; + return 0; +} + +static void hisysevent_set_time(struct hiview_hisysevent *event) +{ + struct timespec64 ts; + struct timezone tz = sys_tz; + int tz_index = 0; + char time_zone[TIME_ZONE_LEN]; + int tz_hour; + int tz_min; + long long millisecs = 0; + + ktime_get_real_ts64(&ts); + millisecs = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; + event->time = millisecs; + + tz_hour = (-tz.tz_minuteswest) / MINUTE_TO_SECS; + time_zone[tz_index++] = tz_hour >= 0 ? '+' : '-'; + tz_min = (-tz.tz_minuteswest) % MINUTE_TO_SECS; + sprintf(&time_zone[tz_index], "%02u%02u", abs(tz_hour), abs(tz_min)); + time_zone[TIME_ZONE_LEN - 1] = '\0'; + event->tz = kstrdup(time_zone, GFP_ATOMIC); +} + +static bool is_valid_string(const char *str, unsigned int max_len) +{ + unsigned int len = 0; + unsigned int i; + + if (!str) + return false; + + len = strlen(str); + if (len == 0 || len > max_len) + return false; + + for (i = 0; i < len; i++) { + if (!isalpha(str[i]) && str[i] != '_') + return false; + } + return true; +} + +static bool is_valid_num_of_param(struct hiview_hisysevent *event) +{ + if (!event) + return false; + + if (event->payload_cnt > MAX_PARAM_NUMBER) + return false; + + event->payload_cnt++; + return true; +} + +struct hiview_hisysevent * +hisysevent_create(const char *domain, const char *name, enum hisysevent_type type) +{ + struct hiview_hisysevent *event = NULL; + + if (!is_valid_string(domain, MAX_DOMAIN_LENGTH)) { + pr_err("invalid event domain"); + return NULL; + } + if (!is_valid_string(name, MAX_EVENT_NAME_LENGTH)) { + pr_err("invalid event name"); + return NULL; + } + + event = kmalloc(sizeof(*event), GFP_KERNEL); + if (!event) + return NULL; + memset(event, 0, sizeof(*event)); + + event->domain = kstrdup(domain, GFP_ATOMIC); + if (!event->domain) + goto create_err; + + event->name = kstrdup(name, GFP_ATOMIC); + if (!event->name) + goto create_err; + + event->type = type; + event->pid = current->pid; + event->tid = current->tgid; + event->uid = current_uid().val; + hisysevent_set_time(event); + if (!event->tz) + goto create_err; + + event->payload_cnt = 0; + pr_info("create hisysevent succ, domain=%s, name=%s, type=%d", + event->domain, event->name, event->type); + return (void *)event; + +create_err: + hisysevent_destroy(&event); + return NULL; +} + +void hisysevent_destroy(struct hiview_hisysevent **event) +{ + struct hisysevent_payload *payload = NULL; + + if (!*event) + return; + + kfree(*event->domain); + kfree(*event->name); + kfree(*event->tz); + payload = *event->head; + while (payload) { + struct hisysevent_payload *temp = payload; + + payload = payload->next; + hisysevent_payload_destroy(temp); + } + kfree(*event); + *event = NULL; +} + +int hisysevent_put_integer(struct hiview_hisysevent *event, const char *key, long long value) +{ + struct hisysevent_payload *payload = NULL; + + if (!event) { + pr_err("invalid event"); + return -EINVAL; + } + if (!is_valid_num_of_param(event)) { + pr_err("invalid num of param"); + return -EINVAL; + } + if (!is_valid_string(key, MAX_PARAM_NAME_LENGTH)) { + pr_err("invalid key"); + return -EINVAL; + } + + payload = hisysevent_get_or_create_payload(event, key); + if (!payload) { + pr_err("failed to get or create payload"); + return -ENOMEM; + } + + payload->value = kmalloc(PARAM_INT_MAX_LEN, GFP_KERNEL); + if (!payload->value) + return -ENOMEM; + + memset(payload->value, 0, PARAM_INT_MAX_LEN); + snprintf(payload->value, PARAM_INT_MAX_LEN, "%lld", value); + return 0; +} + +int hisysevent_put_double(struct hiview_hisysevent *event, const char *key, double value) +{ + struct hisysevent_payload *payload = NULL; + + if (!event) { + pr_err("invalid event"); + return -EINVAL; + } + if (!is_valid_num_of_param(event)) { + pr_err("invalid num of param"); + return -EINVAL; + } + if (!is_valid_string(key, MAX_PARAM_NAME_LENGTH)) { + pr_err("invalid key"); + return -EINVAL; + } + + payload = hisysevent_get_or_create_payload(event, key); + if (!payload) { + pr_err("failed to get or create payload"); + return -ENOMEM; + } + + payload->value = kmalloc(PARAM_DBL_MAX_LEN, GFP_KERNEL); + if (!payload->value) + return -ENOMEM; + + memset(payload->value, 0, PARAM_DBL_MAX_LEN); + snprintf(payload->value, PARAM_DBL_MAX_LEN, "%lf", value); + return 0; +} + +int hisysevent_put_string(struct hiview_hisysevent *event, const char *key, const char *value) +{ + struct hisysevent_payload *payload = NULL; + int len = 0; + + if (!event) { + pr_err("invalid event"); + return -EINVAL; + } + if (!is_valid_num_of_param(event)) { + pr_err("invalid num of param"); + return -EINVAL; + } + if (!is_valid_string(key, MAX_PARAM_NAME_LENGTH)) { + pr_err("invalid key"); + return -EINVAL; + } + if (!value) { + pr_err("invalid value"); + return -EINVAL; + } + + payload = hisysevent_get_or_create_payload(event, key); + if (!payload) { + pr_err("failed to get or create payload"); + return -ENOMEM; + } + + len = strlen(value); + if (len > PARAM_STR_MAX_LEN) { + pr_warn("string cannot exceed 8KB, len=%d", len); + len = PARAM_STR_MAX_LEN; + } + + payload->value = kmalloc(len + 3, GFP_KERNEL); + if (!payload->value) + return -ENOMEM; + + memset(payload->value, 0, len + 3); + snprintf(payload->value, len + 3, "\"%s\"", value); + return 0; +} + +int hisysevent_write(struct hiview_hisysevent *event) +{ + struct iov_iter iter; + mm_segment_t oldfs; + char *data = NULL; + struct file *filp = NULL; + struct iovec vec[3]; + unsigned long vcount = 0; + int ret; + + ret = hisysevent_convert_json(event, &data); + if (ret != 0 || !data) { + pr_err("failed to convert event to string"); + return -EINVAL; + } + pr_info("write data=%s", data); + + filp = filp_open(HISYSEVENT_WRITER_DEV, O_WRONLY, 0); + + if (!filp || IS_ERR(filp)) { + ret = PTR_ERR(filp); + pr_err("failed to access '%s', res=%d", HISYSEVENT_WRITER_DEV, ret); + vfree(data); + return -ENODEV; + } + + vec[vcount].iov_base = &CHECK_CODE; + vec[vcount++].iov_len = sizeof(CHECK_CODE); + vec[vcount].iov_base = data; + vec[vcount++].iov_len = strlen(data) + 1; + + oldfs = get_fs(); + set_fs(KERNEL_DS); + iov_iter_init(&iter, WRITE, vec, vcount, iov_length(vec, vcount)); + ret = vfs_iter_write(filp, &iter, &filp->f_pos, 0); + set_fs(oldfs); + + if (ret < 0) { + pr_err("failed to write event to '%s' , res=%d", HISYSEVENT_WRITER_DEV, ret); + ret = -EIO; + } + + filp_close(filp, NULL); + vfree(data); + return ret; +} diff --git a/drivers/staging/zerohung/zrhung_event.c b/drivers/staging/zerohung/zrhung_event.c index be0428d4edfa..04b94a77e46e 100644 --- a/drivers/staging/zerohung/zrhung_event.c +++ b/drivers/staging/zerohung/zrhung_event.c @@ -5,285 +5,30 @@ #define pr_fmt(fmt) "zrhung " fmt -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include -#define MINUTE_TO_SECS 60 -#define SEC_TO_MILLISEC 1000 -#define MILLISEC_TO_NANOSEC (1000 * 1000) -#define TIME_ZONE_LEN 6 -#define HISYSEVENT_MAX_STR_LEN 1024 -#define HISYSEVENT_INFO_BUF_LEN 1024 -#define HISYSEVENT_WRITER_DEV "/dev/bbox" -static int CHECK_CODE = 0x7BCDABCD; - -#define BUF_POINTER_FORWARD \ -do { \ - if (tmp_len >= 0 && tmp_len < len) { \ - tmp += tmp_len; \ - len -= tmp_len; \ - } else { \ - pr_err("string over length"); \ - tmp += len; \ - len = 0; \ - } \ -} while (0) - -struct hisysevent { - char *domain; - char *event_name; - unsigned int type; - long long time; - char *tz; - unsigned int pid; - unsigned int tid; - unsigned int uid; - char *msg; -}; - -int hisysevent_set_time(struct hisysevent *event) -{ - struct timespec64 ts; - struct timezone tz = sys_tz; - int tz_index = 0; - char time_zone[TIME_ZONE_LEN]; - int tz_hour; - int tz_min; - long long millisecs = 0; - - if (!event) { - pr_err("invalid event"); - return -EINVAL; - } - - ktime_get_real_ts64(&ts); - millisecs = ts.tv_sec * SEC_TO_MILLISEC + ts.tv_nsec / MILLISEC_TO_NANOSEC; - event->time = millisecs; - tz_hour = (-tz.tz_minuteswest) / MINUTE_TO_SECS; - time_zone[tz_index++] = tz_hour >= 0 ? '+' : '-'; - tz_min = (-tz.tz_minuteswest) % MINUTE_TO_SECS; - sprintf(&time_zone[tz_index], "%02u%02u", abs(tz_hour), abs(tz_min)); - time_zone[TIME_ZONE_LEN - 1] = '\0'; - event->tz = kstrdup(time_zone, GFP_ATOMIC); - - return 0; -} - -int hisysevent_set_msg(struct hisysevent *event, const char *msg_buf) -{ - int len; - - if (!event) { - pr_err("invalid event"); - return -EINVAL; - } - - len = strlen(msg_buf); - if ((!msg_buf) || (msg_buf[0] == 0) || len > HISYSEVENT_MAX_STR_LEN) { - pr_err("invalid msg_buf"); - return -EINVAL; - } - - event->msg = kstrdup(msg_buf, GFP_ATOMIC); - - return 0; -} - -struct hisysevent *create_hisysevent(const char *domain, const char *event_name) -{ - struct hisysevent *event = NULL; - - event = vmalloc(sizeof(*event)); - if (!event) { - pr_err("failed to vmalloc for event"); - return NULL; - } - - memset(event, 0, sizeof(*event)); - - if ((!domain) || (domain[0] == 0)) { - pr_err("valid domain"); - vfree(event); - return NULL; - } - event->domain = kstrdup(domain, GFP_ATOMIC); - - if ((!event_name) || (event_name[0] == 0)) { - pr_err("valid event_name"); - kfree(event->domain); - vfree(event); - return NULL; - } - event->event_name = kstrdup(event_name, GFP_ATOMIC); - event->type = ZRHUNG_EVENT_TYPE; - - pr_info("create hisysevent succ, domain=%s, event_name=%s, type=%u", event->domain, - event->event_name, event->type); - - return (void *)event; -} - -struct hisysevent *inner_build_hisysevent(const char *domain, const char *event_name, - const char *msg_buf) -{ - struct hisysevent *event = NULL; - - event = create_hisysevent(domain, event_name); - hisysevent_set_time(event); - event->pid = current->pid; - event->tid = current->tgid; - event->uid = current_uid().val; - hisysevent_set_msg(event, msg_buf); - - return (void *)event; -} - -void zrhung_hisysevent_destroy(struct hisysevent *event) -{ - if (!event->domain) { - kfree(event->domain); - event->domain = NULL; - } - if (!event->event_name) { - kfree(event->event_name); - event->event_name = NULL; - } - if (!event->tz) { - kfree(event->tz); - event->tz = NULL; - } - if (!event->msg) { - kfree(event->msg); - event->msg = NULL; - } - - vfree(event); -} - -int hisysevent_convert_string(struct hisysevent *event, char **buf_ptr) -{ - int len; - char *tmp; - int tmp_len; - int base_index = 0; - static const char * const base_param_keys[] = {"domain_", "name_", "type_", "time_", "tz_", - "pid_", "tid_", "uid_", "MSG"}; - int buf_len = HISYSEVENT_INFO_BUF_LEN; - char *buf = vmalloc(buf_len); - - if (!buf) { - pr_err("failed to malloc buff for convert_string"); - return -ENOMEM; - } - memset(buf, 0, buf_len); - - len = buf_len; - tmp = buf; - - tmp_len = snprintf(tmp, len, "{\"%s\":\"%s\",", base_param_keys[base_index++], event->domain); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":\"%s\",", base_param_keys[base_index++], event->event_name); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":%u,", base_param_keys[base_index++], event->type); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":%lld,", base_param_keys[base_index++], event->time); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":\"%s\",", base_param_keys[base_index++], event->tz); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":%u,", base_param_keys[base_index++], event->pid); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":%u,", base_param_keys[base_index++], event->tid); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":%u,", base_param_keys[base_index++], event->uid); - BUF_POINTER_FORWARD; - - tmp_len = snprintf(tmp, len, "\"%s\":\"%s\"}", base_param_keys[base_index++], event->msg); - BUF_POINTER_FORWARD; - - *buf_ptr = buf; - - return (HISYSEVENT_INFO_BUF_LEN - len); -} - -int zrhung_hisysevent_write(struct hisysevent *event) -{ - struct iov_iter iter; - mm_segment_t oldfs; - char *data = NULL; - struct file *filp = NULL; - struct iovec vec[3]; - unsigned long vcount = 0; - int ret; - - hisysevent_convert_string(event, &data); - if (!data) { - pr_err("failed to convert string"); - return -EINVAL; - } - - filp = filp_open(HISYSEVENT_WRITER_DEV, O_WRONLY, 0); - - if ((!filp) || IS_ERR(filp)) { - ret = PTR_ERR(filp); - pr_err("access '%s' failed, res=%d", HISYSEVENT_WRITER_DEV, ret); - vfree(data); - return -ENODEV; - } - - vec[vcount].iov_base = &CHECK_CODE; - vec[vcount++].iov_len = sizeof(CHECK_CODE); - vec[vcount].iov_base = data; - vec[vcount++].iov_len = strlen(data) + 1; - - oldfs = get_fs(); - set_fs(KERNEL_DS); - iov_iter_init(&iter, WRITE, vec, vcount, iov_length(vec, vcount)); - ret = vfs_iter_write(filp, &iter, &filp->f_pos, 0); - set_fs(oldfs); - - if (ret < 0) { - pr_err("write '%s' failed, res=%d", HISYSEVENT_WRITER_DEV, ret); - ret = -EIO; - goto out; - } - -out: - filp_close(filp, NULL); - vfree(data); - return ret; -} +#include +#include int zrhung_send_event(const char *domain, const char *event_name, const char *msg_buf) { - struct hisysevent *event = NULL; + struct hiview_hisysevent *event = NULL; int ret = 0; - event = inner_build_hisysevent(domain, event_name, msg_buf); - + event = hisysevent_create(domain, event_name, FAULT); if (!event) { - pr_err("failed to build event"); + pr_err("failed to create event"); return -EINVAL; } + ret = hisysevent_put_string(event, "MSG", msg_buf); + if (ret != 0) { + pr_err("failed to put sting to event, ret=%d", ret); + goto hisysevent_end; + } + ret = hisysevent_write(event); - ret = zrhung_hisysevent_write(event); - zrhung_hisysevent_destroy(event); +hisysevent_end: + hisysevent_destroy(&event); return ret; } diff --git a/include/dfx/hiview_hisysevent.h b/include/dfx/hiview_hisysevent.h new file mode 100644 index 000000000000..df13a6a5429b --- /dev/null +++ b/include/dfx/hiview_hisysevent.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2022 Huawei Technologies Co., Ltd. All rights reserved. + */ + +#ifndef HIVIEW_HISYSEVENT_H +#define HIVIEW_HISYSEVENT_H + +enum hisysevent_type { + /* fault event */ + FAULT = 1, + + /* statistic event */ + STATISTIC = 2, + + /* security event */ + SECURITY = 3, + + /* behavior event */ + BEHAVIOR = 4 +}; + +struct hiview_hisysevent; + +struct hiview_hisysevent *hisysevent_create(const char *domain, const char *name, enum hisysevent_type type); +void hisysevent_destroy(struct hiview_hisysevent *event); +int hisysevent_put_integer(struct hiview_hisysevent *event, const char *key, long long value); +int hisysevent_put_double(struct hiview_hisysevent *event, const char *key, double value); +int hisysevent_put_string(struct hiview_hisysevent *event, const char *key, const char *value); +int hisysevent_write(struct hiview_hisysevent *event); + +#endif /* HIVIEW_HISYSEVENT_H */ -- Gitee