Loading...
Searching...
No Matches
Connection and Session API

## Functions

const struct ly_ctx * | sr_acquire_context (sr_conn_ctx_t *conn)
| Get the libyang context used by a connection. Can be used in an application for working with data and schemas.
int | sr_connect (const sr_conn_options_t opts, sr_conn_ctx_t **conn)
| Connects to the sysrepo datastore.
int | sr_disconnect (sr_conn_ctx_t *conn)
| Disconnect from the sysrepo datastore.
sr_cid_t | sr_get_cid (sr_conn_ctx_t *conn)
| Get the connection ID.
uint32_t | sr_get_content_id (sr_conn_ctx_t *conn)
| Get content ID of the current YANG module set. It conforms to the requirements for ietf-yang-library "content-id" node value.
int | sr_get_error_data (const sr_error_info_err_t *err, uint32_t idx, uint32_t *size, const void **data)
| Get a specific chunk of error data.
int | sr_get_plugins (sr_conn_ctx_t *conn, const char ***ds_plugins, const char ***ntf_plugins)
| Get loaded plugins of a connection.
uid_t | sr_get_su_uid (void)
| Get the sysrepo SUPERUSER UID.
void | sr_release_context (sr_conn_ctx_t *conn)
| Release libyang context obtained from a session/connection.
const struct ly_ctx * | sr_session_acquire_context (sr_session_ctx_t *session)
| Get the libyang context used by a connection. Can be used in an application for working with data and schemas.
void | sr_session_del_orig_data (sr_session_ctx_t *session)
| Remove all pushed event originator data.
int | sr_session_dup_error (sr_session_ctx_t *src_session, sr_session_ctx_t *trg_session)
| Copy the first error (if any) from a session to a callback session.
sr_conn_ctx_t * | sr_session_get_connection (sr_session_ctx_t *session)
| Get the connection the session was created on.
sr_datastore_t | sr_session_get_ds (sr_session_ctx_t *session)
| Learn the datastore a session operates on.
int | sr_session_get_error (sr_session_ctx_t *session, const sr_error_info_t **error_info)
| Retrieve information about the error that has occurred during the last operation executed within provided session.
uint32_t | sr_session_get_id (sr_session_ctx_t *session)
| Return the assigned session ID of the sysrepo session.
int | sr_session_get_orig_data (sr_session_ctx_t *session, uint32_t idx, uint32_t *size, const void **data)
| Get a specific chunk of event originator data in a callback.
const char * | sr_session_get_orig_name (sr_session_ctx_t *session)
| Get event originator name.
const char * | sr_session_get_user (sr_session_ctx_t *session)
| Get the effective user of a session.
int | sr_session_notif_buffer (sr_session_ctx_t *session)
| Use notification buffering for the session.
int | sr_session_push_error_data (sr_session_ctx_t *session, uint32_t size, const void *data)
| Push (add) another chunk of error data for a failed callback communicated back to the originator. Its meaning is specific to the error data format (which must be set prior to calling this function) identifier and can be read from the error structure by the originator using sr_get_error_data().
int | sr_session_push_orig_data (sr_session_ctx_t *session, uint32_t size, const void *data)
| Push (add) another chunk of event originator data used for all events sent on this session. Its meaning is specific to the originator name (which must be set prior to calling this function) and can be read from the implicit event session in the callbacks using sr_session_get_orig_data().
void | sr_session_release_context (sr_session_ctx_t *session)
| Release libyang context obtained from a session/connection.
int | sr_session_set_error (sr_session_ctx_t *session, const char *err_format_name, sr_error_t err_code, const char *err_msg_format,...)
| Set an error for a failed callback communicated back to the originator. Does not print the message.
int | sr_session_set_error_format (sr_session_ctx_t *session, const char *error_format)
| Deprecated, use sr_session_set_error().
int | sr_session_set_error_message (sr_session_ctx_t *session, const char *format,...)
| Deprecated, use sr_session_set_error().
int | sr_session_set_orig_name (sr_session_ctx_t *session, const char *orig_name)
| Set event originator name used for all events sent on this session. It can then be read from the implicit event session in the callbacks using sr_session_get_orig_name(). This name should be used for interpreting the data set by sr_session_push_orig_data().
int | sr_session_set_user (sr_session_ctx_t *session, const char *user)
| Set the effective user of a session to a different one than the process owner.
int | sr_session_start (sr_conn_ctx_t *conn, const sr_datastore_t datastore, sr_session_ctx_t **session)
| Start a new session.
int | sr_session_stop (sr_session_ctx_t *session)
| Stop the session and releases resources tied to it.
int | sr_session_switch_ds (sr_session_ctx_t *session, sr_datastore_t ds)
| Change datastore which the session operates on. All subsequent calls will be issued on the chosen datastore. Previous calls are not affected.
int | sr_session_unsubscribe (sr_session_ctx_t *session)
| Unsubscribe all subscriptions created by this session.
typedef struct sr_conn_ctx_s | sr_conn_ctx_t
| Sysrepo connection.

## Detailed Description

## Typedef Documentation

## sr_conn_ctx_t

typedef struct sr_conn_ctx_s sr_conn_ctx_t

Sysrepo connection.

Definition at line 89 of file sysrepo_types.h.

## Function Documentation

## sr_acquire_context()

const struct ly_ctx * sr_acquire_context | ( | sr_conn_ctx_t * | conn | ) |

Get the libyang context used by a connection. Can be used in an application for working with data and schemas.

Note
  This context must not be changed. Also, to prevent the context from being destroyed by sysrepo, it is locked and after no longer needing the context sr_release_context() must be called. Otherwise, API functions changing the context will fail with time out.

Parameters

  [in] | conn | Connection to use.

Returns
  Const libyang context.

## sr_connect()

int sr_connect | ( | const sr_conn_options_t | opts,
|  | sr_conn_ctx_t ** | conn )

Connects to the sysrepo datastore.

Note
  Do not use fork(2) after creating a connection. Sysrepo internally stores the connection ID of every connection. Forking will duplicate the connection and ID resulting in a mismatch.

Parameters

  [in] | opts | Connection options.
  [out] | conn | Created connection.

Returns
  Error code (SR_ERR_OK on success).

## sr_disconnect()

int sr_disconnect | ( | sr_conn_ctx_t * | conn | ) |

Disconnect from the sysrepo datastore.

Cleans up and frees connection context allocated by sr_connect. All sessions and subscriptions started within the connection will be automatically stopped and cleaned up too.

Note
  On error the function should be retried and must eventually succeed.

Parameters

  [in] | conn | Connection acquired with sr_connect call to free.

Returns
  Error code (SR_ERR_OK on success).

## sr_get_cid()

sr_cid_t sr_get_cid | ( | sr_conn_ctx_t * | conn | ) |

Get the connection ID.

Parameters

  [in] | conn | Connection to use.

Returns
  CID of the connection.

## sr_get_content_id()

uint32_t sr_get_content_id | ( | sr_conn_ctx_t * | conn | ) |

Get content ID of the current YANG module set. It conforms to the requirements for ietf-yang-library "content-id" node value.

Parameters

  [in] | conn | Connection to use.

Returns
  Content ID.

## sr_get_error_data()

int sr_get_error_data | ( | const sr_error_info_err_t * | err,
|  | uint32_t | idx,
|  | uint32_t * | size,
|  | const void ** | data )

Get a specific chunk of error data.

If the error is a well-known one, it is possible to use helper utility functions instead of repeatedly calling this function.

Parameters

  [in] | err | Error structure to use.
  [in] | idx | Index of the error data chunk, starts at 0.
  [out] | size | Optional size of the error `data` chunk.
  [out] | data | Pointer to an opaque error data chunk.

Returns
  Error code (SR_ERR_OK on success).

## sr_get_plugins()

int sr_get_plugins | ( | sr_conn_ctx_t * | conn,
|  | const char *** | ds_plugins,
|  | const char *** | ntf_plugins )

Get loaded plugins of a connection.

Parameters

  [in] | conn | Connection to use.
  [out] | ds_plugins | Optional pointer to array of datastore plugins ended by NULL.
  [out] | ntf_plugins | Optional pointer to array of notification plugins ended by NULL.

Returns
  Error code (SR_ERR_OK on success).

## sr_get_su_uid()

uid_t sr_get_su_uid | ( | void |  | ) |

Get the sysrepo SUPERUSER UID.

Returns
  Sysrepo SU UID.

## sr_release_context()

void sr_release_context | ( | sr_conn_ctx_t * | conn | ) |

