The following example logs Hello, World! at Directory Server startup.
#include "slapi-plugin.h"
Slapi_PluginDesc desc = {
"Hello, World", /* plug-in identifier */
"Sun Microsystems, Inc.", /* vendor name */
"6.0", /* plug-in revision number */
"My first plug-in" /* plug-in description */
};
/* Log a greeting at server startup if info logging is on for plug-ins */
int
hello()
{
slapi_log_info_ex(
SLAPI_LOG_INFO_AREA_PLUGIN, /* Log if info logging is */
SLAPI_LOG_INFO_LEVEL_DEFAULT, /* set for plug-ins. */
SLAPI_LOG_NO_MSGID, /* No client at startup */
SLAPI_LOG_NO_CONNID, /* No conn. at startup */
SLAPI_LOG_NO_OPID, /* No op. at startup */
"hello() in My first plug-in", /* Origin of this message */
"Hello, World!\n" /* Informational message */
);
return 0;
}
/* Register the plug-in with the server */
#ifdef _WIN32
__declspec(dllexport)
#endif
int
hello_init(Slapi_PBlock * pb)
{
int rc = 0; /* 0 means success */
rc |= slapi_pblock_set( /* Plug-in API version */
pb,
SLAPI_PLUGIN_VERSION,
SLAPI_PLUGIN_CURRENT_VERSION
);
rc |= slapi_pblock_set( /* Plug-in description */
pb,
SLAPI_PLUGIN_DESCRIPTION,
(void *) &desc;
);
rc |= slapi_pblock_set( /* Startup function */
pb,
SLAPI_PLUGIN_START_FN,
(void *) hello
);
return rc;
}
To log the greeting, the plug-in includes a function to be called at Directory Server startup. The plug-in also includes an initialization function to register the plug-in description, supported API version, and startup function with Directory Server.
The startup function specifies that the message is from a plug-in, SLAPI_LOG_INFO_AREA_PLUGIN. The startup function also specifies that the message should be logged when informational logging is activated, SLAPI_LOG_INFO_LEVEL_DEFAULT. For this log message, no client connection information is available, SLAPI_LOG_NO_MSGID, SLAPI_LOG_NO_CONNID, SLAPI_LOG_NO_OPID. Directory Server calls the function at startup before any clients have connected. The function specifies where the log message originates, "hello() in My first plug-in". Finally, the function provides the famous log message.
The initialization function is named hello_init(). This function modifies the parameter block, pb, with the function slapi_pblock_set(). The function thus registers the plug-in API version supported, the plug-in description, and the functions offered to Directory Server by this plug-in. As required for all plug-in initialization functions, hello_init() returns 0 on success, -1 on failure. The function slapi_pblock_set() returns 0 if successful, -1 otherwise. Therefore, you do not need to set the return code to -1 explicitly in Example 3–1.