]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev.c
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / libudev / libudev.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
33a5cc29 2
33a5cc29 3#include <stdio.h>
33a5cc29 4
b4bbcaa9
TA
5#include "libudev.h"
6
b5efdb8a 7#include "alloc-util.h"
3b7d3320 8#include "errno-util.h"
93a1f792 9#include "log.h"
33a5cc29 10
ce1d6d7f
KS
11/**
12 * SECTION:libudev
13 * @short_description: libudev context
ce1d6d7f
KS
14 */
15
1e511322
KS
16/**
17 * udev:
18 *
ce1d6d7f 19 * Opaque object representing the library context.
1e511322 20 */
ba6929f6 21struct udev {
3c6ac219 22 unsigned n_ref;
912541b0 23 void *userdata;
ba6929f6
KS
24};
25
1e511322
KS
26/**
27 * udev_get_userdata:
28 * @udev: udev library context
29 *
30 * Retrieve stored data pointer from library context. This might be useful
25e773ee 31 * to access from callbacks.
1e511322
KS
32 *
33 * Returns: stored userdata
34 **/
9ee08c8d 35_public_ void* udev_get_userdata(struct udev *udev) {
bc54df90
YW
36 assert_return(udev, NULL);
37
912541b0 38 return udev->userdata;
c8e32461
KS
39}
40
1e511322
KS
41/**
42 * udev_set_userdata:
43 * @udev: udev library context
44 * @userdata: data pointer
45 *
46 * Store custom @userdata in the library context.
47 **/
25e773ee 48_public_ void udev_set_userdata(struct udev *udev, void *userdata) {
bc54df90 49 if (!udev)
912541b0 50 return;
bc54df90 51
912541b0 52 udev->userdata = userdata;
c8e32461
KS
53}
54
33a5cc29
KS
55/**
56 * udev_new:
57 *
e8112e67 58 * Create udev library context. This only allocates the basic data structure.
33a5cc29
KS
59 *
60 * The initial refcount is 1, and needs to be decremented to
be7de409 61 * release the resources of the udev library context.
33a5cc29
KS
62 *
63 * Returns: a new udev library context
64 **/
9ee08c8d 65_public_ struct udev* udev_new(void) {
912541b0 66 struct udev *udev;
912541b0 67
5ccb44a5 68 udev = new(struct udev, 1);
fd05c424
YW
69 if (!udev)
70 return_with_errno(NULL, ENOMEM);
5ccb44a5
YW
71
72 *udev = (struct udev) {
73 .n_ref = 1,
74 };
912541b0 75
912541b0 76 return udev;
33a5cc29
KS
77}
78
79/**
80 * udev_ref:
81 * @udev: udev library context
82 *
83 * Take a reference of the udev library context.
84 *
85 * Returns: the passed udev library context
86 **/
3c6ac219 87DEFINE_PUBLIC_TRIVIAL_REF_FUNC(struct udev, udev);
33a5cc29
KS
88
89/**
90 * udev_unref:
91 * @udev: udev library context
92 *
93 * Drop a reference of the udev library context. If the refcount
be7de409 94 * reaches zero, the resources of the context will be released.
33a5cc29 95 *
c1959569 96 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
33a5cc29 97 **/
9ee08c8d 98_public_ struct udev* udev_unref(struct udev *udev) {
3c6ac219 99 if (!udev)
20bbd54f 100 return NULL;
3c6ac219
YW
101
102 assert(udev->n_ref > 0);
103 udev->n_ref--;
104 if (udev->n_ref > 0)
86b52a39 105 /* This is different from our convention, but let's keep backward
3c6ac219
YW
106 * compatibility. So, do not use DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC()
107 * macro to define this function. */
20bbd54f 108 return udev;
3c6ac219 109
5fecf46d 110 return mfree(udev);
33a5cc29
KS
111}
112
113/**
114 * udev_set_log_fn:
115 * @udev: udev library context
f47ad593 116 * @log_fn: function to be called for log messages
33a5cc29 117 *
25e773ee 118 * This function is deprecated.
33a5cc29
KS
119 *
120 **/
bc54df90 121_public_ void udev_set_log_fn(
9ee08c8d
YW
122 struct udev *udev,
123 void (*log_fn)(struct udev *udev,
124 int priority,
125 const char *file,
126 int line,
127 const char *fn,
128 const char *format,
129 va_list args)) {
25e773ee 130 return;
7d563a17
KS
131}
132
1e511322
KS
133/**
134 * udev_get_log_priority:
135 * @udev: udev library context
136 *
25e773ee 137 * This function is deprecated.
1e511322 138 *
1e511322 139 **/
25e773ee
KS
140_public_ int udev_get_log_priority(struct udev *udev) {
141 return log_get_max_level();
7d563a17
KS
142}
143
1e511322
KS
144/**
145 * udev_set_log_priority:
146 * @udev: udev library context
f47ad593 147 * @priority: the new log priority
1e511322 148 *
25e773ee
KS
149 * This function is deprecated.
150 *
1e511322 151 **/
25e773ee
KS
152_public_ void udev_set_log_priority(struct udev *udev, int priority) {
153 log_set_max_level(priority);
7d563a17 154}