2 * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <sys/types.h>
28 #include <sys/socket.h>
37 struct sockaddr_un saddr;
41 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
43 struct udev_ctrl *uctrl;
45 uctrl = malloc(sizeof(struct udev_ctrl));
48 memset(uctrl, 0x00, sizeof(struct udev_ctrl));
51 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
52 if (uctrl->sock < 0) {
53 err(udev, "error getting socket: %s\n", strerror(errno));
58 uctrl->saddr.sun_family = AF_LOCAL;
59 strcpy(uctrl->saddr.sun_path, socket_path);
60 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
61 /* translate leading '@' to abstract namespace */
62 if (uctrl->saddr.sun_path[0] == '@')
63 uctrl->saddr.sun_path[0] = '\0';
67 void udev_ctrl_unref(struct udev_ctrl *uctrl)
74 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
76 struct udevd_ctrl_msg ctrl_msg;
79 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
80 strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
84 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
86 ctrl_msg.intval = intval;
88 err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
90 err(uctrl->udev, "error sending message: %s\n", strerror(errno));
95 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
97 ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
101 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
103 ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
107 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
109 ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
113 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
115 ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
119 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
121 ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
125 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
127 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
131 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
133 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);