HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-event/sd-event.c

181,814 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 231ed955b540a6e71c82e47bc4d98bc301ac14ac

Lines 2001–3000 of 5,529

 
                _cleanup_free_ char *cg = NULL;
                r = cg_pid_get_path(0, &cg);
                if (r < 0)
                        return r;
 
                _cleanup_free_ char *cgroup_file = strjoin(info->name, ".pressure");
                if (!cgroup_file)
                        return -ENOMEM;
 
                w = path_join("/sys/fs/cgroup", cg, cgroup_file);
                if (!w)
                        return -ENOMEM;
 
                watch = w;
 
                watch_fallback = strjoin("/proc/pressure/", info->name);
                if (!watch_fallback)
                        return -ENOMEM;
 
                /* Android uses three levels in its userspace low memory killer logic:
                 *     some  70000 1000000
                 *     some 100000 1000000
                 *     full  70000 1000000
                 *
                 * GNOME's low memory monitor uses:
                 *     some  70000 1000000
                 *     some 100000 1000000
                 *     full 100000 1000000
                 *
                 * We'll default to the middle level that both agree on. Except we do it on a 2s window
                 * (i.e. 200ms per 2s, rather than 100ms per 1s), because that's the window duration the
                 * kernel will allow us to do unprivileged, also in the future. */
                if (asprintf((char**) &write_buffer,
                             "%s " USEC_FMT " " USEC_FMT,
                             PRESSURE_DEFAULT_TYPE,
                             PRESSURE_DEFAULT_THRESHOLD_USEC,
                             PRESSURE_DEFAULT_WINDOW_USEC) < 0)
                        return -ENOMEM;
 
                write_buffer_size = strlen(write_buffer) + 1;
                locked = false;
        }
 
        path_fd = open(watch, O_PATH|O_CLOEXEC);
        if (path_fd < 0) {
                if (errno != ENOENT)
                        return -errno;
 
                /* We got ENOENT. Two options now: try the fallback if we have one, or return the error as is
                 * (when based on user/env config). */
 
                if (!watch_fallback) {
                        assert(locked);
                        return -ENOENT;
                }
 
                path_fd = open(watch_fallback, O_PATH|O_CLOEXEC);
                if (path_fd < 0) {
                        if (errno == ENOENT) /* PSI is not available in the kernel even under the fallback path? */
                                return -EOPNOTSUPP;
                        return -errno;
                }
        }
 
        if (fstat(path_fd, &st) < 0)
                return -errno;
 
        if (S_ISSOCK(st.st_mode)) {
                fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
                if (fd < 0)
                        return -errno;
 
                r = connect_unix_path(fd, path_fd, NULL);
                if (r < 0)
                        return r;
 
                events = EPOLLIN;
 
        } else if (S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode)) {
                fd = fd_reopen(path_fd, (write_buffer_size > 0 ? O_RDWR : O_RDONLY) |O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
                if (fd < 0)
                        return fd;
 
                if (S_ISREG(st.st_mode)) {
                        struct statfs sfs;
 
                        /* If this is a regular file validate this is a procfs or cgroupfs file, where we look for EPOLLPRI */
 
                        if (fstatfs(fd, &sfs) < 0)
                                return -errno;
 
                        if (!is_fs_type(&sfs, PROC_SUPER_MAGIC) &&
                            !is_fs_type(&sfs, CGROUP2_SUPER_MAGIC))
                                return -ENOTTY;
 
                        events = EPOLLPRI;
                } else
                        /* For fifos and char devices just watch for EPOLLIN */
                        events = EPOLLIN;
 
        } else if (S_ISDIR(st.st_mode))
                return -EISDIR;
        else
                return -EBADF;
 
        s->pressure.fd = TAKE_FD(fd);
        s->pressure.write_buffer = TAKE_PTR(write_buffer);
        s->pressure.write_buffer_size = write_buffer_size;
        s->pressure.events = events;
        s->pressure.locked = locked;
 
        /* So here's the thing: if we are talking to PSI we need to write the watch string before adding the
         * fd to epoll (if we ignore this, then the watch won't work). Hence we'll not actually register the
         * fd with the epoll right-away. Instead, we just add the event source to a list of pressure event
         * sources on which writes must be executed before the first event loop iteration is executed. (We
         * could also write the data here, right away, but we want to give the caller the freedom to call
         * sd_event_source_set_{memory,cpu,io}_pressure_type() and
         * sd_event_source_set_{memory,cpu,io}_pressure_period() before we write it. */
 
        if (s->pressure.write_buffer_size > 0)
                source_pressure_add_to_write_list(s);
        else {
                r = source_pressure_register(s, s->enabled);
                if (r < 0)
                        return r;
        }
 
        if (ret)
                *ret = s;
        TAKE_PTR(s);
 
        return 0;
}
 
