chiark / gitweb /
96e4b29be3d200893b334a0b09561ab5c3da6f6d
[elogind.git] / udev / udev-control.c
1 /*
2  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
7  * 
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.
12  * 
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.
16  *
17  */
18
19 #include "config.h"
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30
31 #include "udev.h"
32 #include "udevd.h"
33
34 struct udev_ctrl {
35         struct udev *udev;
36         int sock;
37         struct sockaddr_un saddr;
38         socklen_t addrlen;
39 };
40
41 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
42 {
43         struct udev_ctrl *uctrl;
44
45         uctrl = malloc(sizeof(struct udev_ctrl));
46         if (uctrl == NULL)
47                 return NULL;
48         memset(uctrl, 0x00, sizeof(struct udev_ctrl));
49         uctrl->udev = udev;
50
51         uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
52         if (uctrl->sock < 0) {
53                 err(udev, "error getting socket: %s\n", strerror(errno));
54                 free(uctrl);
55                 return NULL;
56         }
57
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';
64         return uctrl;
65 }
66
67 void udev_ctrl_unref(struct udev_ctrl *uctrl)
68 {
69         if (uctrl == NULL)
70                 return;
71         close(uctrl->sock);
72 }
73
74 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
75 {
76         struct udevd_ctrl_msg ctrl_msg;
77         int err;
78
79         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
80         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
81         ctrl_msg.type = type;
82
83         if (buf != NULL)
84                 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
85         else
86                 ctrl_msg.intval = intval;
87
88         err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
89         if (err == -1) {
90                 err(uctrl->udev, "error sending message: %s\n", strerror(errno));
91         }
92         return err;
93 }
94
95 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
96 {
97         ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
98         return 0;
99 }
100
101 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
102 {
103         ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
104         return 0;
105 }
106
107 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
108 {
109         ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
110         return 0;
111 }
112
113 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
114 {
115         ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
116         return 0;
117 }
118
119 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
120 {
121         ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
122         return 0;
123 }
124
125 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
126 {
127         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
128         return 0;
129 }
130
131 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
132 {
133         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
134         return 0;
135 }