Groups | Search | Server Info | Login | Register


Groups > comp.os.linux.development.system > #380

Re: Calling libiptc API from a separate thread in a C program throws segmentati

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.os.linux.development.system
Subject Re: Calling libiptc API from a separate thread in a C program throws segmentati
Date 2012-01-17 18:48 +0000
Organization A noiseless patient Spider
Message-ID <20120117102310.171@kylheku.com> (permalink)
References <bKSdndZeXcWVzYjS4p2dnAA@giganews.com>

Show all headers | View raw


On 2012-01-17, kkgarg78 <nospam_kamalkgarg@gmail.com.invalid> wrote:
> 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.

Even if there is no change to the rules? Are you monitoring for changes,
or updating?

>  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().

I'm looing at the library sources online. It does not look thread-safe at all,
though it's not obvious how that could cause a crash in the above program,
since your use of the library is serialized (no concurrrency between
your main and thread_func).

You should be fine if you have a dedicated thread which calls this
library, or put a big mutex around it.

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

This is not how you compile programs for multithreading. You want:

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

I.e. the -pthread gcc option, not -lpthread. It is not enough to link the
threading library (and -pthread does that).

One consequence of using -lpthread alone is that the "errno" variable will
not be properly thread-specific.

At one point -pthread was equivalent to -D_REENTRANT and -lpthread,
but that may change in the future if it has not already; it's better
to use the -pthread interface.

Back to comp.os.linux.development.system | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Calling libiptc API from a separate thread in a C program throws segmentati kkgarg78 <nospam_kamalkgarg@gmail.com.invalid> - 2012-01-17 04:40 -0600
  Re: Calling libiptc API from a separate thread in a C program throws segmentati Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 18:48 +0000
    Re: Calling libiptc API from a separate thread in a C program throws segmentati Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-17 21:26 +0000
      Re: Calling libiptc API from a separate thread in a C program throws segmentati Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 22:03 +0000
        Re: Calling libiptc API from a separate thread in a C program throws segmentati Richard Kettlewell <rjk@greenend.org.uk> - 2012-01-17 22:07 +0000
          Re: Calling libiptc API from a separate thread in a C program throws segmentati Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 22:34 +0000
            Re: Calling libiptc API from a separate thread in a C program throws segmentati Richard Kettlewell <rjk@greenend.org.uk> - 2012-01-17 22:51 +0000
              Re: Calling libiptc API from a separate thread in a C program throws segmentati Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 23:13 +0000
        Re: Calling libiptc API from a separate thread in a C program throws segmentati Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-17 22:56 +0000
  Re: Calling libiptc API from a separate thread in a C program throws segmentati Richard Kettlewell <rjk@greenend.org.uk> - 2012-01-21 11:29 +0000

csiph-web