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