From 5967e6c3e41c426b717c930f2dcc98859980e847 Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Mon, 5 Apr 1999 12:55:56 +0000 Subject: [PATCH] Copied from Linux sysdeps directory. --- sysdeps/kernel/ppp.c | 157 ++++++++++++++++++++++++++++++++++++++- sysdeps/kernel/siglist.c | 34 ++++++++- 2 files changed, 189 insertions(+), 2 deletions(-) diff --git a/sysdeps/kernel/ppp.c b/sysdeps/kernel/ppp.c index 6d0905d5..145d163f 100644 --- a/sysdeps/kernel/ppp.c +++ b/sysdeps/kernel/ppp.c @@ -23,9 +23,19 @@ #include #include +#include #include -static const unsigned long _glibtop_sysdeps_ppp = 0; +#include +#include +#include +#include + +#include + +static const unsigned long _glibtop_sysdeps_ppp = +(1 << GLIBTOP_PPP_STATE) + (1 << GLIBTOP_PPP_BYTES_IN) + +(1 << GLIBTOP_PPP_BYTES_OUT); /* Init function. */ @@ -35,10 +45,155 @@ glibtop_init_ppp_s (glibtop *server) server->sysdeps.ppp = _glibtop_sysdeps_ppp; } +static int +get_ISDN_stats (glibtop *server, int *in, int *out) +{ + unsigned long *isdn_stats, *ptr; + int fd, i; + + *in = *out = 0; + + isdn_stats = glibtop_calloc_r (server, ISDN_MAX_CHANNELS * 2, + sizeof (unsigned long)); + + fd = open ("/dev/isdninfo", O_RDONLY); + if (fd < 0) { + glibtop_free_r (server, isdn_stats); + return FALSE; + } + + if ((ioctl (fd, IIOCGETCPS, isdn_stats) < 0) && (errno != 0)) { + glibtop_free_r (server, isdn_stats); + close (fd); + + return FALSE; + } + + for (i = 0, ptr = isdn_stats; i < ISDN_MAX_CHANNELS; i++) { + *in += *ptr++; *out += *ptr++; + } + + glibtop_free_r (server, isdn_stats); + close (fd); + + return TRUE; +} + +static int is_ISDN_on (glibtop *server, int *online) +{ + FILE *f = 0; + char buffer [BUFSIZ], *p; + int i; + + /* Perhaps I should try to explain this code a little bit. + * + * ------------------------------------------------------------ + * This is from the manpage of isdninfo(4): + * + * DESCRIPTION + * /dev/isdninfo is a character device with major number 45 + * and minor number 255. It delivers status information from + * the Linux ISDN subsystem to user level. + * + * DATA FORMAT + * When reading from this device, the current status of the + * Linux ISDN subsystem is delivered in 6 lines of text. Each + * line starts with a tag string followed by a colon and + * whitespace. After that the status values are appended sep- + * arated by whitespace. + * + * flags is the tag of line 5. In this line for every driver + * slot, it's B-Channel status is shown. If no driver + * is registered in a slot, a ? is shown. For every + * established B-Channel of the driver, a bit is set + * in the shown value. The driver's first channel is + * mapped to bit 0, the second channel to bit 1 and so + * on. + * ------------------------------------------------------------ + * + * So we open /dev/isdninfo, discard the first four lines of text + * and then check whether we have something that is not `0' or `?' + * in one of the flags fields. + * + * Sounds complicated, but I don't see any other way to check whether + * we are connected. Also, this is the method some other ISDN tools + * for Linux use. + * + * Martin + */ + + f = fopen ("/dev/isdninfo", "r"); + + if (!f) return FALSE; + + for (i = 0; i < 5; i++) { + if (fgets (buffer, BUFSIZ, f) == NULL) { + fclose (f); + return FALSE; + } + } + + if (strncmp (buffer, "flags:", 6)) { + fclose (f); + return FALSE; + } + + p = buffer+6; + + while (*p) { + char *end = p; + + if (isspace (*p)) { + p++; + continue; + } + + for (end = p; *end && !isspace (*end); end++) + ; + + if (*end == 0) + break; + else + *end = 0; + + if (!strcmp (p, "?") || !strcmp (p, "0")) { + p = end+1; + continue; + } + + fclose (f); + + *online = TRUE; + return TRUE; + } + + fclose (f); + + *online = FALSE; + return TRUE; +} + /* Provides PPP/ISDN information. */ void glibtop_get_ppp_s (glibtop *server, glibtop_ppp *buf, unsigned short device) { + int in, out, online; + + glibtop_init_s (&server, GLIBTOP_SYSDEPS_PPP, 0); + memset (buf, 0, sizeof (glibtop_ppp)); + + if (is_ISDN_on (server, &online)) { + buf->state = online ? GLIBTOP_PPP_STATE_ONLINE : + GLIBTOP_PPP_STATE_HANGUP; + buf->flags |= (1 << GLIBTOP_PPP_STATE); + } + + if (get_ISDN_stats (server, &in, &out)) { + buf->bytes_in = in; + buf->bytes_out = out; + buf->flags |= (1 << GLIBTOP_PPP_BYTES_IN) | + (1 << GLIBTOP_PPP_BYTES_OUT); + } } diff --git a/sysdeps/kernel/siglist.c b/sysdeps/kernel/siglist.c index 94abfdc6..d00223c0 100644 --- a/sysdeps/kernel/siglist.c +++ b/sysdeps/kernel/siglist.c @@ -25,4 +25,36 @@ #include const glibtop_signame glibtop_sys_siglist [] = -{ { 0, NULL, NULL } }; +{ { 1, "SIGHUP", NULL }, /* Hangup (POSIX). */ + { 2, "SIGINT", NULL }, /* Interrupt (ANSI). */ + { 3, "SIGQUIT", NULL }, /* Quit (POSIX). */ + { 4, "SIGILL", NULL }, /* Illegal instruction (ANSI). */ + { 5, "SIGTRAP", NULL }, /* Trace trap (POSIX). */ + { 6, "SIGABRT", NULL }, /* Abort (ANSI). */ + { 7, "SIGBUS", NULL }, /* BUS error (4.2 BSD). */ + { 8, "SIGFPE", NULL }, /* Floating-point exception (ANSI). */ + { 9, "SIGKILL", NULL }, /* Kill, unblockable (POSIX). */ + { 10, "SIGUSR1", NULL }, /* User-defined signal 1 (POSIX). */ + { 11, "SIGSEGV", NULL }, /* Segmentation violation (ANSI). */ + { 12, "SIGUSR2", NULL }, /* User-defined signal 2 (POSIX). */ + { 13, "SIGPIPE", NULL }, /* Broken pipe (POSIX). */ + { 14, "SIGALRM", NULL }, /* Alarm clock (POSIX). */ + { 15, "SIGTERM", NULL }, /* Termination (ANSI). */ + { 16, "SIGSTKFLT", NULL }, /* ??? */ + { 17, "SIGCHLD", NULL }, /* Child status has changed (POSIX). */ + { 18, "SIGCONT", NULL }, /* Continue (POSIX). */ + { 19, "SIGSTOP", NULL }, /* Stop, unblockable (POSIX). */ + { 20, "SIGTSTP", NULL }, /* Keyboard stop (POSIX). */ + { 21, "SIGTTIN", NULL }, /* Background read from tty (POSIX). */ + { 22, "SIGTTOU", NULL }, /* Background write to tty (POSIX). */ + { 23, "SIGURG", NULL }, /* Urgent condition on socket (4.2 BSD). */ + { 24, "SIGXCPU", NULL }, /* CPU limit exceeded (4.2 BSD). */ + { 25, "SIGXFSZ", NULL }, /* File size limit exceeded (4.2 BSD). */ + { 26, "SIGVTALRM", NULL }, /* Virtual alarm clock (4.2 BSD). */ + { 27, "SIGPROF", NULL }, /* Profiling alarm clock (4.2 BSD). */ + { 28, "SIGWINCH", NULL }, /* Window size change (4.3 BSD, Sun). */ + { 29, "SIGIO", NULL }, /* I/O now possible (4.2 BSD). */ + { 30, "SIGPWR", NULL }, /* Power failure restart (System V). */ + { 31, "SIGUNUSED", NULL }, + { 0, NULL, NULL }, +};