[klibc] [rfc] standalone kinit/resume

maximilian attems maks at sternwelten.at
Mon Jul 10 17:39:36 PDT 2006


git mv resume.c resumelib.c and the addition of a minimal resume.c,
which calls resume() from resumelib produces a small standalone resume.
happy to hear feedback on the needed kinit/Kbuild changes.

resulting resume bin is small:
ls -l /usr/lib/klibc/bin/resume
-rwxr-xr-x 1 root root 2904 2006-07-11 02:11 /usr/lib/klibc/bin/resume

it reduces busybox dependency of Debian initramfs-tools:
if [ -e /sys/power/resume ]; then
        major_minor=$(ls -l ${resume} | awk '{printf "%d:%d", $5, $6}')
	echo $major_minor >/sys/power/resume
fi
as one could simply call
resume ${resume}

---

 Kbuild      |   12 +++++---
 resume.c    |   75 ++++++-----------------------------------------------
 resumelib.c |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 71 deletions(-)

diff --git a/usr/kinit/Kbuild b/usr/kinit/Kbuild
index 0000e89..7669ecb 100644
--- a/usr/kinit/Kbuild
+++ b/usr/kinit/Kbuild
@@ -2,19 +2,21 @@ #
 # Kbuild file for kinit
 #
 
-static-y := kinit
+static-y := kinit static/resume
 kinit-y  := kinit.o do_mounts.o ramdisk_load.o initrd.o
 kinit-y	 += name_to_dev.o devname.o
 kinit-y  += getarg.o getintfile.o open.o readfile.o xpio.o
-kinit-y  += do_mounts_md.o do_mounts_mtd.o nfsroot.o resume.o
+kinit-y  += do_mounts_md.o do_mounts_mtd.o nfsroot.o resumelib.o
 
 kinit-y  += ipconfig/
 kinit-y  += nfsmount/
 kinit-y  += run-init/
 kinit-y  += fstype/
 
-shared-y := kinit.shared
+shared-y := kinit.shared shared/resume
 kinit.shared-y := $(kinit-y)
+static/resume-y := resume.o resumelib.o getarg.o name_to_dev.o devname.o
+shared/resume-y := resume.o resumelib.o getarg.o name_to_dev.o devname.o
 
 # Additional include paths files
 KLIBCCFLAGS += -I$(srctree)/$(src)/ipconfig \
@@ -25,7 +27,7 @@ KLIBCCFLAGS += -I$(srctree)/$(src)/ipcon
 # Cleaning
 targets := kinit kinit.g kinit.shared kinit.shared.g
 subdir- := ipconfig nfsmount run-init fstype
-
+clean-dirs := static shared
 
 # install binary
-install-y := kinit kinit.shared
+install-y := kinit kinit.shared $(static-y)
diff --git a/usr/kinit/resume.c b/usr/kinit/resume.c
index 952af96..00e5405 100644
--- a/usr/kinit/resume.c
+++ b/usr/kinit/resume.c
@@ -4,81 +4,24 @@
  * Handle resume from suspend-to-disk
  */
 
-#include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include <sys/stat.h>
-#include <sys/sysmacros.h>
-#include <linux/config.h>	/* For CONFIG_PM_STD_PARTITION */
 
-#include "kinit.h"
-#include "do_mounts.h"
 #include "resume.h"
 
-#ifndef CONFIG_PM_STD_PARTITION
-# define CONFIG_PM_STD_PARTITION ""
-#endif
+char *progname;
 
-int do_resume(int argc, char *argv[])
+static __noreturn usage(void)
 {
-	const char *resume_file = CONFIG_PM_STD_PARTITION;
-	const char *resume_arg;
-
-	resume_arg = get_arg(argc, argv, "resume=");
-	resume_file = resume_arg ? resume_arg : resume_file;
-	/* No resume device specified */
-	if (!resume_file[0])
-		return 0;
-
-	/* Fix: we either should consider reverting the device back to
-	   ordinary swap, or (better) put that code into swapon */
-	/* Noresume requested */
-	if (get_flag(argc, argv, "noresume"))
-		return 0;
-	return	resume(resume_file);
+	fprintf(stderr, "Usage: %s /dev/<resumedevice>\n", progname);
+	exit(1);
 }
 
