From 8e7d1b7bb59aef943ab8d73de08a6882032526a3 Mon Sep 17 00:00:00 2001 From: Aaron Andersen Date: Fri, 2 Jan 2026 18:08:42 -0500 Subject: [PATCH] Refactor: drop do_ prefix from iterate_proc() and switch_root() The do_ prefix is conventionally reserved for local helper functions. Move switch_root() declaration to private.h alongside iterate_proc() and remove the now-empty initramfs.h header. --- src/Makefile.am | 2 +- src/api.c | 7 +++---- src/initramfs.c | 7 +++---- src/initramfs.h | 36 ------------------------------------ src/private.h | 3 ++- src/sig.c | 8 ++++---- 6 files changed, 13 insertions(+), 50 deletions(-) delete mode 100644 src/initramfs.h diff --git a/src/Makefile.am b/src/Makefile.am index 835bc075..bd126600 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,7 +63,7 @@ finit_SOURCES = api.c cgroup.c cgroup.h \ exec.c finit.c finit.h \ stty.c \ helpers.c helpers.h \ - initramfs.c initramfs.h \ + initramfs.c \ iwatch.c iwatch.h \ log.c log.h \ mdadm.c mount.c \ diff --git a/src/api.c b/src/api.c index cca415ea..381c2197 100644 --- a/src/api.c +++ b/src/api.c @@ -42,7 +42,6 @@ #include "cond.h" #include "conf.h" #include "helpers.h" -#include "initramfs.h" #include "log.h" #include "plugin.h" #include "private.h" @@ -250,7 +249,7 @@ static void bypass_shutdown(void *unused) * Handle switch_root API command. * Parses data: "newroot\0newinit\0" * Sends ACK before attempting switch_root since it doesn't return on success. - * Returns: result from do_switch_root() on failure, doesn't return on success. + * Returns: result from switch_root() on failure, doesn't return on success. */ static int do_switch_root_api(int sd, struct init_request *rq) { @@ -271,7 +270,7 @@ static int do_switch_root_api(int sd, struct init_request *rq) /* * Send ACK first, since we won't return from - * do_switch_root() on success. + * switch_root() on success. */ rq->cmd = INIT_CMD_ACK; if (write(sd, rq, sizeof(*rq)) != sizeof(*rq)) @@ -279,7 +278,7 @@ static int do_switch_root_api(int sd, struct init_request *rq) close(sd); /* This does not return on success */ - result = do_switch_root(newroot, newinit); + result = switch_root(newroot, newinit); if (result) logit(LOG_ERR, "switch_root failed: %s", strerror(errno)); diff --git a/src/initramfs.c b/src/initramfs.c index 0258bc4d..59fff4ac 100644 --- a/src/initramfs.c +++ b/src/initramfs.c @@ -43,7 +43,6 @@ #include "finit.h" #include "cond.h" #include "helpers.h" -#include "initramfs.h" #include "log.h" #include "plugin.h" #include "private.h" @@ -142,7 +141,7 @@ static int kill_cb(int pid, void *data) * This function does not return on success - it exec's the new init. * On failure, it returns -1 and sets errno. */ -int do_switch_root(const char *newroot, const char *newinit) +int switch_root(const char *newroot, const char *newinit) { struct stat newroot_st, oldroot_st; char init_path[PATH_MAX]; @@ -209,11 +208,11 @@ int do_switch_root(const char *newroot, const char *newinit) /* Kill remaining processes (except kernel threads and ourselves) */ signo = SIGTERM; - do_iterate_proc(kill_cb, &signo); + iterate_proc(kill_cb, &signo); do_usleep(500000); /* Give them 500ms */ signo = SIGKILL; - do_iterate_proc(kill_cb, &signo); + iterate_proc(kill_cb, &signo); /* Reap zombies */ while (waitpid(-1, NULL, WNOHANG) > 0) diff --git a/src/initramfs.h b/src/initramfs.h deleted file mode 100644 index 33135e49..00000000 --- a/src/initramfs.h +++ /dev/null @@ -1,36 +0,0 @@ -/* Initramfs switch_root support - * - * Copyright (c) 2008-2025 Joachim Wiberg - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef FINIT_INITRAMFS_H_ -#define FINIT_INITRAMFS_H_ - -int do_switch_root(const char *newroot, const char *newinit); - -#endif /* FINIT_INITRAMFS_H_ */ - -/** - * Local Variables: - * indent-tabs-mode: t - * c-file-style: "linux" - * End: - */ diff --git a/src/private.h b/src/private.h index 15fbda57..1612803b 100644 --- a/src/private.h +++ b/src/private.h @@ -59,7 +59,8 @@ void plugin_script_run(hook_point_t no); int plugin_init (uev_ctx_t *ctx); void plugin_exit (void); -void do_iterate_proc (int (*cb)(int, void *), void *data); +void iterate_proc (int (*cb)(int, void *), void *data); +int switch_root (const char *newroot, const char *newinit); #endif /* FINIT_PRIVATE_H_ */ diff --git a/src/sig.c b/src/sig.c index a8421fd1..e3e25b52 100644 --- a/src/sig.c +++ b/src/sig.c @@ -208,7 +208,7 @@ static void fs_swapoff(void) * * https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/ */ -void do_iterate_proc(int (*cb)(int, void *), void *data) +void iterate_proc(int (*cb)(int, void *), void *data) { DIR *dirp; @@ -290,7 +290,7 @@ static int do_wait(int secs) ; has_proc = 0; iterations--; - do_iterate_proc(status_cb, &has_proc); + iterate_proc(status_cb, &has_proc); } while (has_proc && iterations > 0); @@ -322,10 +322,10 @@ void do_shutdown(shutop_t op) * Tell remaining non-monitored processes to exit, give them * time to exit gracefully, 2 sec was customary, we go for 1. */ - do_iterate_proc(kill_cb, &signo); + iterate_proc(kill_cb, &signo); if (do_wait(1)) { signo = SIGKILL; - do_iterate_proc(kill_cb, &signo); + iterate_proc(kill_cb, &signo); } /* Exit plugins gracefully */