]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev.c
659aa2b743d8aa72b70b6a822fd5987491dd2a39
[thirdparty/systemd.git] / src / libudev / libudev.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "libudev.h"
6
7 #include "alloc-util.h"
8 #include "errno-util.h"
9 #include "log.h"
10
11 /**
12 * SECTION:libudev
13 * @short_description: libudev context
14 */
15
16 /**
17 * udev:
18 *
19 * Opaque object representing the library context.
20 */
21 struct udev {
22 unsigned n_ref;
23 void *userdata;
24 };
25
26 /**
27 * udev_get_userdata:
28 * @udev: udev library context
29 *
30 * Retrieve stored data pointer from library context. This might be useful
31 * to access from callbacks.
32 *
33 * Returns: stored userdata
34 **/
35 _public_ void* udev_get_userdata(struct udev *udev) {
36 assert_return(udev, NULL);
37
38 return udev->userdata;
39 }
40
41 /**
42 * udev_set_userdata:
43 * @udev: udev library context
44 * @userdata: data pointer
45 *
46 * Store custom @userdata in the library context.
47 **/
48 _public_ void udev_set_userdata(struct udev *udev, void *userdata) {
49 if (!udev)
50 return;
51
52 udev->userdata = userdata;
53 }
54
55 /**
56 * udev_new:
57 *
58 * Create udev library context. This only allocates the basic data structure.
59 *
60 * The initial refcount is 1, and needs to be decremented to
61 * release the resources of the udev library context.
62 *
63 * Returns: a new udev library context
64 **/
65 _public_ struct udev* udev_new(void) {
66 struct udev *udev;
67
68 udev = new(struct udev, 1);
69 if (!udev)
70 return_with_errno(NULL, ENOMEM);
71
72 *udev = (struct udev) {
73 .n_ref = 1,
74 };
75
76 return udev;
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 **/
87 DEFINE_PUBLIC_TRIVIAL_REF_FUNC(struct udev, udev);
88
89 /**
90 * udev_unref:
91 * @udev: udev library context
92 *
93 * Drop a reference of the udev library context. If the refcount
94 * reaches zero, the resources of the context will be released.
95 *
96 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
97 **/
98 _public_ struct udev* udev_unref(struct udev *udev) {
99 if (!udev)
100 return NULL;
101
102 assert(udev->n_ref > 0);
103 udev->n_ref--;
104 if (udev->n_ref > 0)
105 /* This is different from our convention, but let's keep backward
106 * compatibility. So, do not use DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC()
107 * macro to define this function. */
108 return udev;
109
110 return mfree(udev);
111 }
112
113 /**
114 * udev_set_log_fn:
115 * @udev: udev library context
116 * @log_fn: function to be called for log messages
117 *
118 * This function is deprecated.
119 *
120 **/
121 _public_ void udev_set_log_fn(
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)) {
130 return;
131 }
132
133 /**
134 * udev_get_log_priority:
135 * @udev: udev library context
136 *
137 * This function is deprecated.
138 *
139 **/
140 _public_ int udev_get_log_priority(struct udev *udev) {
141 return log_get_max_level();
142 }
143
144 /**
145 * udev_set_log_priority:
146 * @udev: udev library context
147 * @priority: the new log priority
148 *
149 * This function is deprecated.
150 *
151 **/
152 _public_ void udev_set_log_priority(struct udev *udev, int priority) {
153 log_set_max_level(priority);
154 }