-int resume(const char *resume_file)
+int main(int argc, char *argv[], char *envp[])
 {
-	char device_string[64];
-	int powerfd = -1;
-	dev_t resume_device;
-	int len;
-
-	resume_device = name_to_dev_t(resume_file);
-
-	if (major(resume_device) == 0) {
-		fprintf(stderr, "Invalid resume device: %s\n", resume_file);
-		goto failure;
-	}
-
-	if ((powerfd = open("/sys/power/resume", O_WRONLY)) < 0)
-		goto fail_r;
-
-	len = snprintf(device_string, sizeof device_string, "%u:%u",
-		       major(resume_device), minor(resume_device));
-
-	/* This should never happen */
-	if (len >= sizeof device_string)
-		goto fail_r;
-
-	DEBUG(("kinit: trying to resume from %s\n", resume_file));
-
-	if (write(powerfd, device_string, len) != len)
-		goto fail_r;
-
-	/* Okay, what are we still doing alive... */
-failure:
-	if (powerfd >= 0)
-		close(powerfd);
-	fprintf(stderr, "Resume failed, doing normal boot...\n");
-	return -1;
+	progname = argv[0];
+	if (argc != 2)
+		usage();
 
-fail_r:
-	fprintf(stderr, "Cannot write /sys/power/resume "
-			"(no software suspend kernel support?)\n");
-	goto failure;
+	return resume(argv[1]);
 }
diff --git a/usr/kinit/resumelib.c b/usr/kinit/resumelib.c
new file mode 100644
index 0000000..2683a5d
--- /dev/null
+++ b/usr/kinit/resumelib.c
@@ -0,0 +1,84 @@
+/*
+ * resumelib.c
+ *
+ * Handle resume from suspend-to-disk
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <linux/config.h>	/* For CONFIG_PM_STD_PARTITION */
+
+#include "kinit.h"
+#include "do_mounts.h"
+#include "resume.h"
+
+#ifndef CONFIG_PM_STD_PARTITION
+# define CONFIG_PM_STD_PARTITION ""
+#endif
+
+int do_resume(int argc, char *argv[])
+{
+	const char *resume_file = CONFIG_PM_STD_PARTITION;
+	const char *resume_arg;
+
+	resume_arg = get_arg(argc, argv, "resume=");
+	resume_file = resume_arg ? resume_arg : resume_file;
+	/* No resume device specified */
+	if (!resume_file[0])
+		return 0;
+
+	/* Fix: we either should consider reverting the device back to
+	   ordinary swap, or (better) put that code into swapon */
+	/* Noresume requested */
+	if (get_flag(argc, argv, "noresume"))
+		return 0;
+	return	resume(resume_file);
+}
+
+int resume(const char *resume_file)
+{
+	char device_string[64];
+	int powerfd = -1;
+	dev_t resume_device;
+	int len;
+
+	resume_device = name_to_dev_t(resume_file);
+
+	if (major(resume_device) == 0) {
+		fprintf(stderr, "Invalid resume device: %s\n", resume_file);
+		goto failure;
+	}
+
+	if ((powerfd = open("/sys/power/resume", O_WRONLY)) < 0)
+		goto fail_r;
+
+	len = snprintf(device_string, sizeof device_string, "%u:%u",
+		       major(resume_device), minor(resume_device));
+
+	/* This should never happen */
+	if (len >= sizeof device_string)
+		goto fail_r;
+
+	DEBUG(("kinit: trying to resume from %s\n", resume_file));
+
+	if (write(powerfd, device_string, len) != len)
+		goto fail_r;
+
+	/* Okay, what are we still doing alive... */
+failure:
+	if (powerfd >= 0)
+		close(powerfd);
+	fprintf(stderr, "Resume failed, doing normal boot...\n");
+	return -1;
+
+fail_r:
+	fprintf(stderr, "Cannot write /sys/power/resume "
+			"(no software suspend kernel support?)\n");
+	goto failure;
+}

-- 
maks



More information about the klibc mailing list