1/*
2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
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 <stddef.h>
21#include <signal.h>
22#include <unistd.h>
23#include <errno.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
28#include <dirent.h>
29#include <fcntl.h>
30#include <syslog.h>
31#include <time.h>
32#include <getopt.h>
33#include <sys/select.h>
34#include <sys/wait.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <sys/un.h>
38#include <sys/stat.h>
39#include <sys/ioctl.h>
40#include <linux/types.h>
41#include <linux/netlink.h>
42
43#include "udev.h"
44#include "udev_rules.h"
45#include "udevd.h"
46#include "udev_selinux.h"
47
48static int debug_trace;
49static int verbose;
50
51static struct udev_rules rules;
52static int udevd_sock = -1;
53static int uevent_netlink_sock = -1;
54static int inotify_fd = -1;
55static pid_t sid;
56
57static int signal_pipe[2] = {-1, -1};
58static volatile int sigchilds_waiting;
59static volatile int udev_exit;
60static volatile int reload_config;
61static int run_exec_q;
62static int stop_exec_q;
63static int max_childs;
64static int max_childs_running;
65static char udev_log[32];
66
67static LIST_HEAD(exec_list);
68static LIST_HEAD(running_list);
69
70
71#ifdef USE_LOG
72void log_message(int priority, const char *format, ...)
73{
74	va_list args;
75
76	if (priority > udev_log_priority)
77		return;
78
79	if (verbose) {
80		printf("[%d] ", (int) getpid());
81		va_start(args, format);
82		vprintf(format, args);
83		va_end(args);
84		printf("\n");
85	} else {
86		va_start(args, format);
87		vsyslog(priority, format, args);
88		va_end(args);
89	}
90}
91
92#endif
93
94static void asmlinkage udev_event_sig_handler(int signum)
95{
96	if (signum == SIGALRM)
97		exit(1);
98}
99
100static int udev_event_process(struct udevd_uevent_msg *msg)
101{
102	struct sigaction act;
103	struct udevice *udev;
104	int i;
105	int retval;
106
107	/* set signal handlers */
108	memset(&act, 0x00, sizeof(act));
109	act.sa_handler = (void (*)(int)) udev_event_sig_handler;
110	sigemptyset (&act.sa_mask);
111	act.sa_flags = 0;
112	sigaction(SIGALRM, &act, NULL);
113
114	/* reset to default */
115	act.sa_handler = SIG_DFL;
116	sigaction(SIGINT, &act, NULL);
117	sigaction(SIGTERM, &act, NULL);
118	sigaction(SIGCHLD, &act, NULL);
119	sigaction(SIGHUP, &act, NULL);
120
121	/* trigger timeout to prevent hanging processes */
122	alarm(UDEV_ALARM_TIMEOUT);
123
124	/* reconstruct event environment from message */
125	for (i = 0; msg->envp[i]; i++)
126		putenv(msg->envp[i]);
127
128	udev = udev_device_init(NULL);
129	if (udev == NULL)
130		return -1;
131	strlcpy(udev->action, msg->action, sizeof(udev->action));
132	sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver);
133	udev->devt = msg->devt;
134
135	retval = udev_device_event(&rules, udev);
136
137	/* run programs collected by RUN-key*/
138	if (retval == 0 && !udev->ignore_device && udev_run) {
139		struct name_entry *name_loop;
140
141		dbg("executing run list");
142		list_for_each_entry(name_loop, &udev->run_list, node) {
143			if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
144				pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
145			else {
146				char program[PATH_SIZE];
147
148				strlcpy(program, name_loop->name, sizeof(program));
149				udev_rules_apply_format(udev, program, sizeof(program));
150				if (run_program(program, udev->dev->subsystem, NULL, 0, NULL,
151						(udev_log_priority >= LOG_INFO)))
152					retval = -1;
153			}
154		}
155	}
156
157	udev_device_cleanup(udev);
158	return retval;
159}
160
161enum event_state {
162	EVENT_QUEUED,
163	EVENT_FINISHED,
164	EVENT_FAILED,
165};
166
167static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state)
168{
169	char filename[PATH_SIZE];
170	char filename_failed[PATH_SIZE];
171	size_t start;
172	struct udevd_uevent_msg *loop_msg;
173	int fd;
174
175	/* add location of queue files */
176	strlcpy(filename, udev_root, sizeof(filename));
177	strlcat(filename, "/", sizeof(filename));
178	start = strlcat(filename, EVENT_QUEUE_DIR"/", sizeof(filename));
179	strlcat(filename, msg->devpath, sizeof(filename));
180	path_encode(&filename[start], sizeof(filename) - start);
181
182	/* add location of failed files */
183	strlcpy(filename_failed, udev_root, sizeof(filename_failed));
184	strlcat(filename_failed, "/", sizeof(filename_failed));
185	start = strlcat(filename_failed, EVENT_FAILED_DIR"/", sizeof(filename_failed));
186	strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
187	path_encode(&filename_failed[start], sizeof(filename) - start);
188
189	switch (state) {
190	case EVENT_QUEUED:
191		unlink(filename_failed);
192		delete_path(filename_failed);
193		create_path(filename);
194		fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
195		if (fd > 0)
196			close(fd);
197		return;
198	case EVENT_FINISHED:
199	case EVENT_FAILED:
200		unlink(filename_failed);
201		delete_path(filename_failed);
202
203		/* don't remove, if events for the same path are still pending */
204		list_for_each_entry(loop_msg, &running_list, node)
205			if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
206				return;
207
208		list_for_each_entry(loop_msg, &exec_list, node)
209			if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
210				return;
211
212		/* move failed events to the failed directory */
213		if (state == EVENT_FAILED) {
214			create_path(filename_failed);
215			rename(filename, filename_failed);
216		} else {
217			unlink(filename);
218		}
219
220		/* clean up the queue directory */
221		delete_path(filename);
222
223		return;
224	}
225}
226
227static void msg_queue_delete(struct udevd_uevent_msg *msg)
228{
229	list_del(&msg->node);
230
231	/* mark as failed, if add event returns non-zero */
232	if (msg->exitstatus && strcmp(msg->action, "add") == 0)
233		export_event_state(msg, EVENT_FAILED);
234	else
235		export_event_state(msg, EVENT_FINISHED);
236
237	free(msg);
238}
239
240static void udev_event_run(struct udevd_uevent_msg *msg)
241{
242	pid_t pid;
243	int retval;
244
245	pid = fork();
246	switch (pid) {
247	case 0:
248		/* child */
249		close(uevent_netlink_sock);
250		close(udevd_sock);
251		if (inotify_fd >= 0)
252			close(inotify_fd);
253		close(signal_pipe[READ_END]);
254		close(signal_pipe[WRITE_END]);
255		logging_close();
256
257		logging_init("udevd-event");
258		setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
259
260		retval = udev_event_process(msg);
261		info("seq %llu finished", msg->seqnum);
262
263		logging_close();
264		if (retval)
265			exit(1);
266		exit(0);
267	case -1:
268		err("fork of child failed: %s", strerror(errno));
269		msg_queue_delete(msg);
270		break;
271	default:
272		/* get SIGCHLD in main loop */
273		info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
274		     msg->seqnum, pid,  msg->action, msg->subsystem, time(NULL) - msg->queue_time);
275		msg->pid = pid;
276	}
277}
278
279static void msg_queue_insert(struct udevd_uevent_msg *msg)
280{
281	char filename[PATH_SIZE];
282	int fd;
283
284	msg->queue_time = time(NULL);
285
286	strlcpy(filename, udev_root, sizeof(filename));
287	strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
288	fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
289	if (fd >= 0) {
290		char str[32];
291		int len;
292
293		len = sprintf(str, "%llu\n", msg->seqnum);
294		write(fd, str, len);
295		close(fd);
296	}
297
298	export_event_state(msg, EVENT_QUEUED);
299
300	/* run one event after the other in debug mode */
301	if (debug_trace) {
302		list_add_tail(&msg->node, &running_list);
303		udev_event_run(msg);
304		waitpid(msg->pid, NULL, 0);
305		msg_queue_delete(msg);
306		return;
307	}
308
309	/* run all events with a timeout set immediately */
310	if (msg->timeout != 0) {
311		list_add_tail(&msg->node, &running_list);
312		udev_event_run(msg);
313		return;
314	}
315
316	list_add_tail(&msg->node, &exec_list);
317	run_exec_q = 1;
318}
319
320static int mem_size_mb(void)
321{
322	FILE* f;
323	char buf[4096];
324	long int memsize = -1;
325
326	f = fopen("/proc/meminfo", "r");
327	if (f == NULL)
328		return -1;
329
330	while (fgets(buf, sizeof(buf), f) != NULL) {
331		long int value;
332
333		if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
334			memsize = value / 1024;
335			break;
336		}
337	}
338
339	fclose(f);
340	return memsize;
341}
342
343static int cpu_count(void)
344{
345	FILE* f;
346	char buf[4096];
347	int count = 0;
348
349	f = fopen("/proc/stat", "r");
350	if (f == NULL)
351		return -1;
352
353	while (fgets(buf, sizeof(buf), f) != NULL) {
354		if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]))
355			count++;
356	}
357
358	fclose(f);
359	if (count == 0)
360		return -1;
361	return count;
362}
363
364static int running_processes(void)
365{
366	FILE* f;
367	char buf[4096];
368	int running = -1;
369
370	f = fopen("/proc/stat", "r");
371	if (f == NULL)
372		return -1;
373
374	while (fgets(buf, sizeof(buf), f) != NULL) {
375		int value;
376
377		if (sscanf(buf, "procs_running %u", &value) == 1) {
378			running = value;
379			break;
380		}
381	}
382
383	fclose(f);
384	return running;
385}
386
387/* return the number of process es in our session, count only until limit */
388static int running_processes_in_session(pid_t session, int limit)
389{
390	DIR *dir;
391	struct dirent *dent;
392	int running = 0;
393
394	dir = opendir("/proc");
395	if (!dir)
396		return -1;
397
398	/* read process info from /proc */
399	for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
400		int f;
401		char procdir[64];
402		char line[256];
403		const char *pos;
404		char state;
405		pid_t ppid, pgrp, sess;
406		int len;
407
408		if (!isdigit(dent->d_name[0]))
409			continue;
410
411		snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
412		procdir[sizeof(procdir)-1] = '\0';
413
414		f = open(procdir, O_RDONLY);
415		if (f == -1)
416			continue;
417
418		len = read(f, line, sizeof(line)-1);
419		close(f);
420
421		if (len <= 0)
422			continue;
423		else
424			line[len] = '\0';
425
426		/* skip ugly program name */
427		pos = strrchr(line, ')') + 2;
428		if (pos == NULL)
429			continue;
430
431		if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
432			continue;
433
434		/* count only processes in our session */
435		if (sess != session)
436			continue;
437
438		/* count only running, no sleeping processes */
439		if (state != 'R')
440			continue;
441
442		running++;
443		if (limit > 0 && running >= limit)
444			break;
445	}
446	closedir(dir);
447
448	return running;
449}
450
451static int compare_devpath(const char *running, const char *waiting)
452{
453	int i;
454
455	for (i = 0; i < PATH_SIZE; i++) {
456		/* identical device event found */
457		if (running[i] == '\0' && waiting[i] == '\0')
458			return 1;
459
460		/* parent device event found */
461		if (running[i] == '\0' && waiting[i] == '/')
462			return 2;
463
464		/* child device event found */
465		if (running[i] == '/' && waiting[i] == '\0')
466			return 3;
467
468		/* no matching event */
469		if (running[i] != waiting[i])
470			break;
471	}
472
473	return 0;
474}
475
476/* lookup event for identical, parent, child, or physical device */
477static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
478{
479	struct udevd_uevent_msg *loop_msg;
480	int childs_count = 0;
481
482	/* check exec-queue which may still contain delayed events we depend on */
483	list_for_each_entry(loop_msg, &exec_list, node) {
484		/* skip ourself and all later events */
485		if (loop_msg->seqnum >= msg->seqnum)
486			break;
487
488		/* check identical, parent, or child device event */
489		if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
490			dbg("%llu, device event still pending %llu (%s)",
491			    msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
492			return 2;
493		}
494
495		/* check physical device event (special case of parent) */
496		if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
497			if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
498				dbg("%llu, physical device event still pending %llu (%s)",
499				    msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
500				return 3;
501			}
502	}
503
504	/* check runing-queue for still running events */
505	list_for_each_entry(loop_msg, &running_list, node) {
506		if (limit && childs_count++ > limit) {
507			dbg("%llu, maximum number (%i) of childs reached", msg->seqnum, childs_count);
508			return 1;
509		}
510
511		/* check identical, parent, or child device event */
512		if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
513			dbg("%llu, device event still running %llu (%s)",
514			    msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
515			return 2;
516		}
517
518		/* check physical device event (special case of parent) */
519		if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
520			if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
521				dbg("%llu, physical device event still running %llu (%s)",
522				    msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
523				return 3;
524			}
525	}
526	return 0;
527}
528
529/* serializes events for the identical and parent and child devices */
530static void msg_queue_manager(void)
531{
532	struct udevd_uevent_msg *loop_msg;
533	struct udevd_uevent_msg *tmp_msg;
534	int running;
535
536	if (list_empty(&exec_list))
537		return;
538
539	running = running_processes();
540	dbg("%d processes runnning on system", running);
541	if (running < 0)
542		running = max_childs_running;
543
544	list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
545		/* check running processes in our session and possibly throttle */
546		if (running >= max_childs_running) {
547			running = running_processes_in_session(sid, max_childs_running+10);
548			dbg("at least %d processes running in session", running);
549			if (running >= max_childs_running) {
550				dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
551				return;
552			}
553		}
554
555		/* serialize and wait for parent or child events */
556		if (devpath_busy(loop_msg, max_childs) != 0) {
557			dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
558			continue;
559		}
560
561		/* move event to run list */
562		list_move_tail(&loop_msg->node, &running_list);
563		udev_event_run(loop_msg);
564		running++;
565		dbg("moved seq %llu to running list", loop_msg->seqnum);
566	}
567}
568
569static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
570{
571	int bufpos;
572	int i;
573	struct udevd_uevent_msg *msg;
574	char *physdevdriver_key = NULL;
575	int maj = 0;
576	int min = 0;
577
578	msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size);
579	if (msg == NULL)
580		return NULL;
581	memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size);
582
583	/* copy environment buffer and reconstruct envp */
584	memcpy(msg->envbuf, buf, buf_size);
585	bufpos = 0;
586	for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
587		int keylen;
588		char *key;
589
590		key = &msg->envbuf[bufpos];
591		keylen = strlen(key);
592		msg->envp[i] = key;
593		bufpos += keylen + 1;
594		dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
595
596		/* remember some keys for further processing */
597		if (strncmp(key, "ACTION=", 7) == 0)
598			msg->action = &key[7];
599		else if (strncmp(key, "DEVPATH=", 8) == 0)
600			msg->devpath = &key[8];
601		else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
602			msg->subsystem = &key[10];
603		else if (strncmp(key, "DRIVER=", 7) == 0)
604			msg->driver = &key[7];
605		else if (strncmp(key, "SEQNUM=", 7) == 0)
606			msg->seqnum = strtoull(&key[7], NULL, 10);
607		else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
608			msg->physdevpath = &key[12];
609		else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0)
610			physdevdriver_key = key;
611		else if (strncmp(key, "MAJOR=", 6) == 0)
612			maj = strtoull(&key[6], NULL, 10);
613		else if (strncmp(key, "MINOR=", 6) == 0)
614			min = strtoull(&key[6], NULL, 10);
615		else if (strncmp(key, "TIMEOUT=", 8) == 0)
616			msg->timeout = strtoull(&key[8], NULL, 10);
617	}
618	msg->devt = makedev(maj, min);
619	msg->envp[i++] = "UDEVD_EVENT=1";
620
621	if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) {
622		/* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
623		msg->envp[i++] = &physdevdriver_key[7];
624		msg->driver = &physdevdriver_key[14];
625	}
626
627	msg->envp[i] = NULL;
628
629	if (msg->devpath == NULL || msg->action == NULL) {
630		info("DEVPATH or ACTION missing, ignore message");
631		free(msg);
632		return NULL;
633	}
634	return msg;
635}
636
637/* receive the udevd message from userspace */
638static void get_ctrl_msg(void)
639{
640	struct udevd_ctrl_msg ctrl_msg;
641	ssize_t size;
642	struct msghdr smsg;
643	struct cmsghdr *cmsg;
644	struct iovec iov;
645	struct ucred *cred;
646	char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
647	int *intval;
648	char *pos;
649
650	memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
651	iov.iov_base = &ctrl_msg;
652	iov.iov_len = sizeof(struct udevd_ctrl_msg);
653
654	memset(&smsg, 0x00, sizeof(struct msghdr));
655	smsg.msg_iov = &iov;
656	smsg.msg_iovlen = 1;
657	smsg.msg_control = cred_msg;
658	smsg.msg_controllen = sizeof(cred_msg);
659
660	size = recvmsg(udevd_sock, &smsg, 0);
661	if (size <  0) {
662		if (errno != EINTR)
663			err("unable to receive user udevd message: %s", strerror(errno));
664		return;
665	}
666	cmsg = CMSG_FIRSTHDR(&smsg);
667	cred = (struct ucred *) CMSG_DATA(cmsg);
668
669	if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
670		err("no sender credentials received, message ignored");
671		return;
672	}
673
674	if (cred->uid != 0) {
675		err("sender uid=%i, message ignored", cred->uid);
676		return;
677	}
678
679	if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) {
680		err("message magic '%s' doesn't match, ignore it", ctrl_msg.magic);
681		return;
682	}
683
684	switch (ctrl_msg.type) {
685	case UDEVD_CTRL_ENV:
686		pos = strchr(ctrl_msg.buf, '=');
687		if (pos == NULL) {
688			err("wrong key format '%s'", ctrl_msg.buf);
689			break;
690		}
691		pos[0] = '\0';
692		if (pos[1] == '\0') {
693			info("udevd message (ENV) received, unset '%s'", ctrl_msg.buf);
694			unsetenv(ctrl_msg.buf);
695		} else {
696			info("udevd message (ENV) received, set '%s=%s'", ctrl_msg.buf, &pos[1]);
697			setenv(ctrl_msg.buf, &pos[1], 1);
698		}
699		break;
700	case UDEVD_CTRL_STOP_EXEC_QUEUE:
701		info("udevd message (STOP_EXEC_QUEUE) received");
702		stop_exec_q = 1;
703		break;
704	case UDEVD_CTRL_START_EXEC_QUEUE:
705		info("udevd message (START_EXEC_QUEUE) received");
706		stop_exec_q = 0;
707		msg_queue_manager();
708		break;
709	case UDEVD_CTRL_SET_LOG_LEVEL:
710		intval = (int *) ctrl_msg.buf;
711		info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
712		udev_log_priority = *intval;
713		sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
714		putenv(udev_log);
715		break;
716	case UDEVD_CTRL_SET_MAX_CHILDS:
717		intval = (int *) ctrl_msg.buf;
718		info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
719		max_childs = *intval;
720		break;
721	case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING:
722		intval = (int *) ctrl_msg.buf;
723		info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs=%i", *intval);
724		max_childs_running = *intval;
725		break;
726	case UDEVD_CTRL_RELOAD_RULES:
727		info("udevd message (RELOAD_RULES) received");
728		reload_config = 1;
729		break;
730	default:
731		err("unknown control message type");
732	}
733}
734
735/* receive the kernel user event message and do some sanity checks */
736static struct udevd_uevent_msg *get_netlink_msg(void)
737{
738	struct udevd_uevent_msg *msg;
739	int bufpos;
740	ssize_t size;
741	static char buffer[UEVENT_BUFFER_SIZE+512];
742	char *pos;
743
744	size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
745	if (size <  0) {
746		if (errno != EINTR)
747			err("unable to receive kernel netlink message: %s", strerror(errno));
748		return NULL;
749	}
750
751	if ((size_t)size > sizeof(buffer)-1)
752		size = sizeof(buffer)-1;
753	buffer[size] = '\0';
754	dbg("uevent_size=%zi", size);
755
756	/* start of event payload */
757	bufpos = strlen(buffer)+1;
758	msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
759	if (msg == NULL)
760		return NULL;
761
762	/* validate message */
763	pos = strchr(buffer, '@');
764	if (pos == NULL) {
765		err("invalid uevent '%s'", buffer);
766		free(msg);
767		return NULL;
768	}
769	pos[0] = '\0';
770
771	if (msg->action == NULL) {
772		info("no ACTION in payload found, skip event '%s'", buffer);
773		free(msg);
774		return NULL;
775	}
776
777	if (strcmp(msg->action, buffer) != 0) {
778		err("ACTION in payload does not match uevent, skip event '%s'", buffer);
779		free(msg);
780		return NULL;
781	}
782
783	return msg;
784}
785
786static void asmlinkage sig_handler(int signum)
787{
788	switch (signum) {
789		case SIGINT:
790		case SIGTERM:
791			udev_exit = 1;
792			break;
793		case SIGCHLD:
794			/* set flag, then write to pipe if needed */
795			sigchilds_waiting = 1;
796			break;
797		case SIGHUP:
798			reload_config = 1;
799			break;
800	}
801
802	/* write to pipe, which will wakeup select() in our mainloop */
803	write(signal_pipe[WRITE_END], "", 1);
804}
805
806static void udev_done(int pid, int exitstatus)
807{
808	/* find msg associated with pid and delete it */
809	struct udevd_uevent_msg *msg;
810
811	list_for_each_entry(msg, &running_list, node) {
812		if (msg->pid == pid) {
813			info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
814			     exitstatus, time(NULL) - msg->queue_time);
815			msg->exitstatus = exitstatus;
816			msg_queue_delete(msg);
817
818			/* there may be events waiting with the same devpath */
819			run_exec_q = 1;
820			return;
821		}
822	}
823}
824
825static void reap_sigchilds(void)
826{
827	pid_t pid;
828	int status;
829
830	while (1) {
831		pid = waitpid(-1, &status, WNOHANG);
832		if (pid <= 0)
833			break;
834		if (WIFEXITED(status))
835			status = WEXITSTATUS(status);
836		else if (WIFSIGNALED(status))
837			status = WTERMSIG(status) + 128;
838		else
839			status = 0;
840		udev_done(pid, status);
841	}
842}
843
844static int init_udevd_socket(void)
845{
846	struct sockaddr_un saddr;
847	socklen_t addrlen;
848	const int feature_on = 1;
849	int retval;
850
851	memset(&saddr, 0x00, sizeof(saddr));
852	saddr.sun_family = AF_LOCAL;
853	/* use abstract namespace for socket path */
854	strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
855	addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
856
857	udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
858	if (udevd_sock == -1) {
859		err("error getting socket: %s", strerror(errno));
860		return -1;
861	}
862
863	/* the bind takes care of ensuring only one copy running */
864	retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
865	if (retval < 0) {
866		err("bind failed: %s", strerror(errno));
867		close(udevd_sock);
868		udevd_sock = -1;
869		return -1;
870	}
871
872	/* enable receiving of the sender credentials */
873	setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
874
875	return 0;
876}
877
878static int init_uevent_netlink_sock(void)
879{
880	struct sockaddr_nl snl;
881	const int buffersize = 16 * 1024 * 1024;
882	int retval;
883
884	memset(&snl, 0x00, sizeof(struct sockaddr_nl));
885	snl.nl_family = AF_NETLINK;
886	snl.nl_pid = getpid();
887	snl.nl_groups = 1;
888
889	uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
890	if (uevent_netlink_sock == -1) {
891		err("error getting socket: %s", strerror(errno));
892		return -1;
893	}
894
895	/* set receive buffersize */
896	setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
897
898	retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
899	if (retval < 0) {
900		err("bind failed: %s", strerror(errno));
901		close(uevent_netlink_sock);
902		uevent_netlink_sock = -1;
903		return -1;
904	}
905	return 0;
906}
907
908static void export_initial_seqnum(void)
909{
910	char filename[PATH_SIZE];
911	int fd;
912	char seqnum[32];
913	ssize_t len = 0;
914
915	strlcpy(filename, sysfs_path, sizeof(filename));
916	strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
917	fd = open(filename, O_RDONLY);
918	if (fd >= 0) {
919		len = read(fd, seqnum, sizeof(seqnum)-1);
920		close(fd);
921	}
922	if (len <= 0) {
923		strcpy(seqnum, "0\n");
924		len = 3;
925	}
926	strlcpy(filename, udev_root, sizeof(filename));
927	strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
928	create_path(filename);
929	fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
930	if (fd >= 0) {
931		write(fd, seqnum, len);
932		close(fd);
933	}
934}
935
936int main(int argc, char *argv[], char *envp[])
937{
938	int retval;
939	int fd;
940	struct sigaction act;
941	fd_set readfds;
942	const char *value;
943	int daemonize = 0;
944	int option;
945	static const struct option options[] = {
946		{ "daemon", 0, NULL, 'd' },
947		{ "debug-trace", 0, NULL, 't' },
948		{ "verbose", 0, NULL, 'v' },
949		{ "help", 0, NULL, 'h' },
950		{ "version", 0, NULL, 'V' },
951		{}
952	};
953	int rc = 1;
954	int maxfd;
955
956	logging_init("udevd");
957	udev_config_init();
958	selinux_init();
959	dbg("version %s", UDEV_VERSION);
960
961	while (1) {
962		option = getopt_long(argc, argv, "dtvhV", options, NULL);
963		if (option == -1)
964			break;
965
966		switch (option) {
967		case 'd':
968			daemonize = 1;
969			break;
970		case 't':
971			debug_trace = 1;
972			break;
973		case 'v':
974			verbose = 1;
975			if (udev_log_priority < LOG_INFO)
976				udev_log_priority = LOG_INFO;
977			break;
978		case 'h':
979			printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose] [--version]\n");
980			goto exit;
981		case 'V':
982			printf("%s\n", UDEV_VERSION);
983			goto exit;
984		default:
985			goto exit;
986		}
987	}
988
989	if (getuid() != 0) {
990		fprintf(stderr, "root privileges required\n");
991		err("root privileges required");
992		goto exit;
993	}
994
995	/* make sure std{in,out,err} fd's are in a sane state */
996	fd = open("/dev/null", O_RDWR);
997	if (fd < 0) {
998		fprintf(stderr, "cannot open /dev/null\n");
999		err("cannot open /dev/null");
1000	}
1001	if (fd > STDIN_FILENO)
1002		dup2(fd, STDIN_FILENO);
1003	if (write(STDOUT_FILENO, 0, 0) < 0)
1004		dup2(fd, STDOUT_FILENO);
1005	if (write(STDERR_FILENO, 0, 0) < 0)
1006		dup2(fd, STDERR_FILENO);
1007
1008	/* init sockets to receive events */
1009	if (init_udevd_socket() < 0) {
1010		if (errno == EADDRINUSE) {
1011			fprintf(stderr, "another udev daemon already running\n");
1012			err("another udev daemon already running");
1013			rc = 1;
1014		} else {
1015			fprintf(stderr, "error initializing udevd socket\n");
1016			err("error initializing udevd socket");
1017			rc = 2;
1018		}
1019		goto exit;
1020	}
1021
1022	if (init_uevent_netlink_sock() < 0) {
1023		fprintf(stderr, "error initializing netlink socket\n");
1024		err("error initializing netlink socket");
1025		rc = 3;
1026		goto exit;
1027	}
1028
1029	/* setup signal handler pipe */
1030	retval = pipe(signal_pipe);
1031	if (retval < 0) {
1032		err("error getting pipes: %s", strerror(errno));
1033		goto exit;
1034	}
1035
1036	retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
1037	if (retval < 0) {
1038		err("error fcntl on read pipe: %s", strerror(errno));
1039		goto exit;
1040	}
1041	retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
1042	if (retval < 0) {
1043		err("error fcntl on read pipe: %s", strerror(errno));
1044		goto exit;
1045	}
1046
1047	retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
1048	if (retval < 0) {
1049		err("error fcntl on write pipe: %s", strerror(errno));
1050		goto exit;
1051	}
1052	retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
1053	if (retval < 0) {
1054		err("error fcntl on write pipe: %s", strerror(errno));
1055		goto exit;
1056	}
1057
1058	/* parse the rules and keep them in memory */
1059	sysfs_init();
1060	udev_rules_init(&rules, 1);
1061
1062	export_initial_seqnum();
1063
1064	if (daemonize) {
1065		pid_t pid;
1066
1067		pid = fork();
1068		switch (pid) {
1069		case 0:
1070			dbg("daemonized fork running");
1071			break;
1072		case -1:
1073			err("fork of daemon failed: %s", strerror(errno));
1074			rc = 4;
1075			goto exit;
1076		default:
1077			dbg("child [%u] running, parent exits", pid);
1078			rc = 0;
1079			goto exit;
1080		}
1081	}
1082
1083	/* redirect std{out,err} fd's */
1084	if (!verbose)
1085		dup2(fd, STDOUT_FILENO);
1086	dup2(fd, STDERR_FILENO);
1087	if (fd > STDERR_FILENO)
1088		close(fd);
1089
1090	/* set scheduling priority for the daemon */
1091	setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1092
1093	chdir("/");
1094	umask(022);
1095
1096	/* become session leader */
1097	sid = setsid();
1098	dbg("our session is %d", sid);
1099
1100	/* OOM_DISABLE == -17 */
1101	fd = open("/proc/self/oom_adj", O_RDWR);
1102	if (fd < 0)
1103		err("error disabling OOM: %s", strerror(errno));
1104	else {
1105		write(fd, "-17", 3);
1106		close(fd);
1107	}
1108
1109	/* set signal handlers */
1110	memset(&act, 0x00, sizeof(struct sigaction));
1111	act.sa_handler = (void (*)(int)) sig_handler;
1112	sigemptyset(&act.sa_mask);
1113	act.sa_flags = SA_RESTART;
1114	sigaction(SIGINT, &act, NULL);
1115	sigaction(SIGTERM, &act, NULL);
1116	sigaction(SIGCHLD, &act, NULL);
1117	sigaction(SIGHUP, &act, NULL);
1118
1119	/* watch rules directory */
1120	inotify_fd = inotify_init();
1121	if (inotify_fd >= 0)
1122		inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1123	else if (errno == ENOSYS)
1124		err("the kernel does not support inotify, udevd can't monitor configuration file changes");
1125	else
1126		err("inotify_init failed: %s", strerror(errno));
1127
1128	/* maximum limit of forked childs */
1129	value = getenv("UDEVD_MAX_CHILDS");
1130	if (value)
1131		max_childs = strtoul(value, NULL, 10);
1132	else {
1133		int memsize = mem_size_mb();
1134		if (memsize > 0)
1135			max_childs = 128 + (memsize / 4);
1136		else
1137			max_childs = UDEVD_MAX_CHILDS;
1138	}
1139	info("initialize max_childs to %u", max_childs);
1140
1141	/* start to throttle forking if maximum number of _running_ childs is reached */
1142	value = getenv("UDEVD_MAX_CHILDS_RUNNING");
1143	if (value)
1144		max_childs_running = strtoull(value, NULL, 10);
1145	else {
1146		int cpus = cpu_count();
1147		if (cpus > 0)
1148			max_childs_running = 8 + (8 * cpus);
1149		else
1150			max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
1151	}
1152	info("initialize max_childs_running to %u", max_childs_running);
1153
1154	/* clear environment for forked event processes */
1155	clearenv();
1156
1157	/* export log_priority , as called programs may want to follow that setting */
1158	sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
1159	putenv(udev_log);
1160	if (debug_trace)
1161		putenv("DEBUG=1");
1162
1163	maxfd = udevd_sock;
1164	maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
1165	maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
1166	maxfd = UDEV_MAX(maxfd, inotify_fd);
1167
1168	while (!udev_exit) {
1169		struct udevd_uevent_msg *msg;
1170		int fdcount;
1171
1172		FD_ZERO(&readfds);
1173		FD_SET(signal_pipe[READ_END], &readfds);
1174		FD_SET(udevd_sock, &readfds);
1175		FD_SET(uevent_netlink_sock, &readfds);
1176		if (inotify_fd >= 0)
1177			FD_SET(inotify_fd, &readfds);
1178
1179		fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
1180		if (fdcount < 0) {
1181			if (errno != EINTR)
1182				err("error in select: %s", strerror(errno));
1183			continue;
1184		}
1185
1186		/* get control message */
1187		if (FD_ISSET(udevd_sock, &readfds))
1188			get_ctrl_msg();
1189
1190		/* get netlink message */
1191		if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1192			msg = get_netlink_msg();
1193			if (msg)
1194				msg_queue_insert(msg);
1195		}
1196
1197		/* received a signal, clear our notification pipe */
1198		if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1199			char buf[256];
1200
1201			read(signal_pipe[READ_END], &buf, sizeof(buf));
1202		}
1203
1204		/* rules directory inotify watch */
1205		if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) {
1206			int nbytes;
1207
1208			/* discard all possible events, we can just reload the config */
1209			if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1210				char *buf;
1211
1212				reload_config = 1;
1213				buf = malloc(nbytes);
1214				if (!buf) {
1215					err("error getting buffer for inotify, disable watching");
1216					close(inotify_fd);
1217					inotify_fd = -1;
1218				}
1219				read(inotify_fd, buf, nbytes);
1220				free(buf);
1221			}
1222		}
1223
1224		/* rules changed, set by inotify or a HUP signal */
1225		if (reload_config) {
1226			reload_config = 0;
1227			udev_rules_cleanup(&rules);
1228			udev_rules_init(&rules, 1);
1229		}
1230
1231		/* forked child has returned */
1232		if (sigchilds_waiting) {
1233			sigchilds_waiting = 0;
1234			reap_sigchilds();
1235		}
1236
1237		if (run_exec_q) {
1238			run_exec_q = 0;
1239			if (!stop_exec_q)
1240				msg_queue_manager();
1241		}
1242	}
1243	rc = 0;
1244
1245exit:
1246	udev_rules_cleanup(&rules);
1247	sysfs_cleanup();
1248	selinux_exit();
1249
1250	if (signal_pipe[READ_END] >= 0)
1251		close(signal_pipe[READ_END]);
1252	if (signal_pipe[WRITE_END] >= 0)
1253		close(signal_pipe[WRITE_END]);
1254
1255	if (udevd_sock >= 0)
1256		close(udevd_sock);
1257	if (inotify_fd >= 0)
1258		close(inotify_fd);
1259	if (uevent_netlink_sock >= 0)
1260		close(uevent_netlink_sock);
1261
1262	logging_close();
1263
1264	return rc;
1265}
1266