From 2ec1815e6ce8da65244c18fbad56c06721f737cb Mon Sep 17 00:00:00 2001 From: "James Dominic P. Guana" Date: Sat, 10 Apr 2021 18:20:21 +0800 Subject: Revert "Revert "Code clean up and add example"" This reverts commit ea08151ae6582a438db76b738293a0194c964b88. --- autogen.sh | 28 ++++++++++++++++ configure.ac | 10 ++++++ doc/reference/libgtop-sections.txt | 34 ++++++++++++++++++++ examples/Makefile.am | 3 ++ examples/disk.c | 65 ++++++++++++++++++++++++++++++++++++++ features.def | 1 + include/glibtop/Makefile.am | 2 +- include/glibtop/command.h | 3 +- include/glibtop/disk.h | 6 +++- include/glibtop/union.h | 2 +- lib/sysdeps.c | 3 ++ libgtop-2.0.pc.in | 2 +- libgtop.spec.in | 1 + src/daemon/main.c | 2 +- src/daemon/slave.c | 6 ++++ sysdeps/common/default.c | 4 +-- sysdeps/linux/disk.c | 33 +++++++++++++------ sysdeps/linux/glibtop_private.h | 2 +- sysdeps/stub/Makefile.am | 2 +- sysdeps/stub_suid/Makefile.am | 2 +- sysdeps/stub_suid/disk.c | 47 +++++++++++++++++++++++++++ 21 files changed, 238 insertions(+), 20 deletions(-) create mode 100644 examples/disk.c create mode 100644 sysdeps/stub_suid/disk.c diff --git a/autogen.sh b/autogen.sh index 9d394bff..ab75b974 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,5 +1,33 @@ #!/bin/sh # Run this to generate all the initial makefiles, etc. +RED='\033[0;31m' +NC='\033[0m' # No Color + +case "$(uname -s)" in + Linux) + if ! [ -x "$(command -v lsblk)" ]; then + echo '' >&2 + echo -e "${RED}*** ERROR:${NC} lsblk cannot be found. Try installing util-linux or util-linux-ng ${RED}***${NC}" >&2 + echo '' >&2 + exit 1 + fi + if ! [ -x "$(command -v sed)" ]; then + echo '' >&2 + echo -e "${RED}*** ERROR:${NC} sed cannot be found. ***" >&2 + echo '' >&2 + exit 1 + fi + if ! [ -x "$(command -v tr)" ]; then + echo '' >&2 + echo -e "${RED}*** ERROR:${NC} tr cannot be found. ***" >&2 + echo '' >&2 + exit 1 + fi + ;; + *) + ;; +esac + test -n "$srcdir" || srcdir=$(dirname "$0") test -n "$srcdir" || srcdir=. diff --git a/configure.ac b/configure.ac index d7215bf0..5426daf1 100644 --- a/configure.ac +++ b/configure.ac @@ -71,6 +71,16 @@ dnl Most people should have a working perl interpreter on their system AC_CHECK_PROGS(PERL, perl5 perl) test -z "$PERL" && AC_MSG_ERROR([You need to have a working perl interpreter.]) +case "${host_os}" in + linux*) + AC_CHECK_TOOL(LSBLK,lsblk) + AC_CHECK_TOOL(SED,sed) + AC_CHECK_TOOL(TR,tr) + ;; + *) + ;; +esac + AC_CHECK_TOOL(CC,gcc) AC_CHECK_TOOL(RANLIB,ranlib) AC_CHECK_TOOL(AS,as) diff --git a/doc/reference/libgtop-sections.txt b/doc/reference/libgtop-sections.txt index ba878943..3d6d710d 100644 --- a/doc/reference/libgtop-sections.txt +++ b/doc/reference/libgtop-sections.txt @@ -29,6 +29,7 @@ glibtop_close_p GLIBTOP_CMND_QUIT GLIBTOP_CMND_SYSDEPS GLIBTOP_CMND_CPU +GLIBTOP_CMND_DISK GLIBTOP_CMND_MEM GLIBTOP_CMND_SWAP GLIBTOP_CMND_UPTIME @@ -678,6 +679,39 @@ glibtop_init_cpu_s glibtop_get_cpu_s +glibtop/disk.h +
+disk +GLIBTOP_DISK_TOTAL +GLIBTOP_DISK_USER +GLIBTOP_DISK_NICE +GLIBTOP_DISK_SYS +GLIBTOP_DISK_IDLE +GLIBTOP_DISK_FREQUENCY +GLIBTOP_XDISK_TOTAL +GLIBTOP_XDISK_USER +GLIBTOP_XDISK_NICE +GLIBTOP_XDISK_SYS +GLIBTOP_XDISK_IDLE +GLIBTOP_XDISK_FLAGS +GLIBTOP_DISK_IOWAIT +GLIBTOP_DISK_IRQ +GLIBTOP_DISK_SOFTIRQ +GLIBTOP_XDISK_IOWAIT +GLIBTOP_XDISK_IRQ +GLIBTOP_XDISK_SOFTIRQ +GLIBTOP_MAX_DISK +GLIBTOP_NDISK +glibtop_disk +glibtop_get_disk +glibtop_get_disk_r +glibtop_get_disk_l +glibtop_init_disk_p +glibtop_get_disk_p +glibtop_init_disk_s +glibtop_get_disk_s +
+ glibtop/swap.h
swap diff --git a/examples/Makefile.am b/examples/Makefile.am index 25585e04..ef192787 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -34,6 +34,9 @@ mountlist_LDADD = $(top_builddir)/lib/libgtop-2.0.la smp_SOURCES = smp.c smp_LDADD = $(top_builddir)/lib/libgtop-2.0.la -lm +disk_SOURCES = disk.c +disk_LDADD = $(top_builddir)/lib/libgtop-2.0.la -lm + timings_SOURCES = timings.c timings_LDADD = $(top_builddir)/lib/libgtop-2.0.la diff --git a/examples/disk.c b/examples/disk.c new file mode 100644 index 00000000..c3c61142 --- /dev/null +++ b/examples/disk.c @@ -0,0 +1,65 @@ +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by James Dominic P. Guana , May 2020. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop 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 General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include + +#include +#include +#include +#include + +#include +#include + +int +main (int argc, char *argv []) +{ + glibtop_disk disk; + char separator [BUFSIZ]; + int ndisk, i; + + glibtop_init(); + + glibtop_get_disk (&disk); + + ndisk = glibtop_global_server->ndisk ? glibtop_global_server->ndisk : 1; + + memset (separator, '-', 91); + separator [92] = '\0'; + + printf("\n\n"); + printf ("ELAPSE "); + printf ("Read Time Read Write Time Write\n"); + printf ("%s\n", separator); + + for (i = 0; i < ndisk; i++) { + printf ("DISK %3d : %12lu %12lu %12lu %12lu\n", i, + (unsigned long) disk.xdisk_sectors_read [i], + (unsigned long) disk.xdisk_time_read [i], + (unsigned long) disk.xdisk_sectors_write [i], + (unsigned long) disk.xdisk_time_write [i]); + + } + + printf ("%s\n\n\n", separator); + + exit (0); +} diff --git a/features.def b/features.def index faab87b1..50cbe72a 100644 --- a/features.def +++ b/features.def @@ -1,4 +1,5 @@ void|cpu +void|disk void|mem void|swap void|uptime diff --git a/include/glibtop/Makefile.am b/include/glibtop/Makefile.am index 6d0e055f..da78ca93 100644 --- a/include/glibtop/Makefile.am +++ b/include/glibtop/Makefile.am @@ -2,7 +2,7 @@ glibtopdir = $(includedir)/libgtop-2.0/glibtop glibtop_HEADERS = close.h loadavg.h prockernel.h procstate.h \ sem_limits.h uptime.h command.h mem.h proclist.h \ - proctime.h shm_limits.h cpu.h msg_limits.h \ + proctime.h shm_limits.h cpu.h disk.h msg_limits.h \ procmem.h procuid.h swap.h \ procsegment.h sysdeps.h global.h \ procsignal.h union.h gnuserv.h \ diff --git a/include/glibtop/command.h b/include/glibtop/command.h index 9a0e47b1..31e89589 100644 --- a/include/glibtop/command.h +++ b/include/glibtop/command.h @@ -60,8 +60,9 @@ G_BEGIN_DECLS #define GLIBTOP_CMND_PROC_WD 26 #define GLIBTOP_CMND_PROC_AFFINITY 27 #define GLIBTOP_CMND_PROC_IO 28 +#define GLIBTOP_CMND_DISK 29 -#define GLIBTOP_MAX_CMND 29 +#define GLIBTOP_MAX_CMND 30 #define _GLIBTOP_PARAM_SIZE 16 diff --git a/include/glibtop/disk.h b/include/glibtop/disk.h index cdde7796..292924f6 100644 --- a/include/glibtop/disk.h +++ b/include/glibtop/disk.h @@ -46,16 +46,20 @@ struct _partition_info { char name[256]; char type[256]; - char raid_num[256]; + char raid_num[256]; int max; }; +typedef struct _partition_info partition_info; + struct _glibtop_disk { + guint64 flags; guint64 xdisk_sectors_read [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_SECTORS_READ */ guint64 xdisk_time_read [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_TIME_READ */ guint64 xdisk_sectors_write [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_SECTORS_WRITE */ guint64 xdisk_time_write [GLIBTOP_NDISK]; /* GLIBTOP_XDISK_TIME_WRITE */ + guint64 xdisk_flags; }; void glibtop_get_disk (glibtop_disk *buf); diff --git a/include/glibtop/union.h b/include/glibtop/union.h index bda7c576..eb57cfc0 100644 --- a/include/glibtop/union.h +++ b/include/glibtop/union.h @@ -61,7 +61,7 @@ typedef union _glibtop_union glibtop_union; union _glibtop_union { glibtop_cpu cpu; - glibtop_disk disk; + glibtop_disk disk; glibtop_mem mem; glibtop_swap swap; glibtop_uptime uptime; diff --git a/lib/sysdeps.c b/lib/sysdeps.c index 96292568..beb950e8 100644 --- a/lib/sysdeps.c +++ b/lib/sysdeps.c @@ -139,6 +139,9 @@ const _glibtop_init_func_t _glibtop_init_hook_p [] = { #if GLIBTOP_SUID_CPU _glibtop_init_cpu_p, #endif +#if GLIBTOP_SUID_DISK + _glibtop_init_disk_p, +#endif #if GLIBTOP_SUID_MEM _glibtop_init_mem_p, #endif diff --git a/libgtop-2.0.pc.in b/libgtop-2.0.pc.in index e1753322..4381ab20 100644 --- a/libgtop-2.0.pc.in +++ b/libgtop-2.0.pc.in @@ -5,7 +5,7 @@ includedir=@includedir@ Name: libgtop Description: Portable System Access Library -Requires: glib-2.0 +Requires: glib-2.0, util-linux Version: @VERSION@ Libs: -L${libdir} -lgtop-2.0 Cflags: -I${includedir}/libgtop-2.0 diff --git a/libgtop.spec.in b/libgtop.spec.in index 5957868e..f4fefa5b 100644 --- a/libgtop.spec.in +++ b/libgtop.spec.in @@ -5,6 +5,7 @@ %define prefix /usr Summary: LibGTop library +Requires: util-linux, sed Name: libgtop Version: %ver Release: %rel diff --git a/src/daemon/main.c b/src/daemon/main.c index 34cbaf50..88e19a09 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -98,7 +98,7 @@ handle_parent_connection (int s) do_output (s, resp, _offset_data (cpu), 0, NULL); break; case GLIBTOP_CMND_DISK: - glibtop_get_disk_l (server, &resp->u.disk.cpu); + glibtop_get_disk_l (server, &resp->u.data.disk); do_output (s, resp, _offset_data (disk), 0, NULL); break; case GLIBTOP_CMND_MEM: diff --git a/src/daemon/slave.c b/src/daemon/slave.c index e15f1efc..6e80b2b8 100644 --- a/src/daemon/slave.c +++ b/src/daemon/slave.c @@ -135,6 +135,12 @@ handle_slave_command (glibtop_command *cmnd, glibtop_response *resp, resp->offset = _offset_data (cpu); break; #endif +#if GLIBTOP_SUID_DISK + case GLIBTOP_CMND_DISK: + glibtop_get_disk_p (server, &resp->u.data.disk); + resp->offset = _offset_data (disk); + break; +#endif #if GLIBTOP_SUID_MEM case GLIBTOP_CMND_MEM: glibtop_get_mem_p (server, &resp->u.data.mem); diff --git a/sysdeps/common/default.c b/sysdeps/common/default.c index 653a4f5a..ff6c3ecd 100644 --- a/sysdeps/common/default.c +++ b/sysdeps/common/default.c @@ -72,9 +72,9 @@ glibtop_get_cpu(glibtop_cpu *buf) * */ void -glibtop_get_cpu (glibtop_cpu *buf) +glibtop_get_disk (glibtop_disk *buf) { - glibtop_get_cpu_l (glibtop_global_server, buf); + glibtop_get_disk_l (glibtop_global_server, buf); } diff --git a/sysdeps/linux/disk.c b/sysdeps/linux/disk.c index 3ad74bcc..c2d341f6 100644 --- a/sysdeps/linux/disk.c +++ b/sysdeps/linux/disk.c @@ -48,7 +48,7 @@ _glibtop_init_disk_s (glibtop *server) // Handle LVM and RAID // void -find_primary_part (_partition_info *primary_part, const char *m) +find_primary_part (partition_info *primary_part, const char *m) { int n = 0, tlvl = 0; char name[256]="",type[256]=""; @@ -190,7 +190,7 @@ find_primary_part (_partition_info *primary_part, const char *m) } int -is_virtual_drive (_partition_info *primary_part, const char *p) +is_virtual_drive (partition_info *primary_part, const char *p) { int i; char name[256]; @@ -220,10 +220,27 @@ is_virtual_drive (_partition_info *primary_part, const char *p) return test; } +int +max_lines (const char *p) +{ + char temp[10]; + int count = 0; + + while (sscanf (p, "%s", temp) == 1) + { + + p = skip_line(p); + count++; + + } + + return count; +} + void glibtop_get_disk_s (glibtop *server, glibtop_disk *buf) { - _partition_info primary_part[GLIBTOP_NDISK]; + partition_info primary_part[GLIBTOP_NDISK]; char buffer [STAT_BUFSIZ], *p, map_buffer [STAT_BUFSIZ], *m; int i; @@ -233,8 +250,6 @@ glibtop_get_disk_s (glibtop *server, glibtop_disk *buf) get_from_pipe (map_buffer, CMD_PIPE); - server->ndisk = GLIBTOP_NDISK; - /* * GLOBAL */ @@ -246,23 +261,23 @@ glibtop_get_disk_s (glibtop *server, glibtop_disk *buf) * PER DISK */ + server->ndisk = max_lines(p); + find_primary_part (primary_part, m); - for (i = 0; i <= server->ndisk; i++) { + for (i = 0; i < server->ndisk; i++) { p = skip_multiple_token (p,2); // skip if disk is the raw device if (!is_virtual_drive (primary_part, p)) { + server->ndisk--; p = skip_line (p); /* move to ^ */ p = skip_multiple_token (p, 2); } - if (!check_disk_line_warn (server, p, i)) - break; - p = skip_token (p); /* prev xdisk_name */ p = skip_token (p); /* prev xdisk_reads_completed */ p = skip_token (p); /* prev xdisk_reads_merged */ diff --git a/sysdeps/linux/glibtop_private.h b/sysdeps/linux/glibtop_private.h index 66fed54c..39101163 100644 --- a/sysdeps/linux/glibtop_private.h +++ b/sysdeps/linux/glibtop_private.h @@ -62,7 +62,7 @@ skip_line (const char *p) } void -get_from_pipe (char *buffer, const char *cmd) +get_from_pipe (char *buffer, const char *cmd); /* * Smart strtoul which handles binary suffixes diff --git a/sysdeps/stub/Makefile.am b/sysdeps/stub/Makefile.am index f4219edb..6d9ce727 100644 --- a/sysdeps/stub/Makefile.am +++ b/sysdeps/stub/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = @AM_CPPFLAGS@ noinst_LTLIBRARIES = libgtop_sysdeps-2.0.la -libgtop_sysdeps_2_0_la_SOURCES = open.c close.c siglist.c cpu.c mem.c swap.c \ +libgtop_sysdeps_2_0_la_SOURCES = open.c close.c siglist.c cpu.c disk.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ sem_limits.c proclist.c procstate.c procuid.c \ proctime.c procmem.c procsignal.c prockernel.c \ diff --git a/sysdeps/stub_suid/Makefile.am b/sysdeps/stub_suid/Makefile.am index fcc7daf9..3f1bf958 100644 --- a/sysdeps/stub_suid/Makefile.am +++ b/sysdeps/stub_suid/Makefile.am @@ -7,7 +7,7 @@ libgtop_sysdeps_2_0_la_SOURCES = nosuid.c siglist.c libgtop_sysdeps_2_0_la_LDFLAGS = $(LT_VERSION_INFO) -libgtop_sysdeps_suid_2_0_la_SOURCES = open.c close.c siglist.c cpu.c mem.c swap.c \ +libgtop_sysdeps_suid_2_0_la_SOURCES = open.c close.c siglist.c cpu.c disk.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ sem_limits.c proclist.c procstate.c procuid.c \ proctime.c procmem.c procsignal.c prockernel.c \ diff --git a/sysdeps/stub_suid/disk.c b/sysdeps/stub_suid/disk.c new file mode 100644 index 00000000..ed5cda02 --- /dev/null +++ b/sysdeps/stub_suid/disk.c @@ -0,0 +1,47 @@ +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig , April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop 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 General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include + +#include + +static const unsigned long _glibtop_sysdeps_disk = 0; + +/* Init function. */ + +void +_glibtop_init_disk_p (glibtop *server) +{ + server->sysdeps.disk = _glibtop_sysdeps_disk; +} + +/* Provides information about disk usage. */ + +void +glibtop_get_disk_p (glibtop *server, glibtop_disk *buf) +{ + glibtop_init_p (server, GLIBTOP_SYSDEPS_DISK, 0); + + memset (buf, 0, sizeof (glibtop_disk)); +} -- cgit v1.2.1