serenity icon indicating copy to clipboard operation
serenity copied to clipboard

LibCore: Introduce and use Core::Environment wrapper for FOOenv() functions

Open AtkinsSJ opened this issue 1 year ago • 9 comments

Another LibC thing that I felt deserved a nicer wrapper. A couple of these we had wrappers for in Core::System, but not all of them, and the environment is a separate enough thing to belong on its own. So, the existing wrapper functions are moved to Core::Environment, and the others filled in.

Initially I thought of keeping a HashMap around of the environment variables, but given that the environ can be modified from other places, it made more sense to just wrap the LibC functions and provide a nicer API.

Of course, "nicer" is my opinion, so I could be wrong. Feedback welcome.

AtkinsSJ avatar Jan 30 '24 18:01 AtkinsSJ

CI failure is legitimate, I need to put replacement implementations in for clearenv() and secure_getenv() on MacOS.

AtkinsSJ avatar Jan 30 '24 19:01 AtkinsSJ

Changes:

  • Rebase!
  • Fix MacOS (hopefully???): Fall back to regular getenv(), and copy the implementation of clearenv() from LibC.
  • Replace the goofy for_each_entry() with an iterator. I know very little about iterators so I might have made a boo-boo so look out for that.
  • Use StringBuilders instead of to_byte_string(). This technically can produce OOM errors, and the other places TRY() those because they already return ErrorOr, but having getenv() be fallible feels weird so I just made it use the non-errory functions.
  • Added Core::Environment::has(name) for that one case. It's pretty nice, I won't lie.
  • Correct that outln() usage.
  • Remove that now-unneeded MacOS include from System.cpp

@alimpfard does this fix your ownership concerns? I'm not 100% sure I understood that.

AtkinsSJ avatar Feb 07 '24 17:02 AtkinsSJ

Use StringBuilders instead of to_byte_string().

And

for (auto entry : entries())
  if (entry.name == name)
    return entry.value;
return {};

would be both always infallible and shorter...

And it's also not like Serenity's, GNU's, or LLVM's libc implements something smarter than this loop: 1, 2, 3.

DanShaders avatar Feb 07 '24 17:02 DanShaders

Changes:

  • Iterate entries() instead of calling getenv()
  • Replace get_secure() with a second parameter to Environment::get(), which defaults to non-secure.
  • Remove unnecessary const qualifiers in the Entry iterator.
  • Rebase on master to make sure nothing's broken. (Second push)

AtkinsSJ avatar Feb 25 '24 17:02 AtkinsSJ

Changes:

  • Go back to calling getenv()/secure_getenv() directly, because otherwise we have to include <sys/auxv.h> which doesn't always exist, including on CI!
  • Rename Environment::Secure to Environment::SecureOnly.

AtkinsSJ avatar Feb 25 '24 17:02 AtkinsSJ

Missing a header for environ on macOS

/Users/runner/work/1/s/Userland/Libraries/LibCore/Environment.cpp:61:24: error: use of undeclared identifier 'environ'

https://dev.azure.com/SerenityOS/SerenityOS/_build/results?buildId=44741&view=logs&j=2351e412-c793-598f-27f8-72c4f2d55ba1&t=9c50333a-ada2-5596-d44b-d8dcee270ad2&l=99

ADKaster avatar Feb 26 '24 00:02 ADKaster

Missing a header for environ on macOS

/Users/runner/work/1/s/Userland/Libraries/LibCore/Environment.cpp:61:24: error: use of undeclared identifier 'environ'

dev.azure.com/SerenityOS/SerenityOS/_build/results?buildId=44741&view=logs&j=2351e412-c793-598f-27f8-72c4f2d55ba1&t=9c50333a-ada2-5596-d44b-d8dcee270ad2&l=99

Changes:

  • Use the environ wrapper so things work on macOS. I'm... really surprised this didn't come up before, I was using environ in multiple places.

AtkinsSJ avatar Feb 26 '24 11:02 AtkinsSJ

Changes:

  • Another macOS fix, marking a parameter as [[maybe_unused]] which isn't used in the macOS code. Deary me, macOS will be the end of me. :laughing:

AtkinsSJ avatar Feb 26 '24 11:02 AtkinsSJ

Changes:

  • Fixed the conflict. :smiling_face_with_tear:

AtkinsSJ avatar Feb 26 '24 21:02 AtkinsSJ