]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libudev / libudev.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2008-2014 Kay Sievers <kay@vrfy.org>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <ctype.h>
22 #include <stdarg.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "libudev.h"
29
30 #include "alloc-util.h"
31 #include "fd-util.h"
32 #include "libudev-private.h"
33 #include "missing.h"
34 #include "string-util.h"
35
36 /**
37 * SECTION:libudev
38 * @short_description: libudev context
39 *
40 * The context contains the default values read from the udev config file,
41 * and is passed to all library operations.
42 */
43
44 /**
45 * udev:
46 *
47 * Opaque object representing the library context.
48 */
49 struct udev {
50 int refcount;
51 void (*log_fn)(struct udev *udev,
52 int priority, const char *file, int line, const char *fn,
53 const char *format, va_list args);
54 void *userdata;
55 };
56
57 /**
58 * udev_get_userdata:
59 * @udev: udev library context
60 *
61 * Retrieve stored data pointer from library context. This might be useful
62 * to access from callbacks.
63 *
64 * Returns: stored userdata
65 **/
66 _public_ void *udev_get_userdata(struct udev *udev) {
67 if (udev == NULL)
68 return NULL;
69 return udev->userdata;
70 }
71
72 /**
73 * udev_set_userdata:
74 * @udev: udev library context
75 * @userdata: data pointer
76 *
77 * Store custom @userdata in the library context.
78 **/
79 _public_ void udev_set_userdata(struct udev *udev, void *userdata) {
80 if (udev == NULL)
81 return;
82 udev->userdata = userdata;
83 }
84
85 /**
86 * udev_new:
87 *
88 * Create udev library context. This reads the udev configuration
89 * file, and fills in the default values.
90 *
91 * The initial refcount is 1, and needs to be decremented to
92 * release the resources of the udev library context.
93 *
94 * Returns: a new udev library context
95 **/
96 _public_ struct udev *udev_new(void) {
97 struct udev *udev;
98
99 udev = new0(struct udev, 1);
100 if (!udev) {
101 errno = ENOMEM;
102 return NULL;
103 }
104 udev->refcount = 1;
105
106 return udev;
107 }
108
109 /**
110 * udev_ref:
111 * @udev: udev library context
112 *
113 * Take a reference of the udev library context.
114 *
115 * Returns: the passed udev library context
116 **/
117 _public_ struct udev *udev_ref(struct udev *udev) {
118 if (udev == NULL)
119 return NULL;
120 udev->refcount++;
121 return udev;
122 }
123
124 /**
125 * udev_unref:
126 * @udev: udev library context
127 *
128 * Drop a reference of the udev library context. If the refcount
129 * reaches zero, the resources of the context will be released.
130 *
131 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
132 **/
133 _public_ struct udev *udev_unref(struct udev *udev) {
134 if (udev == NULL)
135 return NULL;
136 udev->refcount--;
137 if (udev->refcount > 0)
138 return udev;
139 free(udev);
140 return NULL;
141 }
142
143 /**
144 * udev_set_log_fn:
145 * @udev: udev library context
146 * @log_fn: function to be called for log messages
147 *
148 * This function is deprecated.
149 *
150 **/
151 _public_ void udev_set_log_fn(struct udev *udev,
152 void (*log_fn)(struct udev *udev,
153 int priority, const char *file, int line, const char *fn,
154 const char *format, va_list args)) {
155 return;
156 }
157
158 /**
159 * udev_get_log_priority:
160 * @udev: udev library context
161 *
162 * This function is deprecated.
163 *
164 **/
165 _public_ int udev_get_log_priority(struct udev *udev) {
166 return log_get_max_level();
167 }
168
169 /**
170 * udev_set_log_priority:
171 * @udev: udev library context
172 * @priority: the new log priority
173 *
174 * This function is deprecated.
175 *
176 **/
177 _public_ void udev_set_log_priority(struct udev *udev, int priority) {
178 log_set_max_level(priority);
179 }