libsmbclient threadsafeness

Michael B Allen mba2000 at
Mon Oct 18 21:56:03 GMT 2004

David Wuertele said:
> I'm writing a massively-threaded application that makes calls into
> libsmbclient.  I have found that some of the commands are not
> threadsafe, so to be conservative I'm now wrapping every call into
> libsmbclient with pthread_mutex_lock().  For example:
>   pthread_mutex_lock (&libsmbclient_mutex); {
>     ip_list_ptr = name_query (socket_fd, star, 0, True, True,

I don't think anyone is going to be brave enough to tell you which
functions are or are not thread safe.

I would look at creating thread safe calls like this:

ts_cli_nt_session_open(struct cli_state *cli, const int pipe_idx,
pthread_mutex_t *lock)
    BOOL ret;
    ret = cli_nt_session_open(cli, pipe_idx);
    return ret;

This would be a relatively non-intrusive way to make the library
thread-safe and I think you can autogenerate these functions pretty
easily. For example, create a tab delimited text file of prototypes like:

  BOOL\tcli_nt_session_open\tstruct cli_state *cli, const int pipe_idx
  struct cli_state *\tcli_initialise\tstruct cli_state *cli

where \t is a tab and thing use the csvprint utility [1] to print the
functions like:

  $ ./csvprint smbcliprotos.csv "%1\n%2(%3, pthread_mutex_t
*lock)\n{\n\t%1 ret;\n\tpthread_mu...."

Then touch up the source a little if necessary (like functions that return

Also bare in mind that when using a technique like this the network will
effectively be locked from the outside when you make a call. If all state
was in struct cli_state you would be in a position to make concurrent
calls but unforunately the library doesn't support that right now. So when
you say "massively threaded" I hope you realize that using more threads
isn't necessarily going to increase paralellism. In fact if you plan on
doing a lot of smbclient I/O I don't think running more than a handful of
threads would make any difference in performance.

This is actually something that jCIFS is very good at. But of course it's
Java so I don't think that helps.



More information about the samba-technical mailing list