HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/udev/udev-manager.h

5,817 bytesGit mode 100644Download original fileGPL-2.0-or-later

Git blob: 48ac3cc067c13bb577994d97a49b958688264a18

Lines 1–161 of 161

/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
 
#include "sd-device.h"
#include "sd-event.h"
 
#include "list.h"
#include "pidref.h"
#include "socket-util.h"
#include "udev-config.h"
#include "udev-forward.h"
 
/* This should have a higher priority than the device monitor and inotify watch, to make device monitor and
 * inotify event source stopped as soon as possible when the signal is received. Otherwise, we may continue
 * receive events that needs to be serialized anyway. */
#define EVENT_PRIORITY_SIGTERM        (SD_EVENT_PRIORITY_NORMAL - 7)
/* This must have a higher priority than the worker notification, to make IN_IGNORED event received earlier
 * than notifications about requests of adding/removing inotify watches. */
#define EVENT_PRIORITY_INOTIFY_WATCH  (SD_EVENT_PRIORITY_NORMAL - 6)
/* This must have a higher priority than the worker exit event, to make notifications about completions of
 * processing events received before exit. */
#define EVENT_PRIORITY_WORKER_NOTIFY  (SD_EVENT_PRIORITY_NORMAL - 5)
/* This should have a higher priority than timer events about killing long running worker processes or idle
 * worker processes. */
#define EVENT_PRIORITY_WORKER_EXIT    (SD_EVENT_PRIORITY_NORMAL - 4)
/* As mentioned above, this should have a lower priority than the exit event source. */
#define EVENT_PRIORITY_WORKER_TIMER   (SD_EVENT_PRIORITY_NORMAL - 3)
/* This must have a higher priority than the device monitor to prevent uevent floods from starving
 * event_queue_start(), but lower than worker event sources to observe worker completions first. */
#define EVENT_PRIORITY_POST           (SD_EVENT_PRIORITY_NORMAL - 2)
/* This must have a lower priority than the worker event source and post event source. Otherwise, flood of
 * uevents delays worker sigchld events or notifications, and workers will not continue to process events. */
#define EVENT_PRIORITY_DEVICE_MONITOR (SD_EVENT_PRIORITY_NORMAL - 1)
/* This should have a lower priority than most event sources, but let's process earlier than varlink and the
 * legacy control socket. */
#define EVENT_PRIORITY_SIGHUP         (SD_EVENT_PRIORITY_NORMAL + 1)
/* Let's not interrupt the service by any user process, even that requires privileges. */
#define EVENT_PRIORITY_VARLINK        (SD_EVENT_PRIORITY_NORMAL + 2)
/* The event is intended to trigger the post-event source, hence can be the lowest priority. */
#define EVENT_PRIORITY_REQUEUE_EVENT  (SD_EVENT_PRIORITY_NORMAL + 3)
 
typedef struct Manager {
        sd_event *event;
        Hashmap *workers;
        LIST_HEAD(Event, events);
        Event *last_event;
        char *workers_cgroup;
 
        UdevRules *rules;
        Hashmap *properties;
 
        sd_device_monitor *monitor;
        sd_varlink_server *varlink_server;
 
        char *worker_notify_socket_path;
 
        /* used by udev-watch */
        int inotify_fd;
        sd_event_source *inotify_event;
        Set *synthesize_change_child_event_sources;
        Set *synthesized_events;
        sd_event_source *synthesized_events_clear_event_source;
 
        sd_event_source *kill_workers_event;
 
        Hashmap *locked_events_by_disk;
        Prioq *locked_events_by_time;
        sd_event_source *requeue_locked_events_timer_event_source;
 
        usec_t last_usec;
 
        UdevConfig config_by_udev_conf;
        UdevConfig config_by_command;
        UdevConfig config_by_kernel;
        UdevConfig config_by_control;
        UdevConfig config;
 
        bool queue_file_created;
        bool stop_exec_queue;
        bool exit;
} Manager;
 
typedef enum EventState {
        EVENT_UNDEF,
        EVENT_QUEUED,
        EVENT_RUNNING,
        EVENT_LOCKED,
        EVENT_PROCESSED,
} EventState;
 
typedef struct Event {
        /* All events that have not been processed (state != EVENT_PROCESSED) are referenced by the Manager.
         * Additionally, an event may be referenced by events blocked by this event. See event_find_blocker(). */
        unsigned n_ref;
 
        Manager *manager;
        Worker *worker;
        EventState state;
 
        sd_device *dev;
 
        sd_device_action_t action;
        uint64_t seqnum;
        const char *id;
        const char *devpath;
        const char *devpath_old;
        const char *devnode;
 
        /* Used when the device is locked by another program. */
        usec_t requeue_next_usec;
        usec_t requeue_timeout_usec;
        unsigned locked_event_prioq_index;
        char *whole_disk;
        LIST_FIELDS(Event, same_disk);
 
        /* The last blocker for this event. This event must not be processed before the blocker is processed. */
        Event *blocker;
 
        LIST_FIELDS(Event, event);
} Event;
 
typedef enum WorkerState {
        WORKER_UNDEF,
        WORKER_RUNNING,
        WORKER_IDLE,
        WORKER_KILLED,
} WorkerState;
 
typedef struct Worker {
        Manager *manager;
        PidRef pidref;
        sd_event_source *child_event_source;
        sd_event_source *timeout_warning_event_source;
        sd_event_source *timeout_kill_event_source;
        union sockaddr_union address;
        WorkerState state;
        Event *event;
} Worker;
 
Manager* manager_new(void);
Manager* manager_free(Manager *manager);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
 
Worker* worker_free(Worker *worker);
 
int manager_main(Manager *manager);
void manager_reload(Manager *manager, bool force);
void manager_revert(Manager *manager);
void manager_exit(Manager *manager);
 
void notify_ready(Manager *manager);
 
void manager_kill_workers(Manager *manager, int signo);
int manager_reset_kill_workers_timer(Manager *manager);
 
bool devpath_conflict(const char *a, const char *b);
 
int manager_requeue_locked_events_by_device(Manager *manager, sd_device *dev);
 
int manager_create_queue_file(Manager *manager);