_public_ int sd_event_add_memory_pressure(
                sd_event *e,
                sd_event_source **ret,
                sd_event_handler_t callback,
                void *userdata) {
 
        return event_add_pressure(
                        e, ret, callback, userdata,
                        SOURCE_MEMORY_PRESSURE,
                        memory_pressure_callback,
                        PRESSURE_MEMORY);
}
 
static int cpu_pressure_callback(sd_event_source *s, void *userdata) {
        assert(s);
 
        return 0;
}
 
_public_ int sd_event_add_cpu_pressure(
                sd_event *e,
                sd_event_source **ret,
                sd_event_handler_t callback,
                void *userdata) {
 
        return event_add_pressure(
                        e, ret, callback, userdata,
                        SOURCE_CPU_PRESSURE,
                        cpu_pressure_callback,
                        PRESSURE_CPU);
}
 
static int io_pressure_callback(sd_event_source *s, void *userdata) {
        assert(s);
 
        return 0;
}
 
_public_ int sd_event_add_io_pressure(
                sd_event *e,
                sd_event_source **ret,
                sd_event_handler_t callback,
                void *userdata) {
 
        return event_add_pressure(
                        e, ret, callback, userdata,
                        SOURCE_IO_PRESSURE,
                        io_pressure_callback,
                        PRESSURE_IO);
}
 
static void event_free_inotify_data(sd_event *e, InotifyData *d) {
        assert(e);
 
        if (!d)
                return;
 
        assert(hashmap_isempty(d->inodes));
        assert(hashmap_isempty(d->wd));
 
        if (d->buffer_filled > 0)
                LIST_REMOVE(buffered, e->buffered_inotify_data_list, d);
 
        hashmap_free(d->inodes);
        hashmap_free(d->wd);
 
        assert_se(hashmap_remove(e->inotify_data, &d->priority) == d);
 
        if (d->fd >= 0) {
                if (!event_origin_changed(e) &&
                    epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, d->fd, NULL) < 0)
                        log_debug_errno(errno, "Failed to remove inotify fd from epoll, ignoring: %m");
 
                safe_close(d->fd);
        }
        free(d);
}
 
static int event_make_inotify_data(sd_event *e, int64_t priority, InotifyData **ret) {
        _cleanup_close_ int fd = -EBADF;
        InotifyData *d;
        int r;
 
        assert(e);
 
        d = hashmap_get(e->inotify_data, &priority);
        if (d) {
                if (ret)
                        *ret = d;
                return 0;
        }
 
        fd = inotify_init1(IN_NONBLOCK|O_CLOEXEC);
        if (fd < 0)
                return -errno;
 
        fd = fd_move_above_stdio(fd);
 
        d = new(InotifyData, 1);
        if (!d)
                return -ENOMEM;
 
        *d = (InotifyData) {
                .wakeup = WAKEUP_INOTIFY_DATA,
                .fd = TAKE_FD(fd),
                .priority = priority,
        };
 
        r = hashmap_ensure_put(&e->inotify_data, &uint64_hash_ops, &d->priority, d);
        if (r < 0) {
                d->fd = safe_close(d->fd);
                free(d);
                return r;
        }
 
        struct epoll_event ev = {
                .events = EPOLLIN,
                .data.ptr = d,
        };
 
        if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev) < 0) {
                r = -errno;
                d->fd = safe_close(d->fd); /* let's close this ourselves, as event_free_inotify_data() would otherwise
                                            * remove the fd from the epoll first, which we don't want as we couldn't
                                            * add it in the first place. */
                event_free_inotify_data(e, d);
                return r;
        }
 
        if (ret)
                *ret = d;
 
        return 1;
}
 
