From 036f6986ce8ca540a48ff5caa748fa52686661c7 Mon Sep 17 00:00:00 2001 From: Jackie Liu Date: Fri, 20 Mar 2020 09:52:05 +0800 Subject: [PATCH] KYLIN: osinfo: Add /proc/osinfo interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mainline: NA From: NA Category: Feature CVE: NA 内核新增 /proc/osinfo 接口,主要目的是为了方便在特定的情况下 进行内核的信息收集,方便后端开发者快速定位潜在的问题。 Cc: nh #linux-hwe-5.15 Signed-off-by: Jackie Liu Change-Id: Icf07252a70c76ca0883e001667c8bfc0707ecb62 Signed-off-by: chen zhang Reviewed-on: http://gerrit.kylin.com/c/kfocal/+/23609 --- Makefile | 5 +++- fs/proc/Makefile | 18 ++++++++++++ fs/proc/osinfo.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 fs/proc/osinfo.c diff --git a/Makefile b/Makefile index 74eafb2e60ab..d49c52339809 100644 --- a/Makefile +++ b/Makefile @@ -359,6 +359,8 @@ include $(srctree)/scripts/Kbuild.include # Read KERNELRELEASE from include/config/kernel.release (if it exists) KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null) KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION) +SOURCEVERSION = $(shell git rev-parse HEAD 2> /dev/null) + export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION include $(srctree)/scripts/subarch.include @@ -1248,7 +1250,8 @@ define filechk_utsrelease.h echo '"$(KERNELRELEASE)" exceeds $(uts_len) characters' >&2; \ exit 1; \ fi; \ - echo \#define UTS_RELEASE \"$(KERNELRELEASE)\" + (echo \#define UTS_RELEASE \"$(KERNELRELEASE)\"; \ + echo \#define SOURCE_VERSION \"$(SOURCEVERSION)\";) endef define filechk_version.h diff --git a/fs/proc/Makefile b/fs/proc/Makefile index bd08616ed8ba..11caff7766cd 100644 --- a/fs/proc/Makefile +++ b/fs/proc/Makefile @@ -11,6 +11,9 @@ proc-$(CONFIG_MMU) := task_mmu.o proc-y += inode.o root.o base.o generic.o array.o \ fd.o + +$(obj)/osinfo.o: include/generated/compile.h + proc-$(CONFIG_TTY) += proc_tty.o proc-y += cmdline.o proc-y += consoles.o @@ -19,6 +22,7 @@ proc-y += devices.o proc-y += interrupts.o proc-y += loadavg.o proc-y += meminfo.o +proc-y += osinfo.o proc-y += stat.o proc-y += uptime.o proc-y += util.o @@ -34,3 +38,17 @@ proc-$(CONFIG_PROC_VMCORE) += vmcore.o proc-$(CONFIG_PRINTK) += kmsg.o proc-$(CONFIG_PROC_PAGE_MONITOR) += page.o proc-$(CONFIG_BOOT_CONFIG) += bootconfig.o + +# compile.h changes depending on hostname, generation number, etc, +# so we regenerate it always. +# mkcompile_h will make sure to only update the +# actual file if its content has changed. + + chk_compile.h = : + quiet_chk_compile.h = echo ' CHK $@' +silent_chk_compile.h = : +include/generated/compile.h: FORCE + @$($(quiet)chk_compile.h) + $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \ + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" \ + "$(CONFIG_PREEMPT_RT)" "$(CC) $(KBUILD_CFLAGS)" diff --git a/fs/proc/osinfo.c b/fs/proc/osinfo.c new file mode 100644 index 000000000000..39442e07962b --- /dev/null +++ b/fs/proc/osinfo.c @@ -0,0 +1,72 @@ +/* + * fs/proc/osinfo.c + * + * Authors: Jackie Liu + * Copyright (C) 2020, KylinSoft Co., Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +static int osinfo_proc_show(struct seq_file *m, void *v) +{ + struct sysinfo i; + struct timespec64 uptime; + + si_meminfo(&i); + ktime_get_boottime_ts64(&uptime); + + seq_printf(m, "Kernel Version:\t\t%s\n", UTS_RELEASE); + seq_printf(m, "Source Version:\t\t%s\n", + strlen(SOURCE_VERSION) ? SOURCE_VERSION : "Unknown"); + seq_printf(m, "Build Time:\t\t%s\n", UTS_VERSION); + seq_printf(m, "Architecture:\t\t%s\n", UTS_MACHINE); + seq_printf(m, "Online CPUs:\t\t%d\n", num_online_cpus()); + seq_printf(m, "MemTotal:\t\t%ld KB\n" + "PageSize:\t\t%ld KB\n", i.totalram << (PAGE_SHIFT - 10), + PAGE_SIZE >> 10); + seq_printf(m, "Uptime:\t\t\t%lu.%02lu Sec\n", (unsigned long) uptime.tv_sec, + (uptime.tv_nsec / (NSEC_PER_SEC / 100))); + seq_printf(m, "Cmdline:\t\t%s\n", saved_command_line); + + return 0; +} + +static int osinfo_proc_open(struct inode *inode, struct file *file) +{ + return single_open(file, osinfo_proc_show, NULL); +} + +static const struct proc_ops osinfo_proc_ops = { + .proc_open = osinfo_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = single_release, +}; + +static int __init proc_osinfo_init(void) +{ + proc_create("osinfo", 0, NULL, &osinfo_proc_ops); + return 0; +} +fs_initcall(proc_osinfo_init);