+ udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
+ if (udevd_sock == -1) {
+ err("error getting socket: %s", strerror(errno));
+ return -1;
+ }
+
+ /* the bind takes care of ensuring only one copy running */
+ retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
+ if (retval < 0) {
+ err("bind failed: %s", strerror(errno));
+ close(udevd_sock);
+ udevd_sock = -1;
+ return -1;
+ }
+
+ /* enable receiving of the sender credentials */
+ setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
+
+ return 0;
+}
+
+static int init_uevent_netlink_sock(void)
+{
+ struct sockaddr_nl snl;
+ const int buffersize = 16 * 1024 * 1024;
+ int retval;
+
+ memset(&snl, 0x00, sizeof(struct sockaddr_nl));
+ snl.nl_family = AF_NETLINK;
+ snl.nl_pid = getpid();
+ snl.nl_groups = 1;
+
+ uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
+ if (uevent_netlink_sock == -1) {
+ err("error getting socket: %s", strerror(errno));
+ return -1;
+ }
+
+ /* set receive buffersize */
+ setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
+
+ retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
+ if (retval < 0) {
+ err("bind failed: %s", strerror(errno));
+ close(uevent_netlink_sock);
+ uevent_netlink_sock = -1;
+ return -1;
+ }
+ return 0;
+}
+
+static void export_initial_seqnum(void)
+{
+ char filename[PATH_SIZE];
+ int fd;
+ char seqnum[32];
+ ssize_t len = 0;
+
+ strlcpy(filename, sysfs_path, sizeof(filename));
+ strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
+ fd = open(filename, O_RDONLY);
+ if (fd >= 0) {
+ len = read(fd, seqnum, sizeof(seqnum)-1);
+ close(fd);
+ }
+ if (len <= 0) {
+ strcpy(seqnum, "0\n");
+ len = 3;
+ }
+ strlcpy(filename, udev_root, sizeof(filename));
+ strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
+ create_path(filename);
+ fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
+ if (fd >= 0) {
+ write(fd, seqnum, len);
+ close(fd);
+ }
+}
+
+int main(int argc, char *argv[], char *envp[])
+{
+ int retval;
+ int fd;
+ struct sigaction act;
+ fd_set readfds;
+ const char *value;
+ int daemonize = 0;
+ int option;
+ static const struct option options[] = {
+ { "daemon", 0, NULL, 'd' },
+ { "debug-trace", 0, NULL, 't' },
+ { "verbose", 0, NULL, 'v' },
+ { "help", 0, NULL, 'h' },
+ { "version", 0, NULL, 'V' },
+ {}
+ };
+ int rc = 1;
+ int maxfd;
+
+ logging_init("udevd");
+ udev_config_init();
+ selinux_init();
+ dbg("version %s", UDEV_VERSION);
+
+ while (1) {
+ option = getopt_long(argc, argv, "dtvhV", options, NULL);
+ if (option == -1)
+ break;
+
+ switch (option) {
+ case 'd':
+ daemonize = 1;
+ break;
+ case 't':
+ debug_trace = 1;
+ break;
+ case 'v':
+ verbose = 1;
+ if (udev_log_priority < LOG_INFO)
+ udev_log_priority = LOG_INFO;
+ break;
+ case 'h':
+ printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose] [--version]\n");
+ goto exit;
+ case 'V':
+ printf("%s\n", UDEV_VERSION);
+ goto exit;
+ default:
+ goto exit;
+ }
+ }
+
+ if (getuid() != 0) {
+ fprintf(stderr, "root privileges required\n");
+ err("root privileges required");
+ goto exit;
+ }
+
+ /* make sure std{in,out,err} fd's are in a sane state */
+ fd = open("/dev/null", O_RDWR);
+ if (fd < 0) {
+ fprintf(stderr, "cannot open /dev/null\n");
+ err("cannot open /dev/null");
+ }
+ if (fd > STDIN_FILENO)
+ dup2(fd, STDIN_FILENO);
+ if (write(STDOUT_FILENO, 0, 0) < 0)
+ dup2(fd, STDOUT_FILENO);
+ if (write(STDERR_FILENO, 0, 0) < 0)
+ dup2(fd, STDERR_FILENO);
+
+ /* init sockets to receive events */
+ if (init_udevd_socket() < 0) {
+ if (errno == EADDRINUSE) {
+ fprintf(stderr, "another udev daemon already running\n");
+ err("another udev daemon already running");
+ rc = 1;
+ } else {
+ fprintf(stderr, "error initializing udevd socket\n");
+ err("error initializing udevd socket");
+ rc = 2;
+ }
+ goto exit;