1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
|
/*
klogd.c - main program 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.
*/
#include "config.h"
#ifdef FEATURE_KLOGD
/*
* Steve Lord (lord@cray.com) 7th Nov 92
*
* Modified to check for kernel info by Dr. G.W. Wettstein 02/17/93.
*
* Fri Mar 12 16:53:56 CST 1993: Dr. Wettstein
* Modified LogLine to use a newline as the line separator in
* the kernel message buffer.
*
* Added debugging code to dump the contents of the kernel message
* buffer at the start of the LogLine function.
*
* Thu Jul 29 11:40:32 CDT 1993: Dr. Wettstein
* Added syscalls to turn off logging of kernel messages to the
* console when klogd becomes responsible for kernel messages.
*
* klogd now catches SIGTERM and SIGKILL signals. Receipt of these
* signals cases the clean_up function to be called which shuts down
* kernel logging and re-enables logging of messages to the console.
*
* Sat Dec 11 11:54:22 CST 1993: Dr. Wettstein
* Added fixes to allow compilation with no complaints with -Wall.
*
* When the daemon catches a fatal signal (SIGTERM, SIGKILL) a
* message is output to the logfile advising that the daemon is
* going to terminate.
*
* Thu Jan 6 11:54:10 CST 1994: Dr. Wettstein
* Major re-write/re-organization of the code.
*
* Klogd now assigns kernel messages to priority levels when output
* to the syslog facility is requested. The priority level is
* determined by decoding the prioritization sequence which is
* tagged onto the start of the kernel messages.
*
* Added the following program options: -f arg -c arg -s -o -d
*
* The -f switch can be used to specify that output should
* be written to the named file.
*
* The -c switch is used to specify the level of kernel
* messages which are to be directed to the console.
*
* The -s switch causes the program to use the syscall
* interface to the kernel message facility. This can be
* used to override the presence of the /proc filesystem.
*
* The -o switch causes the program to operate in 'one-shot'
* mode. A single call will be made to read the complete
* kernel buffer. The contents of the buffer will be
* output and the program will terminate.
*
* The -d switch causes 'debug' mode to be activated. This
* will cause the daemon to generate LOTS of output to stderr.
*
* The buffer decomposition function (LogLine) was re-written to
* squash a bug which was causing only partial kernel messages to
* be written to the syslog facility.
*
* The signal handling code was modified to properly differentiate
* between the STOP and TSTP signals.
*
* Added pid saving when the daemon detaches into the background. Thank
* you to Juha Virtanen (jiivee@hut.fi) for providing this patch.
*
* Mon Feb 6 07:31:29 CST 1995: Dr. Wettstein
* Significant re-organization of the signal handling code. The
* signal handlers now only set variables. Not earth shaking by any
* means but aesthetically pleasing to the code purists in the group.
*
* Patch to make things more compliant with the file system standards.
* Thanks to Chris Metcalf for prompting this helpful change.
*
* The routines responsible for reading the kernel log sources now
* initialize the buffers before reading. I think that this will
* solve problems with non-terminated kernel messages producing
* output of the form: new old old old
*
* This may also help influence the occassional reports of klogd
* failing under significant load. I think that the jury may still
* be out on this one though. My thanks to Joerg Ahrens for initially
* tipping me off to the source of this problem. Also thanks to
* Michael O'Reilly for tipping me off to the best fix for this problem.
* And last but not least Mark Lord for prompting me to try this as
* a means of attacking the stability problem.
*
* Specifying a - as the arguement to the -f switch will cause output
* to be directed to stdout rather than a filename of -. Thanks to
* Randy Appleton for a patch which prompted me to do this.
*
* Wed Feb 22 15:37:37 CST 1995: Dr. Wettstein
* Added version information to logging startup messages.
*
* Wed Jul 26 18:57:23 MET DST 1995: Martin Schulze
* Added an commandline argument "-n" to avoid forking. This obsoletes
* the compiler define NO_FORK. It's more useful to have this as an
* argument as there are many binary versions and one doesn't need to
* recompile the daemon.
*
* Thu Aug 10 19:01:08 MET DST 1995: Martin Schulze
* Added my pidfile.[ch] to it to perform a better handling with pidfiles.
* Now both, syslogd and klogd, can only be started once. They check the
* pidfile.
*
* Fri Nov 17 15:05:43 CST 1995: Dr. Wettstein
* Added support for kernel address translation. This required moving
* some definitions and includes to the new klogd.h file. Some small
* code cleanups and modifications.
*
* Mon Nov 20 10:03:39 MET 1995
* Added -v option to print the version and exit.
*
* Thu Jan 18 11:19:46 CST 1996: Dr. Wettstein
* Added suggested patches from beta-testers. These address two
* two problems. The first is segmentation faults which occur with
* the ELF libraries. This was caused by passing a null pointer to
* the strcmp function.
*
* Added a second patch to remove the pidfile as part of the
* termination cleanup sequence. This minimizes the potential for
* conflicting pidfiles causing immediate termination at boot time.
*
* Wed Aug 21 09:13:03 CDT 1996: Dr. Wettstein
* Added ability to reload static symbols and kernel module symbols
* under control of SIGUSR1 and SIGUSR2 signals.
*
* Added -p switch to select 'paranoid' behavior with respect to the
* loading of kernel module symbols.
*
* Informative line now printed whenever a state change occurs due
* to signal reception by the daemon.
*
* Added the -i and -I command line switches to signal the currently
* executing daemon.
*
* Tue Nov 19 10:15:36 PST 1996: Leland Olds <olds@eskimo.com>
* Corrected vulnerability to buffer overruns by rewriting LogLine
* routine. Obscenely long kernel messages will now be broken up
* into lines no longer than LOG_LINE_LENGTH.
*
* The last version of LogLine was vulnerable to buffer overruns:
* - Kernel messages longer than LOG_LINE_LENGTH caused a buffer
* overrun.
* - If a line was determined to be shorter than LOG_LINE_LENGTH,
* the routine "ExpandKadds" could cause the line grow by
* an unknown amount and overrun a buffer.
* I turned these routines into a little parsing state machine that
* should not have these problems.
*
* Sun Jun 15 16:23:29 MET DST 1997: Michael Alan Dorman
* Some more glibc patches made by <mdorman@debian.org>.
*
* Thu Aug 21 12:11:27 MET DST 1997: Martin Schulze <joey@infodrom.north.de>
* Fixed little mistake which prevented klogd from accepting a
* console log
*
* Fri Jan 9 00:39:52 CET 1998: Martin Schulze <joey@infodrom.north.de>
* Changed the behaviour of klogd when receiving a terminate
* signal. Now the program terminates immediately instead of
* completing the receipt of a kernel message, i.e the read()
* call. The old behaveiour could result in klogd being
* recognized as being undead, because it'll only die after a
* message has been received.
*
* Fri Jan 9 11:03:48 CET 1998: Martin Schulze <joey@infodrom.north.de>
* Corrected some code that caused klogd to dump core when
* receiving messages containing '%', some of them exist in
* 2.1.78. Thanks to Chu-yeon Park <kokids@doit.ajou.ac.kr> for
* informing me.
*
* Fri Jan 9 23:38:19 CET 1998: Florian La Roche <florian@knorke.saar.de>
* Added -x switch to omit EIP translation and System.map evaluation.
*
* Sun Jan 25 20:47:46 CET 1998: Martin Schulze <joey@infodrom.north.de>
* As the bug covering the %'s introduced a problem with
* unevaluated priorities I've worked out a real fix that strips
* %'s to an even number which is harmless for printf.
*
* Sat Oct 10 20:01:48 CEST 1998: Martin Schulze <joey@infodrom.north.de>
* Added support for TESTING define which will turn klogd into
* stdio-mode used for debugging.
*
* Mon Apr 13 18:18:45 CEST 1998: Martin Schulze <joey@infodrom.north.de>
* Modified System.map read function to try all possible map
* files until a file with matching version is found. Added support for
* Debian release.
*
* Mon Oct 12 13:01:27 MET DST 1998: Martin Schulze <joey@infodrom.north.de>
* Used unsigned long and strtoul() to resolve kernel oops symbols.
*
* Sun Jan 3 18:38:03 CET 1999: Martin Schulze <joey@infodrom.north.de>
* Shortened LOG_LINE_LENGTH in order to get long lines splitted
* up earlier and syslogd has a better chance concatenating them
* together again.
*
* Sat Aug 21 12:27:02 CEST 1999: Martin Schulze <joey@infodrom.north.de>
* Skip newline when reading in messages.
*
* Tue Sep 12 22:14:33 CEST 2000: Martin Schulze <joey@infodrom.ffis.de>
* Don't feed a buffer directly to a printf-type routine, use
* "%s" as format string instead. Thanks to Jouko Pynnönen
* <jouko@solutions.fi> for pointing this out.
*
* Tue Sep 12 22:44:57 CEST 2000: Martin Schulze <joey@infodrom.ffis.de>
* Commandline option `-2': When symbols are expanded, print the
* line twice. Once with addresses converted to symbols, once with the
* raw text. Allows external programs such as ksymoops do their own
* processing on the original data. Thanks to Keith Owens
* <kaos@ocs.com.au> for the patch.
*
* Mon Sep 18 09:32:27 CEST 2000: Martin Schulze <joey@infodrom.ffis.de>
* Added patch to fix priority decoding after moving kernel
* messgages into "%s". Thanks to Solar Designer
* <solar@false.com> for the patch.
*
* Sun Mar 11 20:23:44 CET 2001: Martin Schulze <joey@infodrom.ffis.de>
* Stop LogLine() from being called with wrong argument when a
* former calculation failed already. Thanks to Thomas Roessler
* <roessler@does-not-exist.org> for providing a patch.
*
* Ignore zero bytes, no busy loop is entered anymore. Several
* people have submitted patches: Troels Walsted Hansen
* <troels@thule.no>, Wolfgang Oertl <Wolfgang.Oertl@uibk.ac.at>
* and Thomas Roessler.
*/
/* Includes. */
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#if !defined(__GLIBC__)
#include <linux/time.h>
#endif /* __GLIBC__ */
#include <stdarg.h>
#include <paths.h>
#include <stdlib.h>
#include "klogd.h"
#include "ksyms.h"
#ifndef TESTING
#include "pidfile.h"
#endif
#define __LIBRARY__
#include <linux/unistd.h>
#if !defined(__GLIBC__)
# define __NR_ksyslog __NR_syslog
_syscall3(int,ksyslog,int, type, char *, buf, int, len);
#else
#include <sys/klog.h>
#define ksyslog klogctl
#endif
extern int writeSyslogV(int pri, const char *szFmt, va_list va);
extern int writeSyslog(int iPri, const char *szFmt, ...);
#ifndef _PATH_KLOG
#define _PATH_KLOG "/proc/kmsg"
#endif
#define LOG_BUFFER_SIZE 4096
#define LOG_LINE_LENGTH 1000
#ifndef TESTING
#if defined(FSSTND)
static char *PidFile = _PATH_VARRUN "rklogd.pid";
#else
static char *PidFile = "/etc/rklogd.pid";
#endif
#endif
static int kmsg,
change_state = 0,
terminate = 0,
caught_TSTP = 0,
reload_symbols = 0,
console_log_level = 6;
static int use_syscall = 0,
one_shot = 0,
symbol_lookup = 1,
no_fork = 0; /* don't fork - don't run in daemon mode */
static char *symfile = (char *) 0,
log_buffer[LOG_BUFFER_SIZE];
static FILE *output_file = (FILE *) 0;
static enum LOGSRC {none, proc, kernel} logsrc;
int debugging = 0;
int symbols_twice = 0;
/* Function prototypes. */
extern int ksyslog(int type, char *buf, int len);
static void CloseLogSrc(void);
extern void restart(int sig);
extern void stop_logging(int sig);
extern void stop_daemon(int sig);
extern void reload_daemon(int sig);
static void Terminate(void);
static void SignalDaemon(int);
static void ReloadSymbols(void);
static void ChangeLogging(void);
static enum LOGSRC GetKernelLogSrc(void);
static void LogLine(char *ptr, int len);
static void LogKernelLine(void);
static void LogProcLine(void);
extern int main(int argc, char *argv[]);
extern void Syslog(int priority, char *fmt, ...)
{
va_list ap;
char *argl;
if(debugging)
{
fputs("Logging line:\n", stderr);
fprintf(stderr, "\tLine: %s\n", fmt);
fprintf(stderr, "\tPriority: %d\n", priority);
}
/* Handle output to a file. */
if ( output_file != (FILE *) 0 ) {
va_start(ap, fmt);
vfprintf(output_file, fmt, ap);
va_end(ap);
fputc('\n', output_file);
fflush(output_file);
if (!one_shot)
fsync(fileno(output_file));
return;
}
/* Output using syslog. */
if (!strcmp(fmt, "%s")) {
va_start(ap, fmt);
argl = va_arg(ap, char *);
if (argl[0] == '<' && argl[1] && argl[2] == '>') {
switch ( argl[1] )
{
case '0':
priority = LOG_EMERG;
break;
case '1':
priority = LOG_ALERT;
break;
case '2':
priority = LOG_CRIT;
break;
case '3':
priority = LOG_ERR;
break;
case '4':
priority = LOG_WARNING;
break;
case '5':
priority = LOG_NOTICE;
break;
case '6':
priority = LOG_INFO;
break;
case '7':
default:
priority = LOG_DEBUG;
}
argl += 3;
}
writeSyslog(priority, fmt, argl);
va_end(ap);
#ifdef TESTING
putchar('\n');
#endif
return;
}
va_start(ap, fmt);
writeSyslogV(priority, fmt, ap);
va_end(ap);
#ifdef TESTING
printf ("\n");
#endif
return;
}
static void CloseLogSrc(void)
{
/* Turn on logging of messages to console. */
ksyslog(7, NULL, 0);
/* Shutdown the log sources. */
switch ( logsrc )
{
case kernel:
ksyslog(0, 0, 0);
Syslog(LOG_INFO, "Kernel logging (ksyslog) stopped.");
break;
case proc:
close(kmsg);
Syslog(LOG_INFO, "Kernel logging (proc) stopped.");
break;
case none:
break;
}
if ( output_file != (FILE *) 0 )
fflush(output_file);
return;
}
void restart(int sig)
{
change_state = 1;
caught_TSTP = 0;
return;
}
void stop_logging(int sig)
{
change_state = 1;
caught_TSTP = 1;
return;
}
void stop_daemon(int sig)
{
change_state = 1;
terminate = 1;
return;
}
void reload_daemon(int sig)
{
change_state = 1;
reload_symbols = 1;
if ( sig == SIGUSR2 )
{
++reload_symbols;
}
return;
}
static void Terminate(void)
{
CloseLogSrc();
Syslog(LOG_INFO, "Kernel log daemon terminating.");
sleep(1);
if ( output_file != (FILE *) 0 )
fclose(output_file);
closelog();
#ifndef TESTING
(void) remove_pid(PidFile);
#endif
exit(1);
}
static void SignalDaemon(int sig)
{
#ifndef TESTING
auto int pid = check_pid(PidFile);
kill(pid, sig);
#else
kill(getpid(), sig);
#endif
return;
}
static void ReloadSymbols(void)
{
if (symbol_lookup) {
if ( reload_symbols > 1 )
InitKsyms(symfile);
InitMsyms();
}
reload_symbols = change_state = 0;
return;
}
static void ChangeLogging(void)
{
/* Terminate kernel logging. */
if ( terminate == 1 )
Terminate();
/* Indicate that something is happening. */
Syslog(LOG_INFO, "rklogd %s, ---------- state change ----------\n", \
VERSION);
/* Reload symbols. */
if ( reload_symbols > 0 )
{
ReloadSymbols();
return;
}
/* Stop kernel logging. */
if ( caught_TSTP == 1 )
{
CloseLogSrc();
logsrc = none;
change_state = 0;
return;
}
/*
* The rest of this function is responsible for restarting
* kernel logging after it was stopped.
*
* In the following section we make a decision based on the
* kernel log state as to what is causing us to restart. Somewhat
* groady but it keeps us from creating another static variable.
*/
if ( logsrc != none )
{
Syslog(LOG_INFO, "Kernel logging re-started after SIGSTOP.");
change_state = 0;
return;
}
/* Restart logging. */
logsrc = GetKernelLogSrc();
change_state = 0;
return;
}
static enum LOGSRC GetKernelLogSrc(void)
{
auto struct stat sb;
/* Set level of kernel console messaging.. */
if ( (ksyslog(8, NULL, console_log_level) < 0) && \
(errno == EINVAL) )
{
/*
* An invalid arguement error probably indicates that
* a pre-0.14 kernel is being run. At this point we
* issue an error message and simply shut-off console
* logging completely.
*/
Syslog(LOG_WARNING, "Cannot set console log level - disabling "
"console output.");
ksyslog(6, NULL, 0);
}
/*
* First do a stat to determine whether or not the proc based
* file system is available to get kernel messages from.
*/
if ( use_syscall ||
((stat(_PATH_KLOG, &sb) < 0) && (errno == ENOENT)) )
{
/* Initialize kernel logging. */
ksyslog(1, NULL, 0);
#ifdef DEBRELEASE
Syslog(LOG_INFO, "rklogd %s#%s, log source = ksyslog "
"started.", VERSION, DEBRELEASE);
#else
Syslog(LOG_INFO, "rklogd %s, log source = ksyslog "
"started.", VERSION);
#endif
return(kernel);
}
#ifndef TESTING
if ( (kmsg = open(_PATH_KLOG, O_RDONLY)) < 0 )
{
fprintf(stderr, "rklogd: Cannot open proc file system, " \
"%d - %s.\n", errno, strerror(errno));
ksyslog(7, NULL, 0);
exit(1);
}
#else
kmsg = fileno(stdin);
#endif
#ifdef DEBRELEASE
Syslog(LOG_INFO, "rklogd %ss#%s, log source = %s started.", \
VERSION, DEBRELEASE, _PATH_KLOG);
#else
Syslog(LOG_INFO, "rklogd %s, log source = %s started.", \
VERSION, _PATH_KLOG);
#endif
return(proc);
}
/*
* Copy characters from ptr to line until a char in the delim
* string is encountered or until min( space, len ) chars have
* been copied.
*
* Returns the actual number of chars copied.
*/
static int copyin( char *line, int space,
const char *ptr, int len,
const char *delim )
{
auto int i;
auto int count;
count = len < space ? len : space;
for(i=0; i<count && !strchr(delim, *ptr); i++ ) {
*line++ = *ptr++;
}
return( i );
}
/*
* Messages are separated by "\n". Messages longer than
* LOG_LINE_LENGTH are broken up.
*
* Kernel symbols show up in the input buffer as : "[<aaaaaa>]",
* where "aaaaaa" is the address. These are replaced with
* "[symbolname+offset/size]" in the output line - symbolname,
* offset, and size come from the kernel symbol table.
*
* If a kernel symbol happens to fall at the end of a message close
* in length to LOG_LINE_LENGTH, the symbol will not be expanded.
* (This should never happen, since the kernel should never generate
* messages that long.
*
* To preserve the original addresses, lines containing kernel symbols
* are output twice. Once with the symbols converted and again with the
* original text. Just in case somebody wants to run their own Oops
* analysis on the syslog, e.g. ksymoops.
*/
static void LogLine(char *ptr, int len)
{
enum parse_state_enum {
PARSING_TEXT,
PARSING_SYMSTART, /* at < */
PARSING_SYMBOL,
PARSING_SYMEND /* at ] */
};
static char line_buff[LOG_LINE_LENGTH];
static char *line =line_buff;
static enum parse_state_enum parse_state = PARSING_TEXT;
static int space = sizeof(line_buff)-1;
static char *sym_start; /* points at the '<' of a symbol */
auto int delta = 0; /* number of chars copied */
auto int symbols_expanded = 0; /* 1 if symbols were expanded */
auto int skip_symbol_lookup = 0; /* skip symbol lookup on this pass */
auto char *save_ptr = ptr; /* save start of input line */
auto int save_len = len; /* save length at start of input line */
while( len > 0 )
{
if( space == 0 ) /* line buffer is full */
{
/*
** Line too long. Start a new line.
*/
*line = 0; /* force null terminator */
if ( debugging )
{
fputs("Line buffer full:\n", stderr);
fprintf(stderr, "\tLine: %s\n", line);
}
Syslog( LOG_INFO, "%s", line_buff );
line = line_buff;
space = sizeof(line_buff)-1;
parse_state = PARSING_TEXT;
symbols_expanded = 0;
skip_symbol_lookup = 0;
save_ptr = ptr;
save_len = len;
}
switch( parse_state )
{
case PARSING_TEXT:
delta = copyin( line, space, ptr, len, "\n[" );
line += delta;
ptr += delta;
space -= delta;
len -= delta;
if( space == 0 || len == 0 )
{
break; /* full line_buff or end of input buffer */
}
if( *ptr == '\0' ) /* zero byte */
{
ptr++; /* skip zero byte */
space -= 1;
len -= 1;
break;
}
if( *ptr == '\n' ) /* newline */
{
ptr++; /* skip newline */
space -= 1;
len -= 1;
*line = 0; /* force null terminator */
Syslog( LOG_INFO, "%s", line_buff );
line = line_buff;
space = sizeof(line_buff)-1;
if (symbols_twice) {
if (symbols_expanded) {
/* reprint this line without symbol lookup */
symbols_expanded = 0;
skip_symbol_lookup = 1;
ptr = save_ptr;
len = save_len;
}
else
{
skip_symbol_lookup = 0;
save_ptr = ptr;
save_len = len;
}
}
break;
}
if( *ptr == '[' ) /* possible kernel symbol */
{
*line++ = *ptr++;
space -= 1;
len -= 1;
if (!skip_symbol_lookup)
parse_state = PARSING_SYMSTART; /* at < */
break;
}
/* Now that line_buff is no longer fed to *printf as format
* string, '%'s are no longer "dangerous".
*/
break;
case PARSING_SYMSTART:
if( *ptr != '<' )
{
parse_state = PARSING_TEXT; /* not a symbol */
break;
}
/*
** Save this character for now. If this turns out to
** be a valid symbol, this char will be replaced later.
** If not, we'll just leave it there.
*/
sym_start = line; /* this will point at the '<' */
*line++ = *ptr++;
space -= 1;
len -= 1;
parse_state = PARSING_SYMBOL; /* symbol... */
break;
case PARSING_SYMBOL:
delta = copyin( line, space, ptr, len, ">\n[" );
line += delta;
ptr += delta;
space -= delta;
len -= delta;
if( space == 0 || len == 0 )
{
break; /* full line_buff or end of input buffer */
}
if( *ptr != '>' )
{
parse_state = PARSING_TEXT;
break;
}
*line++ = *ptr++; /* copy the '>' */
space -= 1;
len -= 1;
parse_state = PARSING_SYMEND;
break;
case PARSING_SYMEND:
if( *ptr != ']' )
{
parse_state = PARSING_TEXT; /* not a symbol */
break;
}
/*
** It's really a symbol! Replace address with the
** symbol text.
*/
{
auto int sym_space;
unsigned long value;
auto struct symbol sym;
auto char *symbol;
*(line-1) = 0; /* null terminate the address string */
value = strtoul(sym_start+1, (char **) 0, 16);
*(line-1) = '>'; /* put back delim */
if ( !symbol_lookup || (symbol = LookupSymbol(value, &sym)) == (char *)0 )
{
parse_state = PARSING_TEXT;
break;
}
/*
** verify there is room in the line buffer
*/
sym_space = space + ( line - sym_start );
if( sym_space < strlen(symbol) + 30 ) /*(30 should be overkill)*/
{
parse_state = PARSING_TEXT; /* not enough space */
break;
}
delta = sprintf( sym_start, "%s+%d/%d]",
symbol, sym.offset, sym.size );
space = sym_space + delta;
line = sym_start + delta;
symbols_expanded = 1;
}
ptr++;
len--;
parse_state = PARSING_TEXT;
break;
default: /* Can't get here! */
parse_state = PARSING_TEXT;
}
}
return;
}
static void LogKernelLine(void)
{
auto int rdcnt;
/*
* Zero-fill the log buffer. This should cure a multitude of
* problems with klogd logging the tail end of the message buffer
* which will contain old messages. Then read the kernel log
* messages into this fresh buffer.
*/
memset(log_buffer, '\0', sizeof(log_buffer));
if ( (rdcnt = ksyslog(2, log_buffer, sizeof(log_buffer)-1)) < 0 )
{
if ( errno == EINTR )
return;
fprintf(stderr, "rklogd: Error return from sys_sycall: " \
"%d - %s\n", errno, strerror(errno));
}
else
LogLine(log_buffer, rdcnt);
return;
}
static void LogProcLine(void)
{
auto int rdcnt;
/*
* Zero-fill the log buffer. This should cure a multitude of
* problems with klogd logging the tail end of the message buffer
* which will contain old messages. Then read the kernel messages
* from the message pseudo-file into this fresh buffer.
*/
memset(log_buffer, '\0', sizeof(log_buffer));
if ( (rdcnt = read(kmsg, log_buffer, sizeof(log_buffer)-1)) < 0 )
{
if ( errno == EINTR )
return;
Syslog(LOG_ERR, "Cannot read proc file system: %d - %s.", \
errno, strerror(errno));
}
else
LogLine(log_buffer, rdcnt);
return;
}
/* helper routine to spit out an error message and terminate
* klogd when setting a signal error fails.
*/
void sigactionErrAbort()
{
fprintf(stderr, "rklogd: could net set a signal handler - terminating. Error: %s\n",
strerror(errno));
exit(1);
}
int main(int argc, char *argv[])
{
int ch,
use_output = 0;
char *log_level = (char *) 0,
*output = (char *) 0;
struct sigaction sigAct;
#ifndef TESTING
chdir ("/");
#endif
/* Parse the command-line. */
while ((ch = getopt(argc, argv, "c:df:iIk:nopsvx2")) != EOF)
switch((char)ch)
{
case '2': /* Print lines with symbols twice. */
symbols_twice = 1;
break;
case 'c': /* Set console message level. */
log_level = optarg;
break;
case 'd': /* Activity debug mode. */
debugging = 1;
break;
case 'f': /* Define an output file. */
output = optarg;
use_output++;
break;
case 'i': /* Reload module symbols. */
SignalDaemon(SIGUSR1);
return(0);
case 'I':
SignalDaemon(SIGUSR2);
return(0);
case 'k': /* Kernel symbol file. */
symfile = optarg;
break;
case 'n': /* don't fork */
no_fork++;
break;
case 'o': /* One-shot mode. */
one_shot = 1;
break;
case 'p':
SetParanoiaLevel(1); /* Load symbols on oops. */
break;
case 's': /* Use syscall interface. */
use_syscall = 1;
break;
case 'v':
printf("rklogd %s\n", VERSION);
exit (1);
case 'x':
symbol_lookup = 0;
break;
}
/* Set console logging level. */
if ( log_level != (char *) 0 )
{
if ( (strlen(log_level) > 1) || \
(strchr("12345678", *log_level) == (char *) 0) )
{
fprintf(stderr, "rklogd: Invalid console logging "
"level <%s> specified.\n", log_level);
return(1);
}
console_log_level = *log_level - '0';
}
#ifndef TESTING
/*
* The following code allows klogd to auto-background itself.
* What happens is that the program forks and the parent quits.
* The child closes all its open file descriptors, and issues a
* call to setsid to establish itself as an independent session
* immune from control signals.
*
* fork() is only called if it should run in daemon mode, fork is
* not disabled with the command line argument and there's no
* such process running.
*/
if ( (!one_shot) && (!no_fork) )
{
if (!check_pid(PidFile))
{
if ( fork() == 0 )
{
auto int fl;
int num_fds = getdtablesize();
/* This is the child closing its file descriptors. */
for (fl= 0; fl <= num_fds; ++fl)
{
if ( fileno(stdout) == fl && use_output )
if ( strcmp(output, "-") == 0 )
continue;
close(fl);
}
setsid();
}
else
exit(0);
}
else
{
fputs("rklogd: Already running.\n", stderr);
exit(1);
}
}
/* tuck my process id away */
if (!check_pid(PidFile))
{
if (!write_pid(PidFile))
Terminate();
}
else
{
fputs("rklogd: Already running.\n", stderr);
Terminate();
}
#endif
/* Signal setups.
* Please note that the "original" klogd in sysklogd tries to
* handle SIGKILL and SIGSTOP. That does not work - but as the
* original klogd had no error checking, nobody ever noticed. We
* do now have error checking and consequently those ever-failing
* calls are now removed.
*/
sigemptyset(&sigAct.sa_mask);
sigAct.sa_flags = 0;
/* first, set all signals to ignore
* In this loop, we try blindly to ignore all signals. I am leaving
* intentionally out all error checking. If we can ignore the signal,
* that's nice, but if we can't ... well, so be it ;)
* RGerhards, 2007-06-15
*/
sigAct.sa_handler = SIG_IGN;
for (ch= 1; ch < NSIG ; ++ch)
{
if(ch != SIGKILL && ch != SIGSTOP)
sigaction(ch, &sigAct, NULL);
}
/* Now specific handlers (one after another) */
sigAct.sa_handler = stop_daemon;
if(sigaction(SIGINT, &sigAct, NULL) != 0) sigactionErrAbort();
if(sigaction(SIGTERM, &sigAct, NULL) != 0) sigactionErrAbort();
if(sigaction(SIGHUP, &sigAct, NULL) != 0) sigactionErrAbort();
sigAct.sa_handler = stop_daemon;
if(sigaction(SIGTSTP, &sigAct, NULL) != 0) sigactionErrAbort();
sigAct.sa_handler = restart;
if(sigaction(SIGCONT, &sigAct, NULL) != 0) sigactionErrAbort();
sigAct.sa_handler = reload_daemon;
if(sigaction(SIGUSR1, &sigAct, NULL) != 0) sigactionErrAbort();
if(sigaction(SIGUSR2, &sigAct, NULL) != 0) sigactionErrAbort();
/* Open outputs. */
if ( use_output )
{
if ( strcmp(output, "-") == 0 )
output_file = stdout;
else if ( (output_file = fopen(output, "w")) == (FILE *) 0 )
{
fprintf(stderr, "rklogd: Cannot open output file " \
"%s - %s\n", output, strerror(errno));
return(1);
}
}
/* Handle one-shot logging. */
if ( one_shot )
{
if (symbol_lookup) {
symbol_lookup = (InitKsyms(symfile) == 1);
symbol_lookup |= InitMsyms();
if (symbol_lookup == 0) {
Syslog(LOG_WARNING, "cannot find any symbols, turning off symbol lookups\n");
}
}
if ( (logsrc = GetKernelLogSrc()) == kernel )
LogKernelLine();
else
LogProcLine();
Terminate();
}
/* Determine where kernel logging information is to come from. */
#if defined(KLOGD_DELAY)
sleep(KLOGD_DELAY);
#endif
logsrc = GetKernelLogSrc();
if (symbol_lookup) {
symbol_lookup = (InitKsyms(symfile) == 1);
symbol_lookup |= InitMsyms();
if (symbol_lookup == 0) {
Syslog(LOG_WARNING, "cannot find any symbols, turning off symbol lookups\n");
}
}
/* The main loop. */
/* The main loop will be broken by a signal handler which set the
* terminate variable. That is then cheked in ChangeLogging(), which
* will then terminate klogd.
* RGerhards, 2007-06-15
*/
while(1)
{
if ( change_state )
ChangeLogging();
switch ( logsrc )
{
case kernel:
LogKernelLine();
break;
case proc:
LogProcLine();
break;
case none:
pause();
break;
}
}
}
#else /* #ifdef FEATURE_KLOGD */
#include <stdio.h>
int main()
{
fprintf(stderr, "FEATURE_KLOGD was disabled during make, so rklogd is not available.\n");
return(1);
}
#endif /* #ifdef WITH_KLOGD */
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* tab-width: 8
* End:
* vi:set ai:
*/
|