static int inode_data_compare(const InodeData *x, const InodeData *y) {
        int r;
 
        assert(x);
        assert(y);
 
        r = CMP(x->dev, y->dev);
        if (r != 0)
                return r;
 
        return CMP(x->ino, y->ino);
}
 
static void inode_data_hash_func(const InodeData *d, struct siphash *state) {
        assert(d);
 
        siphash24_compress_typesafe(d->dev, state);
        siphash24_compress_typesafe(d->ino, state);
}
 
DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops, InodeData, inode_data_hash_func, inode_data_compare);
 
static void event_free_inode_data(sd_event *e, InodeData *d) {
        assert(e);
 
        if (!d)
                return;
 
        assert(!d->event_sources);
 
        if (d->fd >= 0) {
                LIST_REMOVE(to_close, e->inode_data_to_close_list, d);
                safe_close(d->fd);
        }
 
        if (d->inotify_data) {
 
                if (d->wd >= 0) {
                        if (d->inotify_data->fd >= 0 && !event_origin_changed(e)) {
                                /* So here's a problem. At the time this runs the watch descriptor might already be
                                 * invalidated, because an IN_IGNORED event might be queued right the moment we enter
                                 * the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very
                                 * likely case to happen. */
 
                                if (inotify_rm_watch(d->inotify_data->fd, d->wd) < 0 && errno != EINVAL)
                                        log_debug_errno(errno, "Failed to remove watch descriptor %i from inotify, ignoring: %m", d->wd);
                        }
 
                        assert_se(hashmap_remove(d->inotify_data->wd, INT_TO_PTR(d->wd)) == d);
                }
 
                assert_se(hashmap_remove(d->inotify_data->inodes, d) == d);
        }
 
        free(d->path);
        free(d);
}
 
static void event_gc_inotify_data(sd_event *e, InotifyData *d) {
        assert(e);
 
        /* Collects the InotifyData object if we don't need it anymore. That's the case if we don't want to
         * watch any inode with it anymore, which in turn happens if no event source of this priority is
         * interested in any inode any longer. That said, we maintain an extra busy counter: if non-zero
         * we'll delay GC (under the expectation that the GC is called again once the counter is
         * decremented). */
 
        if (!d)
                return;
 
        if (!hashmap_isempty(d->inodes))
                return;
 
        if (d->n_busy > 0)
                return;
 
        event_free_inotify_data(e, d);
}
 
static void event_gc_inode_data(sd_event *e, InodeData *d) {
        InotifyData *inotify_data;
 
        assert(e);
 
        if (!d)
                return;
 
        if (d->event_sources)
                return;
 
        inotify_data = d->inotify_data;
        event_free_inode_data(e, d);
 
        event_gc_inotify_data(e, inotify_data);
}
 
static int event_make_inode_data(
                sd_event *e,
                InotifyData *inotify_data,
                dev_t dev,
                ino_t ino,
                InodeData **ret) {
 
        InodeData *d, key;
        int r;
 
        assert(e);
        assert(inotify_data);
 
        key = (InodeData) {
                .ino = ino,
                .dev = dev,
        };
 
        d = hashmap_get(inotify_data->inodes, &key);
        if (d) {
                if (ret)
                        *ret = d;
 
                return 0;
        }
 
        r = hashmap_ensure_allocated(&inotify_data->inodes, &inode_data_hash_ops);
        if (r < 0)
                return r;
 
        d = new(InodeData, 1);
        if (!d)
                return -ENOMEM;
 
        *d = (InodeData) {
                .dev = dev,
                .ino = ino,
                .wd = -1,
                .fd = -EBADF,
                .inotify_data = inotify_data,
        };
 
        r = hashmap_put(inotify_data->inodes, d, d);
        if (r < 0) {
                free(d);
                return r;
        }
 
        if (ret)
                *ret = d;
 
        return 1;
}
 
