1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "path-util.h"
26 #include "string-util.h"
28 #include "udevadm-util.h"
30 static void help(struct udev
*udev
) {
31 printf("%s test-builtin [OPTIONS] COMMAND DEVPATH\n\n"
32 "Test a built-in command.\n\n"
33 " -h --help Print this message\n"
34 " -V --version Print version of the program\n\n"
36 , program_invocation_short_name
);
38 udev_builtin_list(udev
);
41 static int adm_builtin(struct udev
*udev
, int argc
, char *argv
[]) {
42 static const struct option options
[] = {
43 { "version", no_argument
, NULL
, 'V' },
44 { "help", no_argument
, NULL
, 'h' },
49 char filename
[UTIL_PATH_SIZE
];
50 struct udev_device
*dev
= NULL
;
51 enum udev_builtin_cmd cmd
;
52 int rc
= EXIT_SUCCESS
, c
;
54 while ((c
= getopt_long(argc
, argv
, "Vh", options
, NULL
)) >= 0)
64 command
= argv
[optind
++];
65 if (command
== NULL
) {
66 fprintf(stderr
, "command missing\n");
72 syspath
= argv
[optind
++];
73 if (syspath
== NULL
) {
74 fprintf(stderr
, "syspath missing\n");
79 udev_builtin_init(udev
);
81 cmd
= udev_builtin_lookup(command
);
82 if (cmd
>= UDEV_BUILTIN_MAX
) {
83 fprintf(stderr
, "unknown command '%s'\n", command
);
89 /* add /sys if needed */
90 if (!path_startswith(syspath
, "/sys"))
91 strscpyl(filename
, sizeof(filename
), "/sys", syspath
, NULL
);
93 strscpy(filename
, sizeof(filename
), syspath
);
94 delete_trailing_chars(filename
, "/");
96 dev
= udev_device_new_from_syspath(udev
, filename
);
98 fprintf(stderr
, "unable to open device '%s'\n\n", filename
);
103 rc
= udev_builtin_run(dev
, cmd
, command
, true);
105 fprintf(stderr
, "error executing '%s', exit code %i\n\n", command
, rc
);
109 udev_device_unref(dev
);
110 udev_builtin_exit(udev
);
114 const struct udevadm_cmd udevadm_test_builtin
= {
115 .name
= "test-builtin",
117 .help
= "Test a built-in command",