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