KENV(2)

πŸ’– KENV(2) β€” kernel environment πŸ’–

okay bestie, buckle up, because we're about to talk about kenv(2), the system call (not the userland tool, that's a whole different girlie, see SEE ALSO πŸ‘€). It lives in the System Calls Manual and it's literally the kernel's little environment-variable closet. slay.

🌸 LIBRARY 🌸

Standard C Library (libc, -lc). yeah, she's a libc girlie, no extra libraries needed, we love a low-maintenance queen 🫢

✨ SYNOPSIS ✨

#include <kenv.h>

int
kenv(int action, const char *name, char *value, int len);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the kenv() system call manipulates kernel environment variables. it's giving "the userland actions you already know and love" β€” getting, setting and unsetting env vars β€” but make it kernel. and ALSO she can dump, like, every single entry in the kernel environment. main character energy fr fr πŸ’…

the action argument can be one of these babes:

  • KENV_GET β€” get the value of the variable with the given name. the size of the value buffer is given by len, and bestie, that len should be at least KENV_MVALLEN + 1 bytes so you avoid truncation and make sure it's NUL-terminated. don't skip the +1, i'm begging 😭
  • KENV_SET β€” set or add a variable. the name and value are limited to KENV_MNAMELEN and KENV_MVALLEN characters respectively (and no, the NUL terminator does not count toward that). the len argument tells you the length of the value and it must include the NUL terminator. and this one? superuser only, sorry not sorry πŸ‘‘
  • KENV_UNSET β€” unset the variable with the given name. the value and len arguments are literally ignored, they don't matter, they're just there. also superuser only πŸ‘‘
  • KENV_DUMP β€” dump as much of the kernel environment as will fit into value, whose size is given in len. and here's the cute part: if value is NULL, kenv() returns the number of bytes required to copy out the entire environment. so you can ask "how big should my buffer be, bestie?" first. name is ignored here, she's not needed.

πŸŽ€ RETURN VALUES πŸŽ€

so here's the tea on what she gives back:

  • kenv() returns 0 if it was successful in the case of KENV_SET and KENV_UNSET βœ…
  • it returns the number of bytes copied into value in the case of KENV_DUMP and KENV_GET πŸ“
  • and if an error happens? you get -1 and the global variable errno gets set to tell you what went wrong. classic. πŸ’€

🌈 ERRORS 🌈

the kenv() system call will fail if:

  • [EINVAL] β€” the action argument is not a valid option, OR the length of the value is less than 1 for a KENV_SET. (yeah, empty values? not the vibe.)
  • [ENOENT] β€” no value could be found for name for a KENV_GET or KENV_UNSET. she looked. it's not there. πŸ™ˆ
  • [EPERM] β€” a user other than the superuser tried to set or unset a kernel environment variable. bestie you are not the superuser, sit down πŸ’…
  • [EFAULT] β€” a bad address was encountered while attempting to copy in user arguments or copy out value(s). bad pointer, bad vibe.
  • [ENAMETOOLONG] β€” the name or the value is longer than KENV_MNAMELEN or KENV_MVALLEN characters respectively, OR len did not include the NUL terminator for a KENV_SET. too long, too iconic, rejected.

⭐ SEE ALSO ⭐

πŸ’œ AUTHORS πŸ’œ

  • this manual page was written by Chad David <davidc@FreeBSD.org>. iconic of him honestly.
  • the kenv() system call was written by Maxime Henrion <mux@FreeBSD.org>. king behavior πŸ‘‘

and that's the whole page, bestie 🫢 this cutie comes to you from Debian, dated February 20, 2017, and yes she's still serving. go forth and kenv() responsibly πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from