From 50e62d900fb2f21492ebf7f82fc64c9c66af5729 Mon Sep 17 00:00:00 2001 From: Liu Xinwu Date: Wed, 9 May 2018 17:33:56 +0800 Subject: [PATCH] tools: acrn-crashlog: system properties for acrnprobe Acrnprobe needs to know some HW/SW properties, such as board version, build version. These properties APIs are provided in this file. Signed-off-by: Liu Xinwu Reviewed-by: Zhang Yanmin Reviewed-by: Liu Chuansheng Reviewed-by: Zhao Yakui Reviewed-by: Geoffroy Van Cutsem Acked-by: Eddie Dong --- tools/acrn-crashlog/acrnprobe/Makefile | 3 +- .../acrnprobe/include/property.h | 34 +++++ tools/acrn-crashlog/acrnprobe/property.c | 128 ++++++++++++++++++ 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 tools/acrn-crashlog/acrnprobe/include/property.h create mode 100644 tools/acrn-crashlog/acrnprobe/property.c diff --git a/tools/acrn-crashlog/acrnprobe/Makefile b/tools/acrn-crashlog/acrnprobe/Makefile index f46f93e84..d4a530b0d 100644 --- a/tools/acrn-crashlog/acrnprobe/Makefile +++ b/tools/acrn-crashlog/acrnprobe/Makefile @@ -26,7 +26,8 @@ $(BUILDDIR)/acrnprobe/bin/acrnprobe: $(BUILDDIR)/acrnprobe/obj/main.o \ $(BUILDDIR)/acrnprobe/obj/event_handler.o \ $(BUILDDIR)/acrnprobe/obj/crash_reclassify.o \ $(BUILDDIR)/acrnprobe/obj/sender.o \ - $(BUILDDIR)/acrnprobe/obj/startupreason.o + $(BUILDDIR)/acrnprobe/obj/startupreason.o \ + $(BUILDDIR)/acrnprobe/obj/property.o $(CC) -o $@ $^ $(LDFLAGS) clean: diff --git a/tools/acrn-crashlog/acrnprobe/include/property.h b/tools/acrn-crashlog/acrnprobe/include/property.h new file mode 100644 index 000000000..379b44152 --- /dev/null +++ b/tools/acrn-crashlog/acrnprobe/include/property.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 Intel Corporation + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * Copyright (C) 2018 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PROPERTY_H__ +#define __PROPERTY_H__ +#include "load_conf.h" + +#define VERSION_SIZE 256 + +char guuid[VERSION_SIZE]; +char gbuildversion[VERSION_SIZE]; + +int init_properties(struct sender_t *sender); +int swupdated(struct sender_t *sender); + +#endif diff --git a/tools/acrn-crashlog/acrnprobe/property.c b/tools/acrn-crashlog/acrnprobe/property.c new file mode 100644 index 000000000..c6b5ee591 --- /dev/null +++ b/tools/acrn-crashlog/acrnprobe/property.c @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2018 Intel Corporation + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * Copyright (C) 2018 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include "property.h" +#include "log_sys.h" +#include "fsutils.h" + +#define MACHINE_ID "/etc/machine-id" +#define OS_VERSION "/usr/lib/os-release" +#define OS_VERSION_KEY "VERSION_ID=" +#define DEVICE_ID_UNKNOWN "UnknownId" +#define LOG_UUID "uuid.txt" +#define LOG_BUILDID "buildid.txt" + +static void get_device_id(struct sender_t *sender) +{ + int ret; + char *loguuid; + + + ret = asprintf(&loguuid, "%s/%s", sender->outdir, LOG_UUID); + if (ret < 0) { + LOGE("compute string failed, out of memory\n"); + return; + } + + ret = file_read_string(MACHINE_ID, guuid, VERSION_SIZE); + if (ret <= 0) + LOGE("Could not get mmc id: %d (%s)\n", + ret, strerror(-ret)); + else + goto write; + + LOGE("Could not find DeviceId, set it to '%s'\n", + DEVICE_ID_UNKNOWN); + strncpy(guuid, DEVICE_ID_UNKNOWN, strlen(DEVICE_ID_UNKNOWN)); + +write: + overwrite_file(loguuid, guuid); + free(loguuid); +} + +static int get_buildversion(struct sender_t *sender) +{ + int ret; + char lastbuild[VERSION_SIZE]; + char *logbuildid; + char *currentbuild = gbuildversion; + + ret = file_read_key_value(OS_VERSION, OS_VERSION_KEY, gbuildversion); + if (ret <= 0) { + LOGE("failed to get version from %s, error (%s)\n", + OS_VERSION, strerror(-ret)); + return ret; + } + + ret = asprintf(&logbuildid, "%s/%s", sender->outdir, LOG_BUILDID); + if (ret < 0) { + LOGE("compute string failed, out of memory\n"); + return ret; + } + + ret = file_read_string(logbuildid, lastbuild, VERSION_SIZE); + if (ret == -ENOENT || + (ret > 0 && strcmp(currentbuild, lastbuild))) { + /* build changed or file not found, overwrite it */ + ret = overwrite_file(logbuildid, gbuildversion); + if (ret) { + LOGE("create (%s) failed, error (%s)\n", logbuildid, + strerror(-ret)); + goto free; + } + + sender->sw_updated = 1; + ret = 0; + } else if (ret <= 0) { + LOGE("Cannot read %s, error (%s)\n", + logbuildid, strerror(errno)); + } else { + /* buildid is the same */ + sender->sw_updated = 0; + ret = 0; + } +free: + free(logbuildid); + return ret; +} + +int swupdated(struct sender_t *sender) +{ + return sender->sw_updated; +} + +int init_properties(struct sender_t *sender) +{ + int ret; + + ret = get_buildversion(sender); + if (ret) { + LOGE("init properties failed\n"); + return ret; + } + get_device_id(sender); + return 0; +}