static uint32_t inode_data_determine_mask(InodeData *d) {
        bool excl_unlink = true;
        uint32_t combined = 0;
 
        assert(d);
 
        /* Combines the watch masks of all event sources watching this inode. We generally just OR them together, but
         * the IN_EXCL_UNLINK flag is ANDed instead.
         *
         * Note that we add all sources to the mask here, regardless whether enabled, disabled or oneshot. That's
         * because we cannot change the mask anymore after the event source was created once, since the kernel has no
         * API for that. Hence we need to subscribe to the maximum mask we ever might be interested in, and suppress
         * events we don't care for client-side. */
 
        LIST_FOREACH(inotify.by_inode_data, s, d->event_sources) {
 
                if ((s->inotify.mask & IN_EXCL_UNLINK) == 0)
                        excl_unlink = false;
 
                combined |= s->inotify.mask;
        }
 
        return (combined & ~(IN_ONESHOT|IN_DONT_FOLLOW|IN_ONLYDIR|IN_EXCL_UNLINK)) | (excl_unlink ? IN_EXCL_UNLINK : 0);
}
 
static int inode_data_realize_watch(sd_event *e, InodeData *d) {
        uint32_t combined_mask;
        int wd, r;
 
        assert(d);
        assert(d->fd >= 0);
 
        combined_mask = inode_data_determine_mask(d);
 
        if (d->wd >= 0 && combined_mask == d->combined_mask)
                return 0;
 
        r = hashmap_ensure_allocated(&d->inotify_data->wd, NULL);
        if (r < 0)
                return r;
 
        wd = inotify_add_watch_fd(d->inotify_data->fd, d->fd, combined_mask);
        if (wd < 0)
                return wd;
 
        if (d->wd < 0) {
                r = hashmap_put(d->inotify_data->wd, INT_TO_PTR(wd), d);
                if (r < 0) {
                        (void) inotify_rm_watch(d->inotify_data->fd, wd);
                        return r;
                }
 
                d->wd = wd;
 
        } else if (d->wd != wd) {
 
                log_debug("Weird, the watch descriptor we already knew for this inode changed?");
                (void) inotify_rm_watch(d->fd, wd);
                return -EINVAL;
        }
 
        d->combined_mask = combined_mask;
        return 1;
}
 
static int inotify_exit_callback(sd_event_source *s, const struct inotify_event *event, void *userdata) {
        assert(s);
 
        return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
}
 
static int event_add_inotify_fd_internal(
                sd_event *e,
                sd_event_source **ret,
                int fd,
                bool donate,
                const char *path,
                uint32_t mask,
                sd_event_inotify_handler_t callback,
                void *userdata) {
 
        _cleanup_close_ int donated_fd = donate ? fd : -EBADF;
        _cleanup_(source_freep) sd_event_source *s = NULL;
        InotifyData *inotify_data = NULL;
        InodeData *inode_data = NULL;
        struct stat st;
        int r;
 
        assert_return(e, -EINVAL);
        assert_return(e = event_resolve(e), -ENOPKG);
        assert_return(fd >= 0, -EBADF);
        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
        assert_return(!event_origin_changed(e), -ECHILD);
 
        if (!callback)
                callback = inotify_exit_callback;
 
        /* Refuse IN_MASK_ADD since we coalesce watches on the same inode, and hence really don't want to merge
         * masks. Or in other words, this whole code exists only to manage IN_MASK_ADD type operations for you, hence
         * the user can't use them for us. */
        if (mask & IN_MASK_ADD)
                return -EINVAL;
 
        if (fstat(fd, &st) < 0)
                return -errno;
 
        s = source_new(e, !ret, SOURCE_INOTIFY);
        if (!s)
                return -ENOMEM;
 
        s->enabled = mask & IN_ONESHOT ? SD_EVENT_ONESHOT : SD_EVENT_ON;
        s->inotify.mask = mask;
        s->inotify.callback = callback;
        s->userdata = userdata;
 
        /* Allocate an inotify object for this priority, and an inode object within it */
        r = event_make_inotify_data(e, SD_EVENT_PRIORITY_NORMAL, &inotify_data);
        if (r < 0)
                return r;
 
        r = event_make_inode_data(e, inotify_data, st.st_dev, st.st_ino, &inode_data);
        if (r < 0) {
                event_gc_inotify_data(e, inotify_data);
                return r;
        }
 
        /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
         * the event source, until then, for which we need the original inode. */
        if (inode_data->fd < 0) {
                if (donated_fd >= 0)
                        inode_data->fd = TAKE_FD(donated_fd);
                else {
                        inode_data->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
                        if (inode_data->fd < 0) {
                                r = -errno;
                                event_gc_inode_data(e, inode_data);
                                return r;
                        }
                }
 
                LIST_PREPEND(to_close, e->inode_data_to_close_list, inode_data);
 
                _cleanup_free_ char *fd_path = NULL;
                r = fd_get_path(inode_data->fd, &fd_path);
                if (r < 0 && r != -ENOSYS) { /* The path is optional, hence ignore -ENOSYS. */
                        event_gc_inode_data(e, inode_data);
                        return r;
                }
 
                free_and_replace(inode_data->path, fd_path);
        }
 
        /* Link our event source to the inode data object */
        LIST_PREPEND(inotify.by_inode_data, inode_data->event_sources, s);
        s->inotify.inode_data = inode_data;
 
        /* Actually realize the watch now */
        r = inode_data_realize_watch(e, inode_data);
        if (r < 0)
                return r;
 
        if (path)
                (void) sd_event_source_set_description(s, path);
 
        if (ret)
                *ret = s;
        TAKE_PTR(s);
 
        return 0;
}
 
