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