]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / libudev / libudev.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2014 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libudev.h"
28
29 #include "alloc-util.h"
30 #include "fd-util.h"
31 #include "libudev-private.h"
32 #include "missing.h"
33 #include "string-util.h"
34
35 /**
36 * SECTION:libudev
37 * @short_description: libudev context
38 *
39 * The context contains the default values read from the udev config file,
40 * and is passed to all library operations.
41 */
42
43 /**
44 * udev:
45 *
46 * Opaque object representing the library context.
47 */
48 struct udev {
49 int refcount;
50 void (*log_fn)(struct udev *udev,
51 int priority, const char *file, int line, const char *fn,
52 const char *format, va_list args);
53 void *userdata;
54 };
55
56 /**
57 * udev_get_userdata:
58 * @udev: udev library context
59 *
60 * Retrieve stored data pointer from library context. This might be useful
61 * to access from callbacks.
62 *
63 * Returns: stored userdata
64 **/
65 _public_ void *udev_get_userdata(struct udev *udev) {
66 if (udev == NULL)
67 return NULL;
68 return udev->userdata;
69 }
70
71 /**
72 * udev_set_userdata:
73 * @udev: udev library context
74 * @userdata: data pointer
75 *
76 * Store custom @userdata in the library context.
77 **/
78 _public_ void udev_set_userdata(struct udev *udev, void *userdata) {
79 if (udev == NULL)
80 return;
81 udev->userdata = userdata;
82 }
83
84 /**
85 * udev_new:
86 *
87 * Create udev library context. This reads the udev configuration
88 * file, and fills in the default values.
89 *
90 * The initial refcount is 1, and needs to be decremented to
91 * release the resources of the udev library context.
92 *
93 * Returns: a new udev library context
94 **/
95 _public_ struct udev *udev_new(void) {
96 struct udev *udev;
97
98 udev = new0(struct udev, 1);
99 if (!udev) {
100 errno = ENOMEM;
101 return NULL;
102 }
103 udev->refcount = 1;
104
105 return udev;
106 }
107
108 /**
109 * udev_ref:
110 * @udev: udev library context
111 *
112 * Take a reference of the udev library context.
113 *
114 * Returns: the passed udev library context
115 **/
116 _public_ struct udev *udev_ref(struct udev *udev) {
117 if (udev == NULL)
118 return NULL;
119 udev->refcount++;
120 return udev;
121 }
122
123 /**
124 * udev_unref:
125 * @udev: udev library context
126 *
127 * Drop a reference of the udev library context. If the refcount
128 * reaches zero, the resources of the context will be released.
129 *
130 * Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
131 **/
132 _public_ struct udev *udev_unref(struct udev *udev) {
133 if (udev == NULL)
134 return NULL;
135 udev->refcount--;
136 if (udev->refcount > 0)
137 return udev;
138 free(udev);
139 return NULL;
140 }
141
142 /**
143 * udev_set_log_fn:
144 * @udev: udev library context
145 * @log_fn: function to be called for log messages
146 *
147 * This function is deprecated.
148 *
149 **/
150 _public_ void udev_set_log_fn(struct udev *udev,
151 void (*log_fn)(struct udev *udev,
152 int priority, const char *file, int line, const char *fn,
153 const char *format, va_list args)) {
154 return;
155 }
156
157 /**
158 * udev_get_log_priority:
159 * @udev: udev library context
160 *
161 * This function is deprecated.
162 *
163 **/
164 _public_ int udev_get_log_priority(struct udev *udev) {
165 return log_get_max_level();
166 }
167
168 /**
169 * udev_set_log_priority:
170 * @udev: udev library context
171 * @priority: the new log priority
172 *
173 * This function is deprecated.
174 *
175 **/
176 _public_ void udev_set_log_priority(struct udev *udev, int priority) {
177 log_set_max_level(priority);
178 }