Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating number of variable arguments for clone function

I am trying to LD_PRELOAD the linux's clone function. In my LD_PRELOADed version I need to log the input paramters before calling the original clone function. However, the problem is that clone takes variable number of arguments. It is declared like this.

int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...
/* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );

Now to pass these arguments to the original clone function, I must know the number of arguments that were passed in. How can I do that?

like image 655
pythonic Avatar asked Mar 11 '26 12:03

pythonic


2 Answers

Use the va_* functions, these are the way to go when working with variable argument lists.

Here's the man page which also contains an example at the end.

like image 115
Johannes Weiss Avatar answered Mar 14 '26 01:03

Johannes Weiss


If there is a NULL terminator, you can call va_arg while it doesn't return NULL.

like image 23
md5 Avatar answered Mar 14 '26 03:03

md5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!