Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use prctl()

The prototype of prctl is

int prctl(int option, unsigned long arg2, unsigned long arg3,
          unsigned long arg4, unsigned long arg5);

in the man page whereas in the header it is declared as a variadic function:

extern int prctl (int __option, ...) __THROW;

  1. Do I have to call it with 5 arguments when I only need 2?
  2. Do I need to cast args to unsigned long?
like image 614
sigsegv Avatar asked Oct 26 '25 22:10

sigsegv


1 Answers

Just pass what you have to pass and write 0 casted to unsigned long in the rest of arguments or skip them entirely. As prctl is declared as variadic function it will handle this situation accordingly.

const char* name = "The user";
if (prctl(PR_SET_NAME, (unsigned long) name,
         (unsigned long)0, (unsigned long)0, (unsigned long)0) == -1)
{
    // handle error
    perror("prctl failed");
    return -1;
}

or

const char* name = "The user";
if (prctl(PR_SET_NAME, (unsigned long) name) == -1)
{
    // handle error
    perror("prctl failed");
    return -1;
}
like image 56
4pie0 Avatar answered Oct 28 '25 13:10

4pie0



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!