udev-ctrl.c revision ad29a9f14fa8b1932c0e418bfcf1c10ce6a35a33
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson * libudev - interface to udev device information
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson * This library is free software; you can redistribute it and/or
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson * modify it under the terms of the GNU Lesser General Public
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson * License as published by the Free Software Foundation; either
3437829f938dbb44527d91fbbc5f430a1243c5a5JnRouvignac * version 2.1 of the License, or (at your option) any later version.
7c6d2cd054f573532aa1a421840937c100490ce1JnRouvignac/* wire protocol magic must match */
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson unsigned int magic;
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson util_strscpyl(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path),
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilsonstruct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilsonstruct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson conn = calloc(1, sizeof(struct udev_ctrl_connection));
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(uctrl->udev, "unable to receive ctrl connection: %m\n");
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson /* check peer credential of connection */
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(uctrl->udev, "unable to receive credentials of ctrl connection: %m\n");
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(uctrl->udev, "sender uid=%i, message ignored\n", ucred.uid);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson /* enable receiving of the sender credentials in the messages */
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilson setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilsonstruct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstatic int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson strcpy(ctrl_msg_wire.version, "udev-" VERSION);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson /* wait for peer message handling or disconnect */
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilsonint udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilsonint udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonint udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson /* wait for the incoming message */
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson } else if (r == 0) {
1a086715b3a300cb87695667ab6bc0ec72dded0eneil_a_wilson err(udev, "timeout waiting for ctrl message\n");
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(udev, "unable to receive ctrl message: %m\n");
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(udev, "no sender credentials received, message ignored\n");
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(udev, "sender uid=%i, message ignored\n", cred->uid);
520c31a362826a22ded10aecc31dc9d75e07fbf2lutoff if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilsonstruct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
414c37b64ad781961cde03441c436051cf56d441neil_a_wilson dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
480256311ff1597d92043303772f072d8d67fb3ccoulbeckint udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
480256311ff1597d92043303772f072d8d67fb3ccoulbeck if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
480256311ff1597d92043303772f072d8d67fb3ccoulbeckint udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
480256311ff1597d92043303772f072d8d67fb3ccoulbeck if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
480256311ff1597d92043303772f072d8d67fb3ccoulbeckint udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
480256311ff1597d92043303772f072d8d67fb3ccoulbeck if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
480256311ff1597d92043303772f072d8d67fb3ccoulbeckint udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg)
480256311ff1597d92043303772f072d8d67fb3ccoulbeck if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilsonconst char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilson if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilsonint udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilson if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilsonint udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
1acdbf024a6066d97bbd1ef07094be83f649239fneil_a_wilson if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)