-1

I'm trying to invoke the getrusage() system call from kernel space, but I don't know how.

I read about getting the kernel export (Can we call system call in kernel space?), but I don't know what that is or how to use it. Could someone show me how to call a system call from the kernel in C?

Edit: The old title was for a draft of a question, sorry!

Ramsey Alsheikh
  • 179
  • 1
  • 1
  • 7
  • 1
    Your title is very different from your question - could you elaborate? – Jeppe Jun 19 '19 at 18:01
  • Fixed. Sorry about that! – Ramsey Alsheikh Jun 19 '19 at 18:03
  • You could check this thread, which appears to duplicate your question a bit: [getting-getrusage-to-measure-system-time-in-c](https://stackoverflow.com/questions/10509660/getting-getrusage-to-measure-system-time-in-c) You can also check documentations for `getrusage`, e.g [this](http://man7.org/linux/man-pages/man2/getrusage.2.html) – Jeppe Jun 19 '19 at 18:05
  • [`man syscall`](http://man7.org/linux/man-pages/man2/syscall.2.html)? The man page even includes sample code for making system calls. That said, wrapper functions are generally considered the preferred approach (e.g., calling the `getrusage(...)` function rather than making a direct syscall). – Spencer D Jun 19 '19 at 18:14
  • 3
    @SpencerD: `syscall` is for making system calls from user space. – Nate Eldredge Jun 19 '19 at 18:43
  • Take a look in the file `kernel/sys.c`. Search for "getrusage". Those are the kernel functions that implement (among other things) the `getrusage` system call. Since you're in the kernel, you should be able to just call any of those functions directly. – Steve Summit Jun 19 '19 at 22:15
  • @NateEldredge, thank you for the clarification. +1 – Spencer D Jun 20 '19 at 21:00

1 Answers1

2

How to call system call from kernel?

You don't. It doesn't even make sense. It's like asking "How do I enter my house when I'm already inside my house?".

In the kernel there's a whole bunch of functions. Some of them are added to the system call table and some aren't; but if you're already in the kernel you can use them without caring if they were added to the system call table or not. Note that if you're writing a kernel module then it ends up being dynamic linking done when the module is loaded (a bit like the kernel is a shared library).

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • I wonder though - `getrusage` takes a pointer to a buffer to be filled in. The corresponding function in the syscall table, `sys_getrusage` or whatever, is going to be expecting a pointer to userspace memory. Will it still work, on all architectures, if you pass a pointer to kernel memory? – Nate Eldredge Jun 20 '19 at 22:44
  • @NateEldredge: I have no idea how Linux does it. Myself (at least for 64-bit 80x86 where there's a huge number of registers); kernel would shove all the data into registers and return to user-space, and let the user-space library's `getrusage` code unpack the data from registers into the caller's buffer (so that the kernel doesn't have to do all the sanity checks/validation of the library function's caller's buffer). Linux is probably "more generic" though. – Brendan Jun 21 '19 at 02:02
  • @NateEldredge: Of course if the kernel's internal `getrusage()` is only usable from user-space (e.g. uses a buffer from user-space) there might be a different function that obtains the data (e.g. a `getrusage_internal()` that doesn't do any checks on the buffer, which is called by `getrusage()` after it does the checks). Also; it's likely that there's a "process structure" (for each process) that contains the same data, and all code in the kernel can access these structures directly (without bothering with any `getrusage()` wrapper bloat), so trying to use `getrusage()` is a mistake anyway. – Brendan Jun 21 '19 at 02:05