- /* try to connect while daemon to starts */
- tspec.tv_sec = 0;
- tspec.tv_nsec = 100000000; /* 100 millisec */
- loop = UDEVSEND_CONNECT_RETRY;
- while (loop--) {
- retval = connect(sock, &saddr, sizeof(saddr));
- if (retval != -1)
- goto send;
- else
- dbg("retry to connect %d",
- UDEVSEND_CONNECT_RETRY - loop);
- nanosleep(&tspec, NULL);
- }
- dbg("error connecting to daemon, start daemon failed");
- goto exit;
-
-send:
- size = build_hotplugmsg(&message, action, devpath, subsystem, seq);
- retval = send(sock, &message, size, 0);
- if (retval == -1) {
- dbg("error sending message");
- close (sock);
- goto exit;
+ if (errno != ECONNREFUSED) {
+ err("error sending message (%s)", strerror(errno));
+ goto fallback;
+ }
+
+ if (!started_daemon) {
+ info("try to start udevd daemon");
+ retval = start_daemon();
+ if (retval) {
+ dbg("error starting daemon");
+ goto fallback;
+ }
+ dbg("udevd daemon started");
+ started_daemon = 1;
+ } else {
+ dbg("retry to connect %d", UDEVSEND_WAIT_MAX_SECONDS * UDEVSEND_WAIT_LOOP_PER_SECOND - loop);
+ usleep(1000 * 1000 / UDEVSEND_WAIT_LOOP_PER_SECOND);
+ }