2

I am trying to convert the start_time of the linux kernel task_struct into nanoseconds. I need to give it the argument of const struct timespec * but start_time is of type struct timespec.

How would I make it a constant and a pointer to the timespec struct? Example code:

(*kinfo).start_time = timespec_to_ns((*current).start_time);
spenibus
  • 4,339
  • 11
  • 26
  • 35
Smreks
  • 317
  • 1
  • 7
  • 19
  • 2
    It's dangerous to be coding in the kernel if you don't know that you need to pass the address of it: `timespec_to_ns(&current->start_time)` (unless there's a very devious reason to use `timespec_to_ns(&(*current).start_time);`) — or unless you're dramatically over-thinking and about to kick your own shin rather hard. – Jonathan Leffler Sep 17 '15 at 22:57
  • Haha! Thank you! And don't worry, I'm in an operation systems class, and we are trying to intercept system calls in linux kernels on a virtual machine. – Smreks Sep 17 '15 at 23:09

1 Answers1

3

I would recommend picking up a primer on C, since you'll need to be very familiar with C programming (especially since the Linux kernel uses all the C trickery in the book) in order to write kernel code (or modify existing kernel code). However, to answer your question, you'll want to pass a pointer to the value (which is done using the & operator in C). Also, please use the correct dereferencing syntax for pointers to structures (p->attr).

kinfo->start_time = timespec_to_ns(&current->start_time);
cyphar
  • 2,840
  • 1
  • 15
  • 25
  • Please update to "you can pass a pointer to `xyz` by value". Passing by reference is something that is done in C++, not C. I know "pass by reference" is sometimes used to in place of what I've described above, but it is not the same thing, since you can't modify the pointer itself, whereas a reference pointer can be modified within a function body. – Cloud Sep 18 '15 at 00:56
  • I've never liked using that nomenclature in C. Yes, technically it isn't a "reference", but if we can call `*` dereferencing, why can't we call the inverse operation `&` referencing? It's clunky to say "pass a pointer to `abc` by value" as opposed to "pass a reference to `abc`". – cyphar Sep 18 '15 at 01:02
  • This comes from K&R, 2nd Ed. http://stackoverflow.com/questions/27445255/where-does-the-word-dereferencing-come-from It was originally just referred to as "indirection". While "de-referencing" was used by K&R synonymously with "indirection", it's noted that the use of "reference"/"referencing" in C has **always** been incorrect/erroneous. – Cloud Sep 18 '15 at 01:06
  • Yes, I understand that K&R didn't call it that. I understand that, technically, passing a reference is different. **However**, I'm contesting the general consensus that "referencing" is incorrect/erroneous. It's stupid that the inverse operation of **de**referencing is not called **referencing**. It's not like there are a lot of programming languages that actually do passing-by-reference these days anyway... – cyphar Sep 18 '15 at 01:08
  • It sounds bizarre that the definitions do not conform to the mechanics of the English language, I'll give it that, but language is a tricky thing (devil's in the details, etc), so it helps to be very exact, especially in technical topics. – Cloud Sep 18 '15 at 01:10
  • Anyway, I've updated the answer. I do concede that since the two terms are technically different, I guess we're stuck with the terminology. – cyphar Sep 18 '15 at 01:12