chiark / gitweb /
libudev: udev_device_get_devname -> udev_device_get_devnode
[elogind.git] / udev / test-udev.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <unistd.h>
28 #include <syslog.h>
29 #include <grp.h>
30
31 #include "udev.h"
32 #include "udev_rules.h"
33
34 static void asmlinkage sig_handler(int signum)
35 {
36         switch (signum) {
37                 case SIGALRM:
38                         exit(1);
39                 case SIGINT:
40                 case SIGTERM:
41                         exit(20 + signum);
42         }
43 }
44
45 int main(int argc, char *argv[])
46 {
47         struct udev *udev;
48         struct sysfs_device *dev;
49         struct udevice *udevice;
50         const char *maj, *min;
51         struct udev_rules rules;
52         const char *action;
53         const char *devpath;
54         const char *subsystem;
55         struct sigaction act;
56         int retval = -EINVAL;
57
58         udev = udev_new();
59         if (udev == NULL)
60                 exit(1);
61         dbg(udev, "version %s\n", VERSION);
62
63         /* set signal handlers */
64         memset(&act, 0x00, sizeof(act));
65         act.sa_handler = (void (*)(int)) sig_handler;
66         sigemptyset (&act.sa_mask);
67         act.sa_flags = 0;
68         sigaction(SIGALRM, &act, NULL);
69         sigaction(SIGINT, &act, NULL);
70         sigaction(SIGTERM, &act, NULL);
71
72         /* trigger timeout to prevent hanging processes */
73         alarm(UDEV_EVENT_TIMEOUT);
74
75         action = getenv("ACTION");
76         devpath = getenv("DEVPATH");
77         subsystem = getenv("SUBSYSTEM");
78         /* older kernels passed the SUBSYSTEM only as argument */
79         if (subsystem == NULL && argc == 2)
80                 subsystem = argv[1];
81
82         if (action == NULL || subsystem == NULL || devpath == NULL) {
83                 err(udev, "action, subsystem or devpath missing\n");
84                 goto exit;
85         }
86
87         /* export log_priority , as called programs may want to do the same as udev */
88         if (udev_get_log_priority(udev) > 0) {
89                 char priority[32];
90
91                 sprintf(priority, "%i", udev_get_log_priority(udev));
92                 setenv("UDEV_LOG", priority, 1);
93         }
94
95         sysfs_init();
96         udev_rules_init(udev, &rules, 0);
97
98         dev = sysfs_device_get(udev, devpath);
99         if (dev == NULL) {
100                 info(udev, "unable to open '%s'\n", devpath);
101                 goto fail;
102         }
103
104         udevice = udev_device_init(udev);
105         if (udevice == NULL)
106                 goto fail;
107
108         /* override built-in sysfs device */
109         udevice->dev = dev;
110         util_strlcpy(udevice->action, action, sizeof(udevice->action));
111
112         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
113         maj = getenv("MAJOR");
114         min = getenv("MINOR");
115         if (maj != NULL && min != NULL)
116                 udevice->devt = makedev(atoi(maj), atoi(min));
117         else
118                 udevice->devt = udev_device_get_devt(udevice);
119
120         retval = udev_device_event(&rules, udevice);
121
122         /* rules may change/disable the timeout */
123         if (udevice->event_timeout >= 0)
124                 alarm(udevice->event_timeout);
125
126         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
127                 udev_rules_run(udevice);
128
129         udev_device_cleanup(udevice);
130 fail:
131         udev_rules_cleanup(&rules);
132         sysfs_cleanup();
133 exit:
134         udev_unref(udev);
135         if (retval != 0)
136                 return 1;
137         return 0;
138 }