它是安全使用的getenv()的静态初始化,也就是之前的主()? [英] Is it safe to use getenv() in static initializers, that is, before main()?

查看:125
本文介绍了它是安全使用的getenv()的静态初始化,也就是之前的主()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着<一个href=\"http://www.google.com/search?q=advanced+programming+in+the+unix+environment&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a\"相对=nofollow>史蒂文斯,并在 Posix的程序员指南,我能找到的最好是

I looked in Stevens, and in the Posix Programmer's Guide, and the best I can find is

弦称为数组中的环境的过程开始时可用。
  

An array of strings called the enviroment is made available when the process begins. This array is pointed to by the external variable environ, which is defined as:

的extern字符** ENVIRON;

这是说的 ENVIRON 的变量,有我在犹豫。我想说

It's that environ variable that has me hesitating. I want to say

-The调用进程/壳已分配块的空结尾的字符串

-The calling process/shell has already allocated the block of null terminated strings

-the外部变量 ENVIRON 被用作入口点的的getenv()

-the 'external' variable environ is used as the entry point by getenv().

- 事实本身的随时来电的的getenv()的静态初始化中

-ipso facto feel free to call getenv() within a static initializer.

但我找不到任何保证的 ENVIRON 的precedes所有其他静态初始化code的静态初始化。我是不是该得太多?

But I can't find any guarantee that the 'static initialization' of environ precedes all the other static initialization code. Am I overthinking this?

我的的平台(AMD皓龙,红帽4,GCC 3.2.3),设置的 LD_DEBUG 显示, ENVIRON 的获取设置的的我的静态初始化被调用。这是要知道一件很好的事;感谢@ codelogic。但它不一定是结果,我会得到所有平台。

On my platform (AMD Opteron, Redhat 4, GCC 3.2.3), setting LD_DEBUG shows that environ gets set before my static initializers are called. This is a nice thing to know; thanks, @codelogic. But it is not necessarily the result I'd get on all platforms.

此外,虽然我直觉与@ChrisW同意的C / C ++运行时库的行为,这是一种基于经验的只是我的直觉。因此,任何人谁可以管起来从某个地方权威低保 ENVIRON 的报价是有被称为静态初始化之前,加分!

Also, while I agree intuitively with @ChrisW on the behavior of the C/C++ runtime library, this is just my intuition based on experience. So anyone who can pipe up with a quote from someplace authoritative guaranteeing that environ is there before static initializers are called, bonus points!

推荐答案

我认为你可以设置LD_DEBUG看到的确切顺序运行程序:

I think you can run your program with LD_DEBUG set to see the exact order:

LD_DEBUG=all <myprogram>

编辑:
如果你看一下运行时链接程序的源$ C ​​$ C(glibc的2.7),特别是在文件:

If you look at the source code of the runtime linker (glibc 2.7), specifically in files:


  • sysdeps / UNIX / SYSV / Linux的/的init-first.c

  • sysdeps / I386 / INIT-first.c

  • CSU / libc中,start.c

  • sysdeps / I386 /精灵/ start.S中

您将看到的argc,argv的和ENVIRON(__ ENVIRON别名)设置的任何全局构造函数的调用(初始化函数)之前。您可以按照直接从_start开始执行,实际的入口点(start.S中)。正如你所援引史蒂文斯的 的,这表明环境分配在初始化过程的一开始就发生串称为环境的数组,当进程开始提供。此链接器code,它做同样的支持,应该给你心态足够的和平: - )

you will see that argc, argv and environ (alias for __environ) are set before any global constructors are called (the init functions). You can follow the execution starting right from _start, the actual entry point (start.S). As you've quoted Stevens "An array of strings called the enviroment is made available when the process begins", suggesting that environment assignment happens at the very beginning of the process initialization. This backed by the linker code, which does the same, should give you sufficient peace of mind :-)

编辑2:另外值得一提的是设置ENVIRON是早期,足量,即使是运行时链接可以查询,以确定是否要输出冗长(LD_DEBUG)

EDIT 2: Also worth mentioning is that environ is set early enough that even the runtime linker can query it to determine whether or not to output verbosely (LD_DEBUG).

这篇关于它是安全使用的getenv()的静态初始化,也就是之前的主()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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