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