---
name: "sd_bus_new"
section: "3"
platform: "linux"
package: "systemd-libs"
version: "262.1-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sd_bus_new"
---

# sd\_bus\_new(3)

Create a new bus object and create or destroy references to it

## NAME

sd\_bus\_new, sd\_bus\_ref, sd\_bus\_unref, sd\_bus\_unrefp, sd\_bus\_close\_unref, sd\_bus\_close\_unrefp, sd\_bus\_flush\_close\_unref, sd\_bus\_flush\_close\_unrefp - Create a new bus object and create or destroy references to it

## SYNOPSIS

```
#include <systemd/sd-bus.h>
```

**int sd\_bus\_new(sd\_bus \*\****bus***);**

**sd\_bus \*sd\_bus\_ref(sd\_bus \****bus***);**

**sd\_bus \*sd\_bus\_unref(sd\_bus \****bus***);**

**sd\_bus \*sd\_bus\_close\_unref(sd\_bus \****bus***);**

**sd\_bus \*sd\_bus\_flush\_close\_unref(sd\_bus \****bus***);**

**void sd\_bus\_unrefp(sd\_bus \*\****busp***);**

**void sd\_bus\_close\_unrefp(sd\_bus \*\****busp***);**

**void sd\_bus\_flush\_close\_unrefp(sd\_bus \*\****busp***);**

## DESCRIPTION

**sd\_bus\_new()** creates a new bus object. This object is reference-counted, and will be destroyed when all references are gone. Initially, the caller of this function owns the sole reference and the bus object will not be connected to any bus. To connect it to a bus, make sure to set an address with [**sd\_bus\_set\_address**(3)](/sd_bus_set_address/3.md) or a related call, and then start the connection with [**sd\_bus\_start**(3)](/sd_bus_start/3.md).

In most cases, it is better to use [**sd\_bus\_default\_user**(3)](/sd_bus_default_user/3.md), [**sd\_bus\_default\_system**(3)](/sd_bus_default_system/3.md) or related calls instead of the more low-level **sd\_bus\_new()** and **sd\_bus\_start()**. The higher-level functions not only allocate a bus object but also start the connection to a well-known bus in a single function call.

**sd\_bus\_ref()** increases the reference counter of *bus* by one.

**sd\_bus\_unref()** decreases the reference counter of *bus* by one. Once the reference count has dropped to zero, *bus* is destroyed and cannot be used anymore, so further calls to **sd\_bus\_ref()** or **sd\_bus\_unref()** are illegal.

**sd\_bus\_unrefp()** is similar to **sd\_bus\_unref()** but takes a pointer to a pointer to an **sd\_bus** object. This call is useful in conjunction with GCC's and LLVM's **Clean-up Variable Attribute**\[1\]. Note that this function is defined as an inline function. Use a declaration like the following, in order to allocate a bus object that is freed automatically as the code block is left:

```
{
  __attribute__((cleanup(sd_bus_unrefp))) sd_bus *bus = NULL;
  int r;
  ...
  r = sd_bus_default(&bus);
  if (r < 0) {
    errno = -r;
    fprintf(stderr, "Failed to allocate bus: %m\n");
  }
  ...
}
```

**sd\_bus\_ref()** and **sd\_bus\_unref()** execute no operation if the argument is **NULL**. **sd\_bus\_unrefp()** will first dereference its argument, which must not be **NULL**, and will execute no operation if *that* is **NULL**.

**sd\_bus\_close\_unref()** is similar to **sd\_bus\_unref()**, but first executes [**sd\_bus\_close**(3)](/sd_bus_close/3.md), ensuring that the connection is terminated before the reference to the connection is dropped and possibly the object freed.

**sd\_bus\_flush\_close\_unref()** is similar to **sd\_bus\_unref()**, but first executes [**sd\_bus\_flush**(3)](/sd_bus_flush/3.md) as well as [**sd\_bus\_close**(3)](/sd_bus_close/3.md), ensuring that any pending messages are synchronously flushed out before the reference to the connection is dropped and possibly the object freed. This call is particularly useful immediately before exiting from a program as it ensures that any pending outgoing messages are written out, and unprocessed but queued incoming messages released before the connection is terminated and released.

**sd\_bus\_close\_unrefp()** is similar to **sd\_bus\_close\_unref()**, but may be used in GCC's and LLVM's Clean-up Variable Attribute, see above. Similarly, **sd\_bus\_flush\_close\_unrefp()** is similar to **sd\_bus\_flush\_close\_unref()**.

## RETURN VALUE

On success, **sd\_bus\_new()** returns 0 or a positive integer. On failure, it returns a negative errno-style error code.

**sd\_bus\_ref()** always returns the argument.

**sd\_bus\_unref()** and **sd\_bus\_flush\_close\_unref()** always return **NULL**.

### Errors

Returned errors may indicate the following problems:

**-ENOMEM**

Memory allocation failed.

## NOTES

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

All functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. Different threads may access the same object at different times. Multiple independent objects may be used from different threads in parallel.

The code described here uses [**getenv**(3)](/getenv/3.md), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call [**setenv**(3)](/setenv/3.md) from a parallel thread. It is recommended to only do calls to **setenv()** from an early phase of the program when no other threads have been started.

## HISTORY

**sd\_bus\_new()**, **sd\_bus\_ref()**, and **sd\_bus\_unref()** were added in version 209.

**sd\_bus\_unrefp()** was added in version 229.

**sd\_bus\_flush\_close\_unref()** and **sd\_bus\_flush\_close\_unrefp()** were added in version 240.

**sd\_bus\_close\_unref()** and **sd\_bus\_close\_unrefp()** were added in version 241.

## SEE ALSO

[**systemd**(1)](/systemd/1.md), [**sd-bus**(3)](/sd-bus/3.md), [**sd\_bus\_default\_user**(3)](/sd_bus_default_user/3.md), [**sd\_bus\_default\_system**(3)](/sd_bus_default_system/3.md), [**sd\_bus\_open\_user**(3)](/sd_bus_open_user/3.md), [**sd\_bus\_open\_system**(3)](/sd_bus_open_system/3.md), [**sd\_bus\_close**(3)](/sd_bus_close/3.md)

## NOTES

- 1\.

  Clean-up Variable Attribute

  https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
