libudev-ctrl.c revision fdf52f5e2a6c67bf5a709845b2b10621083f849a
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * libudev - interface to udev device information
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
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/* wire protocol magic must match */
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync unsigned int magic;
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstatic struct udev_ctrl *udev_ctrl_new(struct udev *udev)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncstruct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync /* translate leading '@' to abstract namespace */
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncstruct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncint udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsync err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
4520ec42b031779169b52cb9536f22937fd9bf97vboxsyncstruct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn = calloc(1, sizeof(struct udev_ctrl_connection));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync /* enable receiving of the sender credentials */
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
eb4b342c83eb47585ce579d9aee898e7a33a96d5vboxsyncstruct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsyncstatic int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync /* wait for peer message handling or disconnect */
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (r < 0) {
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (r == 0)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsyncint udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsyncint udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(udev, "unable to receive user udevd message: %m\n");
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync err(udev, "no sender credentials received, message ignored\n");
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync err(udev, "sender uid=%i, message ignored\n", cred->uid);
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 dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncstruct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsyncstruct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
a3bbbe5c597341d9b6041560b790596ee41c2cfavboxsyncint udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
222ed601b92c350df3a504c8f4e4228b58ab5b93vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncconst char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
64fee3f73de7a63db0b27a93be05cd635a149d14vboxsyncint udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
61d4c60e55bc9d7545fc89bd9a32bafe38cea555vboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsync if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
67259dc535458471f701aa4a4f3ff00d5736c0dfvboxsyncint udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)