gnu syslog()、openlog()和closelog()线程安全吗?

时间:2022-04-07 21:00:18

I have been searching for some time and haven't found a definitive answer yet. The only link I found till now which sheds some light on this is here.

我已经寻找了一段时间,还没有找到一个确定的答案。到目前为止,我发现的唯一一个能让我们了解这一点的链接是这里。

3 个解决方案

#1


12  

According to the POSIX Specification syslog(), openlog() and closelog() are thread safe.

根据POSIX规范syslog(), openlog()和closelog()是线程安全的。

There's another thread that answers this question as well and it is dated as far back as 2009. Here's the link syslog_r for Linux?.

还有另一个线索也可以回答这个问题,它可以追溯到2009年。这里是Linux的链接syslog_r。

#2


5  

(While I was working on this experiment, @user3088572 answered the question. Since I had this half-way complete, I'll post it anyway.)

(当我在做这个实验时,@user3088572回答了这个问题。既然我已经完成了一半,我还是把它贴出来吧。

The answer is "Yes", but this was determined purely by experimentation.

答案是肯定的,但这纯粹是由实验决定的。

Source

Main process spawns three threads and waits while threads all print different strings at same time. Then after 1 second, it tells them all to stop and exits.

主进程生成三个线程并等待线程同时打印不同的字符串。然后1秒后,它告诉所有人停止并退出。

#include <pthread.h>
#include <syslog.h>

static int go = 1;

void * routine(void * str)
{
    int c = 0;
    while(go)
        syslog(7, "%d: %s", c++, (char *)str);
}

int main(int argc, char * argv[])
{
    pthread_t t1, t2, t3;
    char str1[100] = "111111111111111111111111111111111111111111111111111111111\n";
    char str2[100] = "222222222222222222222222222222222222222222222222222222222\n";
    char str3[100] = "333333333333333333333333333333333333333333333333333333333\n";

    openlog("syslog-test", LOG_PID, LOG_USER);

    pthread_create(&t1, NULL, &routine, str1);
    pthread_create(&t2, NULL, &routine, str2);
    pthread_create(&t3, NULL, &routine, str3);

    sleep(1);
    go = 0; // threads should stop now

    // wait for threads to exit
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);    

    closelog(); 

    return (0);
}

Results

First of all, there were no core dumps or segmentation faults. So that's good.

首先,没有核心转储或分割错误。这很好。

Also, when observing the system logs (see below), we can see that none of the messages have intermixed characters. Each line is either all 1's, 2's, or 3's.

此外,在观察系统日志时(见下面),我们可以看到没有任何消息具有混杂字符。每一行都是1、2、3。

$ tail /var/log/syslog
Dec 18 16:44:18 mach99 syslog-test[23347]: 68: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 69: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 70: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 51: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 49: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 71: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 52: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 53: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 50: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 72: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 54: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 51: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 73: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 52: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 53: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 74: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 55: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 54: 111111111111111111111111111111111111111111111111111111111

#3


4  

The GNU Libc documentation for syslog describes the methods as currently "MT-safe" which is POSIX-speak for "thread-safe".

syslog的GNU Libc文档将这些方法描述为当前的“MT-safe”,即“线程安全”的posix语言。

The methods are described as "AS-Unsafe" (that is, unsafe for use from an asynchronous signal handler).

这些方法被描述为“不安全”(即从异步信号处理程序中使用的不安全)。

The doc also notes (as of April 2015) that these specifications are "preliminary" and not guaranteed to be true in all future versions. (I think this disclaimer applies to the broad idea of categorizing glibc API functions "safety" properties, and this is still a bit of work in progress. I doubt any of the syslog-specific methods would get any looser though.)

《宣言》还指出(截至2015年4月),这些规范是“初步的”,并不能保证在未来的所有版本中都是正确的。(我认为这个免责声明适用于将glibc API函数归类为“安全”属性的宽泛概念,这方面的工作仍在进行中。我怀疑任何系统特定的方法都不会变得更宽松。

#1


12  

According to the POSIX Specification syslog(), openlog() and closelog() are thread safe.

根据POSIX规范syslog(), openlog()和closelog()是线程安全的。

There's another thread that answers this question as well and it is dated as far back as 2009. Here's the link syslog_r for Linux?.

还有另一个线索也可以回答这个问题,它可以追溯到2009年。这里是Linux的链接syslog_r。

#2


5  

(While I was working on this experiment, @user3088572 answered the question. Since I had this half-way complete, I'll post it anyway.)

(当我在做这个实验时,@user3088572回答了这个问题。既然我已经完成了一半,我还是把它贴出来吧。

The answer is "Yes", but this was determined purely by experimentation.

答案是肯定的,但这纯粹是由实验决定的。

Source

Main process spawns three threads and waits while threads all print different strings at same time. Then after 1 second, it tells them all to stop and exits.

主进程生成三个线程并等待线程同时打印不同的字符串。然后1秒后,它告诉所有人停止并退出。

#include <pthread.h>
#include <syslog.h>

static int go = 1;

void * routine(void * str)
{
    int c = 0;
    while(go)
        syslog(7, "%d: %s", c++, (char *)str);
}

int main(int argc, char * argv[])
{
    pthread_t t1, t2, t3;
    char str1[100] = "111111111111111111111111111111111111111111111111111111111\n";
    char str2[100] = "222222222222222222222222222222222222222222222222222222222\n";
    char str3[100] = "333333333333333333333333333333333333333333333333333333333\n";

    openlog("syslog-test", LOG_PID, LOG_USER);

    pthread_create(&t1, NULL, &routine, str1);
    pthread_create(&t2, NULL, &routine, str2);
    pthread_create(&t3, NULL, &routine, str3);

    sleep(1);
    go = 0; // threads should stop now

    // wait for threads to exit
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);    

    closelog(); 

    return (0);
}

Results

First of all, there were no core dumps or segmentation faults. So that's good.

首先,没有核心转储或分割错误。这很好。

Also, when observing the system logs (see below), we can see that none of the messages have intermixed characters. Each line is either all 1's, 2's, or 3's.

此外,在观察系统日志时(见下面),我们可以看到没有任何消息具有混杂字符。每一行都是1、2、3。

$ tail /var/log/syslog
Dec 18 16:44:18 mach99 syslog-test[23347]: 68: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 69: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 70: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 51: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 49: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 71: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 52: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 53: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 50: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 72: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 54: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 51: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 73: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 52: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 53: 111111111111111111111111111111111111111111111111111111111
Dec 18 16:44:18 mach99 syslog-test[23347]: 74: 222222222222222222222222222222222222222222222222222222222
Dec 18 16:44:18 mach99 syslog-test[23347]: 55: 333333333333333333333333333333333333333333333333333333333
Dec 18 16:44:18 mach99 syslog-test[23347]: 54: 111111111111111111111111111111111111111111111111111111111

#3


4  

The GNU Libc documentation for syslog describes the methods as currently "MT-safe" which is POSIX-speak for "thread-safe".

syslog的GNU Libc文档将这些方法描述为当前的“MT-safe”,即“线程安全”的posix语言。

The methods are described as "AS-Unsafe" (that is, unsafe for use from an asynchronous signal handler).

这些方法被描述为“不安全”(即从异步信号处理程序中使用的不安全)。

The doc also notes (as of April 2015) that these specifications are "preliminary" and not guaranteed to be true in all future versions. (I think this disclaimer applies to the broad idea of categorizing glibc API functions "safety" properties, and this is still a bit of work in progress. I doubt any of the syslog-specific methods would get any looser though.)

《宣言》还指出(截至2015年4月),这些规范是“初步的”,并不能保证在未来的所有版本中都是正确的。(我认为这个免责声明适用于将glibc API函数归类为“安全”属性的宽泛概念,这方面的工作仍在进行中。我怀疑任何系统特定的方法都不会变得更宽松。