10 #include <sys/socket.h>
12 #include <asm/byteorder.h>
14 #include <talloc/talloc.h>
17 #include "discover-client.h"
18 #include "pb-protocol/pb-protocol.h"
20 struct discover_client {
22 struct discover_client_ops ops;
24 struct device **devices;
27 static int discover_client_destructor(void *arg)
29 struct discover_client *client = arg;
37 void discover_client_destroy(struct discover_client *client)
42 static struct device *find_device(struct discover_client *client,
47 for (i = 0; i < client->n_devices; i++) {
48 struct device *dev = client->devices[i];
49 if (!strcmp(dev->id, id))
56 static void device_add(struct discover_client *client, struct device *device)
59 client->devices = talloc_realloc(client, client->devices,
60 struct device *, client->n_devices);
62 client->devices[client->n_devices - 1] = device;
63 talloc_steal(client, device);
64 list_init(&device->boot_options);
66 if (client->ops.device_add)
67 client->ops.device_add(device, client->ops.cb_arg);
70 static void boot_option_add(struct discover_client *client,
71 struct boot_option *opt)
75 dev = find_device(client, opt->device_id);
77 /* we require that devices are already present before any boot options
81 talloc_steal(dev, opt);
82 list_add(&dev->boot_options, &opt->list);
84 if (client->ops.boot_option_add)
85 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
88 static void device_remove(struct discover_client *client, const char *id)
90 struct device *device = NULL;
93 for (i = 0; i < client->n_devices; i++) {
94 if (!strcmp(client->devices[i]->id, id)) {
95 device = client->devices[i];
103 /* remove the device from the client's device array */
105 memmove(&client->devices[i], &client->devices[i+1],
106 (client->n_devices - i) * sizeof(client->devices[0]));
107 client->devices = talloc_realloc(client, client->devices,
108 struct device *, client->n_devices);
111 client->ops.device_remove(device, client->ops.cb_arg);
116 static void plugin_option_add(struct discover_client *client,
117 struct plugin_option *opt)
119 talloc_steal(client, opt);
121 if (client->ops.plugin_option_add)
122 client->ops.plugin_option_add(opt, client->ops.cb_arg);
125 static void plugins_remove(struct discover_client *client)
127 if (client->ops.plugins_remove)
128 client->ops.plugins_remove(client->ops.cb_arg);
131 void discover_client_enumerate(struct discover_client *client)
133 struct boot_option *opt;
134 struct device *device;
137 for (i = 0; i < client->n_devices; i++) {
138 device = client->devices[i];
139 if (client->ops.device_add)
140 client->ops.device_add(device, client->ops.cb_arg);
142 list_for_each_entry(&device->boot_options, opt, list)
143 if (client->ops.boot_option_add)
144 client->ops.boot_option_add(device, opt,
149 static void update_status(struct discover_client *client,
150 struct status *status)
152 if (client->ops.update_status)
153 client->ops.update_status(status, client->ops.cb_arg);
156 static void update_sysinfo(struct discover_client *client,
157 struct system_info *sysinfo)
159 if (client->ops.update_sysinfo)
160 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
163 static void update_config(struct discover_client *client,
164 struct config *config)
166 if (client->ops.update_config)
167 client->ops.update_config(config, client->ops.cb_arg);
170 static int discover_client_process(void *arg)
172 struct discover_client *client = arg;
173 struct pb_protocol_message *message;
174 struct plugin_option *p_opt;
175 struct system_info *sysinfo;
176 struct boot_option *opt;
177 struct status *status;
178 struct config *config;
184 /* We use a temporary context for processing one message; persistent
185 * data is re-parented to the client in the callbacks. */
186 ctx = talloc_new(client);
188 message = pb_protocol_read_message(ctx, client->fd);
193 switch (message->action) {
194 case PB_PROTOCOL_ACTION_DEVICE_ADD:
195 dev = talloc_zero(ctx, struct device);
196 list_init(&dev->boot_options);
198 rc = pb_protocol_deserialise_device(dev, message);
200 pb_log("%s: no device?\n", __func__);
204 device_add(client, dev);
206 case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
207 opt = talloc_zero(ctx, struct boot_option);
209 rc = pb_protocol_deserialise_boot_option(opt, message);
211 pb_log("%s: no boot_option?\n", __func__);
215 boot_option_add(client, opt);
217 case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
218 dev_id = pb_protocol_deserialise_string(ctx, message);
220 pb_log("%s: no device id?\n", __func__);
223 device_remove(client, dev_id);
225 case PB_PROTOCOL_ACTION_STATUS:
226 status = talloc_zero(ctx, struct status);
228 rc = pb_protocol_deserialise_boot_status(status, message);
230 pb_log("%s: invalid status message?\n", __func__);
233 update_status(client, status);
235 case PB_PROTOCOL_ACTION_SYSTEM_INFO:
236 sysinfo = talloc_zero(ctx, struct system_info);
238 rc = pb_protocol_deserialise_system_info(sysinfo, message);
240 pb_log("%s: invalid sysinfo message?\n", __func__);
243 update_sysinfo(client, sysinfo);
245 case PB_PROTOCOL_ACTION_CONFIG:
246 config = talloc_zero(ctx, struct config);
248 rc = pb_protocol_deserialise_config(config, message);
250 pb_log("%s: invalid config message?\n", __func__);
253 update_config(client, config);
255 case PB_PROTOCOL_ACTION_PLUGIN_OPTION_ADD:
256 p_opt = talloc_zero(ctx, struct plugin_option);
258 rc = pb_protocol_deserialise_plugin_option(p_opt, message);
260 pb_log("%s: no plugin_option?\n", __func__);
264 plugin_option_add(client, p_opt);
266 case PB_PROTOCOL_ACTION_PLUGINS_REMOVE:
267 plugins_remove(client);
270 pb_log("%s: unknown action %d\n", __func__, message->action);
279 struct discover_client* discover_client_init(struct waitset *waitset,
280 const struct discover_client_ops *ops, void *cb_arg)
282 struct discover_client *client;
283 struct sockaddr_un addr;
285 client = talloc(NULL, struct discover_client);
289 memcpy(&client->ops, ops, sizeof(client->ops));
290 client->ops.cb_arg = cb_arg;
292 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
293 if (client->fd < 0) {
294 pb_log("%s: socket: %s\n", __func__, strerror(errno));
298 talloc_set_destructor(client, discover_client_destructor);
300 client->n_devices = 0;
301 client->devices = NULL;
303 addr.sun_family = AF_UNIX;
304 strcpy(addr.sun_path, PB_SOCKET_PATH);
306 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
307 pb_log("%s: connect: %s\n", __func__, strerror(errno));
311 waiter_register_io(waitset, client->fd, WAIT_IN,
312 discover_client_process, client);
321 /* accessors for discovered devices */
322 int discover_client_device_count(struct discover_client *client)
324 return client->n_devices;
327 struct device *discover_client_get_device(struct discover_client *client,
330 if (index < 0 || index >= client->n_devices)
333 return client->devices[index];
336 static void create_boot_command(struct boot_command *command,
337 const struct device *device __attribute__((unused)),
338 const struct boot_option *boot_option,
339 const struct pb_boot_data *data)
341 command->option_id = boot_option ? boot_option->id : NULL;
342 command->boot_image_file = data->image;
343 command->initrd_file = data->initrd;
344 command->dtb_file = data->dtb;
345 command->boot_args = data->args;
346 command->args_sig_file = data->args_sig_file;
347 command->console = ttyname(STDIN_FILENO);
350 int discover_client_boot(struct discover_client *client,
351 const struct device *device,
352 const struct boot_option *boot_option,
353 const struct pb_boot_data *data)
355 struct pb_protocol_message *message;
356 struct boot_command boot_command;
359 create_boot_command(&boot_command, device, boot_option, data);
361 len = pb_protocol_boot_len(&boot_command);
363 message = pb_protocol_create_message(client,
364 PB_PROTOCOL_ACTION_BOOT, len);
369 pb_protocol_serialise_boot_command(&boot_command,
370 message->payload, len);
372 rc = pb_protocol_write_message(client->fd, message);
377 int discover_client_cancel_default(struct discover_client *client)
379 struct pb_protocol_message *message;
381 message = pb_protocol_create_message(client,
382 PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
387 return pb_protocol_write_message(client->fd, message);
390 int discover_client_send_reinit(struct discover_client *client)
392 struct pb_protocol_message *message;
394 message = pb_protocol_create_message(client,
395 PB_PROTOCOL_ACTION_REINIT, 0);
400 return pb_protocol_write_message(client->fd, message);
403 int discover_client_send_config(struct discover_client *client,
404 struct config *config)
406 struct pb_protocol_message *message;
409 len = pb_protocol_config_len(config);
411 message = pb_protocol_create_message(client,
412 PB_PROTOCOL_ACTION_CONFIG, len);
416 pb_protocol_serialise_config(config, message->payload, len);
418 return pb_protocol_write_message(client->fd, message);
421 int discover_client_send_url(struct discover_client *client,
424 struct pb_protocol_message *message;
427 len = pb_protocol_url_len(url);
429 message = pb_protocol_create_message(client,
430 PB_PROTOCOL_ACTION_ADD_URL, len);
434 pb_protocol_serialise_url(url, message->payload, len);
436 return pb_protocol_write_message(client->fd, message);
439 int discover_client_send_plugin_install(struct discover_client *client,
442 struct pb_protocol_message *message;
445 len = pb_protocol_url_len(file);
447 message = pb_protocol_create_message(client,
448 PB_PROTOCOL_ACTION_PLUGIN_INSTALL, len);
452 pb_protocol_serialise_url(file, message->payload, len);
454 return pb_protocol_write_message(client->fd, message);