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>
36 struct sockaddr_un saddr;
40 struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
42 struct udev_ctrl *uctrl;
44 uctrl = malloc(sizeof(struct udev_ctrl));
47 memset(uctrl, 0x00, sizeof(struct udev_ctrl));
49 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
50 if (uctrl->sock < 0) {
51 err("error getting socket: %s\n", strerror(errno));
56 uctrl->saddr.sun_family = AF_LOCAL;
57 strcpy(uctrl->saddr.sun_path, socket_path);
58 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
59 /* translate leading '@' to abstract namespace */
60 if (uctrl->saddr.sun_path[0] == '@')
61 uctrl->saddr.sun_path[0] = '\0';
65 void udev_ctrl_unref(struct udev_ctrl *uctrl)
72 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
74 struct udevd_ctrl_msg ctrl_msg;
77 memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
78 strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
82 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
84 ctrl_msg.intval = intval;
86 err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
88 err("error sending message: %s\n", strerror(errno));
93 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
95 ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
99 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
101 ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
105 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
107 ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
111 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
113 ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
117 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
119 ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
123 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
125 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
129 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
131 ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);