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