]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-test-builtin.c
string-util: add delete_trailing_chars() and skip_leading_chars() helpers
[thirdparty/systemd.git] / src / udev / udevadm-test-builtin.c
CommitLineData
d7867b31 1/*
1298001e 2 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
d7867b31
KS
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
d7867b31 18#include <errno.h>
d7867b31 19#include <getopt.h>
07630cea
LP
20#include <stddef.h>
21#include <stdio.h>
22#include <stdlib.h>
d7867b31 23
27458ed6 24#include "path-util.h"
07630cea 25#include "string-util.h"
d7867b31
KS
26#include "udev.h"
27
9ec6e95b 28static void help(struct udev *udev) {
5ac0162c
LP
29 printf("%s builtin [--help] COMMAND SYSPATH\n\n"
30 "Test a built-in command.\n\n"
31 " -h --help Print this message\n"
32 " --version Print version of the program\n\n"
33 "Commands:\n"
34 , program_invocation_short_name);
35
912541b0 36 udev_builtin_list(udev);
d7867b31
KS
37}
38
9ec6e95b 39static int adm_builtin(struct udev *udev, int argc, char *argv[]) {
912541b0
KS
40 static const struct option options[] = {
41 { "help", no_argument, NULL, 'h' },
42 {}
43 };
44 char *command = NULL;
45 char *syspath = NULL;
46 char filename[UTIL_PATH_SIZE];
47 struct udev_device *dev = NULL;
48 enum udev_builtin_cmd cmd;
7643ac9a 49 int rc = EXIT_SUCCESS, c;
912541b0 50
7643ac9a
ZJS
51 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
52 switch (c) {
912541b0
KS
53 case 'h':
54 help(udev);
55 goto out;
56 }
912541b0
KS
57
58 command = argv[optind++];
59 if (command == NULL) {
60 fprintf(stderr, "command missing\n");
61 help(udev);
62 rc = 2;
63 goto out;
64 }
65
66 syspath = argv[optind++];
67 if (syspath == NULL) {
7643ac9a 68 fprintf(stderr, "syspath missing\n");
912541b0
KS
69 rc = 3;
70 goto out;
71 }
72
73 udev_builtin_init(udev);
74
75 cmd = udev_builtin_lookup(command);
76 if (cmd >= UDEV_BUILTIN_MAX) {
77 fprintf(stderr, "unknown command '%s'\n", command);
78 help(udev);
79 rc = 5;
80 goto out;
81 }
82
83 /* add /sys if needed */
27458ed6 84 if (!path_startswith(syspath, "/sys"))
d5a89d7d 85 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
912541b0 86 else
d5a89d7d 87 strscpy(filename, sizeof(filename), syspath);
7546145e 88 delete_trailing_chars(filename, "/");
912541b0
KS
89
90 dev = udev_device_new_from_syspath(udev, filename);
91 if (dev == NULL) {
92 fprintf(stderr, "unable to open device '%s'\n\n", filename);
93 rc = 4;
94 goto out;
95 }
96
0bb91b50
KS
97 rc = udev_builtin_run(dev, cmd, command, true);
98 if (rc < 0) {
99 fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
912541b0
KS
100 rc = 6;
101 }
d7867b31 102out:
912541b0
KS
103 udev_device_unref(dev);
104 udev_builtin_exit(udev);
105 return rc;
d7867b31
KS
106}
107
108const struct udevadm_cmd udevadm_test_builtin = {
912541b0
KS
109 .name = "test-builtin",
110 .cmd = adm_builtin,
5ac0162c 111 .help = "Test a built-in command",
912541b0 112 .debug = true,
d7867b31 113};