chiark / gitweb /
update source file headers
[elogind.git] / udevtest.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <signal.h>
28 #include <syslog.h>
29
30 #include "udev.h"
31 #include "udev_rules.h"
32
33
34 #ifdef USE_LOG
35 void log_message (int priority, const char *format, ...)
36 {
37         va_list args;
38
39         if (priority > udev_log_priority)
40                 return;
41
42         va_start(args, format);
43         vprintf(format, args);
44         va_end(args);
45         if (format[strlen(format)-1] != '\n')
46                 printf("\n");
47 }
48 #endif
49
50 int main(int argc, char *argv[], char *envp[])
51 {
52         struct udev_rules rules = {};
53         char *devpath = NULL;
54         struct udevice *udev;
55         struct sysfs_device *dev;
56         int i;
57         int retval;
58         int rc = 0;
59
60         info("version %s", UDEV_VERSION);
61         udev_config_init();
62         if (udev_log_priority < LOG_INFO)
63                 udev_log_priority = LOG_INFO;
64
65         for (i = 1 ; i < argc; i++) {
66                 char *arg = argv[i];
67
68                 if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
69                         printf("Usage: udevtest [--help] <devpath>\n");
70                         goto exit;
71                 } else
72                         devpath = arg;
73         }
74
75         if (devpath == NULL) {
76                 fprintf(stderr, "devpath parameter missing\n");
77                 rc = 1;
78                 goto exit;
79         }
80
81         sysfs_init();
82         udev_rules_init(&rules, 0);
83
84         /* remove /sys if given */
85         if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
86                 devpath = &devpath[strlen(sysfs_path)];
87
88         dev = sysfs_device_get(devpath);
89         if (dev == NULL) {
90                 fprintf(stderr, "unable to open device '%s'\n", devpath);
91                 rc = 2;
92                 goto exit;
93         }
94
95         udev = udev_device_init();
96         if (udev == NULL) {
97                 fprintf(stderr, "error initializing device\n");
98                 rc = 3;
99                 goto exit;
100         }
101
102         /* override built-in sysfs device */
103         udev->dev = dev;
104         strcpy(udev->action, "add");
105         udev->devt = udev_device_get_devt(udev);
106
107         /* simulate node creation with test flag */
108         udev->test_run = 1;
109
110         setenv("DEVPATH", udev->dev->devpath, 1);
111         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
112         setenv("ACTION", "add", 1);
113
114         info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
115         retval = udev_device_event(&rules, udev);
116         if (retval == 0 && !udev->ignore_device && udev_run) {
117                 struct name_entry *name_loop;
118
119                 list_for_each_entry(name_loop, &udev->run_list, node) {
120                         char program[PATH_SIZE];
121
122                         strlcpy(program, name_loop->name, sizeof(program));
123                         udev_rules_apply_format(udev, program, sizeof(program));
124                         info("run: '%s'", program);
125                 }
126         }
127
128 exit:
129         udev_rules_cleanup(&rules);
130         sysfs_cleanup();
131         return rc;
132 }