Page MenuHomeFreeBSD

jexec: Add -e parameter to customize the environment
ClosedPublic

Authored by dtxdf on Sun, Jan 11, 10:59 PM.
Tags
Referenced Files
F142439569: D54660.id169736.diff
Mon, Jan 19, 11:35 PM
Unknown Object (File)
Mon, Jan 19, 6:30 PM
Unknown Object (File)
Sun, Jan 18, 8:52 PM
Unknown Object (File)
Sun, Jan 18, 8:17 PM
Unknown Object (File)
Sun, Jan 18, 3:37 PM
Unknown Object (File)
Fri, Jan 16, 9:28 PM
Unknown Object (File)
Fri, Jan 16, 3:00 PM
Unknown Object (File)
Fri, Jan 16, 11:56 AM

Details

Summary

Currently, to define a new environment variable or modify an existing one, we need to use env(1), which may or may not be available inside the jail, especially in OCI containers created with the scratch layer (i.e., those containers that are only a single static binary, plus configuration files and related stuff). With this option, we can specify environment variables of arbitrary length for the specified process running inside the jail.

Examples:

This also fixes:

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

Since putenv(3) does not create a copy, I had to implement a function to emulate the old behavior, so now setenv(3) is used after parsing the environment variable.

Align putenv_copy() function declaration for consistency.

Why does it matter that putenv(3) doesn't create a copy?

usr.sbin/jexec/jexec.c
81

Good coding hygiene I guess, but argc is already an int, so you'll never make it past INT_MAX anyway.

You can avoid this temporary array by running the getopt loop a second time when you;re actually setting the variables, ignoring anything except for "-e".

183

This test would be more useful in the optarg stage. Alternately, you could allow equals-less variables as a way to indicate it should be unset.

  • Avoid memory allocations.
  • Parse -e twice. Once to verify correctness. Again to set the variable.

Why does it matter that putenv(3) doesn't create a copy?

Thank you very much for your recommendations, this simplifies the code a lot.

My first idea was to create an array of strings and then set the variables, but since I called putenv(3) and then freed memory, AFAIK this could cause unexpected behavior, although in practice it works because the memory remains there, so to offer a cleaner solution, I created putenv_copy() to emulate the old behavior that copies the string. However, thanks to your recommendations, I haven't had to complicate things too much.

  • Add missing error handling for putenv(3).
usr.sbin/jexec/jexec.c
154

Well I learned about the leading colon in getopt today! But I don't think it'll work here: it doesn't know the difference between the end of options and an option (besides -e) that has a separate argument, and will stop processing early on a command line like "jexec -u user -e FOO=BAR". I think you'll need to use the original option string to prevent this.

157

This should just be optarg, or a non-space-separated "-eFOO=BAR" will include the "-e" in the variable.

  • Use jexec_args variable with all parameters in both getopt calls.
This revision was not accepted when it landed; it landed in state Needs Review.Thu, Jan 15, 5:34 AM
This revision was automatically updated to reflect the committed changes.