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