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