From 02654fa68b2749e112de0156cef31187ebf992e0 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Mon, 18 Oct 2004 21:10:15 +0000 Subject: re-add redhat-support-check (#135145) --- src/Makefile | 9 ++- src/redhat-support-check.c | 184 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/redhat-support-check.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index ba2378cd..11582eee 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CFLAGS+=$(RPM_OPT_FLAGS) -Wall -D_GNU_SOURCE PROGS=usernetctl doexec netreport testd usleep ipcalc initlog minilogd \ - getkey ppp-watch consoletype genhostid kmodule + getkey ppp-watch consoletype genhostid kmodule redhat-support-check PPPWATCH_OBJS=ppp-watch.o shvar.o INITLOG_OBJS=initlog.o process.o USLEEP_OBJS=usleep.o @@ -37,6 +37,7 @@ install: install -m 644 ipcalc.1 $(ROOT)$(mandir)/man1 install -m 644 consoletype.1 $(ROOT)$(mandir)/man1 install -m 644 initlog.conf $(ROOT)/etc + install -m 755 redhat-support-check $(ROOT)/sbin # this daemon and initscript are useful for testing the up/down/status stuff # not installed by default, only comes from sources. @@ -65,11 +66,17 @@ usernetctl.o: usernetctl.c usernetctl: usernetctl.c usernetctl.o $(CC) $(LDFLAGS) -pie -o $@ $@.o +redhat-support-check: redhat-support-check.o + $(CC) $(LDFLAGS) -o $@ redhat-support-check.o -Wl,-Bstatic -lpopt `pkg-config glib-2.0 --libs` -Wl,-Bdynamic + shvar.o: shvar.c $(CC) $(CFLAGS) `pkg-config glib-2.0 --cflags` -c shvar.c -o shvar.o ppp-watch.o: ppp-watch.c $(CC) $(CFLAGS) `pkg-config glib-2.0 --cflags` -c ppp-watch.c -o ppp-watch.o +redhat-support-check.o: redhat-support-check.c + $(CC) $(CFLAGS) `pkg-config glib-2.0 --cflags` -c redhat-support-check.c -o redhat-support-check.o + kmodule: kmodule.o $(CC) $(LDFLAGS) -o $@ $< -Wl,-Bstatic -lpopt -Wl,-Bdynamic -lkudzu -lpci diff --git a/src/redhat-support-check.c b/src/redhat-support-check.c new file mode 100644 index 00000000..e115b57f --- /dev/null +++ b/src/redhat-support-check.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2003-2004 Red Hat, Inc. All rights reserved. + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include + +#define _(String) gettext((String)) + +#define SUPPORTINFO "/var/lib/supportinfo" + +static int min_mem = 0; +static int max_mem = 0; +static int max_cpus = 0; +static char *variant = NULL; + +static char *release_name = NULL; + +static int test = 0; + +#if defined(__i386__) +static inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx) +{ + __asm__("pushl %%ebx; cpuid; movl %%ebx,%1; popl %%ebx" + : "=a"(*eax), "=r"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0" (op)); +} +#endif + +#if defined(__x86_64__) +static inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx) +{ + __asm__("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" + : "=a"(*eax), "=r"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0" (op)); +} +#endif + +guint64 get_memory() { + guint64 npages = sysconf(_SC_PHYS_PAGES); + guint64 pgsize = sysconf(_SC_PAGESIZE); + + return (guint64)(npages * pgsize / 1048576); +} + +unsigned int get_num_cpus() { + int ncpus = sysconf(_SC_NPROCESSORS_ONLN); + u_int32_t eax = 0, ebx = 0, ecx = 0, edx = 0; + +#if defined(__i386__) || defined(__x86_64__) + cpuid(0, &eax, &ebx, &ecx, &edx); + if (ebx == 0x756e6547) { /* Intel */ + cpuid(1, &eax, &ebx, &ecx, &edx); + if (edx & (1 << 28)) { /* has HT */ + int nsibs = (ebx & 0xff0000) >> 16; + return ncpus / nsibs; + } + } +#endif + return ncpus; +} + +int parse_supportinfo() { + gchar *contents, **lines; + gsize len; + int x; + + g_file_get_contents(SUPPORTINFO, &contents, &len, NULL); + if (!contents) + return -1; + lines = g_strsplit(contents,"\n", 0); + for (x = 0; lines[x]; x++) { + if (!strncmp(lines[x],"Variant:",8)) + variant = strdup(lines[x]+9); + if (!strncmp(lines[x],"MinRAM:",7)) + min_mem = atoi(lines[x]+8); + if (!strncmp(lines[x],"MaxRAM:",7)) + max_mem = atoi(lines[x]+8); + if (!strncmp(lines[x],"MaxCPU:",7)) + max_cpus = atoi(lines[x]+8); + } + g_free(contents); + g_strfreev(lines); + return 0; +} + +void get_release_info() { + gchar *contents; + gsize len; + + g_file_get_contents("/etc/redhat-release", &contents, &len, NULL); + if (!contents) + release_name = "Red Hat Linux release 11.7 (Foo)"; + else + release_name = g_strstrip(contents); +} + +int main(int argc, char **argv) { + const struct poptOption options[] = { + POPT_AUTOHELP + { "test", 't', POPT_ARG_NONE, &test, 0, + _("Test mode (assumes 256MB, 1GB, 2 CPUs)"), NULL + }, + { 0, 0, 0, 0, 0, 0 } + }; + poptContext context; + guint64 memory; + int cpus; + int rc = 0; + + setlocale(LC_ALL, ""); + bindtextdomain("redhat-support-check", "/usr/share/locale"); + textdomain("redhat-support-check"); + + context = poptGetContext("redhat-support-check", argc, + (const char **)argv, options, 0); + while ((rc = poptGetNextOpt (context)) > 0); + if (rc < -1) { + fprintf(stderr, "%s: %s\n", + poptBadOption(context, POPT_BADOPTION_NOALIAS), + poptStrerror(rc)); + return 1; + } + rc = 0; + if (test) { + max_mem = 1024; + min_mem = 256; + max_cpus = 2; + variant = "XS"; + } else { + if (parse_supportinfo()) { + fprintf(stderr, + _("Failed to parse supportinfo file.\n")); + return 1; + } + } + + memory = get_memory(); + cpus = get_num_cpus(); + get_release_info(); + + openlog("redhat-support-check", 0, LOG_USER); + if (min_mem && memory < (0.9 * min_mem)) { + printf(_("WARNING: %s requires at least %dMB RAM to run as a supported configuration. (%lluMB detected)\n"), + release_name, min_mem, memory); + syslog(LOG_NOTICE,_("WARNING: %s requires at least %dMB RAM to run as a supported configuration. (%lluMB detected)\n"), + release_name, min_mem, memory); + rc++; + } + if (max_mem && memory > max_mem) { + printf(_("WARNING: %s requires no more than %dMB RAM to run as a supported configuration. (%lluMB detected)\n"), + release_name, max_mem, memory); + syslog(LOG_NOTICE,_("WARNING: %s requires no more than %dMB RAM to run as a supported configuration. (%lluMB detected)\n"), + release_name, max_mem, memory); + rc++; + } + if (max_cpus && cpus > max_cpus) { + printf(_("WARNING: %s requires no more than %d CPUs to run as a supported configuration. (%d detected)\n"), + release_name, max_cpus, cpus); + syslog(LOG_NOTICE,_("WARNING: %s requires no more than %d CPUs to run as a supported configuration. (%d detected)\n"), + release_name, max_cpus, cpus); + rc++; + } + return rc; +} -- cgit v1.2.1