Reindent to Linux KNF

Most of the code base seemed to follow Linux style, loosely.  This patch
brings it all together.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2018-08-05 18:54:27 +02:00
parent 53c4dd817d
commit f1f1ee3563
12 changed files with 1850 additions and 2200 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,23 +1,23 @@
/*
klogd.h - main header file for Linux kernel log daemon.
Copyright (c) 1995 Dr. G.W. Wettstein <greg@wind.rmcc.com>
This file is part of the sysklogd package, a kernel and system log daemon.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
* klogd.h - main header file for Linux kernel log daemon.
* Copyright (c) 1995 Dr. G.W. Wettstein <greg@wind.rmcc.com>
*
* This file is part of the sysklogd package, a kernel and system log daemon.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Symbols and definitions needed by klogd.
@ -33,8 +33,8 @@
/* Function prototypes. */
extern int InitKsyms(char *);
extern int InitMsyms(void);
extern char * ExpandKadds(char *, char *);
extern void SetParanoiaLevel(int);
extern void Syslog(int priority, char *fmt, ...);
extern int InitKsyms(char *);
extern int InitMsyms(void);
extern char *ExpandKadds(char *, char *);
extern void SetParanoiaLevel(int);
extern void Syslog(int priority, char *fmt, ...);

View File

@ -125,34 +125,31 @@
* the first hit.
*/
/* Includes. */
#include <stdlib.h>
#include <malloc.h>
#include <sys/utsname.h>
#include "klogd.h"
#include "ksyms.h"
#include "module.h"
#include <malloc.h>
#include <stdlib.h>
#include <sys/utsname.h>
#define VERBOSE_DEBUGGING 0
int num_syms = 0;
static int i_am_paranoid = 0;
static char vstring[12];
static struct sym_table *sym_array = (struct sym_table *) 0;
int num_syms = 0;
static int i_am_paranoid = 0;
static char vstring[12];
static struct sym_table *sym_array = (struct sym_table *)0;
static char *system_maps[] =
{
"/boot/System.map",
"/System.map",
"/usr/src/linux/System.map",
{
"/boot/System.map",
"/System.map",
"/usr/src/linux/System.map",
#if defined(TEST)
"./System.map",
"./System.map",
#endif
(char *) 0
};
(char *)0
};
#if defined(TEST)
int debugging;
@ -160,14 +157,12 @@ int debugging;
extern int debugging;
#endif
/* Function prototypes. */
static char * FindSymbolFile(void);
static int AddSymbol(unsigned long, char*);
static void FreeSymbols(void);
static int CheckVersion(char *);
static int CheckMapVersion(char *);
static char *FindSymbolFile(void);
static int AddSymbol(unsigned long, char *);
static void FreeSymbols(void);
static int CheckVersion(char *);
static int CheckMapVersion(char *);
/**************************************************************************
* Function: InitKsyms
@ -186,47 +181,35 @@ static int CheckMapVersion(char *);
* A boolean style context is returned. The return value will
* be true if initialization was successful. False if not.
**************************************************************************/
extern int InitKsyms(mapfile)
char *mapfile;
int InitKsyms(char *mapfile)
{
auto char type,
sym[512];
auto int version = 0;
auto unsigned long int address;
auto FILE *sym_file;
unsigned long int address;
FILE *sym_file;
char sym[512];
char type;
int version = 0;
/* Check and make sure that we are starting with a clean slate. */
if ( num_syms > 0 )
if (num_syms > 0)
FreeSymbols();
/*
* Search for and open the file containing the kernel symbols.
*/
if ( mapfile == (char *) 0 )
{
if ( (mapfile = FindSymbolFile()) == (char *) 0 )
{
if (mapfile == NULL) {
if ((mapfile = FindSymbolFile()) == NULL) {
Syslog(LOG_WARNING, "Cannot find a map file.");
if ( debugging )
if (debugging)
fputs("Cannot find a map file.\n", stderr);
return(0);
return (0);
}
}
if ( (sym_file = fopen(mapfile, "r")) == (FILE *) 0 )
{
if ((sym_file = fopen(mapfile, "r")) == NULL) {
Syslog(LOG_WARNING, "Cannot open map file: %s.", mapfile);
if ( debugging )
if (debugging)
fprintf(stderr, "Cannot open map file: %s.\n", mapfile);
return(0);
return (0);
}
/*
@ -237,53 +220,46 @@ extern int InitKsyms(mapfile)
* e-mail me a diff containing a parser with suitable political
* correctness -- GW.
*/
while ( !feof(sym_file) )
{
if ( fscanf(sym_file, "%lx %c %511s\n", &address, &type, sym)
!= 3 )
{
while (!feof(sym_file)) {
if (fscanf(sym_file, "%lx %c %511s\n", &address, &type, sym) != 3) {
Syslog(LOG_ERR, "Error in symbol table input (#1).");
fclose(sym_file);
return(0);
return (0);
}
if ( VERBOSE_DEBUGGING && debugging )
if (VERBOSE_DEBUGGING && debugging)
fprintf(stderr, "Address: %lx, Type: %c, Symbol: %s\n",
address, type, sym);
address, type, sym);
if ( AddSymbol(address, sym) == 0 )
{
if (AddSymbol(address, sym) == 0) {
Syslog(LOG_ERR, "Error adding symbol - %s.", sym);
fclose(sym_file);
return(0);
return (0);
}
if ( version == 0 )
if (version == 0)
version = CheckVersion(sym);
}
Syslog(LOG_INFO, "Loaded %d symbols from %s.", num_syms, mapfile);
switch ( version )
{
case -1:
switch (version) {
case -1:
Syslog(LOG_WARNING, "Symbols do not match kernel version.");
num_syms = 0;
break;
case 0:
Syslog(LOG_WARNING, "Cannot verify that symbols match " \
"kernel version.");
case 0:
Syslog(LOG_WARNING, "Cannot verify that symbols match "
"kernel version.");
break;
case 1:
case 1:
Syslog(LOG_INFO, "Symbols match kernel version %s.", vstring);
break;
}
fclose(sym_file);
return(1);
}
fclose(sym_file);
return (1);
}
/**************************************************************************
* Function: FindSymbolFile
@ -317,61 +293,53 @@ extern int InitKsyms(mapfile)
* caller which points to the name of the file containing
* the symbol table to be used.
**************************************************************************/
static char * FindSymbolFile()
static char *FindSymbolFile(void)
{
auto char *file = (char *) 0,
**mf = system_maps;
auto struct utsname utsname;
static char symfile[100];
struct utsname utsname;
FILE *sym_file = NULL;
char **mf = system_maps;
char *file = NULL;
auto FILE *sym_file = (FILE *) 0;
if (uname(&utsname) < 0) {
Syslog(LOG_ERR, "Cannot get kernel version information.");
return (0);
}
if ( uname(&utsname) < 0 )
{
Syslog(LOG_ERR, "Cannot get kernel version information.");
return(0);
}
if ( debugging )
if (debugging)
fputs("Searching for symbol map.\n", stderr);
for (mf = system_maps; *mf != (char *) 0 && file == (char *) 0; ++mf)
{
sprintf (symfile, "%s-%s", *mf, utsname.release);
if ( debugging )
for (mf = system_maps; *mf != (char *)0 && file == (char *)0; ++mf) {
sprintf(symfile, "%s-%s", *mf, utsname.release);
if (debugging)
fprintf(stderr, "Trying %s.\n", symfile);
if ( (sym_file = fopen(symfile, "r")) != (FILE *) 0 ) {
if ((sym_file = fopen(symfile, "r")) != (FILE *)0) {
if (CheckMapVersion(symfile) == 1)
file = symfile;
fclose (sym_file);
fclose(sym_file);
}
if (sym_file == (FILE *) 0 || file == (char *) 0) {
sprintf (symfile, "%s", *mf);
if ( debugging )
if (sym_file == (FILE *)0 || file == (char *)0) {
sprintf(symfile, "%s", *mf);
if (debugging)
fprintf(stderr, "Trying %s.\n", symfile);
if ( (sym_file = fopen(symfile, "r")) != (FILE *) 0 ) {
if ((sym_file = fopen(symfile, "r")) != (FILE *)0) {
if (CheckMapVersion(symfile) == 1)
file = symfile;
fclose (sym_file);
fclose(sym_file);
}
}
}
/*
* At this stage of the game we are at the end of the symbol
* tables.
*/
if ( debugging )
if (debugging)
fprintf(stderr, "End of search list encountered.\n");
return(file);
return (file);
}
/**************************************************************************
* Function: CheckVersion
*
@ -408,34 +376,25 @@ static char * FindSymbolFile()
* 1:-> The executing kernel is of the same version
* as the version string.
**************************************************************************/
static int CheckVersion(version)
char *version;
static int CheckVersion(char *version)
{
auto int vnum,
major,
minor,
patch;
static char *prefix = { "Version_" };
int vnum;
int major;
int minor;
int patch;
#ifndef TESTING
struct utsname utsname;
int kvnum;
auto struct utsname utsname;
#endif
static char *prefix = { "Version_" };
/* Early return if there is no hope. */
if ( strncmp(version, prefix, strlen(prefix)) == 0 /* ELF */ ||
(*version == '_' &&
strncmp(++version, prefix, strlen(prefix)) == 0 ) /* a.out */ )
if (strncmp(version, prefix, strlen(prefix)) == 0 /* ELF */ ||
(*version == '_' &&
strncmp(++version, prefix, strlen(prefix)) == 0) /* a.out */)
;
else
return(0);
return (0);
/*
* Since the symbol looks like a kernel version we can start
@ -446,11 +405,13 @@ static int CheckVersion(version)
patch = vnum & 0x000000FF;
minor = (vnum >> 8) & 0x000000FF;
major = (vnum >> 16) & 0x000000FF;
if ( debugging )
fprintf(stderr, "Version string = %s, Major = %d, " \
"Minor = %d, Patch = %d.\n", version +
strlen(prefix), major, minor, \
patch);
if (debugging)
fprintf(stderr, "Version string = %s, Major = %d, "
"Minor = %d, Patch = %d.\n",
version +
strlen(prefix),
major, minor,
patch);
sprintf(vstring, "%d.%d.%d", major, minor, patch);
#ifndef TESTING
@ -461,35 +422,32 @@ static int CheckVersion(version)
* values to determine if our system map matches the kernel
* version level.
*/
if ( uname(&utsname) < 0 )
{
if (uname(&utsname) < 0) {
Syslog(LOG_ERR, "Cannot get kernel version information.");
return(0);
return (0);
}
if ( debugging )
fprintf(stderr, "Comparing kernel %s with symbol table %s.\n",\
utsname.release, vstring);
if (debugging)
fprintf(stderr, "Comparing kernel %s with symbol table %s.\n",
utsname.release, vstring);
if ( sscanf (utsname.release, "%d.%d.%d", &major, &minor, &patch) < 3 )
{
if (sscanf(utsname.release, "%d.%d.%d", &major, &minor, &patch) < 3) {
Syslog(LOG_ERR, "Kernel send bogus release string `%s'.",
utsname.release);
return(0);
return (0);
}
/* Compute the version code from data sent by the kernel */
kvnum = (major << 16) | (minor << 8) | patch;
/* Failure. */
if ( vnum != kvnum )
return(-1);
if (vnum != kvnum)
return (-1);
/* Success. */
/* Success. */
#endif
return(1);
return (1);
}
/**************************************************************************
* Function: CheckMapVersion
*
@ -514,20 +472,15 @@ static int CheckVersion(version)
* 1:-> The executing kernel is of the same version
* as the version of the map file.
**************************************************************************/
static int CheckMapVersion(fname)
char *fname;
static int CheckMapVersion(char *fname)
{
int version;
FILE *sym_file;
auto unsigned long int address;
auto char type,
sym[512];
unsigned long int address;
FILE *sym_file;
char sym[512];
char type;
int version;
if ( (sym_file = fopen(fname, "r")) != (FILE *) 0 ) {
if ((sym_file = fopen(fname, "r")) != (FILE *)0) {
/*
* At this point a map file was successfully opened. We
* now need to search this file and look for version
@ -536,49 +489,46 @@ static int CheckMapVersion(fname)
Syslog(LOG_INFO, "Inspecting %s", fname);
version = 0;
while ( !feof(sym_file) && (version == 0) )
{
if ( fscanf(sym_file, "%lx %c %511s\n", &address, \
&type, sym) != 3 )
{
while (!feof(sym_file) && (version == 0)) {
if (fscanf(sym_file, "%lx %c %511s\n", &address,
&type, sym) != 3) {
Syslog(LOG_ERR, "Error in symbol table input (#2).");
fclose(sym_file);
return(0);
return (0);
}
if ( VERBOSE_DEBUGGING && debugging )
fprintf(stderr, "Address: %lx, Type: %c, " \
"Symbol: %s\n", address, type, sym);
if (VERBOSE_DEBUGGING && debugging)
fprintf(stderr, "Address: %lx, Type: %c, "
"Symbol: %s\n",
address, type, sym);
version = CheckVersion(sym);
}
fclose(sym_file);
switch ( version )
{
case -1:
Syslog(LOG_ERR, "Symbol table has incorrect " \
"version number.\n");
switch (version) {
case -1:
Syslog(LOG_ERR, "Symbol table has incorrect "
"version number.\n");
break;
case 0:
if ( debugging )
fprintf(stderr, "No version information " \
"found.\n");
case 0:
if (debugging)
fprintf(stderr, "No version information "
"found.\n");
break;
case 1:
if ( debugging )
fprintf(stderr, "Found table with " \
"matching version number.\n");
case 1:
if (debugging)
fprintf(stderr, "Found table with "
"matching version number.\n");
break;
}
return(version);
return (version);
}
return(0);
return (0);
}
/**************************************************************************
* Function: AddSymbol
*
@ -592,33 +542,24 @@ static int CheckMapVersion(fname)
* A boolean value is assumed. True if the addition is
* successful. False if not.
**************************************************************************/
static int AddSymbol(address, symbol)
unsigned long address;
char *symbol;
static int AddSymbol(unsigned long address, char *symbol)
{
/* Allocate the the symbol table entry. */
sym_array = (struct sym_table *) realloc(sym_array, (num_syms+1) * \
sizeof(struct sym_table));
if ( sym_array == (struct sym_table *) 0 )
return(0);
sym_array = realloc(sym_array, (num_syms + 1) * sizeof(struct sym_table));
if (sym_array == NULL)
return (0);
/* Then the space for the symbol. */
sym_array[num_syms].name = (char *) malloc(strlen(symbol)*sizeof(char)\
+ 1);
if ( sym_array[num_syms].name == (char *) 0 )
return(0);
sym_array[num_syms].name = malloc(strlen(symbol) * sizeof(char) + 1);
if (sym_array[num_syms].name == NULL)
return (0);
sym_array[num_syms].value = address;
strcpy(sym_array[num_syms].name, symbol);
++num_syms;
return(1);
return (1);
}
/**************************************************************************
* Function: LookupSymbol
*
@ -638,37 +579,27 @@ static int AddSymbol(address, symbol)
* If a match is found the pointer to the symbolic name most
* closely matching the address is returned.
**************************************************************************/
char * LookupSymbol(value, sym)
unsigned long value;
struct symbol *sym;
char *LookupSymbol(unsigned long value, struct symbol *sym)
{
auto int lp;
auto char *last;
auto char *name;
struct symbol ksym, msym;
char *last;
char *name;
int lp;
if (!sym_array)
return((char *) 0);
return ((char *)0);
last = sym_array[0].name;
ksym.offset = 0;
ksym.size = 0;
if ( value < sym_array[0].value )
return((char *) 0);
for(lp = 0; lp <= num_syms; ++lp)
{
if ( sym_array[lp].value > value )
{
ksym.offset = value - sym_array[lp-1].value;
ksym.size = sym_array[lp].value - \
sym_array[lp-1].value;
if (value < sym_array[0].value)
return ((char *)0);
for (lp = 0; lp <= num_syms; ++lp) {
if (sym_array[lp].value > value) {
ksym.offset = value - sym_array[lp - 1].value;
ksym.size = sym_array[lp].value -
sym_array[lp - 1].value;
break;
}
last = sym_array[lp].name;
@ -676,30 +607,24 @@ char * LookupSymbol(value, sym)
name = LookupModuleSymbol(value, &msym);
if ( ksym.offset == 0 && msym.offset == 0 )
{
return((char *) 0);
if (ksym.offset == 0 && msym.offset == 0) {
return NULL;
}
if ( ksym.offset == 0 || msym.offset < 0 ||
(ksym.offset > 0 && ksym.offset < msym.offset) )
{
if (ksym.offset == 0 || msym.offset < 0 ||
(ksym.offset > 0 && ksym.offset < msym.offset)) {
sym->offset = ksym.offset;
sym->size = ksym.size;
return(last);
}
else
{
return (last);
} else {
sym->offset = msym.offset;
sym->size = msym.size;
return(name);
return (name);
}
return((char *) 0);
return NULL;
}
/**************************************************************************
* Function: FreeSymbols
*
@ -713,24 +638,20 @@ char * LookupSymbol(value, sym)
* Return: void
**************************************************************************/
static void FreeSymbols()
static void FreeSymbols(void)
{
auto int lp;
int lp;
/* Free each piece of memory allocated for symbol names. */
for(lp = 0; lp < num_syms; ++lp)
for (lp = 0; lp < num_syms; ++lp)
free(sym_array[lp].name);
/* Whack the entire array and initialize everything. */
free(sym_array);
sym_array = (struct sym_table *) 0;
sym_array = (struct sym_table *)0;
num_syms = 0;
return;
}
/**************************************************************************
* Function: LogExpanded
*
@ -749,23 +670,16 @@ static void FreeSymbols()
* Return: void
**************************************************************************/
extern char * ExpandKadds(line, el)
char *line;
char *el;
char *ExpandKadds(char *line, char *el)
{
auto char dlm,
*kp,
*sl = line,
*elp = el,
*symbol;
unsigned long int value;
struct symbol sym;
char *symbol;
char *elp = el;
char *sl = line;
char *kp;
char num[15];
auto unsigned long int value;
auto struct symbol sym;
char dlm;
sym.offset = 0;
sym.size = 0;
@ -787,52 +701,47 @@ extern char * ExpandKadds(line, el)
* kernel whenever a module is loaded or unloaded. I am
* open for patches.
*/
if ( i_am_paranoid &&
(strstr(line, "Oops:") != (char *) 0) && !InitMsyms() )
if (i_am_paranoid &&
(strstr(line, "Oops:") != (char *)0) && !InitMsyms())
Syslog(LOG_WARNING, "Cannot load kernel module symbols.\n");
/*
* Early return if there do not appear to be any kernel
* messages in this line.
*/
if ( (num_syms == 0) ||
(kp = strstr(line, "[<")) == (char *) 0 )
{
if ((num_syms == 0) ||
(kp = strstr(line, "[<")) == (char *)0) {
strcpy(el, line);
return(el);
return (el);
}
/* Loop through and expand all kernel messages. */
do
{
while ( sl < kp+1 )
do {
while (sl < kp + 1)
*elp++ = *sl++;
/* Now poised at a kernel delimiter. */
if ( (kp = strstr(sl, ">]")) == (char *) 0 )
{
if ((kp = strstr(sl, ">]")) == (char *)0) {
strcpy(el, sl);
return(el);
return (el);
}
dlm = *kp;
strncpy(num,sl+1,kp-sl-1);
num[kp-sl-1] = '\0';
value = strtoul(num, (char **) 0, 16);
if ( (symbol = LookupSymbol(value, &sym)) == (char *) 0 )
strncpy(num, sl + 1, kp - sl - 1);
num[kp - sl - 1] = '\0';
value = strtoul(num, (char **)0, 16);
if ((symbol = LookupSymbol(value, &sym)) == (char *)0)
symbol = sl;
strcat(elp, symbol);
elp += strlen(symbol);
if ( debugging )
fprintf(stderr, "Symbol: %s = %lx = %s, %x/%d\n", \
sl+1, value, \
(sym.size==0) ? symbol+1 : symbol, \
sym.offset, sym.size);
if (debugging)
fprintf(stderr, "Symbol: %s = %lx = %s, %x/%d\n",
sl + 1, value,
(sym.size == 0) ? symbol + 1 : symbol,
sym.offset, sym.size);
value = 2;
if ( sym.size != 0 )
{
if (sym.size != 0) {
--value;
++kp;
elp += sprintf(elp, "+0x%x/0x%02x", sym.offset, sym.size);
@ -840,16 +749,14 @@ extern char * ExpandKadds(line, el)
strncat(elp, kp, value);
elp += value;
sl = kp + value;
if ( (kp = strstr(sl, "[<")) == (char *) 0 )
if ((kp = strstr(sl, "[<")) == (char *)0)
strcat(elp, sl);
}
while ( kp != (char *) 0);
if ( debugging )
fprintf(stderr, "Expanded line: %s\n", el);
return(el);
}
} while (kp != (char *)0);
if (debugging)
fprintf(stderr, "Expanded line: %s\n", el);
return (el);
}
/**************************************************************************
* Function: SetParanoiaLevel
@ -864,17 +771,12 @@ extern char * ExpandKadds(line, el)
* present when resolving kernel exceptions.
* Return: void
**************************************************************************/
extern void SetParanoiaLevel(level)
int level;
void SetParanoiaLevel(int level)
{
i_am_paranoid = level;
return;
}
/*
* Setting the -DTEST define enables the following code fragment to
* be compiled. This produces a small standalone program which will
@ -885,37 +787,30 @@ extern void SetParanoiaLevel(level)
#include <stdarg.h>
extern int main(int, char **);
extern int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
auto char line[1024], eline[2048];
char line[1024], eline[2048];
debugging = 1;
if ( !InitKsyms((char *) 0) )
{
if (!InitKsyms((char *)0)) {
fputs("ksym: Error loading system map.\n", stderr);
return(1);
return (1);
}
while ( !feof(stdin) )
{
while (!feof(stdin)) {
fgets(line, sizeof(line), stdin);
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; /* Trash NL char */
if (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0'; /* Trash NL char */
memset(eline, '\0', sizeof(eline));
ExpandKadds(line, eline);
fprintf(stdout, "%s\n", eline);
}
return(0);
return (0);
}
extern void Syslog(int priority, char *fmt, ...)
void Syslog(int priority, char *fmt, ...)
{
va_list ap;
@ -930,9 +825,8 @@ extern void Syslog(int priority, char *fmt, ...)
#endif
/**
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* tab-width: 8
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

View File

@ -103,34 +103,33 @@
* the first hit.
*/
/* Includes. */
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <malloc.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <malloc.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#ifdef SYSV
#include <fcntl.h>
#else
#include <sys/msgbuf.h>
#endif
#include <sys/stat.h>
#include "module.h"
#include <stdarg.h>
#include <paths.h>
#include <linux/version.h>
#include <paths.h>
#include <stdarg.h>
#include <sys/stat.h>
#include "klogd.h"
#include "ksyms.h"
#define KSYMS "/proc/kallsyms"
#define KSYMS "/proc/kallsyms"
static int num_modules = 0;
struct Module *sym_array_modules = (struct Module *) 0;
static int num_modules = 0;
struct Module *sym_array_modules = (struct Module *)0;
static int have_modules = 0;
@ -140,12 +139,11 @@ static int debugging = 1;
extern int debugging;
#endif
/* Function prototypes. */
static void FreeModules(void);
static int AddSymbol(const char *);
static void FreeModules(void);
static int AddSymbol(const char *);
struct Module *AddModule(const char *);
static int symsort(const void *, const void *);
static int symsort(const void *, const void *);
/* Imported from ksym.c */
extern int num_syms;
@ -166,49 +164,46 @@ extern int num_syms;
* True if loading is successful.
**************************************************************************/
extern int InitMsyms()
extern int InitMsyms(void)
{
auto int rtn,
tmp;
FILE *ksyms;
char buf[128];
char *p;
char buf[128];
int rtn;
int tmp;
/* Initialize the kernel module symbol table. */
FreeModules();
ksyms = fopen(KSYMS, "r");
if ( ksyms == NULL )
{
if ( errno == ENOENT )
if (ksyms == NULL) {
if (errno == ENOENT)
Syslog(LOG_INFO, "No module symbols loaded - "
"kernel modules not enabled.\n");
"kernel modules not enabled.\n");
else
Syslog(LOG_ERR, "Error loading kernel symbols " \
"- %s\n", strerror(errno));
return(0);
Syslog(LOG_ERR, "Error loading kernel symbols "
"- %s\n",
strerror(errno));
return (0);
}
if ( debugging )
if (debugging)
fprintf(stderr, "Loading kernel module symbols - "
"Source: %s\n", KSYMS);
"Source: %s\n",
KSYMS);
while ( fgets(buf, sizeof(buf), ksyms) != NULL )
{
while (fgets(buf, sizeof(buf), ksyms) != NULL) {
if (num_syms > 0 && index(buf, '[') == NULL)
continue;
p = index(buf, ' ');
if ( p == NULL )
if (p == NULL)
continue;
if ( buf[strlen(buf)-1] == '\n' )
buf[strlen(buf)-1] = '\0';
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
/* overlong lines will be ignored above */
AddSymbol(buf);
@ -219,45 +214,39 @@ extern int InitMsyms()
have_modules = 1;
/* Sort the symbol tables in each module. */
for (rtn = tmp = 0; tmp < num_modules; ++tmp)
{
for (rtn = tmp = 0; tmp < num_modules; ++tmp) {
rtn += sym_array_modules[tmp].num_syms;
if ( sym_array_modules[tmp].num_syms < 2 )
if (sym_array_modules[tmp].num_syms < 2)
continue;
qsort(sym_array_modules[tmp].sym_array, \
sym_array_modules[tmp].num_syms, \
qsort(sym_array_modules[tmp].sym_array,
sym_array_modules[tmp].num_syms,
sizeof(struct sym_table), symsort);
}
if ( rtn == 0 )
if (rtn == 0)
Syslog(LOG_INFO, "No module symbols loaded.");
else
Syslog(LOG_INFO, "Loaded %d %s from %d module%s", rtn, \
(rtn == 1) ? "symbol" : "symbols", \
Syslog(LOG_INFO, "Loaded %d %s from %d module%s", rtn,
(rtn == 1) ? "symbol" : "symbols",
num_modules, (num_modules == 1) ? "." : "s.");
return(1);
return (1);
}
static int symsort(p1, p2)
const void *p1;
const void *p2;
static int symsort(const void *p1, const void *p2)
{
auto const struct sym_table *sym1 = p1,
*sym2 = p2;
const struct sym_table *sym1 = p1,
*sym2 = p2;
if ( sym1->value < sym2->value )
return(-1);
if ( sym1->value == sym2->value )
return(0);
return(1);
if (sym1->value < sym2->value)
return (-1);
if (sym1->value == sym2->value)
return (0);
return (1);
}
/**************************************************************************
* Function: FreeModules
*
@ -268,44 +257,38 @@ static int symsort(p1, p2)
*
* Return: void
**************************************************************************/
static void FreeModules()
static void FreeModules(void)
{
auto int nmods,
nsyms;
auto struct Module *mp;
struct Module *mp;
int nmods;
int nsyms;
/* Check to see if the module symbol tables need to be cleared. */
have_modules = 0;
if ( num_modules == 0 )
if (num_modules == 0)
return;
if ( sym_array_modules == NULL )
if (sym_array_modules == NULL)
return;
for (nmods = 0; nmods < num_modules; ++nmods)
{
for (nmods = 0; nmods < num_modules; ++nmods) {
mp = &sym_array_modules[nmods];
if ( mp->num_syms == 0 )
if (mp->num_syms == 0)
continue;
for (nsyms= 0; nsyms < mp->num_syms; ++nsyms)
for (nsyms = 0; nsyms < mp->num_syms; ++nsyms)
free(mp->sym_array[nsyms].name);
free(mp->sym_array);
if ( mp->name != NULL )
if (mp->name != NULL)
free(mp->name);
}
free(sym_array_modules);
sym_array_modules = (struct Module *) 0;
sym_array_modules = (struct Module *)0;
num_modules = 0;
return;
}
/**************************************************************************
* Function: AddModule
*
@ -319,33 +302,21 @@ static void FreeModules()
* Return: struct Module *
**************************************************************************/
struct Module *AddModule(module)
const char *module;
struct Module *AddModule(const char *module)
{
struct Module *mp;
if ( num_modules == 0 )
{
if (num_modules == 0) {
sym_array_modules = (struct Module *)malloc(sizeof(struct Module));
if ( sym_array_modules == NULL )
{
if (sym_array_modules == NULL) {
Syslog(LOG_WARNING, "Cannot allocate Module array.\n");
return NULL;
}
mp = sym_array_modules;
}
else
{
} else {
/* Allocate space for the module. */
mp = (struct Module *) \
realloc(sym_array_modules, \
(num_modules+1) * sizeof(struct Module));
if ( mp == NULL )
{
mp = realloc(sym_array_modules, (num_modules + 1) * sizeof(struct Module));
if (mp == NULL) {
Syslog(LOG_WARNING, "Cannot allocate Module array.\n");
return NULL;
}
@ -358,7 +329,7 @@ struct Module *AddModule(module)
mp->sym_array = NULL;
mp->num_syms = 0;
if ( module != NULL )
if (module != NULL)
mp->name = strdup(module);
else
mp->name = NULL;
@ -366,7 +337,6 @@ struct Module *AddModule(module)
return mp;
}
/**************************************************************************
* Function: AddSymbol
*
@ -387,78 +357,68 @@ struct Module *AddModule(module)
* A boolean value is assumed. True if the addition is
* successful. False if not.
**************************************************************************/
static int AddSymbol(line)
const char *line;
static int AddSymbol(const char *line)
{
char *module;
unsigned long address;
char *p;
static char *lastmodule = NULL;
struct Module *mp;
unsigned long address;
char *module;
char *p;
module = index(line, '[');
if ( module != NULL )
{
if (module != NULL) {
p = index(module, ']');
if ( p != NULL )
if (p != NULL)
*p = '\0';
p = module++;
while ( isspace(*(--p)) );
while (isspace(*(--p)))
;
*(++p) = '\0';
}
p = index(line, ' ');
if ( p == NULL )
return(0);
if (p == NULL)
return (0);
*p = '\0';
address = strtoul(line, (char **) 0, 16);
address = strtoul(line, (char **)0, 16);
p += 3;
if ( num_modules == 0 ||
( lastmodule == NULL && module != NULL ) ||
( module == NULL && lastmodule != NULL) ||
( module != NULL && strcmp(module, lastmodule)))
{
if (num_modules == 0 ||
(lastmodule == NULL && module != NULL) ||
(module == NULL && lastmodule != NULL) ||
(module != NULL && strcmp(module, lastmodule))) {
mp = AddModule(module);
if ( mp == NULL )
return(0);
}
else
mp = &sym_array_modules[num_modules-1];
if (mp == NULL)
return (0);
} else
mp = &sym_array_modules[num_modules - 1];
lastmodule = mp->name;
/* Allocate space for the symbol table entry. */
mp->sym_array = (struct sym_table *) realloc(mp->sym_array, \
(mp->num_syms+1) * sizeof(struct sym_table));
mp->sym_array = (struct sym_table *)realloc(mp->sym_array,
(mp->num_syms + 1) * sizeof(struct sym_table));
if ( mp->sym_array == (struct sym_table *) 0 )
return(0);
if (mp->sym_array == (struct sym_table *)0)
return (0);
mp->sym_array[mp->num_syms].name = strdup(p);
if ( mp->sym_array[mp->num_syms].name == (char *) 0 )
return(0);
if (mp->sym_array[mp->num_syms].name == (char *)0)
return (0);
/* Stuff interesting information into the module. */
mp->sym_array[mp->num_syms].value = address;
++mp->num_syms;
return(1);
return (1);
}
/**************************************************************************
* Function: LookupModuleSymbol
*
@ -478,73 +438,58 @@ static int AddSymbol(line)
* If a match is found the pointer to the symbolic name most
* closely matching the address is returned.
**************************************************************************/
extern char * LookupModuleSymbol(value, sym)
unsigned long value;
struct symbol *sym;
char *LookupModuleSymbol(unsigned long value, struct symbol *sym)
{
auto int nmod,
nsym;
auto struct sym_table *last;
auto struct Module *mp;
static char ret[100];
struct sym_table *last;
struct Module *mp;
int nmod;
int nsym;
sym->size = 0;
sym->offset = 0;
if ( num_modules == 0 )
return((char *) 0);
for (nmod = 0; nmod < num_modules; ++nmod)
{
if (num_modules == 0)
return NULL;
for (nmod = 0; nmod < num_modules; ++nmod) {
mp = &sym_array_modules[nmod];
/*
* Run through the list of symbols in this module and
* see if the address can be resolved.
*/
for(nsym = 1, last = &mp->sym_array[0];
nsym < mp->num_syms;
++nsym)
{
if ( mp->sym_array[nsym].value > value )
{
if ( sym->size == 0 ||
(value - last->value) < sym->offset ||
( (sym->offset == (value - last->value)) &&
(mp->sym_array[nsym].value-last->value) < sym->size ) )
{
sym->offset = value - last->value;
sym->size = mp->sym_array[nsym].value - \
last->value;
ret[sizeof(ret)-1] = '\0';
if ( mp->name == NULL )
snprintf(ret, sizeof(ret)-1,
"%s", last->name);
else
snprintf(ret, sizeof(ret)-1,
"%s:%s", mp->name, last->name);
}
break;
for (nsym = 1, last = &mp->sym_array[0];
nsym < mp->num_syms;
++nsym) {
if (mp->sym_array[nsym].value > value) {
if (sym->size == 0 ||
(value - last->value) < sym->offset ||
((sym->offset == (value - last->value)) &&
(mp->sym_array[nsym].value - last->value) < sym->size)) {
sym->offset = value - last->value;
sym->size = mp->sym_array[nsym].value -
last->value;
ret[sizeof(ret) - 1] = '\0';
if (mp->name == NULL)
snprintf(ret, sizeof(ret) - 1,
"%s", last->name);
else
snprintf(ret, sizeof(ret) - 1,
"%s:%s", mp->name, last->name);
}
break;
}
last = &mp->sym_array[nsym];
}
}
if ( sym->size > 0 )
return(ret);
if (sym->size > 0)
return (ret);
/* It has been a hopeless exercise. */
return((char *) 0);
return NULL;
}
/*
* Setting the -DTEST define enables the following code fragment to
* be compiled. This produces a small standalone program which will
@ -554,52 +499,38 @@ extern char * LookupModuleSymbol(value, sym)
#include <stdarg.h>
extern int main(int, char **);
int main(argc, argv)
int argc;
char *argv[];
int main(int argc, char *argv[])
{
auto int lp, syms;
int lp, syms;
if ( !InitMsyms() )
{
if (!InitMsyms()) {
fprintf(stderr, "Cannot load module symbols.\n");
return(1);
return (1);
}
printf("Number of modules: %d\n\n", num_modules);
for(lp= 0; lp < num_modules; ++lp)
{
printf("Module #%d = %s, Number of symbols = %d\n", lp + 1, \
for (lp = 0; lp < num_modules; ++lp) {
printf("Module #%d = %s, Number of symbols = %d\n", lp + 1,
sym_array_modules[lp].name == NULL
?"kernel space"
:sym_array_modules[lp].name, \
? "kernel space"
: sym_array_modules[lp].name,
sym_array_modules[lp].num_syms);
for (syms= 0; syms < sym_array_modules[lp].num_syms; ++syms)
{
for (syms = 0; syms < sym_array_modules[lp].num_syms; ++syms) {
printf("\tSymbol #%d\n", syms + 1);
printf("\tName: %s\n", \
printf("\tName: %s\n",
sym_array_modules[lp].sym_array[syms].name);
printf("\tAddress: %lx\n\n", \
printf("\tAddress: %lx\n\n",
sym_array_modules[lp].sym_array[syms].value);
}
}
FreeModules();
return(0);
return (0);
}
extern void Syslog(int priority, char *fmt, ...)
void Syslog(int priority, char *fmt, ...)
{
va_list ap;
@ -615,9 +546,8 @@ extern void Syslog(int priority, char *fmt, ...)
#endif /* TEST */
/**
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* tab-width: 8
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

View File

@ -1,29 +1,28 @@
/*
ksym.h - Definitions for symbol table utilities.
Copyright (c) 1995, 1996 Dr. G.W. Wettstein <greg@wind.rmcc.com>
Copyright (c) 1996 Enjellic Systems Development
This file is part of the sysklogd package, a kernel and system log daemon.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
* ksym.h - Definitions for symbol table utilities.
* Copyright (c) 1995, 1996 Dr. G.W. Wettstein <greg@wind.rmcc.com>
* Copyright (c) 1996 Enjellic Systems Development
*
* This file is part of the sysklogd package, a kernel and system log daemon.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Variables, structures and type definitions static to this module. */
struct symbol
{
struct symbol {
char *name;
int size;
int offset;
@ -31,5 +30,5 @@ struct symbol
/* Function prototypes. */
extern char * LookupSymbol(unsigned long, struct symbol *);
extern char * LookupModuleSymbol(unsigned long int, struct symbol *);
extern char *LookupSymbol(unsigned long, struct symbol *);
extern char *LookupModuleSymbol(unsigned long int, struct symbol *);

View File

@ -1,24 +1,24 @@
/*
module.h - Miscellaneous module definitions
Copyright (c) 1996 Richard Henderson <rth@tamu.edu>
Copyright (c) 2004-7 Martin Schulze <joey@infodrom.org>
This file is part of the sysklogd package.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
* module.h - Miscellaneous module definitions
* Copyright (c) 1996 Richard Henderson <rth@tamu.edu>
* Copyright (c) 2004-7 Martin Schulze <joey@infodrom.org>
*
* This file is part of the sysklogd package.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* ChangeLog:
*
@ -35,17 +35,15 @@
* ksym_mod.c over here.
*/
struct sym_table
{
unsigned long value;
char *name;
struct sym_table {
unsigned long value;
char *name;
};
struct Module
{
struct Module {
struct sym_table *sym_array;
int num_syms;
int num_syms;
char *name;
char *name;
};

View File

@ -29,15 +29,15 @@
cat /proc/oops Display current log level and last oops time
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#define MODNAME "oops"
#define PROCNAME "oops"
#define MODNAME "oops"
#define PROCNAME "oops"
MODULE_AUTHOR("Martin Schulze <joey@infodrom.org>");
MODULE_DESCRIPTION("Oops module from klogd");
@ -46,48 +46,49 @@ MODULE_LICENSE("GPL");
static DEFINE_MUTEX(oops_lock);
struct oops_t {
unsigned long lastoops;
int loglevel;
unsigned long lastoops;
int loglevel;
};
static struct oops_t oops_data;
static int procflag = 0;
struct code {
char *name;
int level;
char *name;
int level;
};
struct code priorities[] = {
{"emerg", 0},
{"panic", 0}, /* DEPRECATED */
{"alert", 1},
{"crit", 2},
{"err", 3},
{"error", 3}, /* DEPRECATED */
{"warning", 4},
{"warn", 4}, /* DEPRECATED */
{"notice", 5},
{"info", 6},
{"debug", 7},
{NULL, -1}
{ "emerg", 0 },
{ "panic", 0 }, /* DEPRECATED */
{ "alert", 1 },
{ "crit", 2 },
{ "err", 3 },
{ "error", 3 }, /* DEPRECATED */
{ "warning", 4 },
{ "warn", 4 }, /* DEPRECATED */
{ "notice", 5 },
{ "info", 6 },
{ "debug", 7 },
{ NULL, -1 }
};
void oops_decode_level (char *line)
void oops_decode_level(char *line)
{
char *p;
struct code *prio;
struct code *prio;
char *p;
if (strncmp(line, "level:", 6))
return;
if (strncmp(line, "level:", 6))
return;
for (p = (char *)(line) + 6;*p == ' ' || *p == '\t';p++);
for (p = (char *)(line) + 6; *p == ' ' || *p == '\t'; p++)
;
for (prio = priorities; prio->name; prio++)
if (!strcmp(p, prio->name)) {
oops_data.loglevel = prio->level;
return;
}
for (prio = priorities; prio->name; prio++)
if (!strcmp(p, prio->name)) {
oops_data.loglevel = prio->level;
return;
}
}
/*
@ -95,156 +96,160 @@ void oops_decode_level (char *line)
*/
static void oops(void)
{
auto unsigned long *p = (unsigned long *) 828282828;
unsigned long *p = (unsigned long *)828282828;
*p = 5;
return;
}
static int oops_proc_open (struct inode *inode, struct file *file)
static int oops_proc_open(struct inode *inode, struct file *file)
{
#ifdef DEBUG
printk (KERN_DEBUG "oops_proc_open().\n");
printk(KERN_DEBUG "oops_proc_open().\n");
#endif
return 0;
return 0;
}
static ssize_t
oops_proc_read (struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
oops_proc_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
char s[70];
int size;
struct code *prio;
char *level = NULL;
struct code *prio;
char *level = NULL;
char s[70];
int size;
#ifdef DEBUG
printk (KERN_DEBUG "oops_proc_read(%d).\n",nbytes);
printk(KERN_DEBUG "oops_proc_read(%d).\n", nbytes);
#endif
if (procflag) {
procflag = 0;
return 0;
}
if (procflag) {
procflag = 0;
return 0;
}
for (prio = priorities;
prio->name && prio->level != oops_data.loglevel;
prio++);
level = prio->name;
for (prio = priorities;
prio->name && prio->level != oops_data.loglevel;
prio++)
;
level = prio->name;
if (oops_data.lastoops == 0)
size = sprintf (s, "Log level: %s\nLast oops: none\n", level);
else {
unsigned long now = get_seconds();
unsigned long delta = now - oops_data.lastoops;
size = sprintf (s, "Log level: %s\nLast oops: %lu (%lu second%s ago)\n",
level, oops_data.lastoops,
delta, delta == 1 ? "" : "s");
}
if (oops_data.lastoops == 0)
size = sprintf(s, "Log level: %s\nLast oops: none\n", level);
else {
unsigned long now = get_seconds();
unsigned long delta = now - oops_data.lastoops;
size = sprintf(s, "Log level: %s\nLast oops: %lu (%lu second%s ago)\n",
level, oops_data.lastoops,
delta, delta == 1 ? "" : "s");
}
if (size < nbytes)
nbytes = size;
if (size < nbytes)
nbytes = size;
if (copy_to_user(buf, s, nbytes))
return -EFAULT;
if (copy_to_user(buf, s, nbytes))
return -EFAULT;
*ppos += nbytes;
*ppos += nbytes;
procflag++;
procflag++;
return nbytes;
return nbytes;
}
static int
oops_proc_release(struct inode *inode, struct file *filp)
{
#ifdef DEBUG
printk (KERN_DEBUG "oops_proc_release().\n");
printk(KERN_DEBUG "oops_proc_release().\n");
#endif
return 0;
return 0;
}
static ssize_t
oops_proc_write(struct file *file, const char __user *buf,
size_t nbytes, loff_t *ppos)
{
char input[100];
int len;
char input[100];
int len;
#ifdef DEBUG
printk (KERN_DEBUG "oops_proc_write(%d).\n", nbytes);
printk(KERN_DEBUG "oops_proc_write(%d).\n", nbytes);
#endif
len = nbytes >= sizeof(input) ? sizeof(input)-1 : nbytes;
len = nbytes >= sizeof(input) ? sizeof(input) - 1 : nbytes;
if (copy_from_user(input, buf, len))
return -EFAULT;
if (copy_from_user(input, buf, len))
return -EFAULT;
input[len] = '\0';
if (input[len-1] == '\n')
input[len-1] = '\0';
input[len] = '\0';
if (input[len - 1] == '\n')
input[len - 1] = '\0';
if (!strncmp(input, "level:", 6))
oops_decode_level(input);
else if (!strcmp(input, "oops")) {
oops_data.lastoops = get_seconds();
oops();
} else
printk ("<%d>%s\n", oops_data.loglevel, input);
if (!strncmp(input, "level:", 6))
oops_decode_level(input);
else if (!strcmp(input, "oops")) {
oops_data.lastoops = get_seconds();
oops();
} else
printk("<%d>%s\n", oops_data.loglevel, input);
return nbytes;
return nbytes;
}
static const struct file_operations oops_proc_operations = {
.read = oops_proc_read,
.release = oops_proc_release,
.write = oops_proc_write,
.open = oops_proc_open,
.read = oops_proc_read,
.release = oops_proc_release,
.write = oops_proc_write,
.open = oops_proc_open,
};
void oops_proc_add (void)
void oops_proc_add(void)
{
struct proc_dir_entry *entry;
struct proc_dir_entry *entry;
mutex_lock (&oops_lock);
mutex_lock(&oops_lock);
entry = create_proc_entry (PROCNAME, 0, NULL);
entry = create_proc_entry(PROCNAME, 0, NULL);
if (entry) {
entry->proc_fops = &oops_proc_operations;
}
if (entry) {
entry->proc_fops = &oops_proc_operations;
}
mutex_unlock (&oops_lock);
mutex_unlock(&oops_lock);
}
void oops_proc_remove (void)
void oops_proc_remove(void)
{
mutex_lock (&oops_lock);
mutex_lock(&oops_lock);
remove_proc_entry(PROCNAME, NULL);
remove_proc_entry(PROCNAME, NULL);
mutex_unlock(&oops_lock);
mutex_unlock(&oops_lock);
}
int oops_init (void)
int oops_init(void)
{
printk (KERN_INFO "Loading module " MODNAME ".\n");
printk(KERN_INFO "Loading module " MODNAME ".\n");
oops_data.lastoops = 0;
oops_data.loglevel = 5;
oops_data.lastoops = 0;
oops_data.loglevel = 5;
oops_proc_add();
oops_proc_add();
return 0;
return 0;
}
void oops_cleanup (void)
void oops_cleanup(void)
{
oops_proc_remove();
oops_proc_remove();
printk (KERN_INFO "Removing module " MODNAME ".\n");
printk(KERN_INFO "Removing module " MODNAME ".\n");
}
module_init(oops_init);
module_exit(oops_cleanup);
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

View File

@ -1,37 +1,37 @@
/*
pidfile.c - interact with pidfiles
Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>
This file is part of the sysklogd package, a kernel and system log daemon.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
*/
* pidfile.c - interact with pidfiles
* Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>
*
* This file is part of the sysklogd package, a kernel and system log daemon.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
*/
/*
* Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze
* First version (v0.2) released
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
/* read_pid
*
@ -39,16 +39,19 @@
* 0 is returned if either there's no pidfile, it's empty
* or no pid can be read.
*/
int read_pid (char *pidfile)
int read_pid(char *pidfile)
{
FILE *f;
int pid;
FILE *fp;
int pid;
if (!(f=fopen(pidfile,"r")))
return 0;
fscanf(f,"%d", &pid);
fclose(f);
return pid;
fp = fopen(pidfile, "r");
if (!fp)
return 0;
fscanf(fp, "%d", &pid);
fclose(fp);
return pid;
}
/* check_pid
@ -57,24 +60,24 @@ int read_pid (char *pidfile)
* table (using /proc) to determine if the process already exists. If
* so 1 is returned, otherwise 0.
*/
int check_pid (char *pidfile)
int check_pid(char *pidfile)
{
int pid = read_pid(pidfile);
int pid = read_pid(pidfile);
/* Amazing ! _I_ am already holding the pid file... */
if ((!pid) || (pid == getpid ()))
return 0;
/* Amazing ! _I_ am already holding the pid file... */
if ((!pid) || (pid == getpid()))
return 0;
/*
* The 'standard' method of doing this is to try and do a 'fake' kill
* of the process. If an ESRCH error is returned the process cannot
* be found -- GW
*/
/* But... errno is usually changed only on error.. */
if (kill(pid, 0) && errno == ESRCH)
return(0);
/*
* The 'standard' method of doing this is to try and do a 'fake' kill
* of the process. If an ESRCH error is returned the process cannot
* be found -- GW
*/
/* But... errno is usually changed only on error.. */
if (kill(pid, 0) && errno == ESRCH)
return (0);
return pid;
return pid;
}
/* write_pid
@ -82,41 +85,40 @@ int check_pid (char *pidfile)
* Writes the pid to the specified file. If that fails 0 is
* returned, otherwise the pid.
*/
int write_pid (char *pidfile)
int write_pid(char *pidfile)
{
FILE *f;
int fd;
int pid;
FILE *fp;
int fd;
int pid;
if ( ((fd = open(pidfile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1)
|| ((f = fdopen(fd, "r+")) == NULL) ) {
fprintf(stderr, "Can't open or create %s.\n", pidfile);
return 0;
}
if (((fd = open(pidfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) || ((fp = fdopen(fd, "r+")) == NULL)) {
fprintf(stderr, "Can't open or create %s.\n", pidfile);
return 0;
}
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
fscanf(f, "%d", &pid);
fclose(f);
printf("Can't lock, lock is held by pid %d.\n", pid);
return 0;
}
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
fscanf(fp, "%d", &pid);
fclose(fp);
printf("Can't lock, lock is held by pid %d.\n", pid);
return 0;
}
pid = getpid();
if (!fprintf(f,"%d\n", pid)) {
printf("Can't write pid , %s.\n", strerror(errno));
close(fd);
return 0;
}
fflush(f);
pid = getpid();
if (!fprintf(fp, "%d\n", pid)) {
printf("Can't write pid , %s.\n", strerror(errno));
close(fd);
return 0;
}
fflush(fp);
if (flock(fd, LOCK_UN) == -1) {
printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
close(fd);
return 0;
}
close(fd);
if (flock(fd, LOCK_UN) == -1) {
printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
close(fd);
return 0;
}
close(fd);
return pid;
return pid;
}
/* touch_pid
@ -124,9 +126,9 @@ int write_pid (char *pidfile)
* Touches the specified pidfile f.ex. when receiving a SIGHUP
* The result from utimensat() is returned
*/
int touch_pid (char *pidfile)
int touch_pid(char *pidfile)
{
return utimensat(0, pidfile, NULL, 0);
return utimensat(0, pidfile, NULL, 0);
}
/* remove_pid
@ -134,8 +136,14 @@ int touch_pid (char *pidfile)
* Remove the the specified file. The result from unlink(2)
* is returned
*/
int remove_pid (char *pidfile)
int remove_pid(char *pidfile)
{
return unlink (pidfile);
return unlink(pidfile);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

View File

@ -1,23 +1,23 @@
/*
pidfile.h - interact with pidfiles
Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>
This file is part of the sysklogd package, a kernel and system log daemon.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
* pidfile.h - interact with pidfiles
* Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>
*
* This file is part of the sysklogd package, a kernel and system log daemon.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
/* read_pid
*

View File

@ -52,32 +52,31 @@ static char sccsid[] = "@(#)syslog.c 5.28 (Berkeley) 6/27/90";
* reconnect klogd to the logger after it went away.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <paths.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <netdb.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <paths.h>
#include <stdio.h>
#include <fcntl.h>
#define _PATH_LOGNAME "/dev/log"
#define _PATH_LOGNAME "/dev/log"
static int LogFile = -1; /* fd for log */
static int connected; /* have done connect */
static int LogStat = 0; /* status bits, set by openlog() */
static const char *LogTag = "syslog"; /* string to tag the entry with */
static int LogFacility = LOG_USER; /* default facility code */
static int LogFile = -1; /* fd for log */
static int connected; /* have done connect */
static int LogStat = 0; /* status bits, set by openlog() */
static const char *LogTag = "syslog"; /* string to tag the entry with */
static int LogFacility = LOG_USER; /* default facility code */
void
syslog(int pri, const char *fmt, ...)
void syslog(int pri, const char *fmt, ...)
{
va_list ap;
@ -86,23 +85,19 @@ syslog(int pri, const char *fmt, ...)
va_end(ap);
}
void
vsyslog(pri, fmt, ap)
int pri;
const char *fmt;
va_list ap;
void vsyslog(int pri, const char *fmt, va_list ap)
{
register int cnt;
register char *p;
int cnt;
char * p;
time_t now;
int fd, saved_errno;
int result;
char tbuf[2048], fmt_cpy[1024], *stdp = (char *) 0;
int fd, saved_errno;
int result;
char tbuf[2048], fmt_cpy[1024], *stdp = NULL;
saved_errno = errno;
/* see if we should just throw out this message */
if (!LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
if (!LOG_MASK(LOG_PRI(pri)) || (pri & ~(LOG_PRIMASK | LOG_FACMASK)))
return;
if (LogFile < 0 || !connected)
openlog(LogTag, LogStat | LOG_NDELAY, LogFacility);
@ -114,16 +109,19 @@ vsyslog(pri, fmt, ap)
/* build the message */
(void)time(&now);
(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
for (p = tbuf; *p; ++p);
for (p = tbuf; *p; ++p)
;
if (LogStat & LOG_PERROR)
stdp = p;
if (LogTag) {
(void)strcpy(p, LogTag);
for (; *p; ++p);
for (; *p; ++p)
;
}
if (LogStat & LOG_PID) {
(void)sprintf(p, "[%d]", getpid());
for (; *p; ++p);
for (; *p; ++p)
;
}
if (LogTag) {
*p++ = ':';
@ -132,18 +130,18 @@ vsyslog(pri, fmt, ap)
/* substitute error message for %m */
{
register char ch, *t1, *t2;
char ch, *t1, *t2;
char *strerror();
for (t1 = fmt_cpy;
(ch = *fmt) != '\0' && t1<fmt_cpy+sizeof(fmt_cpy);
(ch = *fmt) != '\0' && t1 < fmt_cpy + sizeof(fmt_cpy);
++fmt)
if (ch == '%' && fmt[1] == 'm') {
++fmt;
for (t2 = strerror(saved_errno);
(*t1 = *t2++); ++t1);
}
else
(*t1 = *t2++); ++t1)
;
} else
*t1++ = ch;
*t1 = '\0';
}
@ -155,7 +153,7 @@ vsyslog(pri, fmt, ap)
/* output to stderr if requested */
if (LogStat & LOG_PERROR) {
struct iovec iov[2];
register struct iovec *v = iov;
struct iovec *v = iov;
v->iov_base = stdp;
v->iov_len = cnt - (stdp - tbuf);
@ -168,21 +166,20 @@ vsyslog(pri, fmt, ap)
/* output the message to the local logger */
result = write(LogFile, tbuf, cnt + 1);
if (result == -1
&& (errno == ECONNRESET || errno == ENOTCONN || errno == ECONNREFUSED)) {
if (result == -1 && (errno == ECONNRESET || errno == ENOTCONN || errno == ECONNREFUSED)) {
closelog();
openlog(LogTag, LogStat | LOG_NDELAY, LogFacility);
result = write(LogFile, tbuf, cnt + 1);
}
if (result >= 0 || !(LogStat&LOG_CONS))
if (result >= 0 || !(LogStat & LOG_CONS))
return;
/*
* output the message to the console; don't worry about
* blocking, if console blocks everything will.
*/
if ((fd = open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY, 0)) < 0)
if ((fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY, 0)) < 0)
return;
(void)strcat(tbuf, "\r\n");
cnt += 2;
@ -192,24 +189,21 @@ vsyslog(pri, fmt, ap)
}
#ifndef TESTING
static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
#endif
/*
* OPENLOG -- open system log
*/
void
openlog(ident, logstat, logfac)
const char *ident;
int logstat, logfac;
void openlog(const char *ident, int logstat, int logfac)
{
if (ident != NULL)
LogTag = ident;
LogStat = logstat;
#ifdef ALLOW_KERNEL_LOGGING
if ((logfac &~ LOG_FACMASK) == 0)
if ((logfac & ~LOG_FACMASK) == 0)
#else
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
if (logfac != 0 && (logfac & ~LOG_FACMASK) == 0)
#endif
LogFacility = logfac;
@ -217,17 +211,16 @@ openlog(ident, logstat, logfac)
if (LogFile == -1) {
SyslogAddr.sa_family = AF_UNIX;
strncpy(SyslogAddr.sa_data, _PATH_LOGNAME,
sizeof(SyslogAddr.sa_data));
sizeof(SyslogAddr.sa_data));
if (LogStat & LOG_NDELAY) {
LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
/* fcntl(LogFile, F_SETFD, 1); */
/* fcntl(LogFile, F_SETFD, 1); */
}
}
if (LogFile != -1 && !connected &&
connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family)+
strlen(SyslogAddr.sa_data)) != -1)
connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family) + strlen(SyslogAddr.sa_data)) != -1)
#else
LogFile = fileno(stdout);
LogFile = fileno(stdout);
#endif
connected = 1;
}
@ -235,23 +228,20 @@ openlog(ident, logstat, logfac)
/*
* CLOSELOG -- close the system log
*/
void
closelog()
void closelog()
{
#ifndef TESTING
(void) close(LogFile);
(void)close(LogFile);
#endif
LogFile = -1;
connected = 0;
}
static int LogMask = 0xff; /* mask of priorities to be logged */
static int LogMask = 0xff; /* mask of priorities to be logged */
/*
* SETLOGMASK -- set the log mask level
*/
int
setlogmask(pmask)
int pmask;
int setlogmask(int pmask)
{
int omask;
@ -260,3 +250,10 @@ setlogmask(pmask)
LogMask = pmask;
return (omask);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

View File

@ -17,47 +17,38 @@
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/param.h>
extern int main(int, char **);
int main(int argc, char *argv[])
{
auto char *nl,
bufr[512];
auto int logged = 0;
char *nl,
char bufr[512];
int logged = 0;
openlog("DOTEST", LOG_PID, LOG_DAEMON);
if (argc > 1)
{
if ( (*argv[1] == '-') && (*(argv[1]+1) == '\0') )
{
if (argc > 1) {
if ((*argv[1] == '-') && (*(argv[1] + 1) == '\0')) {
while (!feof(stdin))
if ( fgets(bufr, sizeof(bufr), stdin) != \
(char *) 0 )
{
if ( (nl = strrchr(bufr, '\n')) != \
(char *) 0)
if (fgets(bufr, sizeof(bufr), stdin) !=
(char *)0) {
if ((nl = strrchr(bufr, '\n')) !=
(char *)0)
*nl = '\0';
syslog(LOG_INFO, "%s", bufr);
syslog(LOG_INFO, "%s", bufr);
logged += strlen(bufr);
if ( logged > 1024 )
{
if (logged > 1024) {
sleep(1);
logged = 0;
}
}
}
else
} else
while (argc-- > 1)
syslog(LOG_INFO, "%s", argv++[1]);
}
else
{
syslog(LOG_INFO, "%s", argv++ [1]);
} else {
syslog(LOG_EMERG, "EMERG log.");
syslog(LOG_ALERT, "Alert log.");
syslog(LOG_CRIT, "Critical log.");
@ -67,8 +58,15 @@ int main(int argc, char *argv[])
syslog(LOG_INFO, "Info log.");
syslog(LOG_DEBUG, "Debug log.");
closelog();
return(0);
return (0);
}
return(0);
return (0);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/

File diff suppressed because it is too large Load Diff