chiark / gitweb /
release 180
[elogind.git] / src / udevadm-test-builtin.c
1 /*
2  * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
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
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <syslog.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <sys/inotify.h>
31 #include <sys/poll.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include "udev.h"
36
37 static void help(struct udev *udev)
38 {
39         fprintf(stderr, "\n");
40         fprintf(stderr, "Usage: udevadm builtin [--help] <command> <syspath>\n");
41         udev_builtin_list(udev);
42         fprintf(stderr, "\n");
43 }
44
45 static int adm_builtin(struct udev *udev, int argc, char *argv[])
46 {
47         static const struct option options[] = {
48                 { "help", no_argument, NULL, 'h' },
49                 {}
50         };
51         char *command = NULL;
52         char *syspath = NULL;
53         char filename[UTIL_PATH_SIZE];
54         struct udev_device *dev = NULL;
55         enum udev_builtin_cmd cmd;
56         int rc = EXIT_SUCCESS;
57
58         dbg(udev, "version %s\n", VERSION);
59
60         for (;;) {
61                 int option;
62
63                 option = getopt_long(argc, argv, "h", options, NULL);
64                 if (option == -1)
65                         break;
66
67                 switch (option) {
68                 case 'h':
69                         help(udev);
70                         goto out;
71                 }
72         }
73
74         command = argv[optind++];
75         if (command == NULL) {
76                 fprintf(stderr, "command missing\n");
77                 help(udev);
78                 rc = 2;
79                 goto out;
80         }
81
82         syspath = argv[optind++];
83         if (syspath == NULL) {
84                 fprintf(stderr, "syspath missing\n\n");
85                 rc = 3;
86                 goto out;
87         }
88
89         udev_builtin_init(udev);
90
91         cmd = udev_builtin_lookup(command);
92         if (cmd >= UDEV_BUILTIN_MAX) {
93                 fprintf(stderr, "unknown command '%s'\n", command);
94                 help(udev);
95                 rc = 5;
96                 goto out;
97         }
98
99         /* add /sys if needed */
100         if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
101                 util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), syspath, NULL);
102         else
103                 util_strscpy(filename, sizeof(filename), syspath);
104         util_remove_trailing_chars(filename, '/');
105
106         dev = udev_device_new_from_syspath(udev, filename);
107         if (dev == NULL) {
108                 fprintf(stderr, "unable to open device '%s'\n\n", filename);
109                 rc = 4;
110                 goto out;
111         }
112
113         if (udev_builtin_run(dev, cmd, command, true) < 0) {
114                 fprintf(stderr, "error executing '%s'\n\n", command);
115                 rc = 6;
116         }
117 out:
118         udev_device_unref(dev);
119         udev_builtin_exit(udev);
120         return rc;
121 }
122
123 const struct udevadm_cmd udevadm_test_builtin = {
124         .name = "test-builtin",
125         .cmd = adm_builtin,
126         .help = "test a built-in command",
127         .debug = true,
128 };