Release libyang context obtained from a session/connection.

Note
  Must be called for each sr_acquire_context() call.

Parameters

  [in] | conn | Connection to use.

## sr_session_acquire_context()

const struct ly_ctx * sr_session_acquire_context | ( | sr_session_ctx_t * | session | ) |

Get the libyang context used by a connection. Can be used in an application for working with data and schemas.

Similar to and interchangeable with sr_acquire_context().

Parameters

  [in] | session | Session whose connection to use.

Returns
  Const libyang context.

## sr_session_del_orig_data()

void sr_session_del_orig_data | ( | sr_session_ctx_t * | session | ) |

Remove all pushed event originator data.

Parameters

  [in] | session | Session (not DS-specific) to use.

## sr_session_dup_error()

int sr_session_dup_error | ( | sr_session_ctx_t * | src_session,
|  | sr_session_ctx_t * | trg_session )

Copy the first error (if any) from a session to a callback session.

Parameters

  [in] | src_session | Session (not DS-specific) to read the error from.
  [in] | trg_session | Implicit session provided in a callback.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_get_connection()

sr_conn_ctx_t * sr_session_get_connection | ( | sr_session_ctx_t * | session | ) |

Get the connection the session was created on.

Parameters

  [in] | session | Session (not DS-specific) to use.

Returns
  Sysrepo connection.

## sr_session_get_ds()

sr_datastore_t sr_session_get_ds | ( | sr_session_ctx_t * | session | ) |

Learn the datastore a session operates on.

Parameters

  [in] | session | Session to use.

Returns
  Datastore of the session.

## sr_session_get_error()

int sr_session_get_error | ( | sr_session_ctx_t * | session,
|  | const sr_error_info_t ** | error_info )

Retrieve information about the error that has occurred during the last operation executed within provided session.

Parameters

  [in] | session | Session (not DS-specific) to use.
  [out] | error_info | Detailed error information. Be aware that returned pointer may change by the next API call executed within the provided session. Do not free or modify returned values.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_get_id()

uint32_t sr_session_get_id | ( | sr_session_ctx_t * | session | ) |

Return the assigned session ID of the sysrepo session.

Parameters

  [in] | session | Session (not DS-specific) to use.

Returns
  sysrepo SID or 0 in case of error.

## sr_session_get_orig_data()

int sr_session_get_orig_data | ( | sr_session_ctx_t * | session,
|  | uint32_t | idx,
|  | uint32_t * | size,
|  | const void ** | data )

Get a specific chunk of event originator data in a callback.

Parameters

  [in] | session | Implicit session provided in a callback.
  [in] | idx | Index of the data chunk, starts at 0.
  [out] | size | Optional size of the `data` chunk.
  [out] | data | Pointer to an opaque data chunk.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_get_orig_name()

const char * sr_session_get_orig_name | ( | sr_session_ctx_t * | session | ) |

Get event originator name.

Parameters

  [in] | session | Implicit session provided in a callback.

Returns
  Originator name if set, empty string "" otherwise.

## sr_session_get_user()

const char * sr_session_get_user | ( | sr_session_ctx_t * | session | ) |

Get the effective user of a session.

Parameters

  [in] | session | Session (not DS-specific) to use.

Returns
  Session user.

## sr_session_notif_buffer()

int sr_session_notif_buffer | ( | sr_session_ctx_t * | session | ) |

Use notification buffering for the session.

When a notification is sent using this session for a module that supports replay (notification should be stored), the notification function does not wait until it is stored but delegates this work to a special thread and returns.

Parameters

  [in] | session | Session (not DS-specific) whose notifications will be buffered.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_push_error_data()

int sr_session_push_error_data | ( | sr_session_ctx_t * | session,
|  | uint32_t | size,
|  | const void * | data )

Push (add) another chunk of error data for a failed callback communicated back to the originator. Its meaning is specific to the error data format (which must be set prior to calling this function) identifier and can be read from the error structure by the originator using sr_get_error_data().

Parameters

  [in] | session | Implicit session provided in a callback.
  [in] | size | Size of the error `data` chunk.
  [in] | data | Pointer to an opaque error data chunk.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_push_orig_data()

int sr_session_push_orig_data | ( | sr_session_ctx_t * | session,
|  | uint32_t | size,
|  | const void * | data )

