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