chiark / gitweb /
4e339ff4833310d87048fb096862759529b6d137
[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         int sock;
36         struct sockaddr_un saddr;
37         socklen_t addrlen;
38 };
39
40 struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
41 {
42         struct udev_ctrl *uctrl;
43
44         uctrl = malloc(sizeof(struct udev_ctrl));
45         if (uctrl == NULL)
46                 return NULL;
47         memset(uctrl, 0x00, sizeof(struct udev_ctrl));
48
49         uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
50         if (uctrl->sock < 0) {
51                 err("error getting socket: %s\n", strerror(errno));
52                 free(uctrl);
53                 return NULL;
54         }
55
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';
62         return uctrl;
63 }
64
65 void udev_ctrl_unref(struct udev_ctrl *uctrl)
66 {
67         if (uctrl == NULL)
68                 return;
69         close(uctrl->sock);
70 }
71
72 static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
73 {
74         struct udevd_ctrl_msg ctrl_msg;
75         int err;
76
77         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
78         strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
79         ctrl_msg.type = type;
80
81         if (buf != NULL)
82                 strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
83         else
84                 ctrl_msg.intval = intval;
85
86         err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
87         if (err == -1) {
88                 err("error sending message: %s\n", strerror(errno));
89         }
90         return err;
91 }
92
93 int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
94 {
95         ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
96         return 0;
97 }
98
99 int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
100 {
101         ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
102         return 0;
103 }
104
105 int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
106 {
107         ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
108         return 0;
109 }
110
111 int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
112 {
113         ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
114         return 0;
115 }
116
117 int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
118 {
119         ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
120         return 0;
121 }
122
123 int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
124 {
125         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
126         return 0;
127 }
128
129 int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
130 {
131         ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
132         return 0;
133 }