Push (add) another chunk of event originator data used for all events sent on this session. Its meaning is specific to the originator name (which must be set prior to calling this function) and can be read from the implicit event session in the callbacks using sr_session_get_orig_data().

Parameters

  [in] | session | Session (not DS-specific) to use.
  [in] | size | Size of the `data` chunk.
  [in] | data | Pointer to an opaque data chunk.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_release_context()

void sr_session_release_context | ( | sr_session_ctx_t * | session | ) |

Release libyang context obtained from a session/connection.

Similar to and interchangeable with sr_release_context().

Parameters

  [in] | session | Session whose connection to use.

## sr_session_set_error()

int sr_session_set_error | ( | sr_session_ctx_t * | session,
|  | const char * | err_format_name,
|  | sr_error_t | err_code,
|  | const char * | err_msg_format,
|  |  | ... )

Set an error for a failed callback communicated back to the originator. Does not print the message.

Note
  Intended for diff-check, change, RPC/action, or operational callbacks to be used on the provided session.
  Error format `err_format_name` should be used for interpreting the error data set by sr_session_push_error_data(). There are some well-known error formats defined and those errors can be written/read using helper utility functions.

Parameters

  [in] | session | Implicit session provided in a callback.
  [in] | err_format_name | Optional arbitrary error format identifier, set its error data using sr_session_push_error_data().
  [in] | err_code | Error code of the error.
  [in] | err_msg_format | Error message format.
  [in] | ... | Error message format arguments.

## sr_session_set_error_format()

int sr_session_set_error_format | ( | sr_session_ctx_t * | session,
|  | const char * | error_format )

Deprecated, use sr_session_set_error().

## sr_session_set_error_message()

int sr_session_set_error_message | ( | sr_session_ctx_t * | session,
|  | const char * | format,
|  |  | ... )

Deprecated, use sr_session_set_error().

## sr_session_set_orig_name()

int sr_session_set_orig_name | ( | sr_session_ctx_t * | session,
|  | const char * | orig_name )

Set event originator name used for all events sent on this session. It can then be read from the implicit event session in the callbacks using sr_session_get_orig_name(). This name should be used for interpreting the data set by sr_session_push_orig_data().

The following originator names are well-known:

- netopeer2 for the NETCONF server
- rousette for the RESTCONF server
- sysrepo-cli for the direct sysrepo mode of the interactive CLI

Parameters

  [in] | session | Session (not DS-specific) to use.
  [in] | orig_name | Arbitrary originator name.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_set_user()

int sr_session_set_user | ( | sr_session_ctx_t * | session,
|  | const char * | user )

Set the effective user of a session to a different one than the process owner.

Required SUPERUSER access.

Parameters

  [in] | session | Session (not DS-specific) to change.
  [in] | user | System user.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_start()

int sr_session_start | ( | sr_conn_ctx_t * | conn,
|  | const sr_datastore_t | datastore,
|  | sr_session_ctx_t ** | session )

Start a new session.

Parameters

  [in] | conn | Connection to use.
  [in] | datastore | Datastore on which to operate, can be later changed using sr_session_switch_ds().
  [out] | session | Created session.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_stop()

int sr_session_stop | ( | sr_session_ctx_t * | session | ) |

Stop the session and releases resources tied to it.

Also releases any locks held and frees subscriptions created (only) by this session.

Note
  On error the function should be retried and must eventually succeed. Subscriptions, even if they no longer handle any events are never freed and should be freed manually using sr_unsubscribe.

Parameters

  [in] | session | Session to free.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_switch_ds()

int sr_session_switch_ds | ( | sr_session_ctx_t * | session,
|  | sr_datastore_t | ds )

Change datastore which the session operates on. All subsequent calls will be issued on the chosen datastore. Previous calls are not affected.

Parameters

  [in] | session | Session to modify.
  [in] | ds | New datastore that will be operated on.

Returns
  Error code (SR_ERR_OK on success).

## sr_session_unsubscribe()

int sr_session_unsubscribe | ( | sr_session_ctx_t * | session | ) |

Unsubscribe all subscriptions created by this session.

Note
  Subscriptions, even if they no longer handle any events are never freed and should be freed manually using sr_unsubscribe.

Parameters

  [in] | session | Session to use.

Returns
  Error code (SR_ERR_OK on success).