PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/fpm/fpm_env.c

http://github.com/dreamcat4/php-fpm
C | 175 lines | 130 code | 38 blank | 7 comment | 31 complexity | 0a4fd9b06a4afbb8ea43992adbc4402f MD5 | raw file
  1. /* $Id: fpm_env.c,v 1.15 2008/09/18 23:19:59 anight Exp $ */
  2. /* (c) 2007,2008 Andrei Nigmatulin */
  3. #include "fpm_config.h"
  4. #ifdef HAVE_ALLOCA_H
  5. #include <alloca.h>
  6. #endif
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "fpm_env.h"
  11. #include "zlog.h"
  12. #ifndef HAVE_SETENV
  13. #ifdef (__sparc__ || __sparc)
  14. int setenv(name, value, clobber)
  15. char *name;
  16. char *value;
  17. int clobber;
  18. {
  19. char *malloc();
  20. char *getenv();
  21. char *cp;
  22. if (clobber == 0 && getenv(name) != 0)
  23. { return (0); }
  24. if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
  25. { return (1); }
  26. sprintf(cp, "%s=%s", name, value);
  27. return (putenv(cp));
  28. }
  29. #else
  30. int setenv(char *name, char *value, int overwrite)
  31. {
  32. int name_len = strlen(name);
  33. int value_len = strlen(value);
  34. char *var = alloca(name_len + 1 + value_len + 1);
  35. memcpy(var, name, name_len);
  36. var[name_len] = '=';
  37. memcpy(var + name_len + 1, value, value_len);
  38. var[name_len + 1 + value_len] = '\0';
  39. return putenv(var);
  40. }
  41. #endif
  42. #endif
  43. #ifndef HAVE_CLEARENV
  44. void clearenv()
  45. {
  46. char **envp;
  47. char *s;
  48. /* this algo is the only one known to me
  49. that works well on all systems */
  50. while (*(envp = environ)) {
  51. char *eq = strchr(*envp, '=');
  52. s = strdup(*envp);
  53. if (eq) { s[eq - *envp] = '\0'; }
  54. unsetenv(s);
  55. free(s);
  56. }
  57. }
  58. #endif
  59. #ifndef HAVE_UNSETENV
  60. void unsetenv(const char *name)
  61. {
  62. if(getenv(name)!=NULL)
  63. {
  64. int ct=0;
  65. int del=0;
  66. while(environ[ct] != NULL)
  67. {
  68. if (nvmatch(name, environ[ct]) != 0) del=ct;
  69. { ct++; }
  70. }
  71. /* isn't needed free here?? */
  72. environ[del]=environ[ct-1];
  73. environ[ct-1]=NULL;
  74. }
  75. }
  76. static char * nvmatch(s1, s2)
  77. register char *s1, *s2;
  78. {
  79. while(*s1 == *s2++)
  80. {
  81. if(*s1++ == '=')
  82. { return(s2); }
  83. }
  84. if(*s1 == '\0' && *(s2-1) == '=')
  85. { return(s2); }
  86. return(NULL);
  87. }
  88. #endif
  89. int fpm_env_init_child(struct fpm_worker_pool_s *wp)
  90. {
  91. struct key_value_s *kv;
  92. clearenv();
  93. for (kv = wp->config->environment; kv; kv = kv->next) {
  94. setenv(kv->key, kv->value, 1);
  95. }
  96. if (wp->user) {
  97. setenv("USER", wp->user, 1);
  98. }
  99. if (wp->home) {
  100. setenv("HOME", wp->home, 1);
  101. }
  102. return 0;
  103. }
  104. static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
  105. {
  106. struct key_value_s *kv;
  107. kv = wp->config->environment;
  108. for (kv = wp->config->environment; kv; kv = kv->next) {
  109. if (*kv->value == '$') {
  110. char *value = getenv(kv->value + 1);
  111. if (!value) { value = ""; }
  112. free(kv->value);
  113. kv->value = strdup(value);
  114. }
  115. /* autodetected values should be removed
  116. if these vars specified in config */
  117. if (!strcmp(kv->key, "USER")) {
  118. free(wp->user);
  119. wp->user = 0;
  120. }
  121. if (!strcmp(kv->key, "HOME")) {
  122. free(wp->home);
  123. wp->home = 0;
  124. }
  125. }
  126. return 0;
  127. }
  128. int fpm_env_init_main()
  129. {
  130. struct fpm_worker_pool_s *wp;
  131. for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
  132. if (0 > fpm_env_conf_wp(wp)) {
  133. return -1;
  134. }
  135. }
  136. return 0;
  137. }