chiark / gitweb /
udev: place opening { at the same line as the function declaration
[elogind.git] / src / udev / udevadm-test-builtin.c
1 /*
2  * Copyright (C) 2011 Kay Sievers <kay@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         fprintf(stderr, "\n");
39         fprintf(stderr, "Usage: udevadm builtin [--help] COMMAND SYSPATH\n");
40         udev_builtin_list(udev);
41         fprintf(stderr, "\n");
42 }
43
44 static int adm_builtin(struct udev *udev, int argc, char *argv[]) {
45         static const struct option options[] = {
46                 { "help", no_argument, NULL, 'h' },
47                 {}
48         };
49         char *command = NULL;
50         char *syspath = NULL;
51         char filename[UTIL_PATH_SIZE];
52         struct udev_device *dev = NULL;
53         enum udev_builtin_cmd cmd;
54         int rc = EXIT_SUCCESS, c;
55
56         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
57                 switch (c) {
58                 case 'h':
59                         help(udev);
60                         goto out;
61                 }
62
63         command = argv[optind++];
64         if (command == NULL) {
65                 fprintf(stderr, "command missing\n");
66                 help(udev);
67                 rc = 2;
68                 goto out;
69         }
70
71         syspath = argv[optind++];
72         if (syspath == NULL) {
73                 fprintf(stderr, "syspath missing\n");
74                 rc = 3;
75                 goto out;
76         }
77
78         udev_builtin_init(udev);
79
80         cmd = udev_builtin_lookup(command);
81         if (cmd >= UDEV_BUILTIN_MAX) {
82                 fprintf(stderr, "unknown command '%s'\n", command);
83                 help(udev);
84                 rc = 5;
85                 goto out;
86         }
87
88         /* add /sys if needed */
89         if (!startswith(syspath, "/sys"))
90                 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
91         else
92                 strscpy(filename, sizeof(filename), syspath);
93         util_remove_trailing_chars(filename, '/');
94
95         dev = udev_device_new_from_syspath(udev, filename);
96         if (dev == NULL) {
97                 fprintf(stderr, "unable to open device '%s'\n\n", filename);
98                 rc = 4;
99                 goto out;
100         }
101
102         rc = udev_builtin_run(dev, cmd, command, true);
103         if (rc < 0) {
104                 fprintf(stderr, "error executing '%s', exit code %i\n\n", command, rc);
105                 rc = 6;
106         }
107 out:
108         udev_device_unref(dev);
109         udev_builtin_exit(udev);
110         return rc;
111 }
112
113 const struct udevadm_cmd udevadm_test_builtin = {
114         .name = "test-builtin",
115         .cmd = adm_builtin,
116         .help = "test a built-in command",
117         .debug = true,
118 };