]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev.c
tree-wide: drop copyright headers from frequent contributors
[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 {
912541b0
KS
32 int refcount;
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 }
912541b0 85 udev->refcount = 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 **/
25e773ee 98_public_ struct udev *udev_ref(struct udev *udev) {
912541b0
KS
99 if (udev == NULL)
100 return NULL;
101 udev->refcount++;
102 return udev;
33a5cc29
KS
103}
104
105/**
106 * udev_unref:
107 * @udev: udev library context
108 *
109 * Drop a reference of the udev library context. If the refcount
be7de409 110 * reaches zero, the resources of the context will be released.
33a5cc29 111 *
c1959569 112 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
33a5cc29 113 **/
25e773ee 114_public_ struct udev *udev_unref(struct udev *udev) {
912541b0 115 if (udev == NULL)
20bbd54f 116 return NULL;
912541b0
KS
117 udev->refcount--;
118 if (udev->refcount > 0)
20bbd54f 119 return udev;
5fecf46d 120 return mfree(udev);
33a5cc29
KS
121}
122
123/**
124 * udev_set_log_fn:
125 * @udev: udev library context
f47ad593 126 * @log_fn: function to be called for log messages
33a5cc29 127 *
25e773ee 128 * This function is deprecated.
33a5cc29
KS
129 *
130 **/
54cf0b7f 131_public_ void udev_set_log_fn(struct udev *udev,
912541b0
KS
132 void (*log_fn)(struct udev *udev,
133 int priority, const char *file, int line, const char *fn,
25e773ee
KS
134 const char *format, va_list args)) {
135 return;
7d563a17
KS
136}
137
1e511322
KS
138/**
139 * udev_get_log_priority:
140 * @udev: udev library context
141 *
25e773ee 142 * This function is deprecated.
1e511322 143 *
1e511322 144 **/
25e773ee
KS
145_public_ int udev_get_log_priority(struct udev *udev) {
146 return log_get_max_level();
7d563a17
KS
147}
148
1e511322
KS
149/**
150 * udev_set_log_priority:
151 * @udev: udev library context
f47ad593 152 * @priority: the new log priority
1e511322 153 *
25e773ee
KS
154 * This function is deprecated.
155 *
1e511322 156 **/
25e773ee
KS
157_public_ void udev_set_log_priority(struct udev *udev, int priority) {
158 log_set_max_level(priority);
7d563a17 159}