2
* Copyright (c) 2004 SuSE Linux AG
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program (see the file COPYING); if not, write to the
16
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24
#include <sys/ioctl.h>
25
#include <sys/types.h>
36
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
38
# define restrict __restrict__
42
#define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
44
static unsigned long int scan_one(const char* buff, const char *key)
46
const char *b = strstr(buff, key);
47
unsigned long val = 0;
51
fprintf(stderr, "ERROR: no hit for %s\n", key);
55
if (sscanf(b + strlen(key), " %lu", &val) != 1)
60
int read_proc(unsigned long int * const prcs_run, unsigned long int * const prcs_blked)
62
char StatBuf[2048], *ptr = &StatBuf[0];
63
unsigned long int running, blocked;
71
if ((stat = fopen("/proc/stat", "r")) == (FILE*)0) {
72
fprintf(stderr, "ERROR: could not open /proc/stat: %s\n", strerror(errno));
76
len = sizeof(StatBuf);
77
while ((len > 0) && (fgets(ptr, len, stat))) {
86
/* These fields are not present in /proc/stat for 2.4 kernels or GNU/kFreeBSD */
87
running = scan_one(StatBuf, "procs_running");
88
blocked = scan_one(StatBuf, "procs_blocked");
90
if (running == ~0UL || blocked == ~0UL)
94
*prcs_blked = blocked;
102
struct termios ltio, otio;
103
struct console *restrict next;
105
static struct console *restrict consoles;
106
static dev_t comparedev;
107
static char* scandev(DIR *dir)
109
char *name = (char*)0;
115
while ((dent = readdir(dir))) {
118
if (fstatat(fd, dent->d_name, &st, 0) < 0)
120
if (!S_ISCHR(st.st_mode))
122
if (comparedev != st.st_rdev)
124
if ((size_t)snprintf(path, sizeof(path), "/dev/%s", dent->d_name) >= sizeof(path))
126
name = realpath(path, NULL);
132
void detect_consoles(void)
135
if ((fc = fopen("/proc/consoles", "r"))) {
139
dir = opendir("/dev");
142
while ((fscanf(fc, "%*s %*s (%[^)]) %d:%d", &fbuf[0], &maj, &min) == 3)) {
143
struct console *restrict tail;
146
if (!strchr(fbuf, 'E'))
148
comparedev = makedev(maj, min);
154
if (posix_memalign((void*)&tail, sizeof(void*), alignof(typeof(struct console))) != 0)
155
perror("memory allocation");
157
tail->next = (struct console*)0;
163
consoles->next = tail;