0

So I'm developing something on eBPF.

I needed to use the unistd.h header because I wanted to use sleep function.

However, I realized that when I type in

#include <unistd.h>

these headers don't get included and I would get an error saying:

warning : implicit delcaration of function 'sleep' is invalid in C99 [-Wimplicit-Function-declaration] sleep (1);

I thought I have done something wrong somewhere else on the code so I tried to include that header on the example from the tutorial and it didn't work that way neither.

So from the code I have attached below, I tried to put a one second of delay before the message would be published.

Has anyone had a same issue and have somehow found a way to use that header inside the c code?

I would very much appreciate it if someone could land me some help!

Thanks a million!

I tried updating the kernel and moved all the header files to ёusr/local/includeё directory because it said on the internet that this is the place where the compiler first checks for headers but still didn't work.

So here's the code I tried but didn't work.

from bcc import BPF
BPF(text = 'int kprobe__sys_clone(void *ctx)
{
  #include <unistd.h>

  sleep(1);
  bpf_trace_printk("Hello World!\\n");
  return 0;
}
').trace_print()
pchaigno
  • 11,313
  • 2
  • 29
  • 54
Rosè
  • 345
  • 2
  • 13
  • Kernel code cannot sleep using `sleep()` – Ctx Mar 28 '19 at 15:28
  • Oh no.. Do you think there's any other way I might be able to do that? Plus, I just want to know if there's a way I can use others headers in general. Thank you so much for your comment! – Rosè Mar 28 '19 at 15:36
  • Depends on _why_ you want to sleep. – Ctx Mar 28 '19 at 15:37
  • You could for example use `msleep_interruptible(1000);` to sleep for 1 second (1000ms), but this needs to be in a process context (i.e. no interrupt handler) – Ctx Mar 28 '19 at 15:39
  • Oh thank you so much for helping me out greatly. So what I wanted to do is I wanted to set a sample rate to receive the data. I thought I might use sleep function because it was the only method I could think of..? I'm not really skilled in this area so :/ and thanks for your help again! – Rosè Mar 28 '19 at 16:14
  • The code will be executed any time when `__sys_clone` is entered. There is not much point in sleeping, it would just delay the message. What exactly are you planning to do? – Ctx Mar 28 '19 at 17:30
  • Oh sorry I forgot to write what my intention was. I am trying to set a sample rate? Like I want to be able to control in which time interval I will be retrieving data, so to say?? – Rosè Mar 28 '19 at 17:42
  • Oh and the example I put above is just an example I put to ask how I could implement a header :) I will try to use msleep_interruptible with that code I'm working on! Maybe I will make bpf sleep for a certain interval and make it retrieve data, sleep again, retrieve and so on. Again, thank you so much for your kindness and help! – Rosè Mar 28 '19 at 17:44

1 Answers1

1

I guess what you want is something like, https://github.com/iovisor/bcc/blob/master/examples/networking/xdp/xdp_drop_count.py Search "sleep" in that code.

As @Ctx said, the function happens when clone syscall is triggered. There is no point to sleep() there, neither can you do it in the kernel calling sleep().

You might want to understand the above example to see how it sets intervals to print stuff. Hope that helps.

Richard Li
  • 396
  • 1
  • 14