X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udevsend.c;h=17372d8d9a0ec28568f036ea82be120c93203166;hp=f92ee2b5d8286aa539c575355ee76368b2d43ee6;hb=51a8bb2f361d86013e7579570faba446eed9c66d;hpb=35b7d88c0dab4c1104c127ddd644db22307949c9 diff --git a/udevsend.c b/udevsend.c index f92ee2b5d..17372d8d9 100644 --- a/udevsend.c +++ b/udevsend.c @@ -23,21 +23,30 @@ */ #include -#include -#include +#include +#include +#include #include #include #include +#include #include #include #include -#include +#include #include "udev.h" #include "udev_version.h" #include "udevd.h" #include "logging.h" +unsigned char logname[42]; + +int log_ok(void) +{ + return 1; +} + static inline char *get_action(void) { char *action; @@ -66,7 +75,7 @@ static int build_hotplugmsg(struct hotplug_msg *msg, char *action, char *devpath, char *subsystem, int seqnum) { memset(msg, 0x00, sizeof(*msg)); - msg->mtype = HOTPLUGMSGTYPE; + strfieldcpy(msg->magic, UDEV_MAGIC); msg->seqnum = seqnum; strncpy(msg->action, action, 8); strncpy(msg->devpath, devpath, 128); @@ -108,22 +117,26 @@ static int start_daemon(void) return 0; } - int main(int argc, char* argv[]) { - int msgid; - key_t key; - struct msqid_ds msg_queue; struct hotplug_msg message; char *action; char *devpath; char *subsystem; char *seqnum; int seq; - int retval = -EINVAL; + int retval = 1; int size; int loop; struct timespec tspec; + int sock; + struct sockaddr_un saddr; + socklen_t addrlen; + int started_daemon = 0; + +#ifdef DEBUG + init_logging("udevsend"); +#endif subsystem = argv[1]; if (subsystem == NULL) { @@ -144,47 +157,59 @@ int main(int argc, char* argv[]) } seqnum = get_seqnum(); - if (seqnum == NULL) { - dbg("no seqnum"); - goto exit; - } - seq = atoi(seqnum); - - /* create ipc message queue or get id of our existing one */ - key = ftok(UDEVD_BIN, IPC_KEY_ID); - dbg("using ipc queue 0x%0x", key); - size = build_hotplugmsg(&message, action, devpath, subsystem, seq); - msgid = msgget(key, IPC_CREAT); - if (msgid == -1) { - dbg("error open ipc queue"); + if (seqnum == NULL) + seq = -1; + else + seq = atoi(seqnum); + + sock = socket(AF_LOCAL, SOCK_DGRAM, 0); + if (sock == -1) { + dbg("error getting socket"); goto exit; } - /* send ipc message to the daemon */ - retval = msgsnd(msgid, &message, size, 0); - if (retval == -1) { - dbg("error sending ipc message"); - goto exit; - } + memset(&saddr, 0x00, sizeof(saddr)); + saddr.sun_family = AF_LOCAL; + /* use abstract namespace for socket path */ + strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH); + addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; - /* get state of ipc queue */ - tspec.tv_sec = 0; - tspec.tv_nsec = 10000000; /* 10 millisec */ - loop = UDEVSEND_RETRY_COUNT; + size = build_hotplugmsg(&message, action, devpath, subsystem, seq); + + /* If we can't send, try to start daemon and resend message */ + loop = UDEVSEND_CONNECT_RETRY; while (loop--) { - retval = msgctl(msgid, IPC_STAT, &msg_queue); - if (retval == -1) { - dbg("error getting info on ipc queue"); - goto exit; + retval = sendto(sock, &message, size, 0, (struct sockaddr*)&saddr, addrlen); + if (retval != -1) { + retval = 0; + goto close_and_exit; + } + + if (errno != ECONNREFUSED) { + dbg("error sending message"); + goto close_and_exit; + } + + if (!started_daemon) { + dbg("connect failed, try starting daemon..."); + retval = start_daemon(); + if (retval) { + dbg("error starting daemon"); + goto exit; + } + + dbg("daemon started"); + started_daemon = 1; + } else { + dbg("retry to connect %d", UDEVSEND_CONNECT_RETRY - loop); + tspec.tv_sec = 0; + tspec.tv_nsec = 100000000; /* 100 millisec */ + nanosleep(&tspec, NULL); } - if (msg_queue.msg_qnum == 0) - goto exit; - nanosleep(&tspec, NULL); } - - info("message is still in the ipc queue, starting daemon..."); - retval = start_daemon(); - + +close_and_exit: + close(sock); exit: return retval; }