从 C 程序中的单独线程调用 libiptc API 会引发分段错误

发布于 2024-12-27 12:26:24 字数 2073 浏览 1 评论 0原文

我正在努力使用 libiptc 通过自定义 C 程序执行 iptables 更新。要求是每 2 秒从一个单独的线程调用 iptc API。

我编写了一个简单的 C 程序来尝试从单独的线程调用 iptc API。下面贴出c程序。

/*** thread_iptc.c ***/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void* thread_func(void* unused)
{
        struct iptc_handle *handle = NULL;
        char *table = "filter";

        while (1)
        {
                printf("\nthread_func(): While loop.\n");
                handle = iptc_init(table);
                if (handle) {
                        printf("thread_func(): handle is not NULL.\n");
                        iptc_free(handle);
                }
                else
                        printf("thread_func(): handle is NULL.\n");

                sleep(2);
        }

        return NULL;
}


int main()
{
        struct iptc_handle *handle = NULL;
        char *table = "filter";
        pthread_t thread_id;

        handle = iptc_init(table);
        if (handle) {
                printf("main(): handle is not NULL.\n");
                iptc_free(handle);
        }
        else
                printf("main(): handle is NULL.\n");


        pthread_create(&thread_id, NULL, &thread_func, NULL);
        pthread_join(thread_id, NULL);

        return 0;
}

我面临的问题是,从 main 函数调用时,对 iptc_init() 和 iptc_free() 的调用效果很好。但是,当从 thread_func() 调用时,对 iptc_free() 的调用失败并出现“分段错误”。

程序输出:

# ./test 
main(): handle is not NULL.

thread_func(): While loop.
thread_func(): handle is not NULL.
Segmentation fault

编译:

# gcc -o test thread_iptc.c -lpthread -lext4 -lext6 -lip4tc -lip6tc -liptc -lxtables -ldl

GDB Backtrace

#0  0x00007ffff79be303 in iptc_free () from /lib64/libip4tc.so.0
#1  0x00000000004007f3 in thread_func ()
#2  0x00007ffff7bc77e1 in start_thread () from /lib64/libpthread.so.0
#3  0x00007ffff6efb8ed in clone () from /lib64/libc.so.6

我在编译期间或调用新线程时是否遗漏了某些内容?

I am working on performing iptables update through a custom c program using libiptc. The requirement is to invoke iptc APIs from a separate thread every 2 seconds.

I have written a simple C program to try out invoking of iptc APIs from a separate thread. The c program is pasted below.

/*** thread_iptc.c ***/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void* thread_func(void* unused)
{
        struct iptc_handle *handle = NULL;
        char *table = "filter";

        while (1)
        {
                printf("\nthread_func(): While loop.\n");
                handle = iptc_init(table);
                if (handle) {
                        printf("thread_func(): handle is not NULL.\n");
                        iptc_free(handle);
                }
                else
                        printf("thread_func(): handle is NULL.\n");

                sleep(2);
        }

        return NULL;
}


int main()
{
        struct iptc_handle *handle = NULL;
        char *table = "filter";
        pthread_t thread_id;

        handle = iptc_init(table);
        if (handle) {
                printf("main(): handle is not NULL.\n");
                iptc_free(handle);
        }
        else
                printf("main(): handle is NULL.\n");


        pthread_create(&thread_id, NULL, &thread_func, NULL);
        pthread_join(thread_id, NULL);

        return 0;
}

The problem, I am facing is that call to both iptc_init() and iptc_free() works well when called from main function. However, call to iptc_free() fails with "Segmentation Fault" when called from thread_func().

Program Output:

# ./test 
main(): handle is not NULL.

thread_func(): While loop.
thread_func(): handle is not NULL.
Segmentation fault

Compilation:

# gcc -o test thread_iptc.c -lpthread -lext4 -lext6 -lip4tc -lip6tc -liptc -lxtables -ldl

GDB Backtrace

#0  0x00007ffff79be303 in iptc_free () from /lib64/libip4tc.so.0
#1  0x00000000004007f3 in thread_func ()
#2  0x00007ffff7bc77e1 in start_thread () from /lib64/libpthread.so.0
#3  0x00007ffff6efb8ed in clone () from /lib64/libc.so.6

Am I missing something during compilation or while invoking a new thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文