* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.h"
-
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "libudev.h"
#include "libudev-private.h"
-#define UDEV_CTRL_MAGIC "udevd-128"
+/* last known version with this wire protocol */
+#define UDEV_CTRL_MAGIC 0xdead1dea
enum udev_ctrl_msg_type {
UDEV_CTRL_UNKNOWN,
UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
};
-struct ctrl_msg_wire {
- char magic[32];
+struct udev_ctrl_msg_wire {
+ char version[16];
+ unsigned int magic;
enum udev_ctrl_msg_type type;
union {
int intval;
struct udev_ctrl_msg {
int refcount;
struct udev_ctrl *uctrl;
- struct ctrl_msg_wire ctrl_msg_wire;
+ struct udev_ctrl_msg_wire ctrl_msg_wire;
};
struct udev_ctrl {
{
struct udev_ctrl *uctrl;
- uctrl = malloc(sizeof(struct udev_ctrl));
+ uctrl = calloc(1, sizeof(struct udev_ctrl));
if (uctrl == NULL)
return NULL;
- memset(uctrl, 0x00, sizeof(struct udev_ctrl));
uctrl->refcount = 1;
uctrl->udev = udev;
uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (uctrl->sock < 0) {
- err(udev, "error getting socket: %s\n", strerror(errno));
+ err(udev, "error getting socket: %m\n");
udev_ctrl_unref(uctrl);
return NULL;
}
err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err < 0) {
- err(uctrl->udev, "bind failed: %s\n", strerror(errno));
+ err(uctrl->udev, "bind failed: %m\n");
return err;
}
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
{
- struct ctrl_msg_wire ctrl_msg_wire;
+ struct udev_ctrl_msg_wire ctrl_msg_wire;
int err;
- memset(&ctrl_msg_wire, 0x00, sizeof(struct ctrl_msg_wire));
- strcpy(ctrl_msg_wire.magic, UDEV_CTRL_MAGIC);
+ memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
+ strcpy(ctrl_msg_wire.version, "udev-" VERSION);
+ ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
ctrl_msg_wire.type = type;
if (buf != NULL)
else
ctrl_msg_wire.intval = intval;
- err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+ err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
+ (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err == -1) {
- err(uctrl->udev, "error sending message: %s\n", strerror(errno));
+ err(uctrl->udev, "error sending message: %m\n");
}
return err;
}
int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
{
- ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, optarg);
+ ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
return 0;
}
struct ucred *cred;
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
- uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
+ uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
if (uctrl_msg == NULL)
return NULL;
- memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
uctrl_msg->refcount = 1;
uctrl_msg->uctrl = uctrl;
iov.iov_base = &uctrl_msg->ctrl_msg_wire;
- iov.iov_len = sizeof(struct udev_ctrl_msg);
+ iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
memset(&smsg, 0x00, sizeof(struct msghdr));
smsg.msg_iov = &iov;
size = recvmsg(uctrl->sock, &smsg, 0);
if (size < 0) {
- err(uctrl->udev, "unable to receive user udevd message: %s\n", strerror(errno));
+ err(uctrl->udev, "unable to receive user udevd message: %m\n");
goto err;
}
cmsg = CMSG_FIRSTHDR(&smsg);
goto err;
}
- if (strncmp(uctrl_msg->ctrl_msg_wire.magic, UDEV_CTRL_MAGIC, sizeof(UDEV_CTRL_MAGIC)) != 0 ) {
- err(uctrl->udev, "message magic '%s' doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
+ if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
+ err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
goto err;
}