chiark / gitweb /
get rid of udev_sysdeps.c
[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 "config.h"
20
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <unistd.h>
30 #include <syslog.h>
31 #include <grp.h>
32
33 #include "udev.h"
34 #include "udev_rules.h"
35
36 static void asmlinkage sig_handler(int signum)
37 {
38         switch (signum) {
39                 case SIGALRM:
40                         exit(1);
41                 case SIGINT:
42                 case SIGTERM:
43                         exit(20 + signum);
44         }
45 }
46
47 int main(int argc, char *argv[])
48 {
49         struct udev *udev;
50         struct sysfs_device *dev;
51         struct udevice *udevice;
52         const char *maj, *min;
53         struct udev_rules rules;
54         const char *action;
55         const char *devpath;
56         const char *subsystem;
57         struct sigaction act;
58         int retval = -EINVAL;
59
60         udev = udev_new();
61         if (udev == NULL)
62                 exit(1);
63         dbg(udev, "version %s\n", VERSION);
64
65         /* set signal handlers */
66         memset(&act, 0x00, sizeof(act));
67         act.sa_handler = (void (*)(int)) sig_handler;
68         sigemptyset (&act.sa_mask);
69         act.sa_flags = 0;
70         sigaction(SIGALRM, &act, NULL);
71         sigaction(SIGINT, &act, NULL);
72         sigaction(SIGTERM, &act, NULL);
73
74         /* trigger timeout to prevent hanging processes */
75         alarm(UDEV_EVENT_TIMEOUT);
76
77         action = getenv("ACTION");
78         devpath = getenv("DEVPATH");
79         subsystem = getenv("SUBSYSTEM");
80         /* older kernels passed the SUBSYSTEM only as argument */
81         if (subsystem == NULL && argc == 2)
82                 subsystem = argv[1];
83
84         if (action == NULL || subsystem == NULL || devpath == NULL) {
85                 err(udev, "action, subsystem or devpath missing\n");
86                 goto exit;
87         }
88
89         /* export log_priority , as called programs may want to do the same as udev */
90         if (udev_get_log_priority(udev) > 0) {
91                 char priority[32];
92
93                 sprintf(priority, "%i", udev_get_log_priority(udev));
94                 setenv("UDEV_LOG", priority, 1);
95         }
96
97         sysfs_init();
98         udev_rules_init(udev, &rules, 0);
99
100         dev = sysfs_device_get(udev, devpath);
101         if (dev == NULL) {
102                 info(udev, "unable to open '%s'\n", devpath);
103                 goto fail;
104         }
105
106         udevice = udev_device_init(udev);
107         if (udevice == NULL)
108                 goto fail;
109
110         /* override built-in sysfs device */
111         udevice->dev = dev;
112         util_strlcpy(udevice->action, action, sizeof(udevice->action));
113
114         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
115         maj = getenv("MAJOR");
116         min = getenv("MINOR");
117         if (maj != NULL && min != NULL)
118                 udevice->devt = makedev(atoi(maj), atoi(min));
119         else
120                 udevice->devt = udev_device_get_devt(udevice);
121
122         retval = udev_device_event(&rules, udevice);
123
124         /* rules may change/disable the timeout */
125         if (udevice->event_timeout >= 0)
126                 alarm(udevice->event_timeout);
127
128         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
129                 udev_rules_run(udevice);
130
131         udev_device_cleanup(udevice);
132 fail:
133         udev_rules_cleanup(&rules);
134         sysfs_cleanup();
135 exit:
136         udev_unref(udev);
137         if (retval != 0)
138                 return 1;
139         return 0;
140 }