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