]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
42aa9de9a950a3877a7b3dad8c96b96d47305373
[petitboot] / ui / common / discover-client.c
1
2 #include <assert.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <asm/byteorder.h>
12
13 #include <talloc/talloc.h>
14 #include <log/log.h>
15
16 #include "discover-client.h"
17 #include "pb-protocol/pb-protocol.h"
18
19 struct discover_client {
20         int fd;
21         struct discover_client_ops ops;
22         int n_devices;
23         struct device **devices;
24 };
25
26 static int discover_client_destructor(void *arg)
27 {
28         struct discover_client *client = arg;
29
30         if (client->fd >= 0)
31                 close(client->fd);
32
33         return 0;
34 }
35
36 void discover_client_destroy(struct discover_client *client)
37 {
38         talloc_free(client);
39 }
40
41 static struct device *find_device(struct discover_client *client,
42                 const char *id)
43 {
44         int i;
45
46         for (i = 0; i < client->n_devices; i++) {
47                 struct device *dev = client->devices[i];
48                 if (!strcmp(dev->id, id))
49                         return dev;
50         }
51
52         return NULL;
53 }
54
55 static void device_add(struct discover_client *client, struct device *device)
56 {
57         client->n_devices++;
58         client->devices = talloc_realloc(client, client->devices,
59                         struct device *, client->n_devices);
60
61         client->devices[client->n_devices - 1] = device;
62         talloc_steal(client, device);
63
64         if (client->ops.device_add)
65                 client->ops.device_add(device, client->ops.cb_arg);
66 }
67
68 static void boot_option_add(struct discover_client *client,
69                 struct boot_option *opt)
70 {
71         struct device *dev;
72
73         dev = find_device(client, opt->device_id);
74
75         /* we require that devices are already present before any boot options
76          * are added */
77         assert(dev);
78
79         talloc_steal(dev, opt);
80
81         if (client->ops.boot_option_add)
82                 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
83 }
84
85 static void device_remove(struct discover_client *client, const char *id)
86 {
87         struct device *device = NULL;
88         int i;
89
90         for (i = 0; i < client->n_devices; i++) {
91                 if (!strcmp(client->devices[i]->id, id)) {
92                         device = client->devices[i];
93                         break;
94                 }
95         }
96
97         if (!device)
98                 return;
99
100         /* remove the device from the client's device array */
101         client->n_devices--;
102         memmove(&client->devices[i], &client->devices[i+1],
103                         (client->n_devices - i) * sizeof(client->devices[0]));
104         client->devices = talloc_realloc(client, client->devices,
105                         struct device *, client->n_devices);
106
107         /* notify the UI */
108         client->ops.device_remove(device, client->ops.cb_arg);
109
110         talloc_free(device);
111 }
112
113 static int discover_client_process(void *arg)
114 {
115         struct discover_client *client = arg;
116         struct pb_protocol_message *message;
117         struct boot_option *opt;
118         struct device *dev;
119         char *dev_id;
120         int rc;
121
122         message = pb_protocol_read_message(client, client->fd);
123
124         if (!message)
125                 return -1;
126
127         switch (message->action) {
128         case PB_PROTOCOL_ACTION_DEVICE_ADD:
129                 dev = talloc_zero(client, struct device);
130                 list_init(&dev->boot_options);
131
132                 rc = pb_protocol_deserialise_device(dev, message);
133                 if (rc) {
134                         pb_log("%s: no device?\n", __func__);
135                         return 0;
136                 }
137
138                 device_add(client, dev);
139                 break;
140         case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
141                 opt = talloc_zero(client, struct boot_option);
142
143                 rc = pb_protocol_deserialise_boot_option(opt, message);
144                 if (rc) {
145                         pb_log("%s: no boot_option?\n", __func__);
146                         return 0;
147                 }
148
149                 boot_option_add(client, opt);
150                 break;
151         case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
152                 dev_id = pb_protocol_deserialise_string(client, message);
153                 if (!dev_id) {
154                         pb_log("%s: no device id?\n", __func__);
155                         return 0;
156                 }
157                 device_remove(client, dev_id);
158                 break;
159         default:
160                 pb_log("%s: unknown action %d\n", __func__, message->action);
161         }
162
163
164         return 0;
165 }
166
167 struct discover_client* discover_client_init(struct waitset *waitset,
168         const struct discover_client_ops *ops, void *cb_arg)
169 {
170         struct discover_client *client;
171         struct sockaddr_un addr;
172
173         client = talloc(NULL, struct discover_client);
174         if (!client)
175                 return NULL;
176
177         memcpy(&client->ops, ops, sizeof(client->ops));
178         client->ops.cb_arg = cb_arg;
179
180         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
181         if (client->fd < 0) {
182                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
183                 goto out_err;
184         }
185
186         talloc_set_destructor(client, discover_client_destructor);
187
188         client->n_devices = 0;
189         client->devices = NULL;
190
191         addr.sun_family = AF_UNIX;
192         strcpy(addr.sun_path, PB_SOCKET_PATH);
193
194         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
195                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
196                 goto out_err;
197         }
198
199         waiter_register(waitset, client->fd, WAIT_IN, discover_client_process,
200                         client);
201
202         return client;
203
204 out_err:
205         talloc_free(client);
206         return NULL;
207 }
208
209 /* accessors for discovered devices */
210 int discover_client_device_count(struct discover_client *client)
211 {
212         return client->n_devices;
213 }
214
215 struct device *discover_client_get_device(struct discover_client *client,
216                 int index)
217 {
218         if (index < 0 || index >= client->n_devices)
219                 return NULL;
220
221         return client->devices[index];
222 }
223
224 static void create_boot_command(struct boot_command *command,
225                 const struct device *device __attribute__((unused)),
226                 const struct boot_option *boot_option,
227                 const struct pb_boot_data *data)
228 {
229
230         command->option_id = boot_option->id;
231         command->boot_image_file = data->image;
232         command->initrd_file = data->initrd;
233         command->boot_args = data->args;
234 }
235
236 int discover_client_boot(struct discover_client *client,
237                 const struct device *device,
238                 const struct boot_option *boot_option,
239                 const struct pb_boot_data *data)
240 {
241         struct pb_protocol_message *message;
242         struct boot_command boot_command;
243         int len, rc;
244
245         create_boot_command(&boot_command, device, boot_option, data);
246
247         len = pb_protocol_boot_len(&boot_command);
248
249         message = pb_protocol_create_message(client,
250                         PB_PROTOCOL_ACTION_BOOT, len);
251
252         if (!message)
253                 return -1;
254
255         pb_protocol_serialise_boot_command(&boot_command,
256                         message->payload, len);
257
258         rc = pb_protocol_write_message(client->fd, message);
259
260         return rc;
261 }