libminijailpreload: proper env cleaning

We add two utility functions in util.c:
- minijail_unsetenv, which removes a var from a given envp array
- minijail_getenv, which returns a var value from a given envp array
  (same as getenv but working on an arbitrary array)

And a static utility function in util.c:
- getenv_index, which returns the index number of a passed variable
  name, or the size of the passed array if not found
  It's used by the above two utility functions, and has also been
  integrated to minijail_setenv

And use them in libminijailpreload.c to:
- properly remove the minijail-injected .so in LD_PRELOAD, without
  completely trashing any preexisting LD_PRELOAD, as was done before
  in the code. A static func truncate_preload_env() only existing in
  libminijailpreload.c is tasked to do this, and
- properly remove the __MINIJAIL_FD (kFdEnvVar) internal variable from
  the child environment before spawning it. Before, this was done by
  zeroing the proper envp entry, which is flagged as being an invalid
  environment by some programs, such as the MongoDB official client:

Without minijail:
$ /tmp/m/bin/mongo --help >/dev/null; echo $?
0

$ env -i ./minijail0 /tmp/m/bin/mongo --help >/dev/null; echo $?
Failed global initialization: BadValue malformed environment block
libminijail[4045108]: child process 4045109 exited with status 1
1

With minijail, with this patch:
$ env -i ./minijail0 /tmp/m/bin/mongo --help >/dev/null; echo $?
0

This can also be seen using `env`:

Without minijail:
$ env -i /usr/bin/env | xxd
$

With minijail, before this patch:
$ env -i ./minijail0 /usr/bin/env | xxd
00000000: 0a0a                                     ..
$

With minijail, with this patch:
$ env -i ./minijail0 /usr/bin/env | xxd
$

Change-Id: I79739a4c6f527c49a31ce31aaa9085fa574a25ee
diff --git a/util.h b/util.h
index 43dccb0..5ed9f94 100644
--- a/util.h
+++ b/util.h
@@ -329,6 +329,34 @@
  */
 ssize_t getmultiline(char **lineptr, size_t *n, FILE *stream);
 
+/*
+ * minjail_getenv: Get an environment variable from @envp. Semantics match the
+ * standard getenv() function, but this operates on @envp, not the global
+ * environment (usually referred to as `extern char **environ`).
+ *
+ * @env       Address of the environment to read from.
+ * @name      Name of the key to get.
+ *
+ * Returns a pointer to the corresponding environment value. The caller must
+ * take care not to modify the pointed value, as this points directly to memory
+ * pointed to by @envp.
+ * If the environment variable name is not found, returns NULL.
+ */
+char *minijail_getenv(char **env, const char *name);
+
+/*
+ * minjail_unsetenv: Clear the environment variable @name from the @envp array
+ * of pointers to strings that have the KEY=VALUE format. If the operation is
+ * successful, the array will contain one item less than before the call.
+ * Only the first occurence is removed.
+ *
+ * @envp      Address of the environment to clear the variable from.
+ * @name      Name of the variable to clear.
+ *
+ * Returns false and modifies *@envp on success, returns true otherwise.
+ */
+bool minijail_unsetenv(char **envp, const char *name);
+
 #ifdef __cplusplus
 }; /* extern "C" */
 #endif