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