<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->

<refentry id="sd_bus_track_add_name" xmlns:xi="http://www.w3.org/2001/XInclude">

  <refentryinfo>
    <title>sd_bus_track_add_name</title>
    <productname>systemd</productname>
  </refentryinfo>

  <refmeta>
    <refentrytitle>sd_bus_track_add_name</refentrytitle>
    <manvolnum>3</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>sd_bus_track_add_name</refname>
    <refname>sd_bus_track_add_sender</refname>
    <refname>sd_bus_track_remove_name</refname>
    <refname>sd_bus_track_remove_sender</refname>
    <refname>sd_bus_track_count</refname>
    <refname>sd_bus_track_count_sender</refname>
    <refname>sd_bus_track_count_name</refname>
    <refname>sd_bus_track_contains</refname>
    <refname>sd_bus_track_first</refname>
    <refname>sd_bus_track_next</refname>

    <refpurpose>Add, remove and retrieve bus peers tracked in a bus peer tracking object</refpurpose>
  </refnamediv>

  <refsynopsisdiv>
    <funcsynopsis>
      <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_add_name</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>const char* <parameter>name</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_add_sender</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>sd_bus_message* <parameter>message</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_remove_name</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>const char* <parameter>name</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_remove_sender</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>sd_bus_message* <parameter>message</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>unsigned <function>sd_bus_track_count</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_count_name</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>const char* <parameter>name</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_count_sender</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>sd_bus_message* <parameter>message</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>int <function>sd_bus_track_contains</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
        <paramdef>const char* <parameter>name</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>const char* <function>sd_bus_track_first</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
      </funcprototype>

      <funcprototype>
        <funcdef>const char* <function>sd_bus_track_next</function></funcdef>
        <paramdef>sd_bus_track* <parameter>t</parameter></paramdef>
      </funcprototype>

    </funcsynopsis>
  </refsynopsisdiv>

  <refsect1>
    <title>Description</title>

    <para><function>sd_bus_track_add_name()</function> adds a peer to track to a bus peer tracking object. The first
    argument should refer to a bus peer tracking object created with
    <citerefentry><refentrytitle>sd_bus_track_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>, the second
    name should refer to a D-Bus peer name to track, either in unique or well-known service format. If the name is not
    tracked yet it will be added to the list of names to track. If it already is being tracked and non-recursive mode
    is enabled, no operation is executed by this call. If recursive mode is enabled a per-name counter is increased by
    one each time this call is invoked, and <function>sd_bus_track_remove_name()</function> has to be called as many
    times as <function>sd_bus_track_add_name()</function> was invoked before in order to stop tracking of the name. Use
    <citerefentry><refentrytitle>sd_bus_track_set_recursive</refentrytitle><manvolnum>3</manvolnum></citerefentry> to
    switch from the default non-recursive mode to recursive mode, or back. Note that the specified name is tracked as
    it is, well-known names are not resolved to unique names by this call. Note that multiple bus peer tracking objects
    may track the same name.</para>

    <para><function>sd_bus_track_remove_name()</function> undoes the effect of
    <function>sd_bus_track_add_name()</function> and removes a bus peer name from the list of peers to watch. Depending
    on whether non-recursive or recursive mode is enabled for the bus peer tracking object this call will either remove
    the name fully from the tracking object, or will simply decrement the per-name counter by one, removing the name
    only when the counter reaches zero (see above). Note that a bus peer disconnecting from the bus will implicitly
    remove its names fully from the bus peer tracking object, regardless of the current per-name counter.</para>

    <para><function>sd_bus_track_add_sender()</function> and <function>sd_bus_track_remove_sender()</function> are
    similar to <function>sd_bus_track_add_name()</function> and <function>sd_bus_track_remove_name()</function> but
    take a bus message as argument. The sender of this bus message is determined and added to/removed from the bus peer
    tracking object. As messages always originate from unique names, and never from well-known names this means that
    this call will effectively only add unique names to the bus peer tracking object.</para>

    <para><function>sd_bus_track_count()</function> returns the number of names currently being tracked by the
    specified bus peer tracking object. Note that this function always returns the actual number of names tracked, and
    hence if <function>sd_bus_track_add_name()</function> has been invoked multiple times for the same name it is only
    counted as one, regardless if recursive mode is used or not.</para>

    <para><function>sd_bus_track_count_name()</function> returns the current per-name counter for the specified
    name. If non-recursive mode is used this returns either 1 or 0, depending on whether the specified name has been
    added to the tracking object before, or not. If recursive mode has been enabled, values larger than 1 may be
    returned too, in case <function>sd_bus_track_add_name()</function> has been called multiple times for the same
    name.</para>

    <para><function>sd_bus_track_count_sender()</function> is similar to
    <function>sd_bus_track_count_name()</function>, but takes a bus message object and returns the per-name counter
    matching the sender of the message.</para>

    <para><function>sd_bus_track_contains()</function> may be used to determine whether the specified name has been
    added at least once to the specified bus peer tracking object.</para>

    <para><function>sd_bus_track_first()</function> and <function>sd_bus_track_next()</function> may be used to
    enumerate all names currently being tracked by the passed bus peer tracking
    object. <function>sd_bus_track_first()</function> returns the first entry in the object, and resets an internally
    maintained read index. Each subsequent invocation of <function>sd_bus_track_next()</function> returns the next name
    contained in the bus object. If the end is reached <constant>NULL</constant> is returned. If no names have been
    added to the object yet <function>sd_bus_track_first()</function> will return <constant>NULL</constant>
    immediately. The order in which names are returned is undefined; in particular which name is considered the first
    returned is not defined. If recursive mode is enabled and the same name has been added multiple times to the bus
    peer tracking object it is only returned once by this enumeration. If new names are added to or existing names
    removed from the bus peer tracking object while it is being enumerated the enumeration ends on the next invocation
    of <function>sd_bus_track_next()</function> as <constant>NULL</constant> is returned.</para>
  </refsect1>

  <refsect1>
    <title>Return Value</title>

    <para>On success, <function>sd_bus_track_add_name()</function> and <function>sd_bus_track_add_sender()</function>
    return 0 if the specified name has already been added to the bus peer tracking object before and positive if it
    hasn't. On failure, they return a negative errno-style error code.</para>

    <para><function>sd_bus_track_remove_name()</function> and <function>sd_bus_track_remove_sender()</function> return
    positive if the specified name was previously tracked by the bus peer tracking object and has now been removed. In
    non-recursive mode, 0 is returned if the specified name was not being tracked yet. In recursive mode
    <constant>-EUNATCH</constant> is returned in this case. On failure, they return a negative errno-style error
    code.</para>

    <para><function>sd_bus_track_count()</function> returns the number of names currently being tracked, or 0 on
    failure.</para>

    <para><function>sd_bus_track_count_name()</function> and <function>sd_bus_track_count_sender()</function> return
    the current per-name counter for the specified name or the sender of the specified message. Zero is returned for
    names that are not being tracked yet, a positive value for names added at least once. Larger values than 1 are only
    returned in recursive mode. On failure, a negative errno-style error code is returned.</para>

    <para><function>sd_bus_track_contains()</function> returns the passed name if it exists in the bus peer tracking
    object. On failure, and if the name has not been added yet <constant>NULL</constant> is returned.</para>

    <para><function>sd_bus_track_first()</function> and <function>sd_bus_track_next()</function> return the first/next
    name contained in the bus peer tracking object, and <constant>NULL</constant> if the end of the enumeration is
    reached and on error.</para>

    <refsect2>
      <title>Errors</title>

      <para>Returned errors may indicate the following problems:</para>

      <variablelist>

        <varlistentry>
          <term><constant>-EUNATCH</constant></term>

          <listitem><para><function>sd_bus_track_remove_name()</function> or
          <function>sd_bus_track_remove_sender()</function> have been invoked for a name not previously added
          to the bus peer object.</para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>-EINVAL</constant></term>

          <listitem><para>Specified parameter is invalid.</para></listitem>
        </varlistentry>

        <varlistentry>
          <term><constant>-ENOMEM</constant></term>

          <listitem><para>Memory allocation failed.</para></listitem>
        </varlistentry>

      </variablelist>
    </refsect2>
  </refsect1>

  <xi:include href="libsystemd-pkgconfig.xml" />

  <refsect1>
    <title>History</title>
    <para><function>sd_bus_track_add_name()</function>,
    <function>sd_bus_track_add_sender()</function>,
    <function>sd_bus_track_remove_name()</function>,
    <function>sd_bus_track_remove_sender()</function>,
    <function>sd_bus_track_count()</function>,
    <function>sd_bus_track_count_name()</function>,
    <function>sd_bus_track_count_sender()</function>,
    <function>sd_bus_track_contains()</function>,
    <function>sd_bus_track_first()</function>, and
    <function>sd_bus_track_next()</function> were added in version 232.</para>
  </refsect1>

  <refsect1>
    <title>See Also</title>

    <para><simplelist type="inline">
      <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
      <member><citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
      <member><citerefentry><refentrytitle>sd_bus_track_new</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
    </simplelist></para>
  </refsect1>

</refentry>
