prenv.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef prenv_h___
  6. #define prenv_h___
  7. #include "prtypes.h"
  8. /*******************************************************************************/
  9. /*******************************************************************************/
  10. /****************** THESE FUNCTIONS MAY NOT BE THREAD SAFE *********************/
  11. /*******************************************************************************/
  12. /*******************************************************************************/
  13. PR_BEGIN_EXTERN_C
  14. /*
  15. ** PR_GetEnv() -- Retrieve value of environment variable
  16. **
  17. ** Description:
  18. ** PR_GetEnv() is modeled on Unix getenv().
  19. **
  20. **
  21. ** Inputs:
  22. ** var -- The name of the environment variable
  23. **
  24. ** Returns:
  25. ** The value of the environment variable 'var' or NULL if
  26. ** the variable is undefined.
  27. **
  28. ** Restrictions:
  29. ** You'd think that a POSIX getenv(), putenv() would be
  30. ** consistently implemented everywhere. Surprise! It is not. On
  31. ** some platforms, a putenv() where the argument is of
  32. ** the form "name" causes the named environment variable to
  33. ** be un-set; that is: a subsequent getenv() returns NULL. On
  34. ** other platforms, the putenv() fails, on others, it is a
  35. ** no-op. Similarly, a putenv() where the argument is of the
  36. ** form "name=" causes the named environment variable to be
  37. ** un-set; a subsequent call to getenv() returns NULL. On
  38. ** other platforms, a subsequent call to getenv() returns a
  39. ** pointer to a null-string (a byte of zero).
  40. **
  41. ** PR_GetEnv(), PR_SetEnv() provide a consistent behavior
  42. ** across all supported platforms. There are, however, some
  43. ** restrictions and some practices you must use to achieve
  44. ** consistent results everywhere.
  45. **
  46. ** When manipulating the environment there is no way to un-set
  47. ** an environment variable across all platforms. We suggest
  48. ** you interpret the return of a pointer to null-string to
  49. ** mean the same as a return of NULL from PR_GetEnv().
  50. **
  51. ** A call to PR_SetEnv() where the parameter is of the form
  52. ** "name" will return PR_FAILURE; the environment remains
  53. ** unchanged. A call to PR_SetEnv() where the parameter is
  54. ** of the form "name=" may un-set the envrionment variable on
  55. ** some platforms; on others it may set the value of the
  56. ** environment variable to the null-string.
  57. **
  58. ** For example, to test for NULL return or return of the
  59. ** null-string from PR_GetEnv(), use the following code
  60. ** fragment:
  61. **
  62. ** char *val = PR_GetEnv("foo");
  63. ** if ((NULL == val) || ('\0' == *val)) {
  64. ** ... interpret this as un-set ...
  65. ** }
  66. **
  67. ** The caller must ensure that the string passed
  68. ** to PR_SetEnv() is persistent. That is: The string should
  69. ** not be on the stack, where it can be overwritten
  70. ** on return from the function calling PR_SetEnv().
  71. ** Similarly, the string passed to PR_SetEnv() must not be
  72. ** overwritten by other actions of the process. ... Some
  73. ** platforms use the string by reference rather than copying
  74. ** it into the environment space. ... You have been warned!
  75. **
  76. ** Use of platform-native functions that manipulate the
  77. ** environment (getenv(), putenv(),
  78. ** SetEnvironmentVariable(), etc.) must not be used with
  79. ** NSPR's similar functions. The platform-native functions
  80. ** may not be thread safe and/or may operate on different
  81. ** conceptual environment space than that operated upon by
  82. ** NSPR's functions or other environment manipulating
  83. ** functions on the same platform. (!)
  84. **
  85. */
  86. NSPR_API(char*) PR_GetEnv(const char *var);
  87. /*
  88. ** PR_GetEnvSecure() -- get a security-sensitive environment variable
  89. **
  90. ** Description:
  91. **
  92. ** PR_GetEnvSecure() is similar to PR_GetEnv(), but it returns NULL if
  93. ** the program was run with elevated privilege (e.g., setuid or setgid
  94. ** on Unix). This can be used for cases like log file paths which
  95. ** could otherwise be used for privilege escalation. Note that some
  96. ** platforms may have platform-specific privilege elevation mechanisms
  97. ** not recognized by this function; see the implementation for details.
  98. */
  99. NSPR_API(char*) PR_GetEnvSecure(const char *var);
  100. /*
  101. ** PR_SetEnv() -- set, unset or change an environment variable
  102. **
  103. ** Description:
  104. ** PR_SetEnv() is modeled on the Unix putenv() function.
  105. **
  106. ** Inputs:
  107. ** string -- pointer to a caller supplied
  108. ** constant, persistent string of the form name=value. Where
  109. ** name is the name of the environment variable to be set or
  110. ** changed; value is the value assigned to the variable.
  111. **
  112. ** Returns:
  113. ** PRStatus.
  114. **
  115. ** Restrictions:
  116. ** See the Restrictions documented in the description of
  117. ** PR_GetEnv() in this header file.
  118. **
  119. **
  120. */
  121. NSPR_API(PRStatus) PR_SetEnv(const char *string);
  122. /*
  123. ** PR_DuplicateEnvironment() -- Obtain a copy of the environment.
  124. **
  125. ** Description:
  126. ** PR_DuplicateEnvironment() copies the environment so that it can be
  127. ** modified without changing the current process's environment, and
  128. ** then passed to interfaces such as POSIX execve(). In particular,
  129. ** this avoids needing to allocate memory or take locks in the child
  130. ** after a fork(); neither of these is allowed by POSIX after a
  131. ** multithreaded process calls fork(), and PR_SetEnv does both.
  132. **
  133. ** Inputs:
  134. ** none
  135. **
  136. ** Returns:
  137. ** A pointer to a null-terminated array of null-terminated strings,
  138. ** like the traditional global variable "environ". The array and
  139. ** the strings are allocated with PR_Malloc(), and it is the
  140. ** caller's responsibility to free them.
  141. **
  142. ** In case of memory allocation failure, or if the operating system
  143. ** doesn't support reading the entire environment through the global
  144. ** variable "environ" or similar, returns NULL instead.
  145. **
  146. ** Restrictions:
  147. ** Similarly to PR_GetEnv(), this function may not interoperate as
  148. ** expected with the operating system's native environment accessors.
  149. */
  150. NSPR_API(char **) PR_DuplicateEnvironment(void);
  151. PR_END_EXTERN_C
  152. #endif /* prenv_h___ */