_public_ int sd_event_add_inotify_fd(
                sd_event *e,
                sd_event_source **ret,
                int fd,
                uint32_t mask,
                sd_event_inotify_handler_t callback,
                void *userdata) {
 
        return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ false, /* path= */ NULL, mask, callback, userdata);
}
 
_public_ int sd_event_add_inotify(
                sd_event *e,
                sd_event_source **ret,
                const char *path,
                uint32_t mask,
                sd_event_inotify_handler_t callback,
                void *userdata) {
 
        assert_return(path, -EINVAL);
 
        int fd = open(path, O_PATH | O_CLOEXEC |
                            (mask & IN_ONLYDIR ? O_DIRECTORY : 0) |
                            (mask & IN_DONT_FOLLOW ? O_NOFOLLOW : 0));
        if (fd < 0)
                return -errno;
 
        return event_add_inotify_fd_internal(e, ret, fd, /* donate= */ true, path, mask, callback, userdata);
}
 
static sd_event_source* event_source_free(sd_event_source *s) {
        if (!s)
                return NULL;
 
        /* Here's a special hack: when we are called from a
         * dispatch handler we won't free the event source
         * immediately, but we will detach the fd from the
         * epoll. This way it is safe for the caller to unref
         * the event source and immediately close the fd, but
         * we still retain a valid event source object after
         * the callback. */
 
        if (s->dispatching)
                source_disconnect(s);
        else
                source_free(s);
 
        return NULL;
}
 
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source, sd_event_source, event_source_free);
 
_public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
        assert_return(s, -EINVAL);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        return free_and_strdup(&s->description, description);
}
 
_public_ int sd_event_source_get_description(sd_event_source *s, const char **ret) {
        assert_return(s, -EINVAL);
        assert_return(ret, -EINVAL);
 
        if (!s->description)
                return -ENXIO;
 
        *ret = s->description;
        return 0;
}
 
_public_ sd_event* sd_event_source_get_event(sd_event_source *s) {
        assert_return(s, NULL);
        assert_return(!event_origin_changed(s->event), NULL);
 
        return s->event;
}
 
_public_ int sd_event_source_get_pending(sd_event_source *s) {
        assert_return(s, -EINVAL);
        assert_return(s->type != SOURCE_EXIT, -EDOM);
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        return s->pending;
}
 
