我提前为一些代码转储道歉,我已经尽可能多地修剪了不重要的代码:
// Global vars / mutex stuff
extern char **environ;
pthread_mutex_t env_mutex = PTHREAD_MUTEX_INITIALIZER;
int
putenv_r(char *string)
{
int len;
int key_len = 0;
int i;
sigset_t block;
sigset_t old;
sigfillset(&block);
pthread_sigmask(SIG_BLOCK, &block, &old);
// This function is thread-safe
len = strlen(string);
for (int i=0; i < len; i++) {
if (string[i] == '=') {
key_len = i; // Thanks Klas for pointing this out.
break;
}
}
// Need a string like key=value
if (key_len == 0) {
errno = EINVAL; // putenv doesn't normally return this err code
return -1;
}
// We're moving into environ territory so start locking stuff up.
pthread_mutex_lock(&env_mutex);
for (i = 0; environ[i] != NULL; i++) {
if (strncmp(string, environ[i], key_len) == 0) {
// Pointer assignment, so if string changes so does the env.
// This behaviour is POSIX conformant, instead of making a copy.
environ[i] = string;
pthread_mutex_unlock(&env_mutex);
return(0);
}
}
// If we get here, the env var didn't already exist, so we add it.
// Note that malloc isn't async-signal safe. This is why we block signals.
environ[i] = malloc(sizeof(char *));
environ[i] = string;
environ[i+1] = NULL;
// This ^ is possibly incorrect, do I need to grow environ some how?
pthread_mutex_unlock(&env_mutex);
pthread_sigmask(SIG_SETMASK, &old, NULL);
return(0);
}
正如标题所说,我正在尝试编写 putenv
的线程安全、异步信号安全的可重入版本。该代码的工作原理在于它像 putenv
那样设置环境变量,但我确实有一些担忧:
- 我使异步信号安全的方法感觉有点笨手笨脚,只是阻止所有信号(当然 SIGKILL/SIGSTOP 除外)。或者这是最合适的解决方法。
- 我的信号阻塞位置是否过于保守?我知道
strlen
不能保证是异步信号安全的,这意味着我的信号阻塞必须事先发生,但也许我错了。 - 我相当确定它是线程安全的,考虑到所有函数都是线程安全的并且我锁定了与
environ
的交互,但我很乐意证明不是这样。< - 我真的不太确定它是否可重入。虽然不能保证,但我想如果我勾选其他两个方框,它很可能会重入?
我找到了这个问题的另一个解决方案 here ,其中他们只是设置了适当的信号阻塞和互斥锁定(病态押韵),然后正常调用 putenv
。这有效吗?如果是这样,它显然比我的方法简单得多。
对于大块代码感到抱歉,我希望我已经建立了一个MCVE .为了简洁起见,我在代码中遗漏了一些错误检查。谢谢!
如果您想自己测试代码,这是其余代码,包括主要代码:
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
// Prototypes
static void thread_init(void);
int putenv_r(char *string);
int
main(int argc, char *argv[]) {
int ret = putenv_r("mykey=myval");
printf("%d: mykey = %s\n", ret, getenv("mykey"));
return 0;
}
最佳答案
这段代码有问题:
// If we get here, the env var didn't already exist, so we add it.
// Note that malloc isn't async-signal safe. This is why we block signals.
environ[i] = malloc(sizeof(char *));
environ[i] = string;
它在堆上创建一个char *
,将那个char *
的地址分配给environ[i]
,然后覆盖那个值包含在 string
中的地址。那是行不通的。它不保证 environ
之后以 NULL 终止。
因为 char **environ
is a pointer to an array of pointers .数组中的最后一个指针是 NULL
- 这就是代码可以告诉它已到达环境变量列表末尾的方式。
像这样的东西应该会更好:
unsigned int envCount;
for ( envCount = 0; environ[ envCount ]; envCount++ )
{
/* empty loop */;
}
/* since environ[ envCount ] is NULL, the environ array
of pointers has envCount + 1 elements in it */
envCount++;
/* grow the environ array by one pointer */
char ** newEnviron = realloc( environ, ( envCount + 1 ) * sizeof( char * ) );
/* add the new envval */
newEnviron[ envCount - 1 ] = newEnvval;
/* NULL-terminate the array of pointers */
newEnviron[ envCount ] = NULL;
environ = newEnviron;
请注意,没有错误检查,它假设原始 environ
数组是通过调用 malloc()
或类似方法获得的。如果该假设错误,则行为未定义。
关于c - 线程安全、可重入、异步信号安全 putenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41257768/