---
name: "udev_device_unref"
section: "3"
platform: "linux"
package: "libudev-dev"
version: "257.13-1~deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/udev_device_new_from_syspath"
---

# udev\_device\_unref(3)

Create, acquire and release a udev device object

Part of [udev\_device\_new\_from\_syspath(3)](/udev_device_new_from_syspath/3.md).

## NAME

udev\_device\_new\_from\_syspath, udev\_device\_new\_from\_devnum, udev\_device\_new\_from\_subsystem\_sysname, udev\_device\_new\_from\_device\_id, udev\_device\_new\_from\_environment, udev\_device\_ref, udev\_device\_unref - Create, acquire and release a udev device object

## SYNOPSIS

```
#include <libudev.h>
```

**struct udev\_device \*udev\_device\_new\_from\_syspath(struct udev \****udev***, const char \****syspath***);**

**struct udev\_device \*udev\_device\_new\_from\_devnum(struct udev \****udev***, char** *type***, dev\_t** *devnum***);**

**struct udev\_device \*udev\_device\_new\_from\_subsystem\_sysname(struct udev \****udev***, const char \****subsystem***, const char \****sysname***);**

**struct udev\_device \*udev\_device\_new\_from\_device\_id(struct udev \****udev***, const char \****id***);**

**struct udev\_device \*udev\_device\_new\_from\_environment(struct udev \****udev***);**

**struct udev\_device \*udev\_device\_ref(struct udev\_device \****udev\_device***);**

**struct udev\_device \*udev\_device\_unref(struct udev\_device \****udev\_device***);**

## DESCRIPTION

**udev\_device\_new\_from\_syspath()**, **udev\_device\_new\_from\_devnum()**, **udev\_device\_new\_from\_subsystem\_sysname()**, **udev\_device\_new\_from\_device\_id()**, and **udev\_device\_new\_from\_environment()** allocate a new udev device object and returns a pointer to it. This object is opaque and must not be accessed by the caller via different means than functions provided by libudev. Initially, the reference count of the device is 1. You can acquire further references, and drop gained references via **udev\_device\_ref()** and **udev\_device\_unref()**. Once the reference count hits 0, the device object is destroyed and freed.

**udev\_device\_new\_from\_syspath()**, **udev\_device\_new\_from\_devnum()**, **udev\_device\_new\_from\_subsystem\_sysname()**, and **udev\_device\_new\_from\_device\_id()** create the device object based on information found in /sys/, annotated with properties from the udev-internal device database. A syspath is any subdirectory of /sys/, with the restriction that a subdirectory of /sys/devices (or a symlink to one) represents a real device and as such must contain a uevent file. **udev\_device\_new\_from\_devnum()** takes a device type, which can be **b** for block devices or **c** for character devices, as well as a devnum (see [**makedev**(3)](/makedev/3.md)). **udev\_device\_new\_from\_subsystem\_sysname()** looks up devices based on the provided subsystem and sysname (see [**udev\_device\_get\_subsystem**(3)](/udev_device_get_subsystem/3.md) and [**udev\_device\_get\_sysname**(3)](/udev_device_get_sysname/3.md)) and **udev\_device\_new\_from\_device\_id()** looks up devices based on the provided device ID, which is a special string in one of the following four forms:

**Table 1. Device ID strings**

| | |
| --- | --- |
| **Example** | **Explanation** |
| *b8:2* | block device major:minor |
| *c128:1* | char device major:minor |
| *n3* | network device ifindex |
| *+sound:card29* | kernel driver core subsystem:device name |

**udev\_device\_new\_from\_environment()** creates a device from the current environment (see [**environ**(7)](/environ/7.md)). Each key-value pair is interpreted in the same way as if it was received in an uevent (see [**udev\_monitor\_receive\_device**(3)](/udev_monitor_receive_device/3.md)). The keys **DEVPATH**, **SUBSYSTEM**, **ACTION**, and **SEQNUM** are mandatory.

## RETURN VALUE

On success, **udev\_device\_new\_from\_syspath()**, **udev\_device\_new\_from\_devnum()**, **udev\_device\_new\_from\_subsystem\_sysname()**, **udev\_device\_new\_from\_device\_id()** and **udev\_device\_new\_from\_environment()** return a pointer to the allocated udev device. On failure, **NULL** is returned, and *errno* is set appropriately. **udev\_device\_ref()** returns the argument that it was passed, unmodified. **udev\_device\_unref()** always returns **NULL**.

## HISTORY

**udev\_device\_new\_from\_syspath()**, **udev\_device\_new\_from\_devnum()**, **udev\_device\_new\_from\_subsystem\_sysname()**, **udev\_device\_new\_from\_device\_id()**, **udev\_device\_new\_from\_environment()**, **udev\_device\_ref()**, and **udev\_device\_unref()** were added in version 221.

## SEE ALSO

[**udev\_new**(3)](/udev_new/3.md), [**udev\_device\_get\_syspath**(3)](/udev_device_get_syspath/3.md), [**udev\_device\_has\_tag**(3)](/udev_device_has_tag/3.md), [**udev\_enumerate\_new**(3)](/udev_enumerate_new/3.md), [**udev\_monitor\_new\_from\_netlink**(3)](/udev_monitor_new_from_netlink/3.md), [**udev\_list\_entry**(3)](/udev_list_entry/3.md), [**systemd**(1)](/systemd/1.md)
