-/*
- * Copyright (C) 2011 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/>.
- */
+/* SPDX-License-Identifier: GPL-2.0+ */
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
-#include "path-util.h"
-#include "string-util.h"
-#include "udev.h"
+#include "log.h"
+#include "udev-builtin.h"
+#include "udevadm.h"
+#include "udevadm-util.h"
-static void help(struct udev *udev) {
- printf("%s builtin [--help] COMMAND SYSPATH\n\n"
+static const char *arg_command = NULL;
+static const char *arg_syspath = NULL;
+
+static int help(void) {
+ printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
"Test a built-in command.\n\n"
" -h --help Print this message\n"
- " --version Print version of the program\n\n"
+ " -V --version Print version of the program\n\n"
"Commands:\n"
, program_invocation_short_name);
- udev_builtin_list(udev);
+ udev_builtin_list();
+
+ return 0;
}
-static int adm_builtin(struct udev *udev, int argc, char *argv[]) {
+static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, 'V' },
+ { "help", no_argument, NULL, 'h' },
{}
};
- char *command = NULL;
- char *syspath = NULL;
- char filename[UTIL_PATH_SIZE];
- struct udev_device *dev = NULL;
- enum udev_builtin_cmd cmd;
- int rc = EXIT_SUCCESS, c;
-
- while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+
+ int c;
+
+ while ((c = getopt_long(argc, argv, "Vh", options, NULL)) >= 0)
switch (c) {
+ case 'V':
+ return print_version();
case 'h':
- help(udev);
- goto out;
+ return help();
+ case '?':
+ return -EINVAL;
+ default:
+ assert_not_reached("Unknown option");
}
- command = argv[optind++];
- if (command == NULL) {
- fprintf(stderr, "command missing\n");
- help(udev);
- rc = 2;
- goto out;
- }
+ arg_command = argv[optind++];
+ if (!arg_command)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Command missing.");
- syspath = argv[optind++];
- if (syspath == NULL) {
- fprintf(stderr, "syspath missing\n");
- rc = 3;
- goto out;
- }
+ arg_syspath = argv[optind++];
+ if (!arg_syspath)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "syspath missing.");
+
+ return 1;
+}
- udev_builtin_init(udev);
+int builtin_main(int argc, char *argv[], void *userdata) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ UdevBuiltinCommand cmd;
+ int r;
- cmd = udev_builtin_lookup(command);
- if (cmd >= UDEV_BUILTIN_MAX) {
- fprintf(stderr, "unknown command '%s'\n", command);
- help(udev);
- rc = 5;
- goto out;
- }
+ log_set_max_level(LOG_DEBUG);
+
+ r = parse_argv(argc, argv);
+ if (r <= 0)
+ return r;
+
+ udev_builtin_init();
- /* add /sys if needed */
- if (!path_startswith(syspath, "/sys"))
- strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
- else
- strscpy(filename, sizeof(filename), syspath);
- util_remove_trailing_chars(filename, '/');
-
- dev = udev_device_new_from_syspath(udev, filename);
- if (dev == NULL) {
- fprintf(stderr, "unable to open device '%s'\n\n", filename);
- rc = 4;
- goto out;
+ cmd = udev_builtin_lookup(arg_command);
+ if (cmd < 0) {
+ log_error("Unknown command '%s'", arg_command);
+ r = -EINVAL;
+ goto finish;
}
- rc = udev_builtin_run(dev, cmd, command, true);
- if (rc < 0) {
- fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
- rc = 6;
+ r = find_device(arg_syspath, "/sys", &dev);
+ if (r < 0) {
+ log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
+ goto finish;
}
-out:
- udev_device_unref(dev);
- udev_builtin_exit(udev);
- return rc;
-}
-const struct udevadm_cmd udevadm_test_builtin = {
- .name = "test-builtin",
- .cmd = adm_builtin,
- .help = "Test a built-in command",
- .debug = true,
-};
+ r = udev_builtin_run(dev, cmd, arg_command, true);
+ if (r < 0)
+ log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
+
+finish:
+ udev_builtin_exit();
+ return r;
+}