1/* SPDX-License-Identifier: LGPL-2.1+ */
2/***
3 This file is part of systemd.
4
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#ifndef _LIBUDEV_H_
22#define _LIBUDEV_H_
23
24#include <stdarg.h>
25#include <sys/sysmacros.h>
26#include <sys/types.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/*
33 * udev - library context
34 *
35 * reads the udev config and system environment
36 * allows custom logging
37 */
38struct udev;
39struct udev *udev_ref(struct udev *udev);
40struct udev *udev_unref(struct udev *udev);
41struct udev *udev_new(void);
42void udev_set_log_fn(struct udev *udev,
43 void (*log_fn)(struct udev *udev,
44 int priority, const char *file, int line, const char *fn,
45 const char *format, va_list args)) __attribute__ ((deprecated));
46int udev_get_log_priority(struct udev *udev) __attribute__ ((deprecated));
47void udev_set_log_priority(struct udev *udev, int priority) __attribute__ ((deprecated));
48void *udev_get_userdata(struct udev *udev);
49void udev_set_userdata(struct udev *udev, void *userdata);
50
51/*
52 * udev_list
53 *
54 * access to libudev generated lists
55 */
56struct udev_list_entry;
57struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry);
58struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name);
59const char *udev_list_entry_get_name(struct udev_list_entry *list_entry);
60const char *udev_list_entry_get_value(struct udev_list_entry *list_entry);
61/**
62 * udev_list_entry_foreach:
63 * @list_entry: entry to store the current position
64 * @first_entry: first entry to start with
65 *
66 * Helper to iterate over all entries of a list.
67 */
68#define udev_list_entry_foreach(list_entry, first_entry) \
69 for (list_entry = first_entry; \
70 list_entry != NULL; \
71 list_entry = udev_list_entry_get_next(list_entry))
72
73/*
74 * udev_device
75 *
76 * access to sysfs/kernel devices
77 */
78struct udev_device;
79struct udev_device *udev_device_ref(struct udev_device *udev_device);
80struct udev_device *udev_device_unref(struct udev_device *udev_device);
81struct udev *udev_device_get_udev(struct udev_device *udev_device);
82struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath);
83struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum);
84struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname);
85struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id);
86struct udev_device *udev_device_new_from_environment(struct udev *udev);
87/* udev_device_get_parent_*() does not take a reference on the returned device, it is automatically unref'd with the parent */
88struct udev_device *udev_device_get_parent(struct udev_device *udev_device);
89struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device,
90 const char *subsystem, const char *devtype);
91/* retrieve device properties */
92const char *udev_device_get_devpath(struct udev_device *udev_device);
93const char *udev_device_get_subsystem(struct udev_device *udev_device);
94const char *udev_device_get_devtype(struct udev_device *udev_device);
95const char *udev_device_get_syspath(struct udev_device *udev_device);
96const char *udev_device_get_sysname(struct udev_device *udev_device);
97const char *udev_device_get_sysnum(struct udev_device *udev_device);
98const char *udev_device_get_devnode(struct udev_device *udev_device);
99int udev_device_get_is_initialized(struct udev_device *udev_device);
100struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device);
101struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device);
102struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device);
103struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device);
104const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key);
105const char *udev_device_get_driver(struct udev_device *udev_device);
106dev_t udev_device_get_devnum(struct udev_device *udev_device);
107const char *udev_device_get_action(struct udev_device *udev_device);
108unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device);
109unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device);
110const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr);
111int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, char *value);
112int udev_device_has_tag(struct udev_device *udev_device, const char *tag);
113
114/*
115 * udev_monitor
116 *
117 * access to kernel uevents and udev events
118 */
119struct udev_monitor;
120struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor);
121struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor);
122struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor);
123/* kernel and udev generated events over netlink */
124struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name);
125/* bind socket */
126int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor);
127int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size);
128int udev_monitor_get_fd(struct udev_monitor *udev_monitor);
129struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor);
130/* in-kernel socket filters to select messages that get delivered to a listener */
131int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor,
132 const char *subsystem, const char *devtype);
133int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag);
134int udev_monitor_filter_update(struct udev_monitor *udev_monitor);
135int udev_monitor_filter_remove(struct udev_monitor *udev_monitor);
136
137/*
138 * udev_enumerate
139 *
140 * search sysfs for specific devices and provide a sorted list
141 */
142struct udev_enumerate;
143struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate);
144struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate);
145struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate);
146struct udev_enumerate *udev_enumerate_new(struct udev *udev);
147/* device properties filter */
148int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem);
149int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem);
150int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value);
151int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value);
152int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value);
153int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname);
154int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag);
155int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent);
156int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate);
157int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath);
158/* run enumeration with active filters */
159int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate);
160int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate);
161/* return device list */
162struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate);
163
164/*
165 * udev_queue
166 *
167 * access to the currently running udev events
168 */
169struct udev_queue;
170struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue);
171struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue);
172struct udev *udev_queue_get_udev(struct udev_queue *udev_queue);
173struct udev_queue *udev_queue_new(struct udev *udev);
174unsigned long long int udev_queue_get_kernel_seqnum(struct udev_queue *udev_queue) __attribute__ ((deprecated));
175unsigned long long int udev_queue_get_udev_seqnum(struct udev_queue *udev_queue) __attribute__ ((deprecated));
176int udev_queue_get_udev_is_active(struct udev_queue *udev_queue);
177int udev_queue_get_queue_is_empty(struct udev_queue *udev_queue);
178int udev_queue_get_seqnum_is_finished(struct udev_queue *udev_queue, unsigned long long int seqnum) __attribute__ ((deprecated));
179int udev_queue_get_seqnum_sequence_is_finished(struct udev_queue *udev_queue,
180 unsigned long long int start, unsigned long long int end) __attribute__ ((deprecated));
181int udev_queue_get_fd(struct udev_queue *udev_queue);
182int udev_queue_flush(struct udev_queue *udev_queue);
183struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev_queue) __attribute__ ((deprecated));
184
185/*
186 * udev_hwdb
187 *
188 * access to the static hardware properties database
189 */
190struct udev_hwdb;
191struct udev_hwdb *udev_hwdb_new(struct udev *udev);
192struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb);
193struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb);
194struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags);
195
196/*
197 * udev_util
198 *
199 * udev specific utilities
200 */
201int udev_util_encode_string(const char *str, char *str_enc, size_t len);
202
203
204#ifdef __cplusplus
205} /* extern "C" */
206#endif
207
208#endif
209