_public_ int sd_event_source_get_io_fd(sd_event_source *s) {
        assert_return(s, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        return s->io.fd;
}
 
_public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
        int saved_fd, r;
 
        assert_return(s, -EINVAL);
        assert_return(fd >= 0, -EBADF);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        if (s->io.fd == fd)
                return 0;
 
        saved_fd = s->io.fd;
        s->io.fd = fd;
 
        assert(event_source_is_offline(s) == !s->io.registered);
 
        if (s->io.registered) {
                s->io.registered = false;
 
                r = source_io_register(s, s->enabled, s->io.events);
                if (r < 0) {
                        s->io.fd = saved_fd;
                        s->io.registered = true;
                        return r;
                }
 
                (void) epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
        }
 
        if (s->io.owned)
                safe_close(saved_fd);
 
        return 0;
}
 
_public_ int sd_event_source_get_io_fd_own(sd_event_source *s) {
        assert_return(s, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        return s->io.owned;
}
 
_public_ int sd_event_source_set_io_fd_own(sd_event_source *s, int own) {
        assert_return(s, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        s->io.owned = own;
        return 0;
}
 
_public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t *ret) {
        assert_return(s, -EINVAL);
        assert_return(ret, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        *ret = s->io.events;
        return 0;
}
 
_public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
        int r;
 
        assert_return(s, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        /* edge-triggered updates are never skipped, so we can reset edges */
        if (s->io.events == events && !(events & EPOLLET))
                return 0;
 
        r = source_set_pending(s, false);
        if (r < 0)
                return r;
 
        if (event_source_is_online(s)) {
                r = source_io_register(s, s->enabled, events);
                if (r < 0)
                        return r;
        }
 
        s->io.events = events;
 
        return 0;
}
 
_public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t *ret) {
        assert_return(s, -EINVAL);
        assert_return(ret, -EINVAL);
        assert_return(s->type == SOURCE_IO, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        if (!s->pending)
                return -ENODATA;
 
        *ret = s->io.revents;
        return 0;
}
 
_public_ int sd_event_source_get_signal(sd_event_source *s) {
        assert_return(s, -EINVAL);
        assert_return(s->type == SOURCE_SIGNAL, -EDOM);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        return s->signal.sig;
}
 
_public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *ret) {
        assert_return(s, -EINVAL);
        assert_return(ret, -EINVAL);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        *ret = s->priority;
        return 0;
}
 
_public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
        bool rm_inotify = false, rm_inode = false;
        InotifyData *new_inotify_data = NULL;
        InodeData *new_inode_data = NULL;
        int r;
 
        assert_return(s, -EINVAL);
        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        if (s->priority == priority)
                return 0;
 
        if (s->type == SOURCE_INOTIFY) {
                InodeData *old_inode_data;
 
                assert(s->inotify.inode_data);
                old_inode_data = s->inotify.inode_data;
 
                /* We need the original fd to change the priority. If we don't have it we can't change the priority,
                 * anymore. Note that we close any fds when entering the next event loop iteration, i.e. for inotify
                 * events we allow priority changes only until the first following iteration. */
                if (old_inode_data->fd < 0)
                        return -EOPNOTSUPP;
 
                r = event_make_inotify_data(s->event, priority, &new_inotify_data);
                if (r < 0)
                        return r;
                rm_inotify = r > 0;
 
                r = event_make_inode_data(s->event, new_inotify_data, old_inode_data->dev, old_inode_data->ino, &new_inode_data);
                if (r < 0)
                        goto fail;
                rm_inode = r > 0;
 
                if (new_inode_data->fd < 0) {
                        /* Duplicate the fd for the new inode object if we don't have any yet */
                        new_inode_data->fd = fcntl(old_inode_data->fd, F_DUPFD_CLOEXEC, 3);
                        if (new_inode_data->fd < 0) {
                                r = -errno;
                                goto fail;
                        }
 
                        LIST_PREPEND(to_close, s->event->inode_data_to_close_list, new_inode_data);
 
                        _cleanup_free_ char *path = NULL;
                        r = fd_get_path(new_inode_data->fd, &path);
                        if (r < 0 && r != -ENOSYS)
                                goto fail;
 
                        free_and_replace(new_inode_data->path, path);
                }
 
                /* Move the event source to the new inode data structure */
                LIST_REMOVE(inotify.by_inode_data, old_inode_data->event_sources, s);
                LIST_PREPEND(inotify.by_inode_data, new_inode_data->event_sources, s);
                s->inotify.inode_data = new_inode_data;
 
                /* Now create the new watch */
                r = inode_data_realize_watch(s->event, new_inode_data);
                if (r < 0) {
                        /* Move it back */
                        LIST_REMOVE(inotify.by_inode_data, new_inode_data->event_sources, s);
                        LIST_PREPEND(inotify.by_inode_data, old_inode_data->event_sources, s);
                        s->inotify.inode_data = old_inode_data;
                        goto fail;
                }
 
                s->priority = priority;
 
                event_gc_inode_data(s->event, old_inode_data);
 
        } else if (s->type == SOURCE_SIGNAL && event_source_is_online(s)) {
                struct signal_data *old, *d;
 
                /* Move us from the signalfd belonging to the old
                 * priority to the signalfd of the new priority */
 
                assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
 
                s->priority = priority;
 
                r = event_make_signal_data(s->event, s->signal.sig, &d);
                if (r < 0) {
                        s->priority = old->priority;
                        return r;
                }
 
                event_unmask_signal_data(s->event, old, s->signal.sig);
        } else
                s->priority = priority;
 
        event_source_pp_prioq_reshuffle(s);
 
        if (s->type == SOURCE_EXIT)
                prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
 
        return 0;
 
fail:
        if (rm_inode)
                event_free_inode_data(s->event, new_inode_data);
 
        if (rm_inotify)
                event_free_inotify_data(s->event, new_inotify_data);
 
        return r;
}
 
