chiark / gitweb /
508f9f1408cfd4ac2fc8e29c99cc0107122d3f90
[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         selinux_init(udev);
63
64         /* set signal handlers */
65         memset(&act, 0x00, sizeof(act));
66         act.sa_handler = (void (*)(int)) sig_handler;
67         sigemptyset (&act.sa_mask);
68         act.sa_flags = 0;
69         sigaction(SIGALRM, &act, NULL);
70         sigaction(SIGINT, &act, NULL);
71         sigaction(SIGTERM, &act, NULL);
72
73         /* trigger timeout to prevent hanging processes */
74         alarm(UDEV_EVENT_TIMEOUT);
75
76         action = getenv("ACTION");
77         devpath = getenv("DEVPATH");
78         subsystem = getenv("SUBSYSTEM");
79         /* older kernels passed the SUBSYSTEM only as argument */
80         if (subsystem == NULL && argc == 2)
81                 subsystem = argv[1];
82
83         if (action == NULL || subsystem == NULL || devpath == NULL) {
84                 err(udev, "action, subsystem or devpath missing\n");
85                 goto exit;
86         }
87
88         /* export log_priority , as called programs may want to do the same as udev */
89         if (udev_get_log_priority(udev) > 0) {
90                 char priority[32];
91
92                 sprintf(priority, "%i", udev_get_log_priority(udev));
93                 setenv("UDEV_LOG", priority, 1);
94         }
95
96         sysfs_init();
97         udev_rules_init(udev, &rules, 0);
98
99         dev = sysfs_device_get(udev, devpath);
100         if (dev == NULL) {
101                 info(udev, "unable to open '%s'\n", devpath);
102                 goto fail;
103         }
104
105         udevice = udev_device_init(udev);
106         if (udevice == NULL)
107                 goto fail;
108
109         /* override built-in sysfs device */
110         udevice->dev = dev;
111         util_strlcpy(udevice->action, action, sizeof(udevice->action));
112
113         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
114         maj = getenv("MAJOR");
115         min = getenv("MINOR");
116         if (maj != NULL && min != NULL)
117                 udevice->devt = makedev(atoi(maj), atoi(min));
118         else
119                 udevice->devt = udev_device_get_devt(udevice);
120
121         retval = udev_device_event(&rules, udevice);
122
123         /* rules may change/disable the timeout */
124         if (udevice->event_timeout >= 0)
125                 alarm(udevice->event_timeout);
126
127         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
128                 udev_rules_run(udevice);
129
130         udev_device_cleanup(udevice);
131 fail:
132         udev_rules_cleanup(&rules);
133         sysfs_cleanup();
134 exit:
135         selinux_exit(udev);
136         udev_unref(udev);
137         if (retval != 0)
138                 return 1;
139         return 0;
140 }