2005-04-27 05:59:47 +02:00
|
|
|
/*
|
2005-07-22 18:35:58 +02:00
|
|
|
* Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
|
2006-08-28 00:29:11 +02:00
|
|
|
* Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
|
2005-04-27 05:59:47 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-08-28 00:29:11 +02:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-04-27 05:59:47 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-07-30 01:45:23 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2004-03-23 07:22:20 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2005-04-27 05:59:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-11-23 07:34:56 +01:00
|
|
|
#include <fcntl.h>
|
2003-12-03 10:08:46 +01:00
|
|
|
#include <ctype.h>
|
2004-03-23 07:22:20 +01:00
|
|
|
#include <errno.h>
|
2003-12-30 10:21:06 +01:00
|
|
|
#include <signal.h>
|
2004-10-19 08:14:20 +02:00
|
|
|
#include <unistd.h>
|
2005-08-11 17:32:59 +02:00
|
|
|
#include <syslog.h>
|
2008-09-01 20:59:09 +02:00
|
|
|
#include <grp.h>
|
2003-04-10 19:53:46 +02:00
|
|
|
|
2005-04-27 05:59:47 +02:00
|
|
|
#include "udev.h"
|
2005-03-12 22:36:32 +01:00
|
|
|
#include "udev_rules.h"
|
2006-08-18 03:47:59 +02:00
|
|
|
#include "udev_selinux.h"
|
2005-04-27 05:59:47 +02:00
|
|
|
|
2005-03-06 10:15:51 +01:00
|
|
|
#ifdef USE_LOG
|
2005-03-27 00:11:03 +01:00
|
|
|
void log_message(int priority, const char *format, ...)
|
2004-02-12 07:10:26 +01:00
|
|
|
{
|
2004-02-27 04:40:32 +01:00
|
|
|
va_list args;
|
2004-02-13 03:57:06 +01:00
|
|
|
|
2005-03-27 00:11:03 +01:00
|
|
|
if (priority > udev_log_priority)
|
2004-02-13 03:57:06 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2005-03-27 00:11:03 +01:00
|
|
|
vsyslog(priority, format, args);
|
2004-02-13 03:57:06 +01:00
|
|
|
va_end(args);
|
2004-02-12 07:10:26 +01:00
|
|
|
}
|
2004-02-13 03:57:06 +01:00
|
|
|
#endif
|
2004-02-12 07:10:26 +01:00
|
|
|
|
2004-10-14 07:38:15 +02:00
|
|
|
static void asmlinkage sig_handler(int signum)
|
2003-12-30 10:21:06 +01:00
|
|
|
{
|
|
|
|
switch (signum) {
|
2004-10-14 08:13:26 +02:00
|
|
|
case SIGALRM:
|
2004-11-06 14:28:01 +01:00
|
|
|
exit(1);
|
2003-12-30 10:21:06 +01:00
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
exit(20 + signum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-02 23:19:36 +02:00
|
|
|
int main(int argc, char *argv[])
|
2003-12-31 07:31:37 +01:00
|
|
|
{
|
2006-01-09 21:18:00 +01:00
|
|
|
struct sysfs_device *dev;
|
|
|
|
struct udevice *udev;
|
|
|
|
const char *maj, *min;
|
2005-07-05 15:24:41 +02:00
|
|
|
struct udev_rules rules;
|
2004-12-12 02:54:55 +01:00
|
|
|
const char *action;
|
|
|
|
const char *devpath;
|
|
|
|
const char *subsystem;
|
2005-03-04 21:13:18 +01:00
|
|
|
struct sigaction act;
|
2005-08-18 13:07:15 +02:00
|
|
|
int devnull;
|
2004-12-12 02:54:55 +01:00
|
|
|
int retval = -EINVAL;
|
2003-12-31 07:31:37 +01:00
|
|
|
|
2004-12-12 02:27:50 +01:00
|
|
|
if (argc == 2 && strcmp(argv[1], "-V") == 0) {
|
2008-07-30 01:45:23 +02:00
|
|
|
printf("%s\n", VERSION);
|
2004-12-12 02:27:50 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:38:20 +02:00
|
|
|
/* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
|
2005-08-18 13:07:15 +02:00
|
|
|
devnull = open("/dev/null", O_RDWR);
|
|
|
|
if (devnull >= 0) {
|
|
|
|
if (devnull != STDIN_FILENO)
|
|
|
|
dup2(devnull, STDIN_FILENO);
|
|
|
|
if (devnull != STDOUT_FILENO)
|
|
|
|
dup2(devnull, STDOUT_FILENO);
|
|
|
|
if (devnull != STDERR_FILENO)
|
|
|
|
dup2(devnull, STDERR_FILENO);
|
|
|
|
if (devnull > STDERR_FILENO)
|
|
|
|
close(devnull);
|
|
|
|
}
|
|
|
|
|
2004-10-19 03:15:10 +02:00
|
|
|
logging_init("udev");
|
2005-08-18 13:07:15 +02:00
|
|
|
if (devnull < 0)
|
2008-04-20 21:07:06 +02:00
|
|
|
err("open /dev/null failed: %s\n", strerror(errno));
|
2006-01-09 21:18:00 +01:00
|
|
|
udev_config_init();
|
2006-08-18 03:47:59 +02:00
|
|
|
selinux_init();
|
2008-07-30 01:45:23 +02:00
|
|
|
dbg("version %s\n", VERSION);
|
2003-07-17 05:18:06 +02:00
|
|
|
|
2004-06-07 11:56:47 +02:00
|
|
|
/* set signal handlers */
|
2005-04-27 08:15:56 +02:00
|
|
|
memset(&act, 0x00, sizeof(act));
|
2005-03-27 00:11:03 +01:00
|
|
|
act.sa_handler = (void (*)(int)) sig_handler;
|
2004-02-12 07:29:15 +01:00
|
|
|
sigemptyset (&act.sa_mask);
|
2004-11-05 13:16:32 +01:00
|
|
|
act.sa_flags = 0;
|
2004-10-14 08:13:26 +02:00
|
|
|
sigaction(SIGALRM, &act, NULL);
|
2004-02-12 07:29:15 +01:00
|
|
|
sigaction(SIGINT, &act, NULL);
|
|
|
|
sigaction(SIGTERM, &act, NULL);
|
2003-12-30 10:29:16 +01:00
|
|
|
|
2004-11-29 02:15:55 +01:00
|
|
|
/* trigger timeout to prevent hanging processes */
|
2008-04-21 19:00:54 +02:00
|
|
|
alarm(UDEV_EVENT_TIMEOUT);
|
2004-10-14 08:13:26 +02:00
|
|
|
|
2004-12-12 03:29:59 +01:00
|
|
|
action = getenv("ACTION");
|
|
|
|
devpath = getenv("DEVPATH");
|
|
|
|
subsystem = getenv("SUBSYSTEM");
|
|
|
|
/* older kernels passed the SUBSYSTEM only as argument */
|
2006-01-09 21:18:00 +01:00
|
|
|
if (subsystem == NULL && argc == 2)
|
2004-12-12 03:29:59 +01:00
|
|
|
subsystem = argv[1];
|
|
|
|
|
2006-01-09 21:18:00 +01:00
|
|
|
if (action == NULL || subsystem == NULL || devpath == NULL) {
|
2008-04-20 21:07:06 +02:00
|
|
|
err("action, subsystem or devpath missing\n");
|
2005-06-05 15:55:29 +02:00
|
|
|
goto exit;
|
2004-11-23 07:34:56 +01:00
|
|
|
}
|
2004-10-19 13:37:22 +02:00
|
|
|
|
2005-06-16 01:58:47 +02:00
|
|
|
/* export log_priority , as called programs may want to do the same as udev */
|
2005-03-27 00:11:03 +01:00
|
|
|
if (udev_log_priority) {
|
|
|
|
char priority[32];
|
|
|
|
|
|
|
|
sprintf(priority, "%i", udev_log_priority);
|
|
|
|
setenv("UDEV_LOG", priority, 1);
|
|
|
|
}
|
2004-11-23 07:34:56 +01:00
|
|
|
|
2006-01-09 21:18:00 +01:00
|
|
|
sysfs_init();
|
2005-11-05 20:00:31 +01:00
|
|
|
udev_rules_init(&rules, 0);
|
2005-04-02 17:45:35 +02:00
|
|
|
|
2006-01-09 21:18:00 +01:00
|
|
|
dev = sysfs_device_get(devpath);
|
|
|
|
if (dev == NULL) {
|
2008-04-20 21:07:06 +02:00
|
|
|
info("unable to open '%s'\n", devpath);
|
2006-01-09 21:18:00 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-09-01 20:59:09 +02:00
|
|
|
udev = udev_device_init();
|
2006-01-09 21:18:00 +01:00
|
|
|
if (udev == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* override built-in sysfs device */
|
|
|
|
udev->dev = dev;
|
|
|
|
strlcpy(udev->action, action, sizeof(udev->action));
|
|
|
|
|
|
|
|
/* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
|
|
|
|
maj = getenv("MAJOR");
|
|
|
|
min = getenv("MINOR");
|
|
|
|
if (maj != NULL && min != NULL)
|
|
|
|
udev->devt = makedev(atoi(maj), atoi(min));
|
|
|
|
else
|
|
|
|
udev->devt = udev_device_get_devt(udev);
|
|
|
|
|
|
|
|
retval = udev_device_event(&rules, udev);
|
2004-03-24 03:54:34 +01:00
|
|
|
|
2008-04-21 19:00:54 +02:00
|
|
|
/* rules may change/disable the timeout */
|
|
|
|
if (udev->event_timeout >= 0)
|
|
|
|
alarm(udev->event_timeout);
|
|
|
|
|
2007-07-14 20:44:19 +02:00
|
|
|
if (retval == 0 && !udev->ignore_device && udev_run)
|
|
|
|
udev_rules_run(udev);
|
2004-11-23 08:01:57 +01:00
|
|
|
|
2006-01-09 21:18:00 +01:00
|
|
|
udev_device_cleanup(udev);
|
|
|
|
fail:
|
|
|
|
udev_rules_cleanup(&rules);
|
|
|
|
sysfs_cleanup();
|
2007-03-16 21:13:07 +01:00
|
|
|
selinux_exit();
|
2005-03-29 11:25:52 +02:00
|
|
|
|
2005-06-14 16:12:31 +02:00
|
|
|
exit:
|
2004-10-19 03:15:10 +02:00
|
|
|
logging_close();
|
2008-09-01 20:59:09 +02:00
|
|
|
endgrent();
|
2006-01-25 02:21:07 +01:00
|
|
|
if (retval != 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2005-04-27 05:59:47 +02:00
|
|
|
}
|