Calling libiptc API from a separate thread in a C program throws segmentation fault

This is a discussion on Calling libiptc API from a separate thread in a C program throws segmentation fault within the C Programming forums, part of the General Programming Boards category; I am working on performing iptables update through a custom c program using libiptc. The requirement is to invoke iptc ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Calling libiptc API from a separate thread in a C program throws segmentation fault

    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:


    Code:
    /*** 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:


    Code:
    # ./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
    Code:
    #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?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I tried your code and it looks fine for me (ubuntu 11.04, gcc 4.5.2, iptables-dev_1.4.10), i get non-null handles every 2 seconds as expected.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Yes you are right!,

    I found it working on Ubuntu as well. But the same program throws segmentation fault when executed on RHEL6.1. The strange thing is that invoking iptc APIs works good when called from main but fails to work when called from a thread. I got the value of pointer printed and it looks like that iptc_init returns some invalid pointer when called from a thread.

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


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

    Here are the details of my development environment,

    OS: RHEL6.1
    Architecture: x86_64
    IPtables packages installed: iptables-1.4.7-4.el6.x86_64, iptables-devel-1.4.7-4.el6.x86_64

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program has segmentation fault??
    By fsi in forum C Programming
    Replies: 4
    Last Post: 12-04-2011, 10:25 AM
  2. C++ Segmentation Fault When Calling Classes
    By l3xxi in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2011, 09:01 AM
  3. Segmentation Fault when Calling Functions
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 12:04 PM
  4. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 05:47 PM
  5. Segmentation fault !, program works:S
    By jspri in forum C Programming
    Replies: 8
    Last Post: 09-28-2004, 06:25 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21