9 #include <sys/socket.h>
11 #include <asm/byteorder.h>
13 #include <talloc/talloc.h>
16 #include "discover-client.h"
17 #include "pb-protocol/pb-protocol.h"
19 struct discover_client {
21 struct discover_client_ops ops;
23 struct device **devices;
26 static int discover_client_destructor(void *arg)
28 struct discover_client *client = arg;
36 void discover_client_destroy(struct discover_client *client)
41 static struct device *find_device(struct discover_client *client,
46 for (i = 0; i < client->n_devices; i++) {
47 struct device *dev = client->devices[i];
48 if (!strcmp(dev->id, id))
55 static void device_add(struct discover_client *client, struct device *device)
58 client->devices = talloc_realloc(client, client->devices,
59 struct device *, client->n_devices);
61 client->devices[client->n_devices - 1] = device;
62 talloc_steal(client, device);
63 list_init(&device->boot_options);
65 if (client->ops.device_add)
66 client->ops.device_add(device, client->ops.cb_arg);
69 static void boot_option_add(struct discover_client *client,
70 struct boot_option *opt)
74 dev = find_device(client, opt->device_id);
76 /* we require that devices are already present before any boot options
80 talloc_steal(dev, opt);
81 list_add(&dev->boot_options, &opt->list);
83 if (client->ops.boot_option_add)
84 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
87 static void device_remove(struct discover_client *client, const char *id)
89 struct device *device = NULL;
92 for (i = 0; i < client->n_devices; i++) {
93 if (!strcmp(client->devices[i]->id, id)) {
94 device = client->devices[i];
102 /* remove the device from the client's device array */
104 memmove(&client->devices[i], &client->devices[i+1],
105 (client->n_devices - i) * sizeof(client->devices[0]));
106 client->devices = talloc_realloc(client, client->devices,
107 struct device *, client->n_devices);
110 client->ops.device_remove(device, client->ops.cb_arg);
115 static void plugin_option_add(struct discover_client *client,
116 struct plugin_option *opt)
118 talloc_steal(client, opt);
120 if (client->ops.plugin_option_add)
121 client->ops.plugin_option_add(opt, client->ops.cb_arg);
124 static void plugins_remove(struct discover_client *client)
126 if (client->ops.plugins_remove)
127 client->ops.plugins_remove(client->ops.cb_arg);
130 void discover_client_enumerate(struct discover_client *client)
132 struct boot_option *opt;
133 struct device *device;
136 for (i = 0; i < client->n_devices; i++) {
137 device = client->devices[i];
138 if (client->ops.device_add)
139 client->ops.device_add(device, client->ops.cb_arg);
141 list_for_each_entry(&device->boot_options, opt, list)
142 if (client->ops.boot_option_add)
143 client->ops.boot_option_add(device, opt,
148 static void update_status(struct discover_client *client,
149 struct status *status)
151 if (client->ops.update_status)
152 client->ops.update_status(status, client->ops.cb_arg);
155 static void update_sysinfo(struct discover_client *client,
156 struct system_info *sysinfo)
158 if (client->ops.update_sysinfo)
159 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
162 static void update_config(struct discover_client *client,
163 struct config *config)
165 if (client->ops.update_config)
166 client->ops.update_config(config, client->ops.cb_arg);
169 static int discover_client_process(void *arg)
171 struct discover_client *client = arg;
172 struct pb_protocol_message *message;
173 struct plugin_option *p_opt;
174 struct system_info *sysinfo;
175 struct boot_option *opt;
176 struct status *status;
177 struct config *config;
183 /* We use a temporary context for processing one message; persistent
184 * data is re-parented to the client in the callbacks. */
185 ctx = talloc_new(client);
187 message = pb_protocol_read_message(ctx, client->fd);
192 switch (message->action) {
193 case PB_PROTOCOL_ACTION_DEVICE_ADD:
194 dev = talloc_zero(ctx, struct device);
195 list_init(&dev->boot_options);
197 rc = pb_protocol_deserialise_device(dev, message);
199 pb_log("%s: no device?\n", __func__);
203 device_add(client, dev);
205 case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
206 opt = talloc_zero(ctx, struct boot_option);
208 rc = pb_protocol_deserialise_boot_option(opt, message);
210 pb_log("%s: no boot_option?\n", __func__);
214 boot_option_add(client, opt);
216 case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
217 dev_id = pb_protocol_deserialise_string(ctx, message);
219 pb_log("%s: no device id?\n", __func__);
222 device_remove(client, dev_id);
224 case PB_PROTOCOL_ACTION_STATUS:
225 status = talloc_zero(ctx, struct status);
227 rc = pb_protocol_deserialise_boot_status(status, message);
229 pb_log("%s: invalid status message?\n", __func__);
232 update_status(client, status);
234 case PB_PROTOCOL_ACTION_SYSTEM_INFO:
235 sysinfo = talloc_zero(ctx, struct system_info);
237 rc = pb_protocol_deserialise_system_info(sysinfo, message);
239 pb_log("%s: invalid sysinfo message?\n", __func__);
242 update_sysinfo(client, sysinfo);
244 case PB_PROTOCOL_ACTION_CONFIG:
245 config = talloc_zero(ctx, struct config);
247 rc = pb_protocol_deserialise_config(config, message);
249 pb_log("%s: invalid config message?\n", __func__);
252 update_config(client, config);
254 case PB_PROTOCOL_ACTION_PLUGIN_OPTION_ADD:
255 p_opt = talloc_zero(ctx, struct plugin_option);
257 rc = pb_protocol_deserialise_plugin_option(p_opt, message);
259 pb_log("%s: no plugin_option?\n", __func__);
263 plugin_option_add(client, p_opt);
265 case PB_PROTOCOL_ACTION_PLUGINS_REMOVE:
266 plugins_remove(client);
269 pb_log("%s: unknown action %d\n", __func__, message->action);
278 struct discover_client* discover_client_init(struct waitset *waitset,
279 const struct discover_client_ops *ops, void *cb_arg)
281 struct discover_client *client;
282 struct sockaddr_un addr;
284 client = talloc(NULL, struct discover_client);
288 memcpy(&client->ops, ops, sizeof(client->ops));
289 client->ops.cb_arg = cb_arg;
291 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
292 if (client->fd < 0) {
293 pb_log("%s: socket: %s\n", __func__, strerror(errno));
297 talloc_set_destructor(client, discover_client_destructor);
299 client->n_devices = 0;
300 client->devices = NULL;
302 addr.sun_family = AF_UNIX;
303 strcpy(addr.sun_path, PB_SOCKET_PATH);
305 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
306 pb_log("%s: connect: %s\n", __func__, strerror(errno));
310 waiter_register_io(waitset, client->fd, WAIT_IN,
311 discover_client_process, client);
320 /* accessors for discovered devices */
321 int discover_client_device_count(struct discover_client *client)
323 return client->n_devices;
326 struct device *discover_client_get_device(struct discover_client *client,
329 if (index < 0 || index >= client->n_devices)
332 return client->devices[index];
335 static void create_boot_command(struct boot_command *command,
336 const struct device *device __attribute__((unused)),
337 const struct boot_option *boot_option,
338 const struct pb_boot_data *data)
340 command->option_id = boot_option ? boot_option->id : NULL;
341 command->boot_image_file = data->image;
342 command->initrd_file = data->initrd;
343 command->dtb_file = data->dtb;
344 command->boot_args = data->args;
345 command->args_sig_file = data->args_sig_file;
346 command->console = ttyname(STDIN_FILENO);
349 int discover_client_boot(struct discover_client *client,
350 const struct device *device,
351 const struct boot_option *boot_option,
352 const struct pb_boot_data *data)
354 struct pb_protocol_message *message;
355 struct boot_command boot_command;
358 create_boot_command(&boot_command, device, boot_option, data);
360 len = pb_protocol_boot_len(&boot_command);
362 message = pb_protocol_create_message(client,
363 PB_PROTOCOL_ACTION_BOOT, len);
368 pb_protocol_serialise_boot_command(&boot_command,
369 message->payload, len);
371 rc = pb_protocol_write_message(client->fd, message);
376 int discover_client_cancel_default(struct discover_client *client)
378 struct pb_protocol_message *message;
380 message = pb_protocol_create_message(client,
381 PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
386 return pb_protocol_write_message(client->fd, message);
389 int discover_client_send_reinit(struct discover_client *client)
391 struct pb_protocol_message *message;
393 message = pb_protocol_create_message(client,
394 PB_PROTOCOL_ACTION_REINIT, 0);
399 return pb_protocol_write_message(client->fd, message);
402 int discover_client_send_config(struct discover_client *client,
403 struct config *config)
405 struct pb_protocol_message *message;
408 len = pb_protocol_config_len(config);
410 message = pb_protocol_create_message(client,
411 PB_PROTOCOL_ACTION_CONFIG, len);
415 pb_protocol_serialise_config(config, message->payload, len);
417 return pb_protocol_write_message(client->fd, message);
420 int discover_client_send_url(struct discover_client *client,
423 struct pb_protocol_message *message;
426 len = pb_protocol_url_len(url);
428 message = pb_protocol_create_message(client,
429 PB_PROTOCOL_ACTION_ADD_URL, len);
433 pb_protocol_serialise_url(url, message->payload, len);
435 return pb_protocol_write_message(client->fd, message);
438 int discover_client_send_plugin_install(struct discover_client *client,
441 struct pb_protocol_message *message;
444 len = pb_protocol_url_len(file);
446 message = pb_protocol_create_message(client,
447 PB_PROTOCOL_ACTION_PLUGIN_INSTALL, len);
451 pb_protocol_serialise_url(file, message->payload, len);
453 return pb_protocol_write_message(client->fd, message);