]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
33a5cc29 | 2 | |
07630cea LP |
3 | #include <ctype.h> |
4 | #include <stdarg.h> | |
5 | #include <stddef.h> | |
33a5cc29 KS |
6 | #include <stdio.h> |
7 | #include <stdlib.h> | |
33a5cc29 | 8 | #include <string.h> |
33a5cc29 | 9 | |
b4bbcaa9 TA |
10 | #include "libudev.h" |
11 | ||
b5efdb8a | 12 | #include "alloc-util.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
4db17f29 | 14 | #include "missing.h" |
07630cea | 15 | #include "string-util.h" |
33a5cc29 | 16 | |
ce1d6d7f KS |
17 | /** |
18 | * SECTION:libudev | |
19 | * @short_description: libudev context | |
ce1d6d7f KS |
20 | */ |
21 | ||
1e511322 KS |
22 | /** |
23 | * udev: | |
24 | * | |
ce1d6d7f | 25 | * Opaque object representing the library context. |
1e511322 | 26 | */ |
ba6929f6 | 27 | struct udev { |
3c6ac219 | 28 | unsigned n_ref; |
912541b0 | 29 | void *userdata; |
ba6929f6 KS |
30 | }; |
31 | ||
1e511322 KS |
32 | /** |
33 | * udev_get_userdata: | |
34 | * @udev: udev library context | |
35 | * | |
36 | * Retrieve stored data pointer from library context. This might be useful | |
25e773ee | 37 | * to access from callbacks. |
1e511322 KS |
38 | * |
39 | * Returns: stored userdata | |
40 | **/ | |
25e773ee | 41 | _public_ void *udev_get_userdata(struct udev *udev) { |
bc54df90 YW |
42 | assert_return(udev, NULL); |
43 | ||
912541b0 | 44 | return udev->userdata; |
c8e32461 KS |
45 | } |
46 | ||
1e511322 KS |
47 | /** |
48 | * udev_set_userdata: | |
49 | * @udev: udev library context | |
50 | * @userdata: data pointer | |
51 | * | |
52 | * Store custom @userdata in the library context. | |
53 | **/ | |
25e773ee | 54 | _public_ void udev_set_userdata(struct udev *udev, void *userdata) { |
bc54df90 | 55 | if (!udev) |
912541b0 | 56 | return; |
bc54df90 | 57 | |
912541b0 | 58 | udev->userdata = userdata; |
c8e32461 KS |
59 | } |
60 | ||
33a5cc29 KS |
61 | /** |
62 | * udev_new: | |
63 | * | |
e8112e67 | 64 | * Create udev library context. This only allocates the basic data structure. |
33a5cc29 KS |
65 | * |
66 | * The initial refcount is 1, and needs to be decremented to | |
be7de409 | 67 | * release the resources of the udev library context. |
33a5cc29 KS |
68 | * |
69 | * Returns: a new udev library context | |
70 | **/ | |
25e773ee | 71 | _public_ struct udev *udev_new(void) { |
912541b0 | 72 | struct udev *udev; |
912541b0 | 73 | |
5ccb44a5 | 74 | udev = new(struct udev, 1); |
fd05c424 YW |
75 | if (!udev) |
76 | return_with_errno(NULL, ENOMEM); | |
5ccb44a5 YW |
77 | |
78 | *udev = (struct udev) { | |
79 | .n_ref = 1, | |
80 | }; | |
912541b0 | 81 | |
912541b0 | 82 | return udev; |
33a5cc29 KS |
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 | **/ | |
3c6ac219 | 93 | DEFINE_PUBLIC_TRIVIAL_REF_FUNC(struct udev, udev); |
33a5cc29 KS |
94 | |
95 | /** | |
96 | * udev_unref: | |
97 | * @udev: udev library context | |
98 | * | |
99 | * Drop a reference of the udev library context. If the refcount | |
be7de409 | 100 | * reaches zero, the resources of the context will be released. |
33a5cc29 | 101 | * |
c1959569 | 102 | * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise. |
33a5cc29 | 103 | **/ |
25e773ee | 104 | _public_ struct udev *udev_unref(struct udev *udev) { |
3c6ac219 | 105 | if (!udev) |
20bbd54f | 106 | return NULL; |
3c6ac219 YW |
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. */ | |
20bbd54f | 114 | return udev; |
3c6ac219 | 115 | |
5fecf46d | 116 | return mfree(udev); |
33a5cc29 KS |
117 | } |
118 | ||
119 | /** | |
120 | * udev_set_log_fn: | |
121 | * @udev: udev library context | |
f47ad593 | 122 | * @log_fn: function to be called for log messages |
33a5cc29 | 123 | * |
25e773ee | 124 | * This function is deprecated. |
33a5cc29 KS |
125 | * |
126 | **/ | |
bc54df90 YW |
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)) { | |
25e773ee | 132 | return; |
7d563a17 KS |
133 | } |
134 | ||
1e511322 KS |
135 | /** |
136 | * udev_get_log_priority: | |
137 | * @udev: udev library context | |
138 | * | |
25e773ee | 139 | * This function is deprecated. |
1e511322 | 140 | * |
1e511322 | 141 | **/ |
25e773ee KS |
142 | _public_ int udev_get_log_priority(struct udev *udev) { |
143 | return log_get_max_level(); | |
7d563a17 KS |
144 | } |
145 | ||
1e511322 KS |
146 | /** |
147 | * udev_set_log_priority: | |
148 | * @udev: udev library context | |
f47ad593 | 149 | * @priority: the new log priority |
1e511322 | 150 | * |
25e773ee KS |
151 | * This function is deprecated. |
152 | * | |
1e511322 | 153 | **/ |
25e773ee KS |
154 | _public_ void udev_set_log_priority(struct udev *udev, int priority) { |
155 | log_set_max_level(priority); | |
7d563a17 | 156 | } |