了解执行和设置环境变量的要求 [英] understanding requirements for execve and setting environment vars

查看:148
本文介绍了了解执行和设置环境变量的要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在解释老师方面遇到很多麻烦.我们要求澄清,并从他那里得到了以下答复

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

  1. 对于execve,向其发送一个使用导出的变量设置的环境,并创建一个内置命令以生成/bin/bash的子外壳,这样您就可以使用env查看导出的变量.

  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.)

这与我的以下有关Stack Overflow的帖子有关(阅读另一篇文章将有助于您了解我要做什么):

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):

使用新路径execve运行ls命令

我们对此感到非常困惑.再说一次,我将解释我们现在要做什么.与Linux Shell的操作类似,我们需要编写自己的程序来设置环境变量,例如PATH和USER以及用户想要定义的其他任何变量.

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

这将创建一个类似于"dog = spike"的环境变量

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

更重要的是,我们需要能够设置自己的PATH变量并将其发送给exec命令.这是令人困惑的部分,因为基于我们所有的问题,我们不知道我们应该做什么.

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

推荐答案

实际上非常简单.您已经知道参数是char *的列表,并以NULL指针终止.同样,环境只是char *的列表,以NULL指针终止.按照惯例,列表中的值采用VARNAME=var-value的形式,尽管您可以根据需要传递其他格式.

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[] =
    {
        "HOME=/",
        "PATH=/bin:/usr/bin",
        "TZ=UTC0",
        "USER=beelzebub",
        "LOGNAME=tarzan",
        0
    };
    execve(argv[0], &argv[0], envp);
    fprintf(stderr, "Oops!\n");
    return -1;
}

在此示例中,程序将使用参数-cenv运行/bin/sh,这意味着外壳程序将运行在其当前PATH上找到的env程序.此处的环境设置为包含5个正统格式的值.例如,如果将env更改为date(或env; date),则会看到TZ设置的效果.当我在MacOS X机器上运行该命令时,输出为:

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:

USER=beelzebub
PATH=/bin:/usr/bin
PWD=/Users/jleffler/tmp/soq
TZ=UTC0
SHLVL=1
HOME=/
LOGNAME=tarzan
_=/usr/bin/env

shell在我在execve()调用中显式设置的环境变量中添加了环境变量SHLVL_PWD.

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

您还可以做一些更奇妙的事情,例如从您的真实环境中复制其他一些环境变量,这些变量与您要显式设置的变量不冲突.您还可以玩游戏,例如在环境中为一个变量拥有两个值-哪个生效?而且,您可以玩带有包含空格(外壳不太喜欢)的变量名的游戏,或者完全不符合"varname = value"符号(不等号)的条目.

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).

这篇关于了解执行和设置环境变量的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