james james - 1 year ago 68
C Question

understanding requirements for execve and setting environment vars

We are having a lot of trouble interpreting our teacher. We asked for clarification and got the following back from him

  1. For execve, send it a environment you setup with your exported variables and create a builtin command to spawn a subshell of /bin/bash, that way you can see your exported variables using env.

    (He is talking about creating our own environment vars here.)

  2. Yes create your own. You can start by copying environ when your shell starts and add only exported variables

This is related to the following post on Stack Overflow by me (reading this other post will help you understand what I am trying to do):

using a new path with execve to run ls command

We are just very confused about this. One more time I will explain what we are trying to do now. Similar to how your Linux shell does this, we need to write our own program that can set environment variables like PATH and USER and whatever other vars the user wants to define.

An example of how you would call this would be (inside your program at its prompt):

mysetenv dog spike

which would create an environment variable looking like "dog=spike"

More importantly, we need to be able to set our own PATH variable and send it to an
command. This is the confusing part because, based on all of our questions, we don't understand what we are supposed to do.

Answer Source

It is actually very simple. You already know that your arguments are a list of char *, terminated by a NULL pointer. Similarly, the environment is simply a list of char *, terminated by a NULL pointer. Conventionally, the values in the list take the form VARNAME=var-value, though you can pass other formats if you wish.

So, to take a simple case:

#include <unistd.h>
#include <stdio.h>

int main(void)
    char *argv[] = { "/bin/sh", "-c", "env", 0 };
    char *envp[] =
    execve(argv[0], &argv[0], envp);
    fprintf(stderr, "Oops!\n");
    return -1;

In this example, the program will run /bin/sh with arguments -c and env, which means that the shell will run the env program found on its current PATH. The environment here is set to contain 5 values in the orthodox format. If you change env to date (or env; date), you will see the effect of the TZ setting, for example. When I run that on my MacOS X machine, the output is:


The shell has added environment variables SHLVL, _ and PWD to the ones I set explicitly in the execve() call.

You can also do fancier things, such as copy in some of the other environment variables from your genuine environment where they do not conflict with the ones you want to set explicitly. You can also play games like having two values for a single variable in the environment - which one takes effect? And you can play games with variable names that contain spaces (the shell doesn't like that much), or entries that do not match the 'varname=value' notation at all (no equals sign).