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

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

问题描述

Linux 环境变量中可以存储的数据量是否有限制,如果有:是什么?

对于 Windows,我发现以下 知识库文章 总结如下:Windows XP 或更高版本:8191 个字符Windows 2000/NT 4.0:2047 个字符

解决方案

我认为 Linux 上没有每个环境变量的限制.所有环境变量放在一起的总大小在 execve() 时间受到限制.参见参数和环境大小的限制"此处了解更多信息.>

进程可以使用 setenv()putenv() 将环境扩展到 exec 分配的初始空间之外.

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

#include #include #include #include int main(void){size_t 大小 = 1 <<28;/* 256 MB */字符 *var;var = malloc(size);如果(变量 == NULL){perror(malloc");返回 1;}memset(var, 'X', size);var[size - 1] = '';var[0] = 'A';var[1] = '=';如果(putenv(var)!= 0){perror(putenv");返回 1;}/* 演示由 paxdiablo 解释的 E2BIG 故障 */execl("/bin/true", "true", (char *)NULL);perror(execl");printf("A=%s
", getenv("A"));返回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] = '';
  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
", getenv("A"));

  return 0;
}

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

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