环境变量值的最大大小是多少? [英] What is the maximum size of an environment variable value?

查看:790
本文介绍了环境变量值的最大大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于Windows,I在知识库文章中找到以下内容:
Windows XP或更高版本:8191个字符
Windows 2000 / NT 4.0:2047个字符

解决方案

我不认为每个环境的变量限制在Linux上所有环境变量的总体大小在execve()时间有限。请参阅参数和环境大小的限制 here 了解更多信息。



进程可以使用setenv()或putenv()来扩展超出由exec分配的初始空间的环境。



这是一个创建256 MB环境变量的快速而肮脏的程序。

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

int main(void)
{
size_t size = 1<< 28; / * 256 MB * /
char * var;

var = malloc(size);
if(var == NULL){
perror(malloc);
return 1;
}

memset(var,'X',size);
var [size - 1] ='\0';
var [0] ='A';
var [1] ='=';

if(putenv(var)!= 0){
perror(putenv);
return 1;
}

/ *演示由paxdiablo * /
execl(/ bin / true,true,(char *)NULL)解释的E2BIG失败)
perror(execl);


printf(A =%s\\\
,getenv(A));

return 0;
}


Is there a limit to the amount of data that can be stored in an environment variable on Linux, and if so: what is it?

For Windows, I've found following KB article which summarizes to: Windows XP or later: 8191 characters Windows 2000/NT 4.0: 2047 characters

解决方案

I don't think there is a per-environment variable limit on Linux. The total size of all the environment variables put together is limited at execve() time. See "Limits on size of arguments and environment" here for more information.

A process may use setenv() or putenv() to grow the environment beyond the initial space allocated by exec.

Here's a quick and dirty program that creates a 256 MB environment variable.

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

int main(void)
{
    size_t size = 1 << 28; /* 256 MB */
    char *var;

    var = malloc(size);
    if (var == NULL) {
        perror("malloc");
        return 1;
    }

    memset(var, 'X', size);
    var[size - 1] = '\0';
    var[0] = 'A';
    var[1] = '=';

    if (putenv(var) != 0) {
        perror("putenv");
        return 1;
    }

/*  Demonstrate E2BIG failure explained by paxdiablo */
    execl("/bin/true", "true", (char *)NULL);
    perror("execl");   


    printf("A=%s\n", getenv("A"));

    return 0;
}

这篇关于环境变量值的最大大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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