LDAP_OPEN(3)

πŸ’… LDAP_OPEN(3) β€” the libldap glow-up you didn't know you needed

okay so first things first, bestie: this is page LDAP_OPEN(3) from the OpenLDAP LDAP library manual, and no, the filename is not a typo, the page is literally called that even though it's all about ldap_dup and ldap_destroy. iykyk. πŸ’«


✨ NAME ✨

ldap_dup, ldap_destroy, β€” duplicating and destroying LDAP session handles, fr fr 🫢


πŸ“š LIBRARY πŸ“š

OpenLDAP LDAP (libldap, -lldap) β€” that's the shelf you're pulling this off of. πŸŽ€


🧚 SYNOPSIS 🧚

#include <ldap.h>

LDAP *ldap_dup(
       LDAP *old );

int ldap_destroy(
       LDAP *old );

look at her. iconic. not a single wasted character. πŸ‘‘


🌸 DESCRIPTION 🌸

So ldap_dup() duplicates an existing LDAP (LDAP *) session handle. that's it, that's the vibe. the new session handle can be used concurrently with the original session handle β€” like, both of them, at the same time, living their best lives. and in a threaded environment, different threads may execute concurrent requests on the same connection/session without fear of contamination (yes, contamination, the manpage said it, i'm not being dramatic). each session handle manages its own private error results, so nobody's stepping on anybody's feelings. πŸ’–

ldap_destroy() destroys an existing session handle. short king. does exactly what it says.

The ldap_dup() and ldap_destroy() functions are used in conjunction with a "thread safe" version of libldap to enable operation thread safe API calls, so that a single session may be simultaneously used across multiple threads with consistent error handling. literally the whole point. πŸ¦‹

When a session is created through the use of one of the session creation functions β€” including ldap_open(3), ldap_init(3), ldap_initialize(3) or ldap_init_fd(3) β€” an LDAP * session handle is returned to the application. the session handle may be shared amongst threads, however the error codes are unique to a session handle. so if multiple threads are performing different operations using the same session handle, you get inconsistent error codes and return values. messy. we don't like messy. 😭

To prevent this confusion, ldap_dup() is used to duplicate an existing session handle so that multiple threads can share the session, and maintain consistent error information and results. main character move honestly. ✨

ok important part incoming, pay attention bestie πŸ“Œ

The message queues for a session are shared between sibling session handles. results of operations on sibling session handles are accessible to all the sibling session handles. so if your application wants results associated with a specific operation, provide the appropriate msgid to ldap_result(). and applications should avoid calling ldap_result() with LDAP_RES_ANY β€” because that may "steal" and return results in the calling thread that another operation in a different thread, using a different session handle, may require to complete. stealing is not slay in this context. πŸ’€

When ldap_unbind() is called on a session handle with siblings, all the siblings become invalid. whole friend group cancelled. πŸ™ˆ

Siblings must be destroyed using ldap_destroy(). session handle resources associated with the original (LDAP *) will be freed when the last session handle is destroyed, or when ldap_unbind() is called, if no other session handles currently exist. like a group chat that only archives once literally everyone leaves. πŸŽ€


πŸ”₯ ERRORS πŸ”₯

If an error occurs, ldap_dup() will return NULL and errno should be set appropriately. ldap_destroy() will directly return the LDAP code associated to the error (or LDAP_SUCCESS in case of success); errno should be set as well whenever appropriate. two error channels, keep up. πŸ‘‘


🌈 SEE ALSO 🌈

go read them, i'll wait. πŸ’œ


πŸŽ€ ACKNOWLEDGEMENTS πŸŽ€

This work is based on the previously proposed LDAP C API Concurrency Extensions draft (draft-zeilenga-ldap-c-api-concurrency-00.txt) effort β€” credits to the draft girlies. πŸ™ OpenLDAP Software is developed and maintained by The OpenLDAP Project <http://www.openldap.org/>. OpenLDAP Software is derived from the University of Michigan LDAP 3.3 Release. history! lineage! we love a documented origin story. 🌸


and that's the whole page, bestie πŸ’– this was OpenLDAP 2.6.16, dated 2026/10/21 β€” screenshot it, cite it, put it on your fridge. mwah. πŸ’…βœ¨

πŸ“¦ where this page comes from