_public_ int sd_event_source_get_enabled(sd_event_source *s, int *ret) {
        /* Quick mode: the event source doesn't exist and we only want to query boolean enablement state. */
        if (!s && !ret)
                return false;
 
        assert_return(s, -EINVAL);
        assert_return(!event_origin_changed(s->event), -ECHILD);
 
        if (ret)
                *ret = s->enabled;
 
        return s->enabled != SD_EVENT_OFF;
}
 
static int event_source_offline(
                sd_event_source *s,
                int enabled,
                bool ratelimited) {
 
        bool was_offline;
        int r;
 
        assert(s);
        assert(enabled == SD_EVENT_OFF || ratelimited);
 
        /* Unset the pending flag when this event source is disabled */
        if (s->enabled != SD_EVENT_OFF &&
            enabled == SD_EVENT_OFF &&
            !IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
                r = source_set_pending(s, false);
                if (r < 0)
                        return r;
        }
 
        was_offline = event_source_is_offline(s);
        s->enabled = enabled;
        s->ratelimited = ratelimited;
 
        switch (s->type) {
 
        case SOURCE_IO:
                source_io_unregister(s);
                break;
 
        case SOURCE_SIGNAL:
                event_gc_signal_data(s->event, &s->priority, s->signal.sig);
                break;
 
        case SOURCE_CHILD:
                if (!was_offline) {
                        assert(s->event->n_online_child_sources > 0);
                        s->event->n_online_child_sources--;
                }
 
                if (EVENT_SOURCE_WATCH_PIDFD(s))
                        source_child_pidfd_unregister(s);
                else
                        event_gc_signal_data(s->event, &s->priority, SIGCHLD);
                break;
 
        case SOURCE_EXIT:
                prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
                break;
 
        case SOURCE_MEMORY_PRESSURE:
        case SOURCE_CPU_PRESSURE:
        case SOURCE_IO_PRESSURE:
                source_pressure_unregister(s);
                break;
 
        case SOURCE_TIME_REALTIME:
        case SOURCE_TIME_BOOTTIME:
        case SOURCE_TIME_MONOTONIC:
        case SOURCE_TIME_REALTIME_ALARM:
        case SOURCE_TIME_BOOTTIME_ALARM:
        case SOURCE_DEFER:
        case SOURCE_POST:
        case SOURCE_INOTIFY:
                break;
 
        default:
                assert_not_reached();
        }
 
        /* Always reshuffle time prioq, as the ratelimited flag may be changed. */
        event_source_time_prioq_reshuffle(s);