eudev-project / eudev

Repository for eudev development
GNU General Public License v2.0
527 stars 144 forks source link

provide a multicall-binary (Feature request) #163

Closed nolange closed 1 year ago

nolange commented 5 years ago

Hello,

I am currently trying to build a small initramfs with busybox + eudev, binaries are built static. udevd and udevadm contain mosty the same code, so building a single executable nearly halves the required space.

I added a patch, which is good enough for me for building this binary and should outline the idea, but I dont know autotools good enough to know how to add this as an option (or whether you would accept this feature at all).

diff -burN eudev.org/src/shared/macro.h eudev/src/shared/macro.h
--- eudev.org/src/shared/macro.h    2016-11-17 22:14:19.000000000 +0100
+++ eudev/src/shared/macro.h    2018-12-06 15:28:01.025854182 +0100
@@ -37,6 +37,8 @@
 #define _public_ __attribute__ ((visibility("default")))
 #define _alignas_(x) __attribute__((aligned(__alignof(x))))
 #define _cleanup_(x) __attribute__((cleanup(x)))
+#define _weak_alias(name, aliasname) \
+  extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));

 /* Temporarily disable some warnings */
 #define DISABLE_WARNING_DECLARATION_AFTER_STATEMENT                     \
diff -burN eudev.org/src/udev/eudevmc.c eudev/src/udev/eudevmc.c
--- eudev.org/src/udev/eudevmc.c    1970-01-01 01:00:00.000000000 +0100
+++ eudev/src/udev/eudevmc.c    2018-12-06 15:21:25.569977693 +0100
@@ -0,0 +1,55 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+/*
+ * Copyright (C) 2007-2012 Kay Sievers <kay@vrfy.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#include <errno.h>
+
+int udevd_main(int argc, char *argv[]);
+int udevadm_main(int argc, char *argv[]);
+
+int main(int argc, char *argv[]) {
+        typedef int (*mainfct_t)(int argc, char *argv[]);
+        static const struct eudev_applets
+        {
+            const char *name;
+            mainfct_t mainfunction;
+        } applets[] = {
+                { "udevd", &udevd_main },
+                { "udevadm", &udevadm_main },
+        };
+
+        const char *toolname = program_invocation_short_name;
+        int index = sizeof(applets) / sizeof(applets[0]) - 1;
+
+        for ( ; index >= 0; --index)
+        {
+            if(strcmp(applets[index].name, toolname) == 0)
+                break;
+        }
+
+        if (index < 0)
+        {
+            fprintf(stderr, "%s: unsupported applet '%s'\n", "eudevmc", toolname);
+            return 2;
+
+        }
+
+        return applets[index].mainfunction(argc, argv);
+}
diff -burN eudev.org/src/udev/Makefile.am eudev/src/udev/Makefile.am
--- eudev.org/src/udev/Makefile.am  2017-01-05 22:04:16.000000000 +0100
+++ eudev/src/udev/Makefile.am  2018-12-06 14:40:03.998474923 +0100
@@ -17,7 +17,7 @@
    udevadm

 sbin_PROGRAMS = \
-   udevd
+   udevd eudevmc

 udevd_SOURCES = \
    udevd.c
@@ -42,6 +42,24 @@
 udevadm_LDADD = \
    libudev-core.la

+eudevmc_SOURCES = \
+    eudevmc.c \
+   udevd.c \
+   udevadm.c \
+   udevadm-info.c \
+   udevadm-control.c \
+   udevadm-monitor.c \
+   udevadm-hwdb.c \
+   udevadm-settle.c \
+   udevadm-trigger.c \
+   udevadm-test.c \
+   udevadm-test-builtin.c \
+   udevadm-util.c \
+   udevadm-util.h
+
+eudevmc_LDADD = \
+   libudev-core.la
+
 noinst_LTLIBRARIES = \
         libudev-core.la

diff -burN eudev.org/src/udev/udevadm.c eudev/src/udev/udevadm.c
--- eudev.org/src/udev/udevadm.c    2016-11-17 22:14:19.000000000 +0100
+++ eudev/src/udev/udevadm.c    2018-12-06 15:27:31.817566655 +0100
@@ -77,7 +77,7 @@
         return cmd->cmd(udev, argc, argv);
 }

-int main(int argc, char *argv[]) {
+int udevadm_main(int argc, char *argv[]) {
         struct udev *udev;
         static const struct option options[] = {
                 { "debug", no_argument, NULL, 'd' },
@@ -136,3 +136,5 @@
         log_close();
         return rc;
 }
+
+_weak_alias(udevadm_main, main)
diff -burN eudev.org/src/udev/udevd.c eudev/src/udev/udevd.c
--- eudev.org/src/udev/udevd.c  2017-10-15 21:04:38.000000000 +0200
+++ eudev/src/udev/udevd.c  2018-12-06 15:27:28.541534413 +0100
@@ -1121,7 +1121,7 @@
         return 1;
 }

-int main(int argc, char *argv[]) {
+int udevd_main(int argc, char *argv[]) {
         struct udev *udev;
         sigset_t mask;
         FILE *f;
@@ -1567,3 +1567,5 @@
         log_close();
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
+
+_weak_alias(udevd_main, main)
blueness commented 5 years ago

I can't include this in eudev itself since it must conform to the standards set by systemd. We could create a contrib directory with extras.

nolange commented 5 years ago

I am not sure how this would look like. Would you accept just the changes with the renamed main functions (udevd.c, udevadm.c, macro.h)?

blueness commented 5 years ago

i'm sorry, no i can't. i have to keep the separation clean.

CameronNemo commented 5 years ago

@nolange check out busybox applets: uevent & mdev

example configuration, you may only need a portion to get what you want done https://gitlab.com/cameronnemo/upstart-jobs/tree/master/dev/nldev/mdev

nolange commented 5 years ago

@nolange check out busybox applets: uevent & mdev

I found out about uevent after deciding to use eudev, the goal posts roughly are to support existing kernel cmdlines use Predictable Network Interface Names, be consistent with whatever init system + hotplug manager is running in rootfs (sysv / systemd and mdev / udev). And be modular by using a kernel-builtin initramfs while allowing hooks to be added by external initramfs.

I had problems with mdev, supposedly would take me longer to fix up stuff with mdev.

example configuration, you may only need a portion to get what you want done https://gitlab.com/cameronnemo/upstart-jobs/tree/master/dev/nldev/mdev

Thanks, gonna have a look.

mjeveritt commented 4 years ago

See also https://github.com/systemd/systemd/issues/14200

blueness commented 4 years ago

Looks like this is needed. I'll have a look over the weekend.

bbonev commented 1 year ago

@nolange Lots of time passed, hope you are well and still around.

Are you interested in converting this to a PR? I will help with autotools and other obscure stuff

nolange commented 1 year ago

@nolange Lots of time passed, hope you are well and still around.

Still in this plane of existence, but I considered this closed a long time ago.

Not using eudev for almost aslong, so i am sorry but I wont spend time on this.

bbonev commented 1 year ago

Thanks for the feedback!