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

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

问题描述

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

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?

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

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

推荐答案

我认为Linux上没有针对每个环境的变量限制.所有环境变量的总大小在 execve()时受到限制.参见关于参数和环境大小的限制".此处以获取更多信息.

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.

进程可以使用 setenv() putenv()使环境超出exec分配的初始空间.

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

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

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;
}

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

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