libudev-ctrl.c revision fdf52f5e2a6c67bf5a709845b2b10621083f849a
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync/*
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * libudev - interface to udev device information
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync *
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync *
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * This library is free software; you can redistribute it and/or
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * modify it under the terms of the GNU Lesser General Public
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * License as published by the Free Software Foundation; either
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * version 2.1 of the License, or (at your option) any later version.
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync */
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <errno.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <stdio.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <stdlib.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <stddef.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <string.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <unistd.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <sys/types.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <sys/poll.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <sys/socket.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include <sys/un.h>
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include "libudev.h"
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#include "libudev-private.h"
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync/* wire protocol magic must match */
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync#define UDEV_CTRL_MAGIC 0xdead1dea
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncenum udev_ctrl_msg_type {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_UNKNOWN,
a3bbbe5c597341d9b6041560b790596ee41c2cfavboxsync UDEV_CTRL_SET_LOG_LEVEL,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_STOP_EXEC_QUEUE,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_START_EXEC_QUEUE,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_RELOAD_RULES,
a3bbbe5c597341d9b6041560b790596ee41c2cfavboxsync UDEV_CTRL_SET_ENV,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_SET_CHILDREN_MAX,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_PING,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync UDEV_CTRL_EXIT,
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync};
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg_wire {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync char version[16];
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync unsigned int magic;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync enum udev_ctrl_msg_type type;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync union {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int intval;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync char buf[256];
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync };
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync};
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int refcount;
6c6531128b39093daeac902a8705c0bdf15b31ccvboxsync struct udev_ctrl_connection *conn;
6c6531128b39093daeac902a8705c0bdf15b31ccvboxsync struct udev_ctrl_msg_wire ctrl_msg_wire;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync};
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsyncstruct udev_ctrl {
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync int refcount;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync struct udev *udev;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync int sock;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync struct sockaddr_un saddr;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync socklen_t addrlen;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync bool bound;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync bool connected;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync};
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
ab3d9c17d039446a2820517e3f0850f65d5a727fvboxsyncstruct udev_ctrl_connection {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int refcount;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev_ctrl *uctrl;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int sock;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync};
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstatic struct udev_ctrl *udev_ctrl_new(struct udev *udev)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev_ctrl *uctrl;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl = calloc(1, sizeof(struct udev_ctrl));
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (uctrl == NULL)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return NULL;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->refcount = 1;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->udev = udev;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return uctrl;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncstruct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync struct udev_ctrl *uctrl;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl = udev_ctrl_new(udev);
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (uctrl == NULL)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (fd < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl->sock < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(udev, "error getting socket: %m\n");
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync udev_ctrl_unref(uctrl);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync }
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync } else {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->bound = true;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->sock = fd;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->saddr.sun_family = AF_LOCAL;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync strcpy(uctrl->saddr.sun_path, socket_path);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync /* translate leading '@' to abstract namespace */
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (uctrl->saddr.sun_path[0] == '@')
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->saddr.sun_path[0] = '\0';
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return uctrl;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncstruct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncint udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync int err;
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync if (!uctrl->bound) {
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (err < 0) {
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync err = -errno;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(uctrl->udev, "bind failed: %m\n");
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync return err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync err = listen(uctrl->sock, 0);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (err < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err = -errno;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(uctrl->udev, "listen failed: %m\n");
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return err;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync }
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync uctrl->bound = true;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync }
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync return 0;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync}
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync
4520ec42b031779169b52cb9536f22937fd9bf97vboxsyncstruct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync{
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync return uctrl->udev;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl == NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->refcount++;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return uctrl;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl == NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync uctrl->refcount--;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl->refcount > 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return uctrl;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl->sock >= 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync close(uctrl->sock);
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync free(uctrl);
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync return NULL;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync}
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync
4520ec42b031779169b52cb9536f22937fd9bf97vboxsyncint udev_ctrl_get_fd(struct udev_ctrl *uctrl)
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync{
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync if (uctrl == NULL)
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync return -1;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync return uctrl->sock;
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync}
4520ec42b031779169b52cb9536f22937fd9bf97vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev_ctrl_connection *conn;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync const int on = 1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn = calloc(1, sizeof(struct udev_ctrl_connection));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (conn == NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn->refcount = 1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn->uctrl = uctrl;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (conn->sock < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync free(conn);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync /* enable receiving of the sender credentials */
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync udev_ctrl_ref(uctrl);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return conn;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsyncstruct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (conn == NULL)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return NULL;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync conn->refcount++;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync return conn;
31b9cc718cca59b9368ae1d9bcca2cf06b76ed85vboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (conn == NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn->refcount--;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (conn->refcount > 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return conn;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (conn->sock >= 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync close(conn->sock);
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync udev_ctrl_unref(conn->uctrl);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync free(conn);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsyncstatic int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev_ctrl_msg_wire ctrl_msg_wire;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int err = 0;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync strcpy(ctrl_msg_wire.version, "udev-" VERSION);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync ctrl_msg_wire.type = type;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (buf != NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync else
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync ctrl_msg_wire.intval = intval;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (!uctrl->connected) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err = -errno;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync goto out;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->connected = true;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err = -errno;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync goto out;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync /* wait for peer message handling or disconnect */
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync for (;;) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct pollfd pfd[1];
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync int r;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync pfd[0].fd = uctrl->sock;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync pfd[0].events = POLLIN;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync r = poll(pfd, 1, timeout * 1000);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (r < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (errno == EINTR)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync continue;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err = -errno;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync break;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (r > 0 && pfd[0].revents & POLLERR) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err = -EIO;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync break;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync }
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (r == 0)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync err = -ETIMEDOUT;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync break;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncout:
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsyncint udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncint udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev *udev = conn->uctrl->udev;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct udev_ctrl_msg *uctrl_msg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync ssize_t size;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync struct msghdr smsg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct cmsghdr *cmsg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct iovec iov;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync struct ucred *cred;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl_msg == NULL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl_msg->refcount = 1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl_msg->conn = conn;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync udev_ctrl_connection_ref(conn);
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync iov.iov_base = &uctrl_msg->ctrl_msg_wire;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync memset(&smsg, 0x00, sizeof(struct msghdr));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync smsg.msg_iov = &iov;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync smsg.msg_iovlen = 1;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync smsg.msg_control = cred_msg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync smsg.msg_controllen = sizeof(cred_msg);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync size = recvmsg(conn->sock, &smsg, 0);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (size < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(udev, "unable to receive user udevd message: %m\n");
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync goto err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync cmsg = CMSG_FIRSTHDR(&smsg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync cred = (struct ucred *) CMSG_DATA(cmsg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(udev, "no sender credentials received, message ignored\n");
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync goto err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (cred->uid != 0) {
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync err(udev, "sender uid=%i, message ignored\n", cred->uid);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync goto err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync goto err;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync }
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return uctrl_msg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncerr:
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync udev_ctrl_msg_unref(uctrl_msg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg == NULL)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return NULL;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync ctrl_msg->refcount++;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync return ctrl_msg;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync}
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsyncstruct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync{
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync if (ctrl_msg == NULL)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync ctrl_msg->refcount--;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->refcount > 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_msg;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync udev_ctrl_connection_unref(ctrl_msg->conn);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync free(ctrl_msg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_msg->ctrl_msg_wire.intval;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return -1;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync}
a3bbbe5c597341d9b6041560b790596ee41c2cfavboxsync
a3bbbe5c597341d9b6041560b790596ee41c2cfavboxsyncint udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync{
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync return 1;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync return -1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync{
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return 1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return -1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return 1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return -1;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncconst char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_msg->ctrl_msg_wire.buf;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync return NULL;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsyncint udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync{
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_msg->ctrl_msg_wire.intval;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return -1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync return 1;
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync return -1;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync}
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
010f43d198b8b535d5a104ebc23a2c44bfb4a18fvboxsync{
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return 1;
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync return -1;
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync}
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync