1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #pragma once
3 
4 #include "sd-event.h"
5 
6 #include "macro.h"
7 #include "time-util.h"
8 
9 typedef struct UdevCtrl UdevCtrl;
10 
11 typedef enum UdevCtrlMessageType {
12         _UDEV_CTRL_END_MESSAGES,
13         UDEV_CTRL_SET_LOG_LEVEL,
14         UDEV_CTRL_STOP_EXEC_QUEUE,
15         UDEV_CTRL_START_EXEC_QUEUE,
16         UDEV_CTRL_RELOAD,
17         UDEV_CTRL_SET_ENV,
18         UDEV_CTRL_SET_CHILDREN_MAX,
19         UDEV_CTRL_PING,
20         UDEV_CTRL_EXIT,
21 } UdevCtrlMessageType;
22 
23 typedef union UdevCtrlMessageValue {
24         int intval;
25         char buf[256];
26 } UdevCtrlMessageValue;
27 
28 typedef int (*udev_ctrl_handler_t)(UdevCtrl *udev_ctrl, UdevCtrlMessageType type,
29                                    const UdevCtrlMessageValue *value, void *userdata);
30 
31 int udev_ctrl_new_from_fd(UdevCtrl **ret, int fd);
udev_ctrl_new(UdevCtrl ** ret)32 static inline int udev_ctrl_new(UdevCtrl **ret) {
33         return udev_ctrl_new_from_fd(ret, -1);
34 }
35 
36 int udev_ctrl_enable_receiving(UdevCtrl *uctrl);
37 UdevCtrl *udev_ctrl_ref(UdevCtrl *uctrl);
38 UdevCtrl *udev_ctrl_unref(UdevCtrl *uctrl);
39 int udev_ctrl_attach_event(UdevCtrl *uctrl, sd_event *event);
40 int udev_ctrl_start(UdevCtrl *uctrl, udev_ctrl_handler_t callback, void *userdata);
41 sd_event_source *udev_ctrl_get_event_source(UdevCtrl *uctrl);
42 
43 int udev_ctrl_wait(UdevCtrl *uctrl, usec_t timeout);
44 
45 int udev_ctrl_send(UdevCtrl *uctrl, UdevCtrlMessageType type, const void *data);
udev_ctrl_send_set_log_level(UdevCtrl * uctrl,int priority)46 static inline int udev_ctrl_send_set_log_level(UdevCtrl *uctrl, int priority) {
47         return udev_ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, INT_TO_PTR(priority));
48 }
49 
udev_ctrl_send_stop_exec_queue(UdevCtrl * uctrl)50 static inline int udev_ctrl_send_stop_exec_queue(UdevCtrl *uctrl) {
51         return udev_ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, NULL);
52 }
53 
udev_ctrl_send_start_exec_queue(UdevCtrl * uctrl)54 static inline int udev_ctrl_send_start_exec_queue(UdevCtrl *uctrl) {
55         return udev_ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, NULL);
56 }
57 
udev_ctrl_send_reload(UdevCtrl * uctrl)58 static inline int udev_ctrl_send_reload(UdevCtrl *uctrl) {
59         return udev_ctrl_send(uctrl, UDEV_CTRL_RELOAD, NULL);
60 }
61 
udev_ctrl_send_set_env(UdevCtrl * uctrl,const char * key)62 static inline int udev_ctrl_send_set_env(UdevCtrl *uctrl, const char *key) {
63         return udev_ctrl_send(uctrl, UDEV_CTRL_SET_ENV, key);
64 }
65 
udev_ctrl_send_set_children_max(UdevCtrl * uctrl,int count)66 static inline int udev_ctrl_send_set_children_max(UdevCtrl *uctrl, int count) {
67         return udev_ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, INT_TO_PTR(count));
68 }
69 
udev_ctrl_send_ping(UdevCtrl * uctrl)70 static inline int udev_ctrl_send_ping(UdevCtrl *uctrl) {
71         return udev_ctrl_send(uctrl, UDEV_CTRL_PING, NULL);
72 }
73 
udev_ctrl_send_exit(UdevCtrl * uctrl)74 static inline int udev_ctrl_send_exit(UdevCtrl *uctrl) {
75         return udev_ctrl_send(uctrl, UDEV_CTRL_EXIT, NULL);
76 }
77 
78 DEFINE_TRIVIAL_CLEANUP_FUNC(UdevCtrl*, udev_ctrl_unref);
79