#include <linux/stddef.h>
#include "udev.h"
-#include "udev_lib.h"
#include "udev_version.h"
#include "udevd.h"
#include "logging.h"
+/* global variables */
+static int sock = -1;
+
#ifdef LOG
-unsigned char logname[LOGNAME_SIZE];
void log_message (int level, const char *format, ...)
{
va_list args;
{
pid_t pid;
pid_t child_pid;
+ char *const argv[] = { "udevd", NULL };
+ char *const envp[] = { NULL };
pid = fork();
switch (pid) {
child_pid = fork();
switch (child_pid) {
case 0:
- /* daemon */
- setsid();
- chdir("/");
- execl(UDEVD_BIN, "udevd", NULL);
+ /* daemon with empty environment */
+ close(sock);
+ execve(UDEVD_BIN, argv, envp);
dbg("exec of daemon failed");
_exit(1);
case -1:
static void run_udev(const char *subsystem)
{
+ char *const argv[] = { "udev", (char *)subsystem, NULL };
pid_t pid;
pid = fork();
switch (pid) {
case 0:
/* child */
- execl(UDEV_BIN, "udev", subsystem, NULL);
+ execv(UDEV_BIN, argv);
dbg("exec of child failed");
_exit(1);
break;
int loop;
struct sockaddr_un saddr;
socklen_t addrlen;
- const char *subsystem_argv;
- int subsystem_env = 0;
int bufpos = 0;
int retval = 1;
- int sock = -1;
int started_daemon = 0;
+ const char *subsystem = NULL;
logging_init("udevsend");
dbg("version %s", UDEV_VERSION);
- subsystem_argv = argv[1];
- if (subsystem_argv == NULL) {
- dbg("no subsystem");
- goto exit;
- }
-
sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (sock == -1) {
dbg("error getting socket");
goto fallback;
}
- set_cloexec_flag(sock, 1);
-
memset(&saddr, 0x00, sizeof(struct sockaddr_un));
saddr.sun_family = AF_LOCAL;
/* use abstract namespace for socket path */
addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
-
strcpy(usend_msg.magic, UDEV_MAGIC);
/* copy all keys to send buffer */
key = envp[i];
keylen = strlen(key);
+
+ /* prevent loops in the scripts we execute */
+ if (strncmp(key, "UDEVD_EVENT=", 12) == 0) {
+ dbg("seems that the event source is not the kernel, just exit");
+ goto exit;
+ }
+
if (bufpos + keylen >= HOTPLUG_BUFFER_SIZE-1) {
dbg("environment buffer too small, probably not called by the kernel");
continue;
}
- /* older kernels do not have the SUBSYSTEM in the environment */
+ /* remember the SUBSYSTEM */
if (strncmp(key, "SUBSYSTEM=", 10) == 0)
- subsystem_env = 1;
+ subsystem = &key[10];
dbg("add '%s' to env[%i] buffer", key, i);
strcpy(&usend_msg.envbuf[bufpos], key);
bufpos += keylen + 1;
}
- if (!subsystem_env) {
- bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", subsystem_argv) + 1;
- dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", subsystem_argv, i);
+ /* older kernels passed the SUBSYSTEM only as the first argument */
+ if (!subsystem && argc == 2) {
+ bufpos += sprintf(&usend_msg.envbuf[bufpos], "SUBSYSTEM=%s", argv[1]) + 1;
+ dbg("add 'SUBSYSTEM=%s' to env[%i] buffer from argv", argv[1], i);
}
usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
fallback:
info("unable to connect to event daemon, try to call udev directly");
- run_udev(subsystem_argv);
+ run_udev(subsystem);
exit:
if (sock != -1)