- /* get state of ipc queue */
- retval = msgctl(msgid, IPC_STAT, &msg_queue);
- if (retval == -1) {
- dbg("error getting info on ipc queue");
- goto exit;
- }
- if (msg_queue.msg_qnum > 0)
- dbg("%li messages already in the ipc queue", msg_queue.msg_qnum);
-
- /* send ipc message to the daemon */
- retval = msgsnd(msgid, pmsg, size, 0);
- free_hotplugmsg( (struct hotplug_msg*) pmsg);
- if (retval == -1) {
- dbg("error sending ipc message");
- goto exit;
+ usend_msg_len = offsetof(struct udevsend_msg, envbuf) + bufpos;
+ dbg("usend_msg_len=%i", usend_msg_len);
+
+ /* If we can't send, try to start daemon and resend message */
+ loop = SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND;
+ while (--loop) {
+ retval = sendto(sock, &usend_msg, usend_msg_len, 0, (struct sockaddr *)&saddr, addrlen);
+ if (retval != -1) {
+ retval = 0;
+ goto exit;
+ }
+
+ if (errno != ECONNREFUSED) {
+ dbg("error sending message (%s)", strerror(errno));
+ goto fallback;
+ }
+
+ if (!started_daemon) {
+ dbg("try to start udevd daemon");
+ retval = start_daemon();
+ if (retval) {
+ info("error starting daemon");
+ goto fallback;
+ }
+ info("udevd daemon started");
+ started_daemon = 1;
+ } else {
+ dbg("retry to connect %d", SEND_WAIT_MAX_SECONDS * SEND_WAIT_LOOP_PER_SECOND - loop);
+ usleep(1000 * 1000 / SEND_WAIT_LOOP_PER_SECOND);
+ }