From e2cecab9f2bfe269072e7b4873db7750832a707c Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Wed, 9 Jul 2003 04:57:02 +0000 Subject: add C version of redhat-support-check here --- src/Makefile | 9 ++- src/redhat-support-check.c | 163 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 src/redhat-support-check.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 71491075..4281da0e 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 + getkey ppp-watch consoletype redhat-support-check PPPWATCH_OBJS=ppp-watch.o shvar.o INITLOG_OBJS=initlog.o process.o USLEEP_OBJS=usleep.o @@ -35,6 +35,7 @@ install: install -m 644 consoletype.1 $(ROOT)$(mandir)/man1 install -m 644 initlog.conf $(ROOT)/etc install -m 755 mkkerneldoth $(ROOT)/sbin + install -m 755 redhat-support-check $(ROOT)/sbin if uname -m | grep -q s390 ; then \ install -m 755 mkkerneldoth.s390 $(ROOT)/sbin/mkkerneldoth ; \ fi @@ -60,9 +61,15 @@ usleep: $(USLEEP_OBJS) ppp-watch: $(PPPWATCH_OBJS) $(CC) $(LDFLAGS) -o $@ $(PPPWATCH_OBJS) -Wl,-Bstatic `pkg-config glib-2.0 --libs` -Wl,-Bdynamic +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 + diff --git a/src/redhat-support-check.c b/src/redhat-support-check.c new file mode 100644 index 00000000..2a633418 --- /dev/null +++ b/src/redhat-support-check.c @@ -0,0 +1,163 @@ + +#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; + +#ifdef __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 + +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; + +#ifdef __i386__ + 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; + } + 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 < 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