journalctl updates with every other message - kernel-module

I am working on my first kernel module. I've completed a few exercises in The Linux Kernel Module Programming Guide but this is my first one without any guidance.
When I print messages using printk or the pr_err macro journalctl doesn't catch up until I print another message.
For example
int init_module()
{
pr_err("This is a message");
}
void cleanup_module()
{
pr_err("this is a second message");
}
If I insmod this module journalctl will show nothing. When I rmmod journalctl will show both messages and the timestamp will be the same.
If I insmod this module then insmod another module that prints both messages will show after I insmod the second module. The timestamp will be the same for both messages.

If message for printk doesn't contain newline symbol, its output can be delayed.
Normally, you need
pr_err("This is a message\n");
Sometimes, using KERN_INFO or KERN_ALERT also force message to be output immediately. But approach with terminating \n seems to be more clear.
Taken from answers to this question.

Related

Connect two native Contiki NG motes over SLIP

Since the RPL border router example works as either a Cooja mote or a native mote, I thought using the SLIP code in /services/rpl-border-router/native might work. I made a copy of hello-world. I edited hello-world.c to read
#include "contiki.h"
#include "services/rpl-border-router/native/border-router.h"
#include <stdio.h> /* For printf() */
extern int contiki_argc;
extern char **contiki_argv;
/*---------------------------------------------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
static struct etimer timer;
PROCESS_BEGIN();
/* Setup a periodic timer that expires after 10 seconds. */
etimer_set(&timer, CLOCK_SECOND * 10);
slip_config_handle_arguments(contiki_argc, contiki_argv);
slip_init();
while(1) {
printf("Hello, world\n");
/* Wait for the periodic timer to expire and then restart the timer. */
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
etimer_reset(&timer);
}
PROCESS_END();
}
And edited the makefile:
CONTIKI_PROJECT = hello-world
all: $(CONTIKI_PROJECT)
CONTIKI = ../..
include $(CONTIKI)/Makefile.dir-variables
MODULES += $(CONTIKI_NG_SERVICES_DIR)/rpl-border-router
include $(CONTIKI)/Makefile.include
This inclusion of rpl-border-router module was done because I was getting undefined reference to ‘slip_init()’... and undefined reference to ‘slip_config_handle_arguments(contiki_argc, contiki_argv);’... errors when compiling, despite the #include "services/rpl-border-router/native/border-router.h" line in hello-world.c. This does appear to be the right header to include for declarations of these functions, but if I am mistaken please say so.
From the folder where the edited hello-world code resides, I did make distclean, and then make TARGET=native. If I run this edited hello-world with
sudo ./hello-world.native fd00::3 -s ttyS0
it looks like it is starting up, but I see:
…[INFO: Native ] Added global IPv6 address fd00::302:304:506:708
********SLIP started on ``/dev/ttyS0''
Hello, world
[INFO: BR ] RPL-Border router started
********SLIP started on ``/dev/ttyS0''
opened tun device ``/dev/tun0''
So it is trying to start up the border router as well as opening the SLIP interface, and just below the ifconfig tun0 output (which looks fine) I see
…hello-world.native: serial_input: read: Success
ifconfig tun0 down
netstat -nr | awk '{ if ($2 == "tun0") print "route delete -net "$1; }' | sh
So, it seems I need to do something to stop from starting the border router module. I have seen that there are 'slip.c' and 'slip.h' in os/dev/, used in the slip-radio example but the declaration of 'slip_arch_init()' in slip.h without a definition of slip_arch_init() in slip.c has me confused, and motivated me to use 'slip_init()' from the /services/rpl-border-router/native folder, since in there I can see a pretty normal bit of code opening up a serial interface.
I have done a lot of google searching and searched through several of the more complex examples for something similar to what I am trying to do, but haven’t found anything that seems very close. If there is something ready made then or course that would be the greatest help, but I am sure it must be obvious that my understanding of Make and file interrelations is nowhere near where it needs to be.
Just to be clear, my goal at the moment is to get a SLIP interface up and running on the remote VM so that I can get a ping6 response on the VM with the border router. I will worry about writing the callback once I have some baseline connectivity. Maybe this is a misunderstanding on my part as well.

**identifier msqid is removed from the system** meaning

My program was giving me an identifier removed error. I was going through the man page and I didn't understand what could cause this error "identifier removed". I've noticed that this happens everytime I try to send a message for the second time to a user process. The first time sending and recieving is fine. I tried a lot to reproduce a small example but I'm unable to. I don't think anyone here wants to go over so many lines of code. But basically what I am doing is
while(1)
{
if(messsage recieved from oss) //msg type getpid(), message text stores master pid
{
send message to oss //message type master pid, message text stores getpid()
}
}
the user process gets sent a message at random times like: time 1, time 4, time 8 etc.
The first time it sends and recieves its fine.
Identifier removed is the error message corresponding to error EIDRM. My system's man page for msgrcv says this error is returned for the following reason:
While the process was sleeping to receive a message, the message queue was removed.
A message queue is removed by passing command IPC_RMID to msgctl.

generating trap/segfault messages in dmesg

I had a program that was segfaulting.
When I went to investigate and ran dmesg I could see lines like this:
[955.915050] traps: foo_bar[123] general protection ip:7f5fcc2d4306 sp:7ffd9e5868b8 ...
Now the program has been fixed and I'm trying to write some analysis scripts across different systems to find similar messages and was hoping to induce a line in the dmesg log to get a baseline for what to look for and see if there's a difference between, say, a sigbus(10) and a sigill(4)
I tried to do it via kill -11 on the command line . No entry in dmesg
I tried to do it via signal(getpid(), 11) in the code. No entry in dmesg
I tried to do it via signal 11 after attaching in gdb . No entry in dmesg
I tried to do it via writing bad code and it worked for SEGV, but I can't figure out how to trigger a SIGBUS (for example)
I'm guessing that there is more than one path for handling the signal depending on how it occurs and my attempts above just aren't doing it the right way.
How can I trigger/send a signal to my program that'll get a line in dmesg? Is there some kernel or log configuration I can twiddle to get those lines?
Update:
" __builtin_trap: when to use it? " shows how to get a SIGILL but alas doesn't have a signal-agnostic solution)

Executing System Function and Parsing Output

I want to run a system function within a program written in C.
This system function is blocking and can take some time before it returns to stdout. The function to be called is snort, and normally is executed on a raspberry pi as followed:
sudo snort -q -A console -i eth0 -c /etc/snort/snort.conf
In the case snort triggers an alert, the parent program should read that line and turn on a LED. I currently am turning on leds as followed:
void triggerLed(void) {
pinMode(7], OUTPUT);
digitalWrite(7, HIGH);
}
int main(void) {
//Execute this function call: sudo snort -q -A console -i eth0 -c /etc/snort/snort.conf
//while executing
//On new line from readline()
//if strcmp(line,"alert")
triggerLed();
//endif
//end while
}
How would you solve this? I tried monitoring syslog, snort however does not write to syslog as I cannot find any alerts.
fyi: Last week I asked this question on: Execute script on Snort alert . Unfortunately, due to a combination a vaguely formed question and a change of scope I rephrased the question here.
The function you are looking for is system(3). You get the exit code of the process back.
But if you intend to read the output (stdout) of the called process you have to implement a fork(3)/exec(3) combination, reconnecting the child's file descriptors (at least fd 1) and then reading from it.

Linux-kernel: printk from "open" syscall don't work

I have a doubt.
I opened the kernel and I changed the directory linux-3.1.1/fs/open.c
I changed the follow code in the open.c.
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
{
long ret;
printk(KERN_EMERG "Testing\n");
...
}
I put this line only: printk(KERN_EMERG "Testing");
And I include the libraries:<linux/kernel.h> and <linux/printk.h>
So I compiled and rebooted my linux(Ubuntu).
During the rebooting appeared a lot of "Testing" on the screen.
So up to now its Ok.
But now I have a problem.
I created this program in c.
int main()
{
size_t filedesc = open("testefile2.txt",O_CREAT | O_WRONLY,0640);
printf("%d",filedesc);
}
I compiled this program and executed and works good.
But I don´t understand why the "Testing" didn't appeared on the shell.
I mean , if when I reboot the pc appeared a lot of the word "Testing" , why this word doens´t appear when I execute the program above.
Just to add I include this libraries in this code above:
unistd.h , fcntl.h , stdio.h , stdlib.h
Thank you guys.
printk calls appear in the kernel message buffer, not in your process' stdout/stderr
But I don´t understand why the "Testing" didn't appeared on the shell.
I think, this is effect of printk's messages suppression. (more exactly:rate limiting)
Check the messages log or console for
printk: ### messages suppressed.
string.
This feature will stop printing a message, if there were a lot of messages in recent time.
Actual code is as 3.1 kernel: http://lxr.linux.no/#linux+v3.1.1/kernel/printk.c#L1621
1621 * printk rate limiting, lifted from the networking subsystem.
1622 *
1623 * This enforces a rate limit: not more than 10 kernel messages
1624 * every 5s to make a denial-of-service attack impossible.
1625 */
1626 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1627
1628 int __printk_ratelimit(const char *func)
So, As the open syscall is very-very popular (just do an strace -e open /bin/ls - I'll get 15 open syscalls for just starting an simplest ls), the rate limiting will be in effect. It will limit your message to be printed only one time in 5 seconds; not more than 10 messages in single "burst".
I can only suggest to create a special user with known UID and add an UID checking before printk in your additional printk-in